package cn.palmte.work.service;

import cn.palmte.work.bean.ApproveStatusEnum;
import cn.palmte.work.config.activiti.ActConstant;
import cn.palmte.work.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiongshiyan at 2021/10/29 , contact me with email yanshixiong@126.com or phone 15208384257
 */
@Service
public class ProjectInstanceService {
    private static final Logger logger = LoggerFactory.getLogger(ActProcInsService.class);
    @Autowired
    private ProjectRepository projectRepository;
    @Autowired
    private AdminService adminService;
    @Autowired
    private ActProcInsService actProcInsService;
    @Autowired
    private ProjectInstanceRelationRepository projectInstanceRelationRepository;
    @Autowired
    private ActTaskDefService actTaskDefService;


    /**
     * 开启一个概算流程实例
     */
    public void startEstimateProcessInstance(Project project, Admin admin) throws Exception {
        startProcess(project, admin, ActConstant.PROCESS_DEFKEY_ESTIMATE);
    }

    /**
     * 开启一个预算流程实例
     */
    public void startBudgetProcessInstance(Project project, Admin admin) throws Exception {
        startProcess(project, admin, ActConstant.PROCESS_DEFKEY_BUDGET);
    }

    /**
     * 开启一个结算流程实例
     */
    public void startSettleProcessInstance(Project project, Admin admin) throws Exception {
        startProcess(project, admin, ActConstant.PROCESS_DEFKEY_SETTLE);
    }
    /**
     * 开启一个决算流程实例
     */
    public void startFinalProcessInstance(int projectId, Admin admin) throws Exception {
        Project project = projectRepository.findOne(projectId);
        startProcess(project, admin, ActConstant.PROCESS_DEFKEY_FINAL);
    }

    /**
     * 发起销售合同流程实例
     *
     * @param projectId 项目唯一id
     * @param map 流程变量
     * @throws Exception
     */
    public void startSaleContractProcess(int projectId, Map<String, Object> map) throws Exception {
        String processDefkey = ActConstant.PROCESS_DEFKEY_SALE_CONTRACT;
        String businessKey = String.valueOf(projectId);
        logger.info("startSaleContractProcess processDefkey:{}, businessKey：{}", processDefkey, businessKey);
        String processInstanceId = actProcInsService.startProcessInstance(processDefkey, businessKey, map);
        saveRelation(projectId, processDefkey, processInstanceId);

    }



    /**
     * 发起业务采购流程实例
     *
     * @param projectId 项目唯一id
     * @param map 流程变量
     * @throws Exception
     */
    public void startBusinessPurchaseProcess(int projectId, Map<String, Object> map) throws Exception {
        String processDefkey = ActConstant.PROCESS_DEFKEY_BUSINESS_PURCHASE;
        String businessKey = String.valueOf(projectId);
        logger.info("startBusinessPurchaseProcess processDefkey:{}, businessKey：{}", processDefkey, businessKey);
        String processInstanceId = actProcInsService.startProcessInstance(processDefkey, businessKey, map);

        //保存流程实例id与项目的关联关系
        saveRelation(projectId, processDefkey, processInstanceId);
    }

    /**
     * 销售合同流程 审批（通过或者不通过）
     *
     * @param projectId 项目唯一id
     * @param approveType  2-通过  3-不通过
     * @param message 审批意见
     * @return
     */
    public boolean completeSaleContractTask(int projectId, int approveType, String message) {
        ProjectInstanceRelation projectInstance = projectInstanceRelationRepository.findByProjectIdAndProjectType(projectId, ActConstant.PROJECT_TYPE_SALE_CONTRACT);
        if (projectInstance == null) {
            return false;
        }
        actTaskDefService.completeTaskByProcInsId(projectInstance.getProcessInsId(), approveType, message);
        return true;
    }


    /**
     * 业务采购流程 审批（通过或者不通过）
     *
     * @param projectId 项目唯一id
     * @param approveType  2-通过  3-不通过
     * @param message 审批意见
     * @return
     */
    public boolean completeBusinessPurchaseTask(int projectId, int approveType, String message) {
        ProjectInstanceRelation projectInstance = projectInstanceRelationRepository.findByProjectIdAndProjectType(projectId, ActConstant.PROJECT_TYPE_SALE_CONTRACT);
        if (projectInstance == null) {
            return false;
        }
        actTaskDefService.completeTaskByProcInsId(projectInstance.getProcessInsId(), approveType, message);
        return true;
    }


