package cn.iocoder.yudao.module.bpm.controller.admin.definition;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.cc.BpmProcessCcPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.cc.BpmProcessCcRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.cc.BpmProcessCcSaveReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessCcDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmUserGroupDO;
import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessCcService;
import cn.iocoder.yudao.module.bpm.service.definition.BpmUserGroupService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
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.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;

@Tag(name = "管理后台 - BPM 流程抄送配置")
@RestController
@RequestMapping("/bpm/process-cc")
@Validated
public class BpmProcessCcController {

    @Resource
    private BpmProcessCcService processCcService;

    @Resource
    private BpmUserGroupService groupService;

    @Resource
    private DeptApi deptApi;

    @PostMapping("/create")
    @Operation(summary = "创建BPM 流程抄送配置")
    @PreAuthorize("@ss.hasPermission('bpm:process-cc:create')")
    public CommonResult<Long> createProcessCc(@Valid @RequestBody BpmProcessCcSaveReqVO createReqVO) {
        return success(processCcService.createProcessCc(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新BPM 流程抄送配置")
    @PreAuthorize("@ss.hasPermission('bpm:process-cc:update')")
    public CommonResult<Boolean> updateProcessCc(@Valid @RequestBody BpmProcessCcSaveReqVO updateReqVO) {
        processCcService.updateProcessCc(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除BPM 流程抄送配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bpm:process-cc:delete')")
    public CommonResult<Boolean> deleteProcessCc(@RequestParam("id") Long id) {
        processCcService.deleteProcessCc(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得BPM 流程抄送配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bpm:process-cc:query')")
    public CommonResult<BpmProcessCcRespVO> getProcessCc(@RequestParam("id") Long id) {
        BpmProcessCcDO processCc = processCcService.getProcessCc(id);
        BpmProcessCcRespVO respVO = BeanUtils.toBean(processCc, BpmProcessCcRespVO.class);

        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得BPM 流程抄送配置分页")
    @PreAuthorize("@ss.hasPermission('bpm:process-cc:query')")
    public CommonResult<PageResult<BpmProcessCcRespVO>> getProcessCcPage(@Valid BpmProcessCcPageReqVO pageReqVO) {
        PageResult<BpmProcessCcDO> pageResult = processCcService.getProcessCcPage(pageReqVO);
        PageResult<BpmProcessCcRespVO> respVOPageResult = BeanUtils.toBean(pageResult, BpmProcessCcRespVO.class);

        if (CollUtil.isNotEmpty(respVOPageResult.getList())) {

            // 获取公司部门Map
            Map<Long, DeptRespDTO> deptMap = convertMap(deptApi.getCompanyDept().getCheckedData(), DeptRespDTO::getId);
            // 获取列表中所有用户组详情
            List<BpmUserGroupDO> groupDOS = groupService.getUserGroupList(
                    respVOPageResult.getList().stream()
                            .flatMap(item -> item.getUserGroupId().stream())
                            .collect(Collectors.toSet()));
            // 转换Map
            Map<Long, String> groupNameMap = convertMap(groupDOS, BpmUserGroupDO::getId, BpmUserGroupDO::getName);

            respVOPageResult.getList().forEach(data -> {

                // 设置用户组名称
                data.setGroupName(data.getUserGroupId().stream()
                        .map(groupNameMap::get)
                        .collect(Collectors.joining("、")));
                // 设置公司名称
                data.setCompanyName(data.getCompanyDeptId().stream()
                        .map(item -> deptMap.get(item) != null ? deptMap.get(item).getName() : "")
                        .collect(Collectors.joining("、")));
            });
        }

        return success(respVOPageResult);
    }
}