package com.nantian.cloud.dbcapp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.dbcapp.service.DataHomeService;
import com.nantian.cloud.dbcapp.vo.DataHomeDataVo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.nantian.sc.cloud.common.core.util.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author suixin
 * @date 2023/4/6
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/dataHome")
public class DataHomeController {

    @Autowired
    private DataHomeService dataHomeService;

    private final String tradeStr = "水泥,钢铁,电解铝,黄磷,煤电,焦化,合成氨,工业硅,硅材料";
    private final String energyStr = "煤,电力,柴油,原油,焦炭,天然气（气态）";

    /**
     * @Description: 根据指定条件获取综合能耗数据
     * @Author: suixin
     * @Date: 2023/4/6
     * @Return: com.nantian.sc.cloud.common.core.util.R
     * @Param: [regionCode, startTime, endTime, tradeCode, tradeType, current, size]
     */
    @GetMapping("/getData")
    public R getData(String regionCode, String startTime, String endTime, String tradeCode, String tradeType, long current, long size){
        if(StringUtils.isNotBlank(tradeType) && StringUtils.isBlank(tradeCode)){
            return R.failed("请填写行业编码tradeCode！");
        }
        Page<Object> page = new Page<>(current, size);
        return R.ok(dataHomeService.getData(page,regionCode,startTime,endTime,tradeCode,tradeType,tradeStr));
    }

    /**
     * @Description: 获取电力数据趋势(type: 1-按月份，2-按地区，3-按行业)
     * @Author: suixin
     * @Date: 2023/4/6
     * @Return: com.nantian.sc.cloud.common.core.util.R
     * @Param: [regionCode, startTime, endTime, tradeCode, tradeType,type]
     */
    @GetMapping("/getElectricityDataTrends")
    public R getElectricityDataTrends(String regionCode, String startTime, String endTime, String tradeCode, String tradeType, String type){
        if("3".equals(type) && StringUtils.isBlank(tradeType)){
            return R.failed("请填写行业类型tradeType！");
        }
        return R.ok(dataHomeService.getElectricityDatas(regionCode,startTime,endTime,tradeCode,tradeType,type,tradeStr));
    }

    /**
     * @Description: 获取南网电力数据和在线采集电力数据
     * @Author: suixin
     * @Date: 2023/4/6
     * @Return: com.nantian.sc.cloud.common.core.util.R
     * @Param: [regionCode, startTime, endTime, tradeCode, tradeType行业类型 1两高行业 2产品分类行业(tradeStr), current, size]
     */
    @GetMapping("/getElectricityData")
    public R getElectricityData(String regionCode, String startTime, String endTime, String tradeCode, String tradeType, long current, long size){
        if(StringUtils.isNotBlank(tradeType) && StringUtils.isBlank(tradeCode)){
            return R.failed("请填写行业编码tradeCode！");
        }
        Page<Object> page = new Page<>(current,size);
        return R.ok(dataHomeService.getElectricityData(page,regionCode,startTime,endTime,tradeCode,tradeType,tradeStr));
    }


    /**
     * 获取行业能耗排名
     * startTime 开始时间
     * endTime 结束时间
     * tradeType 行业类型 1两高行业 2产品分类行业(tradeStr)
     */
    @GetMapping("/tradeConsumptionRanking")
    public R getTradeConsumptionRanking(String startTime, String endTime, String tradeType){
        Map<String,Object> result = new HashMap<>();
        List<Map<String, String>> ranking = dataHomeService.getTradeRanking(startTime, endTime, tradeType, tradeStr);
        Map<String, String> tradeTotal = dataHomeService.getTradeTotal(startTime, endTime, tradeType, tradeStr);
        result.put("ranking",ranking);
        result.put("total",tradeTotal);
        return R.ok(result);
    }

    /**
     * 获取地区能耗趋势
     * endTime 结束时间
     */
    @GetMapping("/regionConsumptionTrends")
    public R getConsumptionTrends(String endTime){
        Map<String,Object> result = new HashMap<>();
        int yearTmp = Integer.parseInt(StringUtils.substring(endTime, 0, 4));
        String yearStr = (yearTmp-2) + "," + (yearTmp-1) + "," + yearTmp;
        String[] yearArr = yearStr.split(",");
        List<Map<String, String>> trend = dataHomeService.getRegionTrends( yearArr, tradeStr);
        int epcNum = 0;
        for (Map<String, String> map : trend) {
            epcNum += Integer.parseInt(String.valueOf(map.get("epcNum")));
        }
        result.put("epcNum",epcNum);
        result.put("trend",trend);
        result.put("yearArr",yearArr);
        return R.ok(result);
    }

