package com.hzs.system.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.domain.system.sys.SysApproval;
import com.hzs.common.domain.system.sys.SysApprovalItem;
import com.hzs.common.domain.system.sys.SysApprovalSend;
import com.hzs.common.domain.system.sys.ext.SysApprovalExt;
import com.hzs.system.sys.mapper.SysApprovalItemMapper;
import com.hzs.system.sys.mapper.SysApprovalMapper;
import com.hzs.system.sys.mapper.SysApprovalSendMapper;
import com.hzs.system.sys.param.ApprovalParam;
import com.hzs.system.sys.param.ApprovalSaveParam;
import com.hzs.system.sys.service.ISysApprovalService;
import com.hzs.system.sys.vo.ApprovalVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 审批配置表 服务实现类
 *
 * @author hzs
 * @since 2022-09-15
 */
@Service
public class SysApprovalServiceImpl extends ServiceImpl<SysApprovalMapper, SysApproval> implements ISysApprovalService {

    @Autowired
    private SysApprovalItemMapper sysApprovalItemMapper;
    @Autowired
    private SysApprovalSendMapper sysApprovalSendMapper;

    @Override
    public List<ApprovalVO> approvalList(ApprovalParam param, Integer pkCountry) {
        return baseMapper.approvalList(param, pkCountry);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveApproval(List<ApprovalSaveParam> paramList, Long userId, Integer pkCountry) {
        Date nowDate = new Date();

        // 遍历处理业务审批
        for (ApprovalSaveParam param : paramList) {
            SysApproval sysApproval = new SysApproval();
            sysApproval.setApprovalType(param.getApprovalType());
            sysApproval.setApprovalBusiness(param.getApprovalBusiness());
            sysApproval.setEffectiveDate(param.getEffectiveDate());
            sysApproval.setPkCreator(userId);
            sysApproval.setCreationTime(nowDate);
            sysApproval.setPkCountry(pkCountry);
            if (baseMapper.insert(sysApproval) > 0) {
                // 审批保存成功，处理审批明细项
                List<SysApprovalItem> itemList = new ArrayList<>(param.getUserIdList().size());
                for (int i = 0; i < param.getUserIdList().size(); i++) {
                    SysApprovalItem sysApprovalItem = new SysApprovalItem();
                    sysApprovalItem.setPkApproval(sysApproval.getPkId());
                    sysApprovalItem.setApprovalLevel(i + 1);
                    sysApprovalItem.setPkUser(param.getUserIdList().get(i));

                    itemList.add(sysApprovalItem);
                }
                sysApprovalItemMapper.insertBatch(itemList);

                if (CollectionUtil.isNotEmpty(param.getSendIdList())) {
                    // 抄送用户ID列表
                    List<SysApprovalSend> sendList = new ArrayList<>(param.getSendIdList().size());
                    for (Long sendId : param.getSendIdList()) {
                        SysApprovalSend sysApprovalSend = new SysApprovalSend();
                        sysApprovalSend.setPkApproval(sysApproval.getPkId());
                        sysApprovalSend.setPkUser(sendId);
                        sendList.add(sysApprovalSend);
                    }
                    sysApprovalSendMapper.insertBatch(sendList);
                }
            }
        }
    }

    @Override
    public void deleteApproval(Integer id, Long userId, Integer pkCountry) {
        // 删除审批
        UpdateWrapper<SysApproval> approvalUpdateWrapper = new UpdateWrapper<>();
        approvalUpdateWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
        approvalUpdateWrapper.eq(SystemFieldConstants.PK_ID, id);
        approvalUpdateWrapper.set(SystemFieldConstants.DEL_FLAG, EDelFlag.DELETE.getValue());
        approvalUpdateWrapper.set(SystemFieldConstants.PK_MODIFIED, userId);
        approvalUpdateWrapper.set(SystemFieldConstants.MODIFIED_TIME, new Date());
        baseMapper.update(null, approvalUpdateWrapper);
    }

    @Override
    public List<SysApproval> lastApproval(List<Integer> approvalBusinessList, Integer pkCountry) {
        return baseMapper.lastApproval(approvalBusinessList, pkCountry);
    }

    @Override
    public List<SysApprovalExt> lastApprovalAndItem(List<Integer> approvalBusinessList, Integer pkCountry) {
        return baseMapper.lastApprovalAndItem(approvalBusinessList, pkCountry);
    }

}
