package com.yeejoin.amos.boot.module.jyjc.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yeejoin.amos.boot.biz.common.bo.ReginParams;
import com.yeejoin.amos.boot.biz.common.entity.DataDictionary;
import com.yeejoin.amos.boot.biz.common.service.impl.DataDictionaryServiceImpl;
import com.yeejoin.amos.boot.module.common.api.dao.ESEquipmentCategory;
import com.yeejoin.amos.boot.module.common.api.dto.ESEquipmentCategoryDto;
import com.yeejoin.amos.boot.module.common.api.enums.EquipmentClassifityEnum;
import com.yeejoin.amos.boot.module.jg.api.enums.BusinessTypeEnum;
import com.yeejoin.amos.boot.module.jg.api.enums.WorkFlowStatusEnum;
import com.yeejoin.amos.boot.module.jyjc.api.entity.JyjcCmTask;
import com.yeejoin.amos.boot.module.jyjc.api.entity.JyjcInspectionApplication;
import com.yeejoin.amos.boot.module.jyjc.api.entity.JyjcInspectionApplicationEquip;
import com.yeejoin.amos.boot.module.jyjc.api.model.*;
import com.yeejoin.amos.boot.module.jyjc.biz.service.TaskModelServiceImpl;
import com.yeejoin.amos.boot.module.ymt.api.entity.IdxBizJgProjectContraption;
import com.yeejoin.amos.boot.module.ymt.api.enums.FlowStatusEnum;
import com.yeejoin.amos.boot.module.ymt.api.mapper.IdxBizJgProjectContraptionMapper;
import com.yeejoin.amos.feign.workflow.model.ProcessTaskDTO;
import com.yeejoin.amos.feign.workflow.model.TaskResultDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.typroject.tyboot.core.foundation.context.RequestContext;
import org.typroject.tyboot.core.rdbms.orm.entity.BaseEntity;
import org.typroject.tyboot.core.restful.exception.instance.BadRequest;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Administrator
 */
@Service
@Slf4j
public class InspectionPlanCommonServiceImpl {

    @Autowired
    private TaskModelServiceImpl taskModelService;

    @Autowired
    private DataDictionaryServiceImpl dataDictionaryService;

    @Autowired
    private CommonServiceImpl commonService;


    private final static String msgTpl = "%s申请单号：%s，进行%s";

    void createdTaskModel(JyjcCmTask model, JyjcInspectionApplication application) {
        TaskModelDto dto = new TaskModelDto();
        TaskMessageDto ta = new TaskMessageDto();
        ta.setInstanceId(model.getInstanceId());
        ta.setStatus(model.getStatus());
        ta.setPromoter(model.getPromoter());
        ta.setSequenceNbr(Long.parseLong(model.getSSeq()));
        ta.setProcessInstanceId(model.getInstanceId());
        ta.setProcessKey(model.getProcessKey());
        ta.setNextTaskId(model.getNextTaskId());
        ta.setBizType(model.getSType());
        dto.setModel(ta);
        dto.setFlowCreateDate(new Date());
        dto.setTaskName(model.getStatus());
        dto.setFlowCode(model.getNextTaskId());
        dto.setTaskContent(this.buildTaskContent(model.getNextTaskCode(), application));
        dto.setTaskCode(model.getNextTaskCode());
        dto.setTaskType(BusinessTypeEnum.INSPECTION_PLAN.getCode());
        dto.setTaskTypeLabel(BusinessTypeEnum.INSPECTION_PLAN.getName());
        dto.setRelationId(model.getInstanceId());
        dto.setExecuteUserIds(model.getNextExecuteUserIds());
        dto.setTaskStatus(commonService.getDictionaryCodeByName(model.getStatus()));
        dto.setTaskStatusLabel(model.getStatus());
        dto.setFlowStatus(dto.getTaskStatus());
        dto.setFlowStatusLabel(model.getStatus());
        dto.setStartUserId(application.getReceiveUserId());
        dto.setStartUser(application.getReceiveUserName());
        dto.setStartUserCompanyName(application.getInspectionUnitName());
        dto.setStartDate(new Date());
        dto.setNextExecuteUser(model.getNextExecuteUserIds());
        dto.setPageType("edit");
        taskModelService.buildTaskModel(Collections.singletonList(dto));
    }

    void createdTaskModel(JyjcCmTask model, ReginParams reginParams, JyjcInspectionApplication application) {
        TaskModelDto dto = new TaskModelDto();
        TaskMessageDto ta = new TaskMessageDto();
        ta.setInstanceId(model.getInstanceId());
        ta.setStatus(model.getStatus());
        ta.setPromoter(model.getPromoter());
        ta.setSequenceNbr(Long.parseLong(model.getSSeq()));
        ta.setProcessInstanceId(model.getInstanceId());
        ta.setProcessKey(model.getProcessKey());
        ta.setNextTaskId(model.getNextTaskId());
        ta.setBizType(model.getSType());
        dto.setModel(ta);
        dto.setFlowCreateDate(new Date());
        dto.setTaskName(model.getStatus());
        dto.setFlowCode(model.getNextTaskId());
        dto.setTaskContent(this.buildTaskContent(model.getNextTaskCode(), application));
        dto.setTaskCode(model.getNextTaskCode());
        dto.setTaskType(BusinessTypeEnum.INSPECTION_PLAN.getCode());
        dto.setTaskTypeLabel(BusinessTypeEnum.INSPECTION_PLAN.getName());
        dto.setRelationId(model.getInstanceId());
        dto.setExecuteUserIds(model.getNextExecuteUserIds());
        dto.setTaskStatus(commonService.getDictionaryCodeByName(model.getStatus()));
        dto.setTaskStatusLabel(model.getStatus());
        dto.setFlowStatus(dto.getTaskStatus());
        dto.setFlowStatusLabel(model.getStatus());
        dto.setStartUserId(RequestContext.getExeUserId());
        dto.setStartUser(reginParams.getUserModel().getRealName());
        dto.setStartUserCompanyName(reginParams.getCompany().getCompanyName());
        dto.setStartDate(new Date());
        dto.setNextExecuteUser(model.getNextExecuteUserIds());
        dto.setPageType("edit");
        taskModelService.buildTaskModel(Collections.singletonList(dto));
    }

    private String buildTaskContent(String taskCode, JyjcInspectionApplication application) {
        if (taskCode.equals(WorkFlowStatusEnum.INSPECTION_PLAN_ASSIGN_USER.getCode())) {
            return String.format(msgTpl, getDictNameByCode(application.getInspectionType(), "JYJC"), application.getApplicationNo(), WorkFlowStatusEnum.INSPECTION_PLAN_ASSIGN_USER.getPass());
        } else if (taskCode.equals(WorkFlowStatusEnum.INSPECTION_PLAN_INSPECT_CONFIRM.getCode())) {
            return String.format(msgTpl, getDictNameByCode(application.getInspectionType(), "JYJC"), application.getApplicationNo(), WorkFlowStatusEnum.INSPECTION_PLAN_INSPECT_CONFIRM.getPass());
        }
        log.error("非法的任务类型：{}", taskCode);
        return "";
    }


    private String getDictNameByCode(String code, String dictType) {
        DataDictionary dataDictionary = dataDictionaryService.getByCode(code, dictType);
        return dataDictionary != null ? dataDictionary.getName() : "";
    }

}
