package com.tofly.xrtygis.handle.plan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tofly.pojo.plan.Pdasign;
import com.tofly.pojo.plan.Pdasignhis;
import com.tofly.xrtygis.custom.entity.dto.WisdomDisplayDto;
import com.tofly.xrtygis.custom.mapper.CustomDisplayMapper;
import com.tofly.xrtygis.custom.service.CustomDisplayService;
import com.tofly.xrtygis.custom.util.DateTimeUtil;
import com.tofly.xrtygis.entity.Dmaregion;
import com.tofly.xrtygis.entity.TfLeakAnalyse;
import com.tofly.xrtygis.entity.TfNrwAnalyse;
import com.tofly.xrtygis.service.DmaregionService;
import com.tofly.xrtygis.service.TfLeakAnalyseService;
import com.tofly.xrtygis.service.TfNrwAnalyseService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 获取漏损率和产销差状态
 *
 * @author JF
 */
@Component
public class NrwLeakRateStatusHandler {

    @Autowired
    private DmaregionService dmaregionService;

    @Autowired
    private CustomDisplayService customDisplayService;

    @Autowired
    private TfNrwAnalyseService nrwAnalyseService;

    @Autowired
    private TfLeakAnalyseService leakAnalyseService;

    @XxlJob("nrwLeakRateStatusHandler")
    public ReturnT<String> automaticallySignBackHandler(String param) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        Calendar calendar = Calendar.getInstance();
        // 设置为上一个月
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        String censusDate = new SimpleDateFormat( "yyyy-MM").format(calendar.getTime());
        WisdomDisplayDto query = new WisdomDisplayDto();
        query.setCensusDate(censusDate);
        //查询上一个月的所有分区的统计参数记录
        List<Map<String,Object>> dmaCensusList = customDisplayService.viewDmaCensusParam(query);
        for(Map<String,Object> one : dmaCensusList){
            if(one.get("dmaId") == null){
                continue;
            }
            Dmaregion dmaregion = dmaregionService.getOne(new QueryWrapper<Dmaregion>().eq("ID",Long.parseLong(String.valueOf(one.get("dmaId")))));
            if(dmaregion == null){
                continue;
            }
            Float nrwDown = dmaregion.getMinDiff();
            Float nrwUp = dmaregion.getMaxDiff();
            Float nrw = Float.parseFloat(String.valueOf(one.get("poorProductionRate")));
            String nrwStatus = getAnalyseStatus(nrw, nrwUp, nrwDown);

            TfNrwAnalyse tfNrwAnalyse = new TfNrwAnalyse();
            tfNrwAnalyse.setNrwUp(nrwUp);
            tfNrwAnalyse.setNrwDown(nrwDown);
            tfNrwAnalyse.setStatus(nrwStatus);
            tfNrwAnalyse.setDmaId(Long.parseLong(String.valueOf(one.get("dmaId"))));
            tfNrwAnalyse.setDmaLevel(Integer.parseInt(String.valueOf(one.get("partitionLevel"))));
            tfNrwAnalyse.setDmaName(String.valueOf(one.get("dmaName")));

            try {
                tfNrwAnalyse.setStatisticalDate(sdf.parse(String.valueOf(one.get("statisticalDate"))));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            TfNrwAnalyse nrwAnalyseQuery = nrwAnalyseService.getOne(new QueryWrapper<TfNrwAnalyse>()
                    .eq("DMA_ID",tfNrwAnalyse.getDmaId())
                    .eq("STATISTICAL_DATE",tfNrwAnalyse.getStatisticalDate()));
            if(nrwAnalyseQuery == null){
                nrwAnalyseService.save(tfNrwAnalyse);
            }

            Float leakDown = dmaregion.getLeakDown();
            Float leakUp = dmaregion.getLeakUp();
            Float leakRate = Float.parseFloat(String.valueOf(one.get("leakageRate")));
            String leakageStatus = getAnalyseStatus(leakRate, leakUp, leakDown);
            TfLeakAnalyse tfLeakAnalyse = new TfLeakAnalyse();
            tfLeakAnalyse.setLeakUp(leakUp);
            tfLeakAnalyse.setLeakDown(leakDown);
            tfLeakAnalyse.setStatus(leakageStatus);
            tfLeakAnalyse.setDmaId(Long.parseLong(String.valueOf(one.get("dmaId"))));
            tfLeakAnalyse.setDmaLevel(Integer.parseInt(String.valueOf(one.get("partitionLevel"))));
            tfLeakAnalyse.setDmaName(String.valueOf(one.get("dmaName")));

            try {
                tfLeakAnalyse.setStatisticalDate(sdf.parse(String.valueOf(one.get("statisticalDate"))));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            TfLeakAnalyse leakAnalyseQuery = leakAnalyseService.getOne(new QueryWrapper<TfLeakAnalyse>()
                    .eq("DMA_ID",tfLeakAnalyse.getDmaId())
                    .eq("STATISTICAL_DATE",tfLeakAnalyse.getStatisticalDate()));
            if(leakAnalyseQuery == null){
                leakAnalyseService.save(tfLeakAnalyse);
            }
        }
        return ReturnT.SUCCESS;
    }

    //判断正常异常状态
    public String getAnalyseStatus(Float data, Float up, Float down) {

        if(up == null || down == null || data == null){
            return null;
        }

        if(data >= down && data <= up){
            return "1";
        }else {
            return "0";
        }
    }

}
