package com.sky.ioc.service.carbon.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.ioc.config.RestTemplateConfig;
import com.sky.ioc.entity.Cake;
import com.sky.ioc.entity.Indeicator;
import com.sky.ioc.entity.Label;
import com.sky.ioc.entity.News;
import com.sky.ioc.entity.domain.carbon.Carbon;
import com.sky.ioc.entity.domain.carbon.CarbonNotice;
import com.sky.ioc.entity.domain.carbon.CarbonQuota;
import com.sky.ioc.entity.domain.space.Company;
import com.sky.ioc.entity.domain.space.Department;
import com.sky.ioc.entity.params.IocParam;
import com.sky.ioc.entity.params.IocTimeRange;
import com.sky.ioc.mapper.BusMapper;
import com.sky.ioc.mapper.PrintMapper;
import com.sky.ioc.mapper.carbon.CarbonMapper;
import com.sky.ioc.mapper.carbon.CarbonNoticeMapper;
import com.sky.ioc.mapper.carbon.CarbonQuotaMapper;
import com.sky.ioc.mapper.space.CompanyMapper;
import com.sky.ioc.mapper.space.DepartmentMapper;
import com.sky.ioc.service.carbon.CarbonService;
import com.sky.ioc.tool.DateUtil;
import com.sky.ioc.tool.GeneralMethod;
import com.sky.ioc.tool.MathUtils;
import com.sky.ioc.tool.ReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

/**
 * TODO  双碳
 * @Description
 * @Author LunCe
 * @Date 2023/2/22 11:40
 **/

@Service
@Slf4j
public class CarbonServiceImpl implements CarbonService {


    @Autowired
    BusMapper busMapper;

    @Autowired
    PrintMapper printMapper;

    @Autowired
    private RestTemplateConfig restTemplateConfig;

    final static  String CARBON_QUOTA_URL="/api/carbon/CARBON/quota/list";
    final static  String CARBON_NOTICE_URL="/api/carbon/CARBON/notice/list";

    @Autowired
    CarbonQuotaMapper carbonQuotaMapper;

    @Autowired
    CarbonNoticeMapper carbonNoticeMapper;

    @Autowired
    CarbonMapper carbonMapper;

    @Autowired
    DepartmentMapper departmentMapper;


    /**
     * TODO  智能看板 --碳配额
     * @Description
     * @Return com.sky.ioc.tool.ReturnMsg
     * @Author LunCe
     * @date 2023/2/22 11:41
     */
    @Override
    public ReturnMsg getCarbonQuota() {
        String[] param = {"consumed","carbonQuota"};
        String[] labelKey = {"总部","规划院","郑分","广分","上分"};
        List<Label> labels = GeneralMethod.getInstance().dataGeneration(param, labelKey);
        return ReturnMsg.ok(labels);
    }

    @Override
    public ReturnMsg getPrintingCoreIndicators(IocParam iocParam) {
        Indeicator indeicator = new Indeicator();
        List<JSONObject> objects = indeicator.getList();
        float compare = (float) Math.random();
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        compare = Float.parseFloat(decimalFormat.format(compare));
        Double avgCost = printMapper.getAvgCost(iocParam);
        Double avgPaper = printMapper.getAvgPaper(iocParam);
        Double avgCarbon = printMapper.getAvgCarbon(iocParam);
        avgCost = Double.valueOf(decimalFormat.format(avgCost));
        avgPaper = Double.valueOf(decimalFormat.format(avgPaper));
        avgCarbon = Double.valueOf(decimalFormat.format(avgCarbon));
        Integer total = printMapper.getTotal(iocParam);
        total= total==null?0:total;

        objects.get(0).put("value",avgCost);
        objects.get(0).put("compare",compare);

        objects.get(1).put("value",avgPaper);
        objects.get(1).put("compare",compare);

        objects.get(2).put("value",avgCarbon);
        objects.get(2).put("compare",compare);

        objects.get(3).put("value",total);
        objects.get(3).put("compare",compare);
        indeicator.setWorthAttention("⽂印管理持续为您服务");
        indeicator.setList(objects);
        return ReturnMsg.ok(indeicator);
    }

