package com.ruoyi.powerdistribution.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.powerdistribution.domain.PdmRiskRole;
import com.ruoyi.powerdistribution.domain.dto.PdmWorkPlanDTO;
import com.ruoyi.powerdistribution.domain.vo.WorkNumByCompanyStatistics;
import com.ruoyi.powerdistribution.domain.vo.WorkRiskStatistics;
import com.ruoyi.powerdistribution.service.IPdmRiskRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.powerdistribution.mapper.PdmWorkPlanMapper;
import com.ruoyi.powerdistribution.domain.PdmWorkPlan;
import com.ruoyi.powerdistribution.service.IPdmWorkPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 作业计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-01
 */
@Slf4j
@Service
public class PdmWorkPlanServiceImpl implements IPdmWorkPlanService
{
    @Autowired
    private PdmWorkPlanMapper pdmWorkPlanMapper;

    @Autowired
    private IPdmRiskRoleService riskRoleService;

    /**
     * 查询作业计划
     *
     * @param id 作业计划主键
     * @return 作业计划
     */
    @Override
    public PdmWorkPlan selectPdmWorkPlanById(Long id)
    {
        return pdmWorkPlanMapper.selectPdmWorkPlanById(id);
    }

    /**
     * 查询作业计划列表
     *
     * @param pdmWorkPlan 作业计划
     * @return 作业计划
     */
    @Override
    public List<PdmWorkPlan> selectPdmWorkPlanList(PdmWorkPlanDTO pdmWorkPlan)
    {
        return pdmWorkPlanMapper.selectPdmWorkPlanList(pdmWorkPlan);
    }

    /**
     * 新增作业计划
     *
     * @param pdmWorkPlan 作业计划
     * @return 结果
     */
    @Override
    public int insertPdmWorkPlan(PdmWorkPlan pdmWorkPlan)
    {
        pdmWorkPlan.setCreateTime(DateUtils.getNowDate());
        return pdmWorkPlanMapper.insertPdmWorkPlan(pdmWorkPlan);
    }

    /**
     * 修改作业计划
     *
     * @param pdmWorkPlan 作业计划
     * @return 结果
     */
    @Override
    public int updatePdmWorkPlan(PdmWorkPlan pdmWorkPlan)
    {
        pdmWorkPlan.setUpdateTime(DateUtils.getNowDate());
        return pdmWorkPlanMapper.updatePdmWorkPlan(pdmWorkPlan);
    }

    /**
     * 批量删除作业计划
     *
     * @param ids 需要删除的作业计划主键
     * @return 结果
     */
    @Override
    public int deletePdmWorkPlanByIds(Long[] ids)
    {
        return pdmWorkPlanMapper.deletePdmWorkPlanByIds(ids);
    }

    /**
     * 删除作业计划信息
     *
     * @param id 作业计划主键
     * @return 结果
     */
    @Override
    public int deletePdmWorkPlanById(Long id)
    {
        return pdmWorkPlanMapper.deletePdmWorkPlanById(id);
    }

    @Override
    public List<WorkRiskStatistics> getWorkRiskStatistics(String date) {
        return pdmWorkPlanMapper.getWorkRiskStatistics(date);
    }

    @Override
    public List<WorkNumByCompanyStatistics> getWorkNumByCompanyStatistics(WorkNumByCompanyStatistics workNumByCompany) {
        return pdmWorkPlanMapper.getWorkNumByCompanyStatistics(workNumByCompany);
    }

    @Override
    @Transactional
    public void workDataCompute() {
        PdmWorkPlanDTO pdmWorkPlan = new PdmWorkPlanDTO();
        pdmWorkPlan.setRiskType("0");//未处理
        List<PdmWorkPlan> list = pdmWorkPlanMapper.selectPdmWorkPlanList(pdmWorkPlan);
        if(CollectionUtils.isEmpty(list)){
            log.info("没有作业数据需要处理");
            return;
        }
        List<PdmRiskRole> riskRoleList = riskRoleService.selectPdmRiskRoleList(new PdmRiskRole());
        for(PdmWorkPlan plan:list){
            String voltLvlCd =  plan.getVoltLvlCd();
            /**
             * 1，电压等级为其他的数据
             * 2.带电是 停电是
             * 3.带电否 停电否  工作票类型:配电带电作业工作票
             * 4.停电是  工作票类型：第二种工作票*
             */
            if(!"2001008,2001009".contains(voltLvlCd)){
                plan.setExceptionType("1001");//电压等级异常
            }else if(("2006001".equals(plan.getIsEnergizedOpCd()) && "2005001".equals(plan.getPoweroffTypCd()))
            || ("2006002".equals(plan.getIsEnergizedOpCd()) && "2005002".equals(plan.getPoweroffTypCd()) && "2008015".equals(plan.getWorkticketTypCd()))
            || ("2006001".equals(plan.getIsEnergizedOpCd()) && "2008014".equals(plan.getWorkticketTypCd()))){
                plan.setExceptionType("1002");//带电作业异常
            }else{
                //已处理
                plan.setExceptionType("1");
            }
            checkRisk(plan,riskRoleList);
            pdmWorkPlanMapper.updatePdmWorkPlan(plan);
        }

    }

    /**
     * 校验是否高风险
     * @param plan
     * @param riskRoleList
     */
    void checkRisk(PdmWorkPlan plan,List<PdmRiskRole> riskRoleList){
        outerloop:
        for(PdmRiskRole riskRole:riskRoleList){
            JSONObject json = JSONObject.parseObject(riskRole.getRoleDes());
            if(json.containsKey("must")) {
                JSONArray jsonArray = json.getJSONArray("must");//单个
                for (int i = 0; i < jsonArray.size(); i++) {
                    String value = (String) jsonArray.get(i);
                    if (plan.getOpContent().contains(value)) {
                        plan.setRiskType(riskRole.getRoleCode());
                        break outerloop;
                    }
                }
            }
            if(json.containsKey("combination")){
                JSONArray jsonArrayCombination = json.getJSONArray("combination");//组合
                for (int i = 0; i < jsonArrayCombination.size(); i++) {
                    JSONArray jsonArrayChild = jsonArrayCombination.getJSONArray(i);
                    boolean checkResult = true;
                    for (int j = 0; j < jsonArrayChild.size(); j++) {
                        String value = (String) jsonArrayChild.get(j);
                        if (!plan.getOpContent().contains(value)) {
                            checkResult = false;
                            break ;
                        }
                    }
                    if(checkResult){
                        plan.setRiskType(riskRole.getRoleCode());
                        break outerloop;
                    }
                }
            }
        }

        //如果没触发任何规则，则置为1:已处理
        if("0".equals(plan.getRiskType())){
            plan.setRiskType("1");
        }
    }

}
