package com.cool.bang.modules.business.service.impl;

import com.cool.bang.common.base.BaseExamine;
import com.cool.bang.common.base.BaseServiceImpl;
import com.cool.bang.common.constant.enums.BusinessType;
import com.cool.bang.common.constant.enums.FlowType;
import com.cool.bang.common.constant.enums.IncrType;
import com.cool.bang.common.constant.enums.StatusType;
import com.cool.bang.common.tools.number.IncrUtil;
import com.cool.bang.common.tools.secure.SecurityUtil;
import com.cool.bang.modules.approval.domain.ApprovalRecord;
import com.cool.bang.modules.approval.domain.po.ApprovalFlow;
import com.cool.bang.modules.approval.domain.po.ApprovalNode;
import com.cool.bang.modules.approval.domain.po.CompositeNode;
import com.cool.bang.modules.approval.mapper.ApprovalNodeMapper;
import com.cool.bang.modules.approval.mapper.ApprovalRecordMapper;
import com.cool.bang.modules.approval.service.IApprovalFlowService;
import com.cool.bang.modules.approval.service.IApprovalNodeService;
import com.cool.bang.modules.system.domain.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cool.bang.modules.business.mapper.JobQualificationMapper;
import com.cool.bang.modules.business.domain.JobQualification;
import com.cool.bang.modules.business.service.IJobQualificationService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
* 资格申请
*
* @author Sy
* 2024-11-14 15:14
*/
@Service
public class JobQualificationServiceImpl extends BaseServiceImpl<JobQualificationMapper, JobQualification> implements IJobQualificationService {

    @Autowired
    protected IncrUtil incrUtil;
    @Autowired
    protected IApprovalFlowService approvalFlowService;
    @Autowired
    private ApprovalNodeMapper approvalNodeMapper;
    @Autowired
    private ApprovalRecordMapper approvalRecordMapper;

    @Transactional
    @Override
    public boolean qualificationSave(JobQualification param){
        param.setBusinessNumber(IncrType.BZZG.getType() + incrUtil.incr(IncrType.BZZG.getType()));
        param.setDelFlag(StatusType.ENABLE.ordinal());
        param.setCreateTime(new Date());
        initDefaultFlow(param);
        return baseMapper.insert(param) > 0;
    }

    @Override
    public boolean examine(BaseExamine param, JobQualification qualification) {
        // 查询节点信息
        CompositeNode node = approvalNodeMapper.selectNodeById(qualification.getFlowNodeId());
        // 添加审批记录
        User user = SecurityUtil.getCurrentUser();
        ApprovalRecord approvalRecord = new ApprovalRecord();
        approvalRecord.setBusinessNumber(qualification.getBusinessNumber());
        approvalRecord.setFlowNumber(qualification.getFlowNumber());
        approvalRecord.setTableId(qualification.getId());
        approvalRecord.setNodeId(node.getId());
        approvalRecord.setNodeName(node.getName());
        approvalRecord.setApproverId(user.getId());
        approvalRecord.setApprover(user.getRealName());
        approvalRecord.setApprovalTime(new Date());
        approvalRecord.setApprovalStatus(param.getStatus());
        approvalRecord.setApprovalRemark(param.getExamine());
        approvalRecord.insertConfigure();
        approvalRecordMapper.insert(approvalRecord);
        // 审核操作
        if (param.getStatus().equals(StatusType.ENABLE.ordinal())) {
            qualification.setFlowNodeId(node.getNextId());
            qualification.setFlowNodeName(node.getName());
            //
            if (node.getNextId() != null && node.getNextType() == 2) {
                qualification.setFlowNodeName(node.getNextName());
                qualification.setBusinessState(2);
            }
            // 结束节点，业务表状态修改为申请成功，并改为结束节点信息。
            if (node.getNextId() != null && node.getNextType() == 3) {
                qualification.setFlowNodeName(node.getNextName());
                qualification.setBusinessState(3); // 审核成功
            }
        } else {
            qualification.setFlowNodeId(node.getPrevId());
            qualification.setFlowNodeName(node.getPrevName());
            // 如果退回到申请节点，则业务表状态变更为申请退回。
            if (node.getPrevType() == 1) {
                qualification.setFlowNodeId(0L);
                qualification.setFlowNodeName("申请退回");
                qualification.setBusinessState(4); // 申请退回
            }
        }
        return baseMapper.update(qualification) > 0;
    }





    /*
     * 初始化默认申请流程
     */
    private void initDefaultFlow(JobQualification param) {
        ApprovalFlow approvalFlow = approvalFlowService.queryByFlowType(FlowType.ZG_SQ.ordinal());
        param.setFlowNumber(approvalFlow.getFlowNumber());
        if (param.getBusinessState().equals(BusinessType.DEFAULT.ordinal())) {
            param.setFlowNodeId(0L);
            param.setFlowNodeName("暂存");
        } else {
            ApprovalNode approvalNode = approvalNodeMapper.selectStartNodeByFlowId(approvalFlow.getId());
            param.setFlowNodeId(approvalNode.getNextNodeId());
            param.setFlowNodeName(approvalNode.getNodeName());
        }
    }
}
