package cn.iocoder.yudao.module.crm.controller.admin.userlivetree;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.crm.controller.admin.userlivetree.vo.UserLiveTreeListVO;
import cn.iocoder.yudao.module.crm.controller.admin.userlivetree.vo.UserLiveTreePageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.userlivetree.vo.UserLiveTreeRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.userlivetree.vo.UserLiveTreeSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.userlivetree.UserLiveTreeDO;
import cn.iocoder.yudao.module.crm.service.userlivetree.UserLiveTreeService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - crm用户结构树")
@RestController
@RequestMapping("/crm/user-live-tree")
@Validated
public class UserLiveTreeController {

    @Resource
    private UserLiveTreeService userLiveTreeService;

    @Resource
    private AdminUserApi userApi;

    @PostMapping("/create")
    @Operation(summary = "创建crm用户结构树")
    public CommonResult<Long> createUserLiveTree(@Valid @RequestBody UserLiveTreeSaveReqVO createReqVO) {
        return success(userLiveTreeService.createUserLiveTree(createReqVO));
    }


    @GetMapping("/list")
    @Operation(summary = "crm用户结构树列表")
    public CommonResult<List<UserLiveTreeListVO>> userLiveTreeList() {
        return success(userLiveTreeService.userLiveTreeList());
    }

    @PutMapping("/update")
    @Operation(summary = "更新crm用户结构树")
    public CommonResult<Boolean> updateUserLiveTree(@Valid @RequestBody UserLiveTreeSaveReqVO updateReqVO) {
        userLiveTreeService.updateUserLiveTree(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除crm用户结构树")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteUserLiveTree(@RequestParam("id") Long id) {
        userLiveTreeService.deleteUserLiveTree(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得crm用户结构树")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<UserLiveTreeRespVO> getUserLiveTree(@RequestParam("id") Long id) {
        UserLiveTreeDO userLiveTree = userLiveTreeService.getUserLiveTree(id);
        return success(BeanUtils.toBean(userLiveTree, UserLiveTreeRespVO.class));
    }


    @GetMapping("/getItemByPid")
    @Operation(summary = "获取用户直接下级")
    @Parameter(name = "pid", description = "编号", required = true, example = "1024")
    public CommonResult<List<UserLiveTreeRespVO>> getItemByPid(@RequestParam("pid") Long pid) {
        List<UserLiveTreeDO> userLiveTree = userLiveTreeService.getItemByPid(pid);
        return success(BeanUtils.toBean(userLiveTree, UserLiveTreeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得crm用户结构树分页")
    public CommonResult<PageResult<UserLiveTreeListVO>> getUserLiveTreePage(@Valid UserLiveTreePageReqVO pageReqVO) {

        return success(userLiveTreeService.getUserLiveTreePage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出crm用户结构树 Excel")
    @OperateLog(type = EXPORT)
    public void exportUserLiveTreeExcel(@Valid UserLiveTreePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserLiveTreeListVO> list = userLiveTreeService.getUserLiveTreePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "crm用户结构树.xls", "数据", UserLiveTreeRespVO.class,
                        BeanUtils.toBean(list, UserLiveTreeRespVO.class));
    }
}