    /**
     * 获取行业能耗趋势
     * endTime 结束时间
     * tradeType 行业类型 1两高行业 2产品分类行业(tradeStr)
     */
    @GetMapping("/tradeConsumptionTrends")
    public R getConsumptionTrends(String endTime, String tradeType){
        Map<String,Object> result = new HashMap<>();
        int yearTmp = Integer.parseInt(StringUtils.substring(endTime, 0, 4));
        String yearStr = (yearTmp-2) + "," + (yearTmp-1) + "," + yearTmp;
        String[] yearArr = yearStr.split(",");
        List<Map<String, String>> trend = dataHomeService.getTradeTrends( yearArr, tradeType, tradeStr);
        int epcNum = 0;
        for (Map<String, String> map : trend) {
            epcNum += Integer.parseInt(String.valueOf(map.get("epcNum")));
        }
        result.put("epcNum",epcNum);
        result.put("trend",trend);
        result.put("yearArr", yearArr);
        return R.ok(result);
    }

    /**
     * 获取企业能耗趋势(明细)
     * page 分页参数
     * regionCode 地区编码
     * endTime 结束时间
     * tradeCode 行业编码
     * tradeType 行业类型 1两高行业 2产品分类行业(tradeStr)
     */
    @GetMapping("/trendsDetail")
    public R getTrendsDetail(Page page, String regionCode, String endTime, String tradeCode, String tradeType){
        if(StringUtils.isNotBlank(tradeType) && StringUtils.isBlank(tradeCode)){
            return R.failed("请填写行业编码tradeCode！");
        }
        Map<String,Object> result = new HashMap<>();
        int yearTmp = Integer.parseInt(StringUtils.substring(endTime, 0, 4));
        String yearStr = (yearTmp-2) + "," + (yearTmp-1) + "," + yearTmp;
        String[] yearArr = yearStr.split(",");
        IPage<DataHomeDataVo> trend = dataHomeService.getTrendsDetail( page, regionCode, yearArr, tradeCode, tradeType, tradeStr);
        result.put("trend",trend);
        result.put("yearArr",yearArr);
        return R.ok(result);
    }

    /**
     * 获取能源消耗趋势
     * startTime 开始时间
     * endTime 结束时间
     * tradeType 行业类型 1两高行业 2产品分类行业(tradeStr)
     * type 数据类型 1能源消费占比 2能源消耗趋势 3各行业用能情况
     */
    @GetMapping("/energyConsumption")
    public R getEnergyConsumption(String startTime, String endTime, String tradeType, String type){
        if("3".equals(type) && StringUtils.isBlank(tradeType)){
            return R.failed("请填写行业类型tradeType！");
        }
        return R.ok(dataHomeService.getEnergyConsumption(startTime,endTime,tradeType,type,tradeStr,energyStr));
    }

    /**
     * 获取企业能源消耗趋势(明细)
     * startTime 开始时间
     * endTime 结束时间
     * tradeType 行业类型 1两高行业 2产品分类行业(tradeStr)
     * tradeCode 行业编码
     * energyName 能源品种名称
     */
    @GetMapping("/energyConsumptionDetail")
    public R getEnergyConsumptionDetail(Page page, String startTime, String endTime, String tradeType, String tradeCode, String energyName){
        if(StringUtils.isNotBlank(tradeType) && StringUtils.isBlank(tradeCode)){
            return R.failed("请填写行业编码tradeCode！");
        }
        return R.ok(dataHomeService.getEnergyConsumptionDetail(page,startTime,endTime,tradeType,tradeCode,energyName,tradeStr,energyStr));
    }


    /**
     * 获取超限制企业（按地区汇总合计）
     */
    @GetMapping("/getOverLimit")
    public R getOverLimit(String startTime, String endTime){
        return R.ok(dataHomeService.getOverLimit(startTime,endTime));
    }

    /**
     * 获取超限制企业（明细）
     * type:0-正常企业，1-超限企业
     */
    @GetMapping("/getOverLimitDetail")
    public R getOverLimit(String startTime, String endTime,String regionCode, String type){
        return R.ok(dataHomeService.getOverLimitDetail(startTime,endTime,regionCode,tradeStr,type));
    }

}
