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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yeejoin.amos.boot.module.hygf.api.Enum.*;
import com.yeejoin.amos.boot.module.hygf.api.dto.BasicGridAcceptanceDto;
import com.yeejoin.amos.boot.module.hygf.api.dto.WorkflowResultDto;
import com.yeejoin.amos.boot.module.hygf.api.entity.*;
import com.yeejoin.amos.boot.module.hygf.api.mapper.*;
import com.yeejoin.amos.boot.module.hygf.api.service.IBasicGridAcceptanceService;
import com.yeejoin.amos.boot.module.hygf.api.util.NumberUtil;
import com.yeejoin.amos.feign.workflow.model.ActWorkflowBatchDTO;
import com.yeejoin.amos.feign.workflow.model.ActWorkflowStartDTO;
import com.yeejoin.amos.feign.workflow.model.ProcessTaskDTO;
import com.yeejoin.amos.feign.workflow.model.TaskResultDTO;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.typroject.tyboot.core.rdbms.service.BaseService;

import java.util.*;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class BasicGridAcceptanceServiceImpl extends BaseService<BasicGridAcceptanceDto, BasicGridAcceptance, BasicGridAcceptanceMapper> implements IBasicGridAcceptanceService {

    private final String OK = "0";
    private final String PASS = "5";
    @Autowired
    BasicGridAcceptanceMapper basicGridAcceptanceMapper;
    @Autowired
    HygfOnGridMapper onGridMapper;
    @Autowired
    HygfOnGridServiceImpl hygfOnGridServiceImpl;
    @Autowired
    PersonnelBusinessMapper personnelBusinessMapper;
    //    @Autowired
//    WorkflowImpl workflow;
    @Autowired
    AcceptanceCheckItemMapper acceptanceCheckItemMapper;
    @Autowired
    AcceptanceRectificationOrderMapper acceptanceRectificationOrderMapper;
    @Autowired
    RegionalCompaniesMapper regionalCompaniesMapper;
    @Autowired
    WorkOrderMapper workOrderMapper;
    @Autowired
    PeasantHouseholdMapper peasantHouseholdMapper;
    @Autowired
    private WorkFlowService workFlowService;
    @Autowired
    private CommonServiceImpl commonService;

    public Page<BasicGridAcceptanceDto> selectPage(Long regionCompanyId, Long amosDealerId, int current, int size, String projectAddress, String powerStationCode, String ownersName, String gridStatus, String gridConnectionTime, String formType) throws Exception {
        PageHelper.startPage(current, size);
        Map<String, Object> map = new HashMap<>();
        Date date = null;
//        if (StringUtils.isNotEmpty(gridConnectionTime)) {
//            date = DateUtil.formatStringToDate(gridConnectionTime, "yyyy-MM-dd HH:mm:ss");
//        }
        map.put("projectAddress", projectAddress);
        map.put("powerStationCode", powerStationCode);
        map.put("ownersName", ownersName);
        map.put("gridStatus", gridStatus);
        map.put("gridConnectionTime", gridConnectionTime);
        map.put("formType", formType);
        map.put("regionCompanyId", regionCompanyId);
        map.put("amosDealerId", amosDealerId);
        List<BasicGridAcceptanceDto> list = basicGridAcceptanceMapper.selectPageList(map);
        PageInfo<BasicGridAcceptanceDto> page = new PageInfo(list);
        Page<BasicGridAcceptanceDto> pageNew = new Page<>();
        pageNew.setCurrent(current);
        pageNew.setTotal(page.getTotal());
        pageNew.setSize(size);
        pageNew.setRecords(page.getList());
        return pageNew;
    }

    public HygfOnGrid saveEntity(HygfOnGrid grid) {
        if (grid.getSequenceNbr() != null) {
            onGridMapper.updateById(grid);
        } else {
            onGridMapper.insert(grid);
        }

        return grid;
    }

    @GlobalTransactional
    public HygfOnGrid saveAndCommit(HygfOnGrid grid, String userId) {

        BasicGridAcceptance basicGridAcceptance = basicGridAcceptanceMapper.selectOne(new LambdaQueryWrapper<BasicGridAcceptance>().eq(BasicGridAcceptance::getWorkOrderPowerStationId, grid.getWorkOrderPowerStationId()));
        basicGridAcceptance.setGridStatus(GridStatusEnum.DSH.getCode());
        List<ProcessTaskDTO> processTaskDTOS = new ArrayList<>();
        if (grid.getType() != null && "1".equals(grid.getType())) {
//            // 执行工作流
//            StandardDto standardDto = new StandardDto();
//            standardDto.setTaskId(basicGridAcceptance.getNextTaskId());
//            workflow.standard(basicGridAcceptance, standardDto, userId);
            //执行流程
            TaskResultDTO task = new TaskResultDTO();
            task.setTaskId(basicGridAcceptance.getNextTaskId());
            ProcessTaskDTO processTaskDTO = workFlowService.standard(basicGridAcceptance, task, userId);
            processTaskDTOS.add(processTaskDTO);
            updateBasicGridAcceptanceTaskByInstanceId(basicGridAcceptance,basicGridAcceptance.getInstanceId());
        } else {
            // 调用工作流执行第一个节点
//            ProcessDto processDto = new ProcessDto();
//            processDto.setProcessDefinitionKey("hygf_bwys");
//            processDto.setBusinessKey(String.valueOf(basicGridAcceptance.getSequenceNbr()));
//            StartProcessDto startProcessDto = new StartProcessDto();
//            List<ProcessDto> process = new ArrayList<>();
//            process.add(processDto);
//            startProcessDto.setProcess(process);
//            workflow.startProcess(basicGridAcceptance, startProcessDto, userId);

            //开启工作流 并执行一步
            ActWorkflowBatchDTO actWorkflowBatchDTO = new ActWorkflowBatchDTO();
            List<ActWorkflowStartDTO> list = new ArrayList<>();
            ActWorkflowStartDTO dto = new ActWorkflowStartDTO();
            dto.setProcessDefinitionKey(BusinessTypeEnum.HYGF_BWYS.getCode());
            dto.setBusinessKey(String.valueOf(basicGridAcceptance.getSequenceNbr()));
            dto.setCompleteFirstTask(true);
            list.add(dto);
            actWorkflowBatchDTO.setProcess(list);
            processTaskDTOS = workFlowService.startBatch(actWorkflowBatchDTO);
            basicGridAcceptance = workFlowService.getBasicGridAcceptance(basicGridAcceptance, processTaskDTOS, userId);
            // 线上验收
            LambdaUpdateWrapper<PeasantHousehold> up = new LambdaUpdateWrapper<>();
            up.set(PeasantHousehold::getConstructionState, ArrivalStateeEnum.并网中.getCode());
            long idsk = basicGridAcceptance.getPeasantHouseholdId();
            up.eq(PeasantHousehold::getSequenceNbr, idsk);
            peasantHouseholdMapper.update(null, up);

        }

        //防止重复提交
//		if (grid.getSequenceNbr() != null) {
//			onGridMapper.updateById(grid);
//		} else {
//			onGridMapper.insert(grid);
//		}
        LambdaQueryWrapper<HygfOnGrid> hog = new LambdaQueryWrapper<>();
        hog.eq(HygfOnGrid::getPeasantHouseholdId, grid.getPeasantHouseholdId());
        hygfOnGridServiceImpl.saveOrUpdate(grid,hog);

        basicGridAcceptanceMapper.updateById(basicGridAcceptance);
        //发起待办
        List<WorkflowResultDto> workflowResultDtos = workFlowService.buildWorkFlowInfo(processTaskDTOS);
        WorkflowResultDto workflowResultDto = workflowResultDtos.get(0);
        workflowResultDto.setInstanceId(basicGridAcceptance.getInstanceId());
        commonService.buildTaskModel(commonService.buildTaskModelDto(basicGridAcceptance, workflowResultDto, BusinessTypeEnum.HYGF_BWYS));

        return grid;
    }

    private void updateBasicGridAcceptanceTaskByInstanceId(BasicGridAcceptance basicGridAcceptance, String instanceId) {
        Map<String, Object> updateTaskParam = new HashMap<>();
        updateTaskParam.put("flowStatus", FlowStatusEnum.TO_BE_FINISHED.getCode());
        updateTaskParam.put("flowStatusLabel", FlowStatusEnum.TO_BE_FINISHED.getName());
        updateTaskParam.put("taskStatus", FlowStatusEnum.TO_BE_FINISHED.getCode());
        updateTaskParam.put("taskStatusLabel", FlowStatusEnum.TO_BE_FINISHED.getName());
        updateTaskParam.put("model", basicGridAcceptance);
        updateTaskParam.put("relationId", instanceId);
        commonService.updateTaskModel(updateTaskParam);
    }


    public HygfOnGrid modifyEntity(HygfOnGrid grid) {
        onGridMapper.updateById(grid);
        return grid;
    }

    public Boolean deleteForBatch(Long[] sequenceNbrs) {
        if (Objects.isNull(sequenceNbrs) || sequenceNbrs.length == 0) {
            return false;
        }
        Boolean bool = Boolean.TRUE;
        Collection<HygfOnGrid> grids = onGridMapper.selectBatchIds(Arrays.asList(sequenceNbrs));
        for (HygfOnGrid x : grids) {
            x.setIsDelete(true);
            int num = onGridMapper.updateById(x);
            BasicGridAcceptance acceptance = basicGridAcceptanceMapper.selectOne(new LambdaQueryWrapper<BasicGridAcceptance>().eq(BasicGridAcceptance::getWorkOrderPowerStationId, x.getWorkOrderPowerStationId()));
            acceptance.setIsDelete(true);
            int count = basicGridAcceptanceMapper.updateById(acceptance);
            if (0 == num || 0 == count) {
                bool = Boolean.FALSE;
                break;
            }
        }
        return bool;
    }

    @GlobalTransactional
    public void execute(AcceptanceCheckItem dto, String userId) {
        // 查询并网审批信息
        BasicGridAcceptance basicGridAcceptanc = basicGridAcceptanceMapper.selectById(dto.getBasicGridAcceptanceId());
        String nextNodeKey = basicGridAcceptanc.getNextNodeKey();
        if (OK.equals(dto.getApprovalStatus())) {
            if (BasicGridAcceptancEnum.并网管理端工程审核.getCode().equals(nextNodeKey)) {
                basicGridAcceptanc.setGridStatus(GridStatusEnum.YWC.getCode());
                basicGridAcceptanc.setAcceptanceStatus(AcceptanceStatusEnum.待提交验收.getCode());
                // 并网时间
                LambdaUpdateWrapper<HygfOnGrid> upq = new LambdaUpdateWrapper();
                upq.eq(HygfOnGrid::getSequenceNbr, dto.getFonGridId());
                upq.set(HygfOnGrid::getGridConnectionTime, new Date());
                onGridMapper.update(null, upq);
                // 并网完成
                LambdaUpdateWrapper<PeasantHousehold> up = new LambdaUpdateWrapper<>();
                up.set(PeasantHousehold::getConstructionState, ArrivalStateeEnum.并网完成.getCode());
                long idsk = basicGridAcceptanc.getPeasantHouseholdId();
                up.eq(PeasantHousehold::getSequenceNbr, idsk);
                peasantHouseholdMapper.update(null, up);

            } else {

                basicGridAcceptanc.setAcceptanceStatus(AcceptanceStatusEnum.getNodeByKey(nextNodeKey, dto.getApprovalStatus()));
            }

        } else {
            if (BasicGridAcceptancEnum.并网管理端工程审核.getCode().equals(nextNodeKey)) {
                basicGridAcceptanc.setGridStatus(GridStatusEnum.WTG.getCode());
                basicGridAcceptanc.setAcceptanceStatus(AcceptanceStatusEnum.getNodeByKey(nextNodeKey, dto.getApprovalStatus()));
            }
            // 投融/法务/工程/线下验收审核不通过
            else if (BasicGridAcceptancEnum.管理端投融审核.getCode().equals(nextNodeKey) || BasicGridAcceptancEnum.管理端法务审核.getCode().equals(nextNodeKey) || BasicGridAcceptancEnum.管理端工程审核.getCode().equals(nextNodeKey) || BasicGridAcceptancEnum.管理端工程线下验.getCode().equals(nextNodeKey)) {
                AcceptanceRectificationOrder rectificationOrder = new AcceptanceRectificationOrder();
                try {
                    WorkOrder workOrder = workOrderMapper.selectById(basicGridAcceptanc.getWorkOrderId());
                    RegionalCompanies regionalCompanies = regionalCompaniesMapper.selectRegionName(workOrder.getRegionCompanyId());
                    String code = NumberUtil.getCode(CodeEnum.整改单.getCode(), regionalCompanies.getCompanyCode(), regionalCompanies.getRegionalAddress());
                    rectificationOrder.setRectificationOrderCode(code);
                    rectificationOrder.setWorkOrderId(basicGridAcceptanc.getWorkOrderId());
                    rectificationOrder.setWorkOrderPowerStationId(basicGridAcceptanc.getWorkOrderPowerStationId());
                    rectificationOrder.setPeasantHouseholdId(basicGridAcceptanc.getPeasantHouseholdId());
                    rectificationOrder.setProblemDescription(dto.getComment());
                    rectificationOrder.setRectificationStatus(String.valueOf(RectificationStatusEnum.待整改.getCode()));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                acceptanceRectificationOrderMapper.insert(rectificationOrder);
                basicGridAcceptanc.setAcceptanceStatus(AcceptanceStatusEnum.getNodeByKey(nextNodeKey, dto.getApprovalStatus()));
            } else {
                basicGridAcceptanc.setAcceptanceStatus(AcceptanceStatusEnum.getNodeByKey(nextNodeKey, dto.getApprovalStatus()));
            }
        }

        if (nextNodeKey.equals(BasicGridAcceptancEnum.管理端工程线下验.getCode())) {
            dto.setWorkOrderId(basicGridAcceptanc.getWorkOrderId());
            dto.setWorkOrderPowerStationId(basicGridAcceptanc.getWorkOrderPowerStationId());
            dto.setPeasantHouseholdId(basicGridAcceptanc.getPeasantHouseholdId());
            acceptanceCheckItemMapper.insert(dto);
        }
        // 执行工作流
//        StandardDto standardDto = new StandardDto();
//        standardDto.setComment(dto.getComment());
//        standardDto.setResult(dto.getApprovalStatus());
//        standardDto.setTaskId(basicGridAcceptanc.getNextTaskId());
//        VariableDto variable = new VariableDto();
//        variable.setApprovalStatus(dto.getApprovalStatus());
//        variable.setComment(dto.getComment());
//        variable.setOperationTime(dto.getOperationTime());
//        variable.setOperator(dto.getOperator());
//        standardDto.setVariable(variable);
//        BasicGridAcceptance workBasicGridAcceptance = workflow.standard(basicGridAcceptanc, standardDto, userId);
        // 执行流程
        TaskResultDTO taskResultDTO = new TaskResultDTO();
        taskResultDTO.setComment(dto.getComment());
        taskResultDTO.setResult(dto.getApprovalStatus());
        taskResultDTO.setTaskId(basicGridAcceptanc.getNextTaskId());
        HashMap<String, Object> variableMap = new HashMap<>();
        variableMap.put("approvalStatus", dto.getApprovalStatus());
        variableMap.put("comment", dto.getComment());
        variableMap.put("operationTime", dto.getOperationTime());
        variableMap.put("operator", dto.getOperator());
        taskResultDTO.setVariable(variableMap);
        ProcessTaskDTO processTaskDTO = workFlowService.standard(basicGridAcceptanc, taskResultDTO, userId);
        if (basicGridAcceptanc.getNextNodeKey().equals(BasicGridAcceptancEnum.管理端工程线下验.getCode())) {
            // 线下验收
            LambdaUpdateWrapper<PeasantHousehold> up = new LambdaUpdateWrapper<>();
            up.set(PeasantHousehold::getConstructionState, ArrivalStateeEnum.线下验收.getCode());
            long idsk = basicGridAcceptanc.getPeasantHouseholdId();
            up.eq(PeasantHousehold::getSequenceNbr, idsk);
            peasantHouseholdMapper.update(null, up);
        }

        // 验收完成
        System.out.println("验收完成===============================" + basicGridAcceptanc.getNextTaskId());
        System.out.println("验收完成888888===============================" + basicGridAcceptanc.getAcceptanceStatus());

        if ("10".equals(basicGridAcceptanc.getAcceptanceStatus())) {
            // 更新状态
            LambdaUpdateWrapper<PeasantHousehold> up = new LambdaUpdateWrapper<>();
            up.set(PeasantHousehold::getConstructionState, ArrivalStateeEnum.验收完成.getCode());
            long idsk = basicGridAcceptanc.getPeasantHouseholdId();
            up.eq(PeasantHousehold::getSequenceNbr, idsk);
            peasantHouseholdMapper.update(null, up);

        }

        basicGridAcceptanceMapper.updateById(basicGridAcceptanc);
        //更新待办
        WorkflowResultDto workflowResultDto = workFlowService.buildWorkFlowInfo(CollectionUtil.newArrayList(processTaskDTO)).get(0);
        workflowResultDto.setInstanceId(basicGridAcceptanc.getInstanceId());
        updateBasicGridAcceptanceTask(basicGridAcceptanc, workflowResultDto);
    }

    /**
     * 此处先更新完成待办然后在新增待办
     * @param basicGridAcceptance
     * @param workflowResultDto
     */
    private void updateBasicGridAcceptanceTask(BasicGridAcceptance basicGridAcceptance, WorkflowResultDto workflowResultDto) {
       updateBasicGridAcceptanceTaskByInstanceId(basicGridAcceptance,workflowResultDto.getInstanceId());
        commonService.buildTaskModel((commonService.buildTaskModelDto(basicGridAcceptance, workflowResultDto, BusinessTypeEnum.HYGF_BWYS)));
    }
}