    @Override
    public ReturnMsg getPrintingCostTrend(IocParam iocParam) {
        String[] param = {"cost"};
        //String[] labKey = {"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
        //List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> list = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = list.toArray(new String[0]);
        List<Label> labels1 = new ArrayList<>();
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        for (int i = 0; i < labKey.length; i++) {
            Label label = new Label();
            JSONObject jsonObject = new JSONObject();
            IocParam iocParam1 = new IocParam();
            iocParam1.setCompanyId(iocParam.getCompanyId());
            iocParam1.setDeptId(iocParam.getDeptId());
            IocTimeRange iocTimeRange = new IocTimeRange();
            iocTimeRange.setStartDate(labKey[i]+" 00:00:00");
            iocTimeRange.setEndDate(labKey[i]+" 23:59:00");
            iocParam1.setTimeRange(iocTimeRange);
            Double avgCost = printMapper.getAvgCost(iocParam1);
            avgCost =Double.valueOf(decimalFormat.format(avgCost));
            jsonObject.put(param[0],avgCost);
            label.setJsonObject(jsonObject);
            label.setLabel(labKey[i]);
            labels1.add(label);
        }
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getConferenceRoomInfo(IocParam iocParam) {
        String[] param = {"print","inkCartridge"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> list = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = list.toArray(new String[0]);
        List<Label> labels1 = new ArrayList<>();
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        for (int i = 0; i < labKey.length; i++) {
            Label label = new Label();
            JSONObject jsonObject = new JSONObject();
            IocParam iocParam1 = new IocParam();
            IocTimeRange iocTimeRange = new IocTimeRange();
            iocTimeRange.setStartDate(labKey[i]+" 00:00:00");
            iocTimeRange.setEndDate(labKey[i]+" 23:59:00");
            iocParam1.setTimeRange(iocTimeRange);
            Integer totalPaper = printMapper.getTotalPaper(iocParam1);
            Double totalInkbox = printMapper.getTotalInkbox(iocParam1);
            totalInkbox = Double.valueOf(decimalFormat.format(totalInkbox));
            jsonObject.put(param[0],totalPaper);
            jsonObject.put(param[1],totalInkbox);
            label.setJsonObject(jsonObject);
            label.setLabel(labKey[i]);
            labels1.add(label);
        }
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getBusCoreIndicators(IocParam iocParam) {
        Indeicator indeicator = new Indeicator();
        List<JSONObject> objects = indeicator.getList();
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        Double avgCost = busMapper.getAvgCostByCompanyIdAndDeptId(iocParam);
        Double totalHours = busMapper.getTotalHoursByCompanyIdAndDeptId(iocParam);
        Integer totalBus = busMapper.getTotalBusByCompanyIdAndDeptId(iocParam);
        Integer total = 5;
        BigDecimal bigDecimal1 = new BigDecimal(avgCost);
        BigDecimal avgCostDec = bigDecimal1.divide(new BigDecimal(365), 2, RoundingMode.HALF_UP);
        avgCost = avgCostDec.doubleValue();
        double avgTimes = 0d;
        if(total>0){
            if(totalBus>0){
                BigDecimal bigDecimal = new BigDecimal(totalBus);
                BigDecimal avgTimesDec = bigDecimal.divide(new BigDecimal(total), 2, RoundingMode.HALF_UP);
                avgTimes = avgTimesDec.doubleValue();
            }
        }
        ThreadLocalRandom random = ThreadLocalRandom.current();
        double compare = random.nextDouble(-1, 1);
        compare = Double.parseDouble(decimalFormat.format(compare));
        objects.get(0).put("value",total);
        objects.get(0).put("compare",compare);

        objects.get(1).put("value",totalHours);
        objects.get(1).put("compare",compare);

        objects.get(2).put("value",avgCost);
        objects.get(2).put("compare",compare);

        objects.get(3).put("value",avgTimes);
        objects.get(3).put("compare",compare);
        indeicator.setWorthAttention("公车管理持续为您服务");
        indeicator.setList(objects);
        return ReturnMsg.ok(indeicator);
    }

    @Override
    public ReturnMsg getBusInfo(IocParam iocParam) {
        String[] param = {"averageDuration","numberTrips"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> list = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = list.toArray(new String[0]);
        List<Label> labels1 = new ArrayList<>();
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        for (int i = 0; i < labKey.length; i++) {
            Label label = new Label();
            JSONObject jsonObject = new JSONObject();
            IocParam iocParam1 = new IocParam();
            IocTimeRange iocTimeRange = new IocTimeRange();
            iocTimeRange.setStartDate(labKey[i]+" 00:00:00");
            iocTimeRange.setEndDate(labKey[i]+" 23:59:00");
            iocParam1.setTimeRange(iocTimeRange);
            Double totalHours = busMapper.getTotalHoursByCompanyIdAndDeptId(iocParam1);
            Integer total = busMapper.getTotalByCompanyIdAndDeptId(iocParam1);
            double avgHours = 0d;
            if(total>0){
                if(totalHours>0){
                    BigDecimal bigDecimal = new BigDecimal(totalHours);
                    BigDecimal avgHoursDec = bigDecimal.divide(new BigDecimal(total), 2, RoundingMode.HALF_UP);
                    avgHours = avgHoursDec.doubleValue();
                }
            }
            jsonObject.put(param[0],avgHours);
            jsonObject.put(param[1],total);

            label.setJsonObject(jsonObject);
            label.setLabel(labKey[i]);
            labels1.add(label);
        }
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getBusCostTrend(IocParam iocParam) {
        String[] param = {"cost"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> list = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = list.toArray(new String[0]);
        List<Label> labels1 = new ArrayList<>();
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        for (int i = 0; i < labKey.length; i++) {
            Label label = new Label();
            JSONObject jsonObject = new JSONObject();
            IocParam iocParam1 = new IocParam();
            IocTimeRange iocTimeRange = new IocTimeRange();
            iocTimeRange.setStartDate(labKey[i]+" 00:00:00");
            iocTimeRange.setEndDate(labKey[i]+" 23:59:00");
            iocParam1.setTimeRange(iocTimeRange);
            Double avgCost = busMapper.getAvgCostByCompanyIdAndDeptId(iocParam1);
            avgCost = Double.valueOf(decimalFormat.format(avgCost));
            jsonObject.put(param[0],avgCost);
            label.setJsonObject(jsonObject);
            label.setLabel(labKey[i]);
            labels1.add(label);
        }
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getFloorCoreIndicators(IocParam iocParam) {
        Indeicator indeicator = new Indeicator();
        List<JSONObject> objects = indeicator.getList();
        ThreadLocalRandom random = ThreadLocalRandom.current();
        String year = iocParam.getTimeRange().getStartDate().substring(0,4);
        Long quota = carbonQuotaMapper.getQuotaByCompany("",year);
        Long expend = carbonQuotaMapper.getExpendByCompany("",year);
        long discharge = carbonMapper.getDischargeByTime(iocParam);
        long deEmission = carbonMapper.getDeEmissionByTime(iocParam);
        long area =0;//总面积 暂无数据
        long person = 0;//总人数 暂无数据
        Double  compare = random.nextDouble(-1,1);
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        compare = Double.valueOf(decimalFormat.format(compare));
        objects.get(0).put("value",MathUtils.division(quota-expend,quota,2)*100);

        objects.get(1).put("value",discharge);
        objects.get(1).put("compare",compare);

        objects.get(2).put("value",MathUtils.division(discharge,area,2));
        objects.get(2).put("compare",compare);

        objects.get(3).put("value",MathUtils.division(discharge,person,2));
        objects.get(3).put("compare",compare);
        objects.add(new JSONObject());
        objects.get(4).put("value",deEmission);
        objects.get(4).put("compare",compare);

        indeicator.setWorthAttention("节能减排绿色出行");
        indeicator.setList(objects);
        return ReturnMsg.ok(indeicator);
    }

    @Autowired
    CompanyMapper companyMapper;

    @Override
    public ReturnMsg getCarbonInfo(IocParam iocParam) {
       /* String[] param = {"中迅","北京规划院","郑分","广分","上分","成分"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> lists = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = lists.toArray(new String[0]);
        List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        return ReturnMsg.ok(labels1);*/
        ThreadLocalRandom random = ThreadLocalRandom.current();

        Company company =  companyMapper.selectById(1);
        Department department = departmentMapper.selectById(company.getOfficeId());
        List<Department> departments =departmentMapper.selectList(new LambdaQueryWrapper<Department>().eq(Department::getParentId,department.getOriId()));
        String[] param = departments.stream().map(Department::getName).collect(Collectors.toList()).toArray(new String[0]);
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> lists = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = lists.toArray(new String[0]);
        List<Label> labels1 = new ArrayList<>();
        for (int i = 0; i < labKey.length; i++) {
            Label label = new Label();
            IocParam iocParam1 = new IocParam();
            IocTimeRange iocTimeRange = new IocTimeRange();
            iocTimeRange.setStartDate(labKey[i]+" 00:00:00");
            iocTimeRange.setEndDate(labKey[i]+" 23:59:59");
            iocParam1.setTimeRange(iocTimeRange);
            JSONObject jsonObject = new JSONObject();
            for (int j = 0; j < departments.size(); j++) {
                iocParam1.setDeptId(departments.get(j).getId());
                long i1 =  carbonMapper.getDischargeByTime(iocParam1);
                if(i1==0){
                    i1 = random.nextInt(5,50);
                }
                jsonObject.put(param[j],i1);
            }
            label.setJsonObject(jsonObject);
            label.setLabel(labKey[i]);
            labels1.add(label);
        }

        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getCarbonQuotaComposition(IocParam iocParam) {
        String year = iocParam.getTimeRange().getStartDate().substring(0,4);
        String[] param = {"stock","consume"};
        String[] labKey = {"中旬","规划院","成分","上分","广分","郑分"};
        List<String> lists = carbonQuotaMapper.getCompanys(year);
        labKey = lists.toArray(new String[0]);
        List<Label> list = new ArrayList<>();
        for (int i = 0; i < labKey.length; i++) {
            Long quota = carbonQuotaMapper.getQuotaByCompany(labKey[i],year);
            Long expend = carbonQuotaMapper.getExpendByCompany(labKey[i],year);
            Label label = new Label();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(param[0],quota-expend);
            jsonObject.put(param[1],expend);
            label.setJsonObject(jsonObject);
            label.setLabel(labKey[i]);
            list.add(label);
        }
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getCarbonNews(IocParam iocParam) {
        List<News> newsList = new ArrayList<>();
        List<CarbonNotice> lists = carbonNoticeMapper.selectList(new LambdaQueryWrapper<CarbonNotice>()
                .gt(CarbonNotice::getCreateTime,iocParam.getTimeRange().getStartDate())
                .le(CarbonNotice::getCreateTime,iocParam.getTimeRange().getEndDate()));
        for (CarbonNotice carbonNotice : lists) {
           News news =  new News();
           news.setContent(carbonNotice.getContent());
           news.setTital(carbonNotice.getTitle());
           news.setWriter(carbonNotice.getCreateByName());
           news.setDate(carbonNotice.getCreateTime());
           newsList.add(news);
        }
        return ReturnMsg.ok(newsList);
    }

    @Override
    public ReturnMsg getCarbonProportion(IocParam iocParam) {
        List<Cake> list = new ArrayList<>();
        String[] names = {"汽油","柴油","电力","热力","天然气","其他"};
        ThreadLocalRandom random = ThreadLocalRandom.current();
        for(String name:names){
            Long value = carbonMapper.getDischargeBySourceType(iocParam,name);
            if(value==0){
                value = random.nextLong(5,50);
            }
            Cake cake = new Cake();
            cake.setName(name);
            cake.setValue(value);
            list.add(cake);
        }
        return ReturnMsg.ok(list);
    }
    @Override
    public ReturnMsg getPhotovoltaicPower(IocParam iocParam) {
        String[] param = {"realTime"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> lists = DateUtil.getBetweenDays(startStr, endStr);
        String[] labelKey = lists.toArray(new String[0]);
        List<Label> labels = GeneralMethod.getInstance().dataGeneration(param, labelKey);
        return ReturnMsg.ok(labels);
    }


    @Override
    public ReturnMsg getAnalyseCoreIndicators(IocParam iocParam) {
        Indeicator indeicator = new Indeicator();
        List<JSONObject> objects = indeicator.getList();
        ThreadLocalRandom random = ThreadLocalRandom.current();
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        int totalKwh = random.nextInt(10, 100);
        int totalM = random.nextInt(10, 100);
        int costCount = random.nextInt(1000, 5000);
        int costAvg = random.nextInt(300, 1000);
        while (costAvg>costCount){
            costAvg = random.nextInt(300, 1000);
        }
        float compare = (float) Math.random();
        compare = Float.parseFloat(decimalFormat.format(compare));
        objects.get(0).put("value",totalKwh);
        objects.get(0).put("compare",compare);

        objects.get(1).put("value",totalM);
        objects.get(1).put("compare",compare);

        objects.get(2).put("value",costCount);
        objects.get(2).put("compare",compare);

        objects.get(3).put("value",costAvg);
        objects.get(3).put("compare",compare);
        indeicator.setWorthAttention("⼈均成本过⾼，建议合理搭配智慧场景调整能源使⽤策略");
        indeicator.setList(objects);
        return ReturnMsg.ok(indeicator);
    }

    @Override
    public ReturnMsg getAnalyseWaterCoreIndicators(IocParam iocParam) {
        Indeicator indeicator = new Indeicator();
        List<JSONObject> objects = indeicator.getList();
        objects.get(0).put("value",98);
        objects.get(0).put("compare",0.4);

        objects.get(1).put("value",50);
        objects.get(1).put("compare",0.4);

        objects.get(2).put("value",100);
        objects.get(2).put("compare",0.4);

        objects.get(3).put("value",1000);
        objects.get(3).put("compare",0.4);
        indeicator.setWorthAttention("⼈均成本过⾼，建议合理搭配智慧场景调整能源使⽤策略");
        indeicator.setList(objects);
        return ReturnMsg.ok(indeicator);
    }

    @Override
    public ReturnMsg getAnalysePowerCoreIndicators(IocParam iocParam) {
        Indeicator indeicator = new Indeicator();
        List<JSONObject> objects = indeicator.getList();
        objects.get(0).put("value",98);
        objects.get(0).put("compare",0.4);

        objects.get(1).put("value",30);
        objects.get(1).put("compare",0.4);

        objects.get(2).put("value",100);
        objects.get(2).put("compare",0.4);

        objects.get(3).put("value",1000);
        objects.get(3).put("compare",0.4);
        indeicator.setWorthAttention("⼈均成本过⾼，建议合理搭配智慧场景调整能源使⽤策略");
        indeicator.setList(objects);
        return ReturnMsg.ok(indeicator);
    }

    @Override
    public ReturnMsg getAnalyseHotCoreIndicators(IocParam iocParam) {
        Indeicator indeicator = new Indeicator();
        List<JSONObject> objects = indeicator.getList();
        objects.get(0).put("value",98);
        objects.get(0).put("compare",0.4);

        objects.get(1).put("value",50);
        objects.get(1).put("compare",0.4);

        objects.get(2).put("value",100);
        objects.get(2).put("compare",0.4);

        objects.get(3).put("value",1000);
        objects.get(3).put("compare",0.4);
        indeicator.setWorthAttention("⼈均成本过⾼，建议合理搭配智慧场景调整能源使⽤策略");
        indeicator.setList(objects);
        return ReturnMsg.ok(indeicator);
    }

    @Override
    public ReturnMsg getAnalyseColdCoreIndicators(IocParam iocParam) {
        Indeicator indeicator = new Indeicator();
        List<JSONObject> objects = indeicator.getList();
        objects.get(0).put("value",98);
        objects.get(0).put("compare",0.4);

        objects.get(1).put("value",50);
        objects.get(1).put("compare",0.4);

        objects.get(2).put("value",100);
        objects.get(2).put("compare",0.4);

        objects.get(3).put("value",1000);
        objects.get(3).put("compare",0.4);
        indeicator.setWorthAttention("⼈均成本过⾼，建议合理搭配智慧场景调整能源使⽤策略");
        indeicator.setList(objects);
        return ReturnMsg.ok(indeicator);
    }

    @Override
    public ReturnMsg getAnalyseCpvCoreIndicators(IocParam iocParam) {
        Indeicator indeicator = new Indeicator();
        List<JSONObject> objects = indeicator.getList();
        objects.get(0).put("value",98);
        objects.get(0).put("compare",0.4);

        objects.get(1).put("value",50);
        objects.get(1).put("compare",0.4);

        objects.get(2).put("value",100);
        objects.get(2).put("compare",0.4);

        objects.get(3).put("value",1000);
        objects.get(3).put("compare",0.4);
        indeicator.setWorthAttention("⼈均成本过⾼，建议合理搭配智慧场景调整能源使⽤策略");
        indeicator.setList(objects);
        return ReturnMsg.ok(indeicator);
    }

    @Override
    public ReturnMsg getPhotovoltaicProfit() {
        List<Cake> list = new ArrayList<>();
        list.add(new Cake("subsidy"));
        list.add(new Cake("saving"));
        list.add(new Cake("earning"));
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getEnergyTrendsAnalyseWater(IocParam iocParam) {
        String[] param = {"tapWater","recycledWater"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> lists = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = lists.toArray(new String[0]);
        List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getEnergyTrendsAnalysePower(IocParam iocParam) {
        String[] param = {"tip","seal","flat","valley"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> lists = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = lists.toArray(new String[0]);
        List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getEnergyTrendsAnalyseHot(IocParam iocParam) {
        String[] param = {"heatConsumption"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> lists = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = lists.toArray(new String[0]);
        List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getEnergyTrendsAnalyseCold(IocParam iocParam) {
        String[] param = {"refrigerationCapacity","energyConsumption"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> lists = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = lists.toArray(new String[0]);
        List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getEnergyTrendsAnalyseCpv(IocParam iocParam) {
        String[] param = {"powerGeneration","outputPower"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> lists = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = lists.toArray(new String[0]);
        List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getEnergyTrendsTotal(IocParam iocParam) {
        String[] param = {"water","cpv","cold","hot","power"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> lists = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = lists.toArray(new String[0]);
        List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getEnergyTrendsAnalyseCpvInfo(IocParam iocParam) {
        String[] param = {"governmentSubsidies","save"};
        String startStr = iocParam.getTimeRange().getStartDate();
        String endStr = iocParam.getTimeRange().getEndDate();
        List<String> lists = DateUtil.getBetweenDays(startStr, endStr);
        String[] labKey = lists.toArray(new String[0]);
        List<Label> labels1 = GeneralMethod.getInstance().dataGeneration(param, labKey);
        return ReturnMsg.ok(labels1);
    }

    @Override
    public ReturnMsg getEnergyAnalysisWater(IocParam iocParam) {
        List<Cake> list = new ArrayList<>();
        list.add(new Cake("办公区"));
        list.add(new Cake("信息机房"));
        list.add(new Cake("餐厅"));
        list.add(new Cake("停车场"));
        list.add(new Cake("其他"));
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getEnergyAnalysisTotal(IocParam iocParam) {
        List<Cake> list = new ArrayList<>();
        list.add(new Cake("光伏"));
        list.add(new Cake("用热"));
        list.add(new Cake("用冷"));
        list.add(new Cake("用电"));
        list.add(new Cake("用水"));
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getEnergyAnalysisPower(IocParam iocParam) {
        List<Cake> list = new ArrayList<>();
        list.add(new Cake("信息机房"));
        list.add(new Cake("插座"));
        list.add(new Cake("照明"));
        list.add(new Cake("空调"));
        list.add(new Cake("其他"));
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getEnergyAnalysisHot(IocParam iocParam) {
        List<Cake> list = new ArrayList<>();
        System.out.println(list);
        list.add(new Cake("信息机房"));
        list.add(new Cake("停车场"));
        list.add(new Cake("餐厅"));
        list.add(new Cake("办公区"));
        list.add(new Cake("其他"));
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getEnergyAnalysisClod(IocParam iocParam) {
        List<Cake> list = new ArrayList<>();
        System.out.println(list);
        list.add(new Cake("function"));
        list.add(new Cake("whole"));
        list.add(new Cake("clodTotal"));
        list.add(new Cake("perCapitaCost"));
        list.add(new Cake("inletWaterTemperature"));
        list.add(new Cake("outletWaterTemperature"));
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getEnergyAnalysisCpv(IocParam iocParam) {
        List<Cake> list = new ArrayList<>();
        System.out.println(list);
        list.add(new Cake("function"));
        list.add(new Cake("whole"));
        list.add(new Cake("electricTotal"));
        list.add(new Cake("electricDate"));
        list.add(new Cake("profitTotal"));
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getEnergyAnalysisTotalInfo(IocParam iocParam) {
        List<Cake> list = new ArrayList<>();
        System.out.println(list);
        list.add(new Cake("monthlyEnergySaving"));
        list.add(new Cake("annualEnergySaving"));
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getEnergyAnalysisWaterInfo(IocParam iocParam) {
        List<Cake> list = new ArrayList<>();
        System.out.println(list);
        list.add(new Cake("waterConsumptionTotal"));
        list.add(new Cake("perCapitaCost"));
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getEnergyAnalysisPowerInfo(IocParam iocParam) {
        List<Cake> list = new ArrayList<>();
        System.out.println(list);
        list.add(new Cake("powerConsumptionTotal"));
        list.add(new Cake("energyConservation"));
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getEnergyAnalysisHotInfo(IocParam iocParam) {
        List<Cake> list = new ArrayList<>();
        System.out.println(list);
        list.add(new Cake("heatConsumptionTotal"));
        list.add(new Cake("perCapitaCost"));
        return ReturnMsg.ok(list);
    }

    @Override
    public ReturnMsg getCarbonQuotaData() {
        String url =  restTemplateConfig.addBaseUrl(CARBON_QUOTA_URL)+"?pageNum=1&pageSize=1000";
        RestTemplate restTemplate = restTemplateConfig.build();
        // 发送GET请求
        ResponseEntity<Map> responseEntity = restTemplate.getForEntity(url, Map.class);
        // 获取响应对象里的 body 对象
        Map<String, Object> body = responseEntity.getBody();
        // 获取状态码
        Integer code = (Integer)body.get("code");
        // 获取响应信息
        String message = (String)body.get("msg");
        if(code==200){
            ThreadLocalRandom random = ThreadLocalRandom.current();
            ArrayList list = (ArrayList) body.get("rows");
            if(list!=null&&list.size()>0){
                carbonQuotaMapper.delete(null);
                for (int i=0;i<list.size();i++){
                    LinkedHashMap json = (LinkedHashMap) list.get(i);
                    CarbonQuota carbonQuota = new CarbonQuota();
                    carbonQuota.setId((Integer) json.get("id"));
                    carbonQuota.setCompany(String.valueOf(json.get("companyName")));
                    carbonQuota.setYear(String.valueOf(json.get("year")));
                    carbonQuota.setCompany_id(Integer.parseInt(String.valueOf(json.get("companyId"))));
                    Integer quota = Integer.parseInt(String.valueOf(json.get("quota")));
                    Integer expend = random.nextInt(0, quota);//随机生成消耗额
                    carbonQuota.setQuota(quota);
                    carbonQuota.setExpend(expend);
                    carbonQuotaMapper.insert(carbonQuota);
                }
            }
            return ReturnMsg.ok();

        }else{
            log.info("获取碳配额数据："+message);
            return  ReturnMsg.fail(message);
        }
        //return null;
    }
    @Override
    public ReturnMsg getCarbonNoticeData() {
        String url = restTemplateConfig.addBaseUrl(CARBON_NOTICE_URL) + "?pageNum=1&pageSize=1000";
        RestTemplate restTemplate = restTemplateConfig.build();
        // 发送GET请求
        ResponseEntity<Map> responseEntity = restTemplate.getForEntity(url, Map.class);
        // 获取响应对象里的 body 对象
        Map<String, Object> body = responseEntity.getBody();
        // 获取状态码
        Integer code = (Integer)body.get("code");
        // 获取响应信息
        String message = (String)body.get("msg");
        if(code==200){
            ThreadLocalRandom random = ThreadLocalRandom.current();
            ArrayList list = (ArrayList) body.get("rows");
            if(list!=null&&list.size()>0){
                carbonNoticeMapper.delete(null);
                for (int i=0;i<list.size();i++){
                    LinkedHashMap json = (LinkedHashMap) list.get(i);
                    CarbonNotice carbonNotice = new CarbonNotice();
                    carbonNotice.setId((Integer) json.get("id"));
                    carbonNotice.setAttachment(String.valueOf(json.get("attachment")));
                    carbonNotice.setTitle(String.valueOf(json.get("noticeTitle")));
                    carbonNotice.setContent(String.valueOf(json.get("noticeContent")));
                    carbonNotice.setStatus(String.valueOf(json.get("status")));
                    carbonNotice.setCreateByName(String.valueOf(json.get("createByName")));
                    if(json.get("createTime")!=null){
                        carbonNotice.setCreateTime(String.valueOf(json.get("createTime")));
                    }
                    if(json.get("updateTime")!=null){
                        carbonNotice.setUpdateTime(String.valueOf(json.get("updateTime")));
                    }
                    carbonNoticeMapper.insert(carbonNotice);

                }
            }
            return ReturnMsg.ok();

        }else{
            log.info("获取双碳通知数据："+message);
            return  ReturnMsg.fail(message);
        }
        //return null;
    }



}
