package com.ningdatech.pmapi.sys.processDef;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.pmapi.AppTests;
import com.ningdatech.pmapi.common.constant.RegionConst;
import com.ningdatech.pmapi.common.enumeration.ProjectProcessStageEnum;
import com.ningdatech.pmapi.common.helper.RegionCacheHelper;
import com.ningdatech.pmapi.scheduler.contants.TaskContant;
import com.ningdatech.pmapi.sys.model.dto.RegionDTO;
import com.ningdatech.pmapi.sys.service.IRegionService;
import com.wflow.bean.dto.WflowModelHistorysInsertDto;
import com.wflow.bean.entity.WflowForms;
import com.wflow.workflow.bean.process.ProcessNode;
import com.wflow.workflow.service.ProcessModelService;
import com.wflow.workflow.service.WflowFormsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * @Classname ProcessTest
 * @Description
 * @Date 2023/2/4 9:54
 * @Author PoffyZhang
 */
@Slf4j
public class ProcessDefTest extends AppTests {
    @Resource
    private ProcessModelService processModelService;
    @Resource
    private WflowFormsService formsService;
    @Resource
    private RegionCacheHelper regionCacheHelper;
    @Test
    public void test() {
        String processStr = "{\"id\":\"node_039152532706\",\"name\":\"审批单位\",\"type\":\"ORG\",\"props\":{\"regionCode\":\"\",\"orgCode\":\"GO_3bc86256687a4884ae410af00682b762\",\"type\":\"DEFAULT\",\"processDefId\":\"\",\"subNode\":[]},\"children\":{},\"parentId\":\"root\"}";

        Node node = JSON.parseObject(processStr, Node.class);

        OrgProps props = JSON.parseObject(node.getProps().toString(), OrgProps.class);
        System.out.println(props.getOrgCode());
    }

    @Test
    public void date(){
        System.out.println(LocalDateTime.parse("2023-02-05 10:25", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));
    }

    @Test
    public void apply() {
        log.info("=========== 初始化丽水二期 系统表单和流程配置 ======== 任务开始");
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        //1.查出丽水市下的 区县 分别去初始化 表单和流程配置数据
        List<RegionDTO> regions = regionCacheHelper.listChildren(RegionConst.RC_LS, RegionConst.RL_CITY);

        if(CollUtil.isEmpty(regions)){
            throw new BizException("丽水地区数据为空 任务结束!");
        }

        for(RegionDTO region : regions){
            log.info("当前初始化的是 【{}】 流程配置",region.getRegionName());

            Integer[] processTypeList = TaskContant.Wflow.APPLY_PROCESS_TYPE_LIST;
            for(Integer processType : processTypeList){
                String formName = ProjectProcessStageEnum.getDesc(processType);
                WflowModelHistorysInsertDto models = new WflowModelHistorysInsertDto();

                models.setGroupId(1);
                models.setProcessDefId("pd" + IdUtil.objectId());
                models.setFormName(formName);
                models.setProcessType(processType);
                models.setRegionCode(region.getRegionCode());
                String process = "{\"children\":{\"children\":{},\"id\":\"node_039152532706\",\"name\":\"审批人\",\"parentId\":\"root\",\"props\":{\"mode\":\"AND\",\"role\":[],\"refuse\":{\"type\":\"TO_END\",\"target\":\"\"},\"assignedOrg\":[{\"orgName\":\"数转办\",\"orgCode\":\"GO_3bc86256687a4884ae410af00682b762\",\"type\":\"DEFAULT\",\"processDefId\":\"\"}],\"sign\":false,\"assignedType\":\"ASSIGN_ORG\",\"assignedDept\":[]},\"type\":\"SUB\"},\"id\":\"root\",\"name\":\"发起人\",\"props\":{\"formPerms\":[],\"assignedUser\":[]},\"type\":\"ROOT\"}";
                ProcessNode processNode = JSON.parseObject(process,ProcessNode.class);
                models.setProcess(processNode);
                models.setSettings("{\"sign\":false,\"admin\":[],\"notify\":{},\"commiter\":[]}");
                models.setFormItems("[]");

                String formId = processModelService.insertProcess(models);
                if(StringUtils.isNotBlank(formId)){
                    //初始的流程在部署表也存一份，用来查询
                    if(StringUtils.isNotBlank(processModelService.deployProcess(formId,null))){
                        log.info("当前区域 【{}】 流程名[{}] 流程数据已经保存成功",region.getRegionName(),formName);
                    }else{
                        log.info("当前区域 【{}】 流程名[{}] 流程数据保存失败",region.getRegionName(),formName);
                    }
                }else{
                    log.info("当前区域 【{}】流程名[{}]  流程数据保存失败",region.getRegionName(),formName);
                }
            }
        }

        stopWatch.stop();
        log.info("=========== 初始化丽水二期 系统表单和流程配置 ======== 任务结束 {}s",stopWatch.getTotalTimeSeconds());
    }

    public static class OrgProps {
        private String regionCode;

        private String orgCode;

        private String type;

        private String processDefId;

        private List<Node> subNode;

        public String getRegionCode() {
            return regionCode;
        }

        public void setRegionCode(String regionCode) {
            this.regionCode = regionCode;
        }

        public String getOrgCode() {
            return orgCode;
        }

        public void setOrgCode(String orgCode) {
            this.orgCode = orgCode;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getProcessDefId() {
            return processDefId;
        }

        public void setProcessDefId(String processDefId) {
            this.processDefId = processDefId;
        }

        public List<Node> getSubNode() {
            return subNode;
        }

        public void setSubNode(List<Node> subNode) {
            this.subNode = subNode;
        }
    }
}
