package com.hzs.system.sys.controller.manage;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.system.sys.SysUser;
import com.hzs.common.domain.system.sys.ext.SysApprovalExt;
import com.hzs.common.domain.system.sys.ext.SysApprovalItemExt;
import com.hzs.common.domain.system.sys.ext.SysApprovalSendExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.sys.service.*;
import com.hzs.system.sys.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 签呈配置相关
 */
@Slf4j
@RestController
@RequestMapping("/manage/approval")
public class SysApprovalController extends BaseController {

    @Autowired
    private ISysApprovalService iSysApprovalService;
    @Autowired
    private ISysApprovalSendService iSysApprovalSendService;
    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 签呈内容
     *
     * @param approvalBusinessList
     * @return
     */
    @GetMapping("/sign-data/{approvalBusinessList}")
    public AjaxResult signData(@PathVariable("approvalBusinessList") List<Integer> approvalBusinessList) {
        // 查询当前操作用户
        SysUser sysUser = iSysUserService.selectUserById(SecurityUtils.getUserId());
        if (null == sysUser) {
            return AjaxResult.error("用户不存在");
        }

        // 签呈推送列表
        Map<Integer, List<SysApprovalSendExt>> sendMap = new HashMap<>();

        // 查询签呈内容
        List<SysApprovalExt> sysApprovalExtList = iSysApprovalService.lastApprovalAndItem(approvalBusinessList, SecurityUtils.getPkCountry());
        if (CollectionUtil.isNotEmpty(sysApprovalExtList)) {
            // 签呈ID列表
            List<Integer> approvalIdList = sysApprovalExtList.stream().map(SysApprovalExt::getPkId).collect(Collectors.toList());
            // 签呈推送列表
            sendMap = iSysApprovalSendService.queryApprovalSendMap(approvalIdList);
        }

        Date nowDate = new Date();

        // 返回结果列表
        List<ApprovalBusinessVO> resultList = new ArrayList<>();

        for (Integer approvalBusiness : approvalBusinessList) {
            // 审批主体
            SysApprovalExt sysApproval = null;
            // 审批明细
            List<ApprovalBusinessDetailVO> detailList = new ArrayList<>();
            // 抄送人列表
            List<ApprovalBusinessSendVO> sendList = new ArrayList<>();

            for (SysApprovalExt sysApprovalExt : sysApprovalExtList) {
                if (approvalBusiness.equals(sysApprovalExt.getApprovalBusiness())) {
                    sysApproval = sysApprovalExt;
                }
            }
            if (null != sysApproval) {
                // 返回审批明细列表
                for (SysApprovalItemExt sysApprovalItemExt : sysApproval.getItemList()) {
                    detailList.add(ApprovalBusinessDetailVO.builder()
                            .approvalLevel(sysApprovalItemExt.getApprovalLevel())
                            .pkApprover(sysApprovalItemExt.getPkUser())
                            .approver(sysApprovalItemExt.getNickName())
                            .approverDept(sysApprovalItemExt.getDeptName())
                            .build());
                }

                // 返回审核抄送人列表
                List<SysApprovalSendExt> approvalSendList = sendMap.get(sysApproval.getPkId());
                if (CollectionUtil.isNotEmpty(approvalSendList)) {
                    for (SysApprovalSendExt sysApprovalSend : approvalSendList) {
                        sendList.add(ApprovalBusinessSendVO.builder()
                                .pkSend(sysApprovalSend.getPkUser())
                                .send(sysApprovalSend.getNickName())
                                .sendDept(sysApprovalSend.getDeptName())
                                .build());
                    }
                }
            }

            String approvalBusinessVal = null;
            if (EApprovalBusiness.CUSTOM.getValue() == approvalBusiness) {
                approvalBusinessVal = "";
            }
            // 不存在审批配置，没有明细
            resultList.add(ApprovalBusinessVO.builder()
                    .deptName(sysUser.getDeptName())
                    .userName(sysUser.getNickName())
                    .approvalBusiness(approvalBusiness)
                    .approvalBusinessVal(approvalBusinessVal)
                    .creationTime(nowDate)
                    .detailList(detailList)
                    .sendList(sendList)
                    .build());
        }

        return AjaxResult.success(resultList);
    }

}