    private void saveRelation(int projectId, String processDefkey, String processInstanceId) {
        //保存流程实例id与项目的关联关系
        ProjectInstanceRelation relation = new ProjectInstanceRelation();
        relation.setProjectId(projectId);
        relation.setProjectType(ActConstant.PROJECT_TYPE_SALE_CONTRACT);
        relation.setProcessType(processDefkey);
        relation.setProcessInsId(processInstanceId);
        relation.setCreateTime(new Date());
        projectInstanceRelationRepository.saveAndFlush(relation);
    }

    private void startProcess(Project project, Admin admin, String processDefkey) throws Exception {
        HashMap<String, Object> variables = new HashMap<>();
        variables.put(ActConstant.KEY_PROJECT_TYPE, project.getType());
        String businessKey = String.valueOf(project.getId());
        logger.info("startProcess processDefkey:{}, businessKey：{}", processDefkey, businessKey);
        String processInstanceId = actProcInsService.startProcessInstance(processDefkey, businessKey, variables);

        saveProjectInstance(project, processDefkey, processInstanceId);
    }


    public String startProcessBySystem(Project project, String processDefkey, ProjectTaskRecord taskRecord) throws Exception {
        HashMap<String, Object> variables = new HashMap<>();
        variables.put(ActConstant.KEY_PROJECT_TYPE, project.getType());
        String businessKey = String.valueOf(project.getId());
        logger.info("startProcessBySystem processDefkey:{}, businessKey：{}", processDefkey, businessKey);

        String processInstanceId = actProcInsService.startProcessInstanceBySystem(
                processDefkey, businessKey, variables, taskRecord);

        saveProjectInstance(project, processDefkey, processInstanceId);
        return processInstanceId;
    }

    private void saveProjectInstance(Project project, String processDefkey, String processInstanceId) {
        //保存流程实例id与项目的关联关系
        ProjectInstanceRelation relation = new ProjectInstanceRelation();
        relation.setProjectId(project.getId());
        relation.setProcessType(processDefkey);
        relation.setProcessInsId(processInstanceId);
        relation.setCreateTime(new Date());
        projectInstanceRelationRepository.saveAndFlush(relation);
    }

    /**
     * 更新审批人，用于在任务监听器中更新下个审批人
     * @param projectId 项目id
     * @param adminId 审批人id
     */
    public void updateApprover(int projectId, int adminId){
        Project project = projectRepository.findOne(projectId);
        if (project == null) {
            return;
        }
        if (adminId == 0) {
            project.setApproveName("");
        }else {
            Admin admin = adminService.getAdminById(adminId);
            project.setApproveName(admin.getRealName());
        }
        project.setApproveId(adminId);
        project.setLastUpdateTime(new Date());
        projectRepository.saveAndFlush(project);
    }

    /**
     * 更新审批状态
     * @param processKey 审批类型
     * @see ActConstant#PROCESS_DEFKEY_ESTIMATE
     * @see ActConstant#PROCESS_DEFKEY_BUDGET
     * @see ActConstant#PROCESS_DEFKEY_SETTLE
     * @see ActConstant#PROCESS_DEFKEY_FINAL
     */
    public void updateApproveStatus(int projectId, String processKey, ApproveStatusEnum approveStatusEnum){
        Project project = projectRepository.findOne(projectId);
        if(ActConstant.PROCESS_DEFKEY_ESTIMATE.equals(processKey)){
            project.setApproveStatusEstimate(approveStatusEnum.getApproveStatus());
        }else if(ActConstant.PROCESS_DEFKEY_BUDGET.equals(processKey)){
            project.setApproveStatusBudget(approveStatusEnum.getApproveStatus());
        }else if(ActConstant.PROCESS_DEFKEY_SETTLE.equals(processKey)){
            project.setApproveStatusSettle(approveStatusEnum.getApproveStatus());
        }else if(ActConstant.PROCESS_DEFKEY_FINAL.equals(processKey)){
            project.setApproveStatusFinal(approveStatusEnum.getApproveStatus());
        }

        if (approveStatusEnum.getApproveStatus() == ApproveStatusEnum.APPROVAL_PASSED.getApproveStatus()) {
            //审批通过设置审批人为空
            project.setApproveId(0);
            project.setApproveName("");
        }

        project.setLastUpdateTime(new Date());
        projectRepository.saveAndFlush(project);
    }


}
