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

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.project.BpmOAProjectCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.project.BpmOAProjectRespVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAProjectDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAProjectService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
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;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * OA 项目申请 Controller
 *
 * @author 符溶馨
 */
@Tag(name = "管理后台 - OA 项目申请")
@RestController
@RequestMapping("/bpm/oa/project")
@Validated
public class BpmOAProjectController {

    @Resource
    private BpmOAProjectService projectService;

    @Resource
    private AdminUserApi userApi;

    @Resource
    private DeptApi deptApi;

    @PostMapping("/create")
    @Operation(summary = "创建请求申请")
    public CommonResult<Long> createProject(@Valid @RequestBody BpmOAProjectCreateReqVO createReqVO) {

        return success(projectService.createProject(getLoginUserId(), createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOAProjectRespVO> getProject(@RequestParam("id") Long id) {

        BpmOAProjectDO project = projectService.getProject(id);
        BpmOAProjectRespVO respVO = BeanUtils.toBean(project, BpmOAProjectRespVO.class);

        if (respVO != null) {
            Set<Long> userIds = new HashSet<>();
            // 获取项目中所有人员的用户编号列表
            if (CollUtil.isNotEmpty(respVO.getStaff())) {
                userIds.addAll(respVO.getStaff());
            }
            userIds.add(respVO.getDirectorUserId());

            Set<Long> deptIds = new HashSet<>();
            // 获取项目中所有部门编号列表
            if (CollUtil.isNotEmpty(respVO.getParticipationDept())) {
                deptIds.addAll(respVO.getParticipationDept());
            }
            deptIds.add(respVO.getResponsibleDept());

            // 获取所有用户信息
            Map<Long, AdminUserRespDTO> userMap = userApi.getUserMap(userIds);
            // 获取所有部门信息
            Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(deptIds);

            // 设置责任人名称
            respVO.setDirectorUserName(userMap.get(respVO.getDirectorUserId()).getNickname());
            // 设置项目人员名称
            userMap.remove(respVO.getDirectorUserId());
            respVO.setStaffName(userMap.values().stream().map(AdminUserRespDTO::getNickname).collect(Collectors.joining("、")));

            // 设置责任部门名称
            respVO.setResponsibleDeptName(deptMap.get(respVO.getResponsibleDept()).getName());
            // 拼接参与部门名称
            deptMap.remove(respVO.getResponsibleDept());
            respVO.setParticipationDeptName(deptMap.values().stream().map(DeptRespDTO::getName).collect(Collectors.joining("、")));
        }

        return success(respVO);
    }

    @GetMapping("/getByProcessInstanceId")
    @Operation(summary = "获得项目申请")
    @Parameter(name = "processInstanceId", description = "流程实例编号", required = true, example = "1024")
    public CommonResult<BpmOAProjectRespVO> getByProcessInstanceId(@RequestParam("processInstanceId") String processInstanceId) {

        BpmOAProjectDO project = projectService.getByProcessInstanceId(processInstanceId);
        BpmOAProjectRespVO respVO = BeanUtils.toBean(project, BpmOAProjectRespVO.class);

        if (respVO != null) {

            Set<Long> userIds = new HashSet<>();
            // 获取项目中所有人员的用户编号列表
            if (CollUtil.isNotEmpty(respVO.getStaff())) {
                userIds.addAll(respVO.getStaff());
            }
            userIds.add(respVO.getDirectorUserId());

            Set<Long> deptIds = new HashSet<>();
            // 获取项目中所有部门编号列表
            if (CollUtil.isNotEmpty(respVO.getParticipationDept())) {
                deptIds.addAll(respVO.getParticipationDept());
            }
            deptIds.add(respVO.getResponsibleDept());

            // 获取所有用户信息
            Map<Long, AdminUserRespDTO> userMap = userApi.getUserMap(userIds);
            // 获取所有部门信息
            Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(deptIds);

            // 设置责任人名称
            respVO.setDirectorUserName(userMap.get(respVO.getDirectorUserId()).getNickname());
            // 设置项目人员名称
            userMap.remove(respVO.getDirectorUserId());
            respVO.setStaffName(userMap.values().stream().map(AdminUserRespDTO::getNickname).collect(Collectors.joining("、")));

            // 设置责任部门名称
            respVO.setResponsibleDeptName(deptMap.get(respVO.getResponsibleDept()).getName());
            // 拼接参与部门名称
            deptMap.remove(respVO.getResponsibleDept());
            respVO.setParticipationDeptName(deptMap.values().stream().map(DeptRespDTO::getName).collect(Collectors.joining("、")));
        }

        return success(respVO);
    }
}
