package com.ningdatech.pmapi.organization.manage;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.ningdatech.basic.util.StrPool;
import com.ningdatech.pmapi.ding.constants.DingOrganizationContant;
import com.ningdatech.pmapi.organization.model.entity.DingOrganization;
import com.ningdatech.pmapi.organization.service.IDingOrganizationService;
import com.ningdatech.pmapi.organization.constants.ProcDefContant;
import com.wflow.bean.entity.WflowOrgModelHistorys;
import com.wflow.bean.entity.WflowOrgModels;
import com.wflow.enums.ProcessDefTypeEnum;
import com.wflow.service.OrgProcdefHistoryService;
import com.wflow.service.OrgProcdefService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Classname ProcDefManage
 * @Description
 * @Date 2023/7/10 17:48
 * @Author PoffyZhang
 */
@Component
@Slf4j
@AllArgsConstructor
public class ProcDefManage {
    private final IDingOrganizationService organizationService;

    private final OrgProcdefService orgProcdefService;

    private final OrgProcdefHistoryService orgProcdefHistoryService;

    /**
     * 初始化 单位配置
     * @return
     */
    public String init() {
        List<DingOrganization> orgs = organizationService.list(Wrappers.lambdaQuery(DingOrganization.class)
                .in(DingOrganization::getTypeCode, Lists.newArrayList(
                        DingOrganizationContant.UNIT_TYPE,DingOrganizationContant.GOV_TEMPORARY
                )));

        log.info("需要处理初始化单位配置的企业 数量为{}",orgs.size());

        for(DingOrganization org : orgs){
            log.info("当前是 {}",org.getOrganizationName());
            String orgCode = org.getOrganizationCode();

            List<WflowOrgModels> models = orgProcdefService.list(Wrappers.lambdaQuery(WflowOrgModels.class)
                    .eq(WflowOrgModels::getOrgCode, orgCode));

            log.info("已经有的单位流程 {}", JSON.toJSONString(models));

            List<String> types = models.stream().map(WflowOrgModels::getType).collect(Collectors.toList());
            //默认流程
            if(!types.contains(ProcessDefTypeEnum.DEFAULT.name())){
                saveDef(org,ProcessDefTypeEnum.DEFAULT);
            }
            //盖章流程
            if(!types.contains(ProcessDefTypeEnum.SEAL.name())){
                saveDef(org,ProcessDefTypeEnum.SEAL);
            }
            //联审流程
            if(!types.contains(ProcessDefTypeEnum.JOINT_REVIEW.name())){
                saveDef(org,ProcessDefTypeEnum.JOINT_REVIEW);
            }
        }

        return "初始化成功";
    }

    private void saveDef(DingOrganization org,ProcessDefTypeEnum defTypeEnum){
        String procDefId = ProcDefContant.MODELS_ID_PREFIX + IdUtil.objectId();
        WflowOrgModels defaultModel = new WflowOrgModels();
        defaultModel.setProcessDefId(procDefId);
        defaultModel.setIsStop(Boolean.FALSE);
        defaultModel.setProcess(StrPool.BRACE);
        defaultModel.setCreated(LocalDateTime.now());
        defaultModel.setIsDelete(Boolean.FALSE);
        defaultModel.setOrgCode(org.getOrganizationCode());
        defaultModel.setOrgName(org.getOrganizationName());
        defaultModel.setProcessDefName(defTypeEnum.getDesc());
        defaultModel.setType(defTypeEnum.name());
        defaultModel.setSort(1);
        defaultModel.setVersion(1);
        orgProcdefService.save(defaultModel);

        WflowOrgModelHistorys defaultHistory = new WflowOrgModelHistorys();
        defaultHistory.setProcessDefId(procDefId);
        defaultHistory.setProcess(StrPool.BRACE);
        defaultHistory.setCreated(LocalDateTime.now());
        defaultHistory.setOrgCode(org.getOrganizationCode());
        defaultHistory.setOrgName(org.getOrganizationName());
        defaultHistory.setProcessDefName(defTypeEnum.getDesc());
        defaultHistory.setType(defTypeEnum.name());
        defaultHistory.setVersion(0);
        orgProcdefHistoryService.save(defaultHistory);
    }

    public String initByArea(String areaCode) {
        List<DingOrganization> orgs = organizationService.list(Wrappers.lambdaQuery(DingOrganization.class)
                .in(DingOrganization::getTypeCode, Lists.newArrayList(
                        DingOrganizationContant.UNIT_TYPE,DingOrganizationContant.GOV_TEMPORARY
                ))
                .eq(DingOrganization::getDivisionCode,areaCode));

        log.info("{}地区 需要处理初始化单位配置的企业 数量为{}",areaCode,orgs.size());

        for(DingOrganization org : orgs){
            log.info("当前是 {}",org.getOrganizationName());
            String orgCode = org.getOrganizationCode();

            List<WflowOrgModels> models = orgProcdefService.list(Wrappers.lambdaQuery(WflowOrgModels.class)
                    .eq(WflowOrgModels::getOrgCode, orgCode));

            log.info("已经有的单位流程 {}", JSON.toJSONString(models));

            List<String> types = models.stream().map(WflowOrgModels::getType).collect(Collectors.toList());
            //默认流程
            if(!types.contains(ProcessDefTypeEnum.DEFAULT.name())){
                saveDef(org,ProcessDefTypeEnum.DEFAULT);
            }
            //盖章流程
            if(!types.contains(ProcessDefTypeEnum.SEAL.name())){
                saveDef(org,ProcessDefTypeEnum.SEAL);
            }
            //联审流程
            if(!types.contains(ProcessDefTypeEnum.JOINT_REVIEW.name())){
                saveDef(org,ProcessDefTypeEnum.JOINT_REVIEW);
            }
        }

        return "初始化成功";
    }
}
