package com.nantian.cloud.dbcapp.controller;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.dbcapp.entity.EnterpriseState;
import com.nantian.cloud.dbcapp.mapper.jly.EnterpriseHaltMapper;
import com.nantian.cloud.dbcapp.service.EnterpriseService;
import com.nantian.cloud.dbcapp.unit.DateUtil;
import com.nantian.cloud.dbcapp.vo.SuperEpcVo;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @Description: 企业端controller
 * @Param:
 * @return:
 * @Author: zfw
 * @Date: 2020/12/22
 * @Date: 20:05
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/enterprise")
@Api(value = "enterprise", tags = "企业端接口")

public class EnterpriseController {

    private final EnterpriseService enterpriseService;


    /**
     * 查询综合能耗值
     *
     * @param
     * @return R
     */

    @GetMapping("/totalZhnh")
    @SysLog("根据条件获取企业端综合能耗")
    @ApiOperation(value = "根据条件获取企业端综合能耗", notes = "根据条件获取企业端综合能耗")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path")
    })
    public R getTotalZhnh(String enterpriseCode, String time) throws ParseException {
        return R.ok(enterpriseService.getTotalZhnh(enterpriseCode, time), "查询成功");
    }

    /**
     * 查询企业采集点
     *
     * @param
     * @return R
     */
    @GetMapping("/getCollection")
    @SysLog("查询企业采集点")
    @ApiOperation(value = "查询企业采集点", notes = "查询企业采集点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path")
    })
    public R getCollection(String enterpriseCode, String time) {
        return R.ok(enterpriseService.getCollection(enterpriseCode, time), "查询成功");
    }

    /**
     * 查询企业计量器具
     *
     * @param
     * @return R
     */
    @GetMapping("/getInstrument")
    @SysLog("查询企业计量器具")
    @ApiOperation(value = "查询企业计量器具", notes = "查询企业计量器具")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path")
    })
    public R getInstrument(String enterpriseCode, String time) {
        return R.ok(enterpriseService.getInstrument(enterpriseCode, time), "查询成功");
    }


    /**
     * 查询企业能源消费结构
     *
     * @param
     * @return R
     */
    @GetMapping("/getConsumption")
    @SysLog("查询企业能源消费结构")
    @ApiOperation(value = "查询企业能源消费结构", notes = "查询企业能源消费结构")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path")
    })
    public R getConsumption(String enterpriseCode, String time) throws ParseException {
        return R.ok(enterpriseService.getConsumption(enterpriseCode, time), "查询成功");
    }

    /**
     * 查询企业端电力消耗
     *
     * @param
     * @return R
     */
    @GetMapping("/getElectricity")
    @SysLog("查询企业端电力消耗")
    @ApiOperation(value = "查询企业端电力消耗", notes = "查询企业端电力消耗")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path")
    })
    public R getElectricity(String enterpriseCode, String time) {
        return R.ok(enterpriseService.getElectricity(enterpriseCode, time), "查询成功");
    }


    /**
     * 查询企业的产品
     *
     * @param
     * @return R
     */
    @GetMapping("/getProduct")
    @SysLog("查询企业的产品")
    @ApiOperation(value = "查询企业的产品", notes = "查询企业的产品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = false, dataType = "string", paramType = "path")
            // @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path")
    })
    public R getProduct(String enterpriseCode) {
        return R.ok(enterpriseService.getProduct(enterpriseCode), "查询成功");
    }

    /**
     * 查询企业的产品
     *
     * @param
     * @return R
     */
    @GetMapping("/getProductByEpc")
    @SysLog("根据社会信用代码查询企业的产品")
    @ApiOperation(value = "查询企业的产品", notes = "查询企业的产品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = false, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path")
    })
    public R getProductByEpc(String enterpriseCode, String time) throws ParseException {
        return R.ok(enterpriseService.getProductByEpc(enterpriseCode,time), "查询成功");
    }

    /**
     * 查询企业的产品的指标
     *
     * @param
     * @return R
     */
    @GetMapping("/getProductQuota")
    @SysLog("查询产品的指标")
    @ApiOperation(value = "查询产品的指标", notes = "查询产品的指标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品id", required = false, dataType = "Integer", paramType = "path")
            // @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path")
    })
    public R getProductQuota(Integer id) {
        return R.ok(enterpriseService.getProductQuota(id), "查询成功");
    }

    /**
     * 查询企业的产品的指标
     *
     * @param
     * @return R
     */
    @GetMapping("/getProductQuotaByEpc")
    @SysLog("查询企业的产品的指标")
    @ApiOperation(value = "查询企业的产品的指标", notes = "查询企业的产品的指标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业社会信用代码", required = false, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "productName", value = "产品名称", required = false, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "查询时间", required = false, dataType = "String", paramType = "path")
    })
    public R getProductQuotaByEpc(String enterpriseCode, String productName, String time) {
        return R.ok(enterpriseService.getProductQuotaByEpc(enterpriseCode, productName, time), "查询成功");
    }

    /**
     * 查询企业端单耗
     *
     * @param
     * @return R
     */
    @GetMapping("/getUnitConsumption")
    @SysLog("查询企业端对应产品单耗")
    @ApiOperation(value = "查询企业端对应产品单耗", notes = "查询企业端对应产品单耗")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "quotaName", value = "指标名称", required = true, dataType = "quotaName", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path")
    })
    public R getUnitConsumption(String enterpriseCode, String quotaName, String time) throws ParseException {
        return R.ok(enterpriseService.getUnitConsumption(enterpriseCode, quotaName, time), "查询成功");
    }

    /**
     * 企业端对应产品单耗详情
     */
    @GetMapping("/getUnitConsumptionbyyear")
    @SysLog("企业端对应产品单耗详情")
    @ApiOperation(value = "业端对应产品单耗详情", notes = "业端对应产品单耗详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "quotaName", value = "指标名称", required = true, dataType = "quotaName", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "time", paramType = "path")
    })
    public R getUnitConsumptionbyyears(String enterpriseCode, String quotaName, String time) throws ParseException {
        String[] times = time.split("至");
        List<String> monthBetween = DateUtil.getMonthBetween(times[0], times[1]);
        List<JSONArray> list = new ArrayList<>();
        for (String month : monthBetween) {
            JSONArray jsonArray = enterpriseService.getUnitConsumption(enterpriseCode, quotaName, month);
            list.add(jsonArray);
        }
        return R.ok(list, "查询成功");
    }

    /**
     * 查询企业端产品趋势
     *
     * @param
     * @return R
     */
    @GetMapping("/getProductOutput")
    @SysLog("查询企业端产品趋势")
    @ApiOperation(value = "查询企业端产品趋势", notes = "查询企业端产品趋势")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "productId", value = "成品id", required = false, dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path")
    })
    public R getProductOutput(String enterpriseCode, Integer productId, String time) {
        return R.ok(enterpriseService.getProductOutput(enterpriseCode, productId, time), "查询成功");
    }


    /**
     * 查询企业端近五年单位产值综合能耗
     */
    @GetMapping("/getUnitEnergyConsumption")
    @SysLog("查询企业端近五年单位产值综合能耗")
    @ApiOperation(value = "查询企业端近五年单位产值综合能耗", notes = "查询企业端近五年单位产值综合能耗")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "String", paramType = "path")
    })
    public R getUnitEnergyConsumption(String enterpriseCode, String year) {
        return R.ok(enterpriseService.getUnitEnergyConsumption(enterpriseCode, year), "查询成功");
    }

    /**
     * 查询企业端预警信息
     */
    @GetMapping("/getExceptionInfo")
    @SysLog("查询企业端预警信息")
    @ApiOperation(value = "查询企业端预警信息", notes = "查询企业端预警信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path")
    })
    public R getExceptionInfo(String enterpriseCode, String time) {
        return R.ok(enterpriseService.getExceptionInfo(enterpriseCode, time), "查询成功");
    }

    /**
     * 查询企业端近三年单位产品能耗
     *
     * @param
     * @return R
     */
    @GetMapping("/getDhThreeYears")
    @SysLog("查询企业端近三年单位产品能耗")
    @ApiOperation(value = "查询企业端近三年单位产品能耗", notes = "查询企业端近三年单位产品能耗")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path")
    })
    public R getDhThreeYears(String enterpriseCode) throws ParseException {
        return R.ok(enterpriseService.getDhThreeYears(enterpriseCode), "查询成功");
    }

    // ================================================企业端二级页面========================================================================================//

    /***
     * @Description: 按月统计综合能耗(暂时没用)
     * @Param:
     * @return:
     * @Author: zfw
     * @Date: 2020/12/30
     * @Date: 15:20
     */

    @GetMapping("/getZhnhByMonth")
    @SysLog("按月统计综合能耗")
    @ApiOperation(value = "按月统计综合能耗", notes = "按月统计综合能耗")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path")
    })
    public R getZhnhByMonth(String enterpriseCode, String time) {
        return R.ok(enterpriseService.getZhnhByMonth(enterpriseCode, time), "查询成功");
    }

    /***
     * @Description: 按月统计综合能耗
     * @Param:
     * @return:
     * @Author: zfw
     * @Date: 2020/12/30
     * @Date: 15:20
     */

    @GetMapping("/getZhnhByMonthList")
    @SysLog("按月统计综合能耗列表")
    @ApiOperation(value = "按月统计综合能耗列表", notes = "按月统计综合能耗列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "baseCode", value = "核算单元", required = false, dataType = "string", paramType = "path")
    })
    public R getZhnhByMonthList(String enterpriseCode, String time, String baseCode) throws ParseException {
        return R.ok(enterpriseService.getZhnhByMonthList(enterpriseCode, time, baseCode), "查询成功");
    }

    /**/
    @GetMapping("/getProductByMonthList")
    @SysLog("按月统计企业产品产量")
    @ApiOperation(value = "按月统计企业产品产量", notes = "按月统计企业产品产量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "baseCode", value = "核算单元", required = false, dataType = "string", paramType = "path")
    })
    public R getProductByMonthList(String enterpriseCode, String time) {
        return R.ok(enterpriseService.getProductByMonthList(enterpriseCode, time), "查询成功");
    }

    @GetMapping("/getMaterielProductByMonthList")
    @SysLog("按月统计企业物料产品产量")
    @ApiOperation(value = "按月统计企业物料产品产量", notes = "按月统计企业物料产品产量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "baseCode", value = "核算单元", required = false, dataType = "string", paramType = "path")
    })
    public R getMaterielProductByMonthList(String enterpriseCode, String time) throws ParseException {
        return R.ok(enterpriseService.getMaterielProductByMonthList(enterpriseCode, time), "查询成功");
    }

    /***
     * @Description: 得到企业实物量(带单位)
     * @Param:
     * @return:
     * @Author: zfw
     * @Date: 2020/12/30
     * @Date: 15:20
     */

    @GetMapping("/getMaterial")
    @SysLog("获取能源实物量品种")
    @ApiOperation(value = "获取能源实物量品种", notes = "获取能源实物量品种")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "baseCode", value = "信用代码", required = true, dataType = "string", paramType = "path")
    })
    public R getMaterial(String enterpriseCode, String time, String baseCode) throws ParseException {
        return R.ok(enterpriseService.getMaterial(enterpriseCode, time, baseCode), "查询成功");
    }

    /***
     * @Description: 得到企业实物量值 按月
     * @Param:
     * @return:
     * @Author: zfw
     * @Date: 2020/12/30
     * @Date: 15:20
     */

    @GetMapping("/getMaterialValue")
    @SysLog("查询企业实物量")
    @ApiOperation(value = "查询企业实物量", notes = "查询企业实物量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "code", value = "指标名称", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "baseCode", value = "周期", required = true, dataType = "string", paramType = "path")
    })
    public R getMaterialValue(String enterpriseCode, String code, String time, String baseCode) throws ParseException {
        return R.ok(enterpriseService.getMaterialValue(enterpriseCode, code, time, baseCode), "查询成功");
    }

    /***
     * @Description: 查询企业能源使用情况列表
     * @Param:
     * @return:
     * @Author: zfw
     * @Date: 2020/12/30
     * @Date: 15:20
     */

    @GetMapping("/getListValue")
    @SysLog("查询企业能源使用情况")
    @ApiOperation(value = "查询企业能源使用情况", notes = "查询企业能源使用情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "baseCode", value = "核算单元编码", required = true, dataType = "string", paramType = "path")
    })
    public R getListValue(String enterpriseCode, String time, String baseCode) throws ParseException {
        return R.ok(enterpriseService.getListValue(enterpriseCode, time, baseCode), "查询成功");
    }

    /***
     * @Description: 按分工序查询企业耗能情况
     * @Param:
     * @return:
     * @Author: zfw
     * @Date: 2020/12/30
     * @Date: 15:20
     */

    @GetMapping("/getValueByProcedure")
    @SysLog("按分工序查询企业耗能情况")
    @ApiOperation(value = "按分工序查询企业耗能情况", notes = "按分工序查询企业耗能情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "enterpriseCode", paramType = "path"),
            @ApiImplicitParam(name = "baseCode", value = "工序编码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "energyType", value = "能源种类", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "energyUseType", value = "能源用途", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "configureName", value = "配置项名称", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "configureCode", value = "配置项编码", required = true, dataType = "string", paramType = "path"),
    })
    public R getValueByProcedure(String enterpriseCode, String baseCode, String time, String energyType, String energyUseType, String configureName,String configureCode) throws ParseException {
        return R.ok(enterpriseService.getValueByProcedure(enterpriseCode, baseCode, time, energyType, energyUseType, configureName,configureCode), "查询成功");
    }

    /***
     * @Description: 查询计量器具列表
     * @Param:
     * @return:
     * @Author: zfw
     * @Date: 2020/12/30
     * @Date: 15:20
     */

    @GetMapping("/getCollectionValue")
    @SysLog("查询计量器具列表")
    @ApiOperation(value = "查询计量器具列表", notes = "查询计量器具列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "采集点code", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = true, dataType = "String", paramType = "path")
    })
    public R getCollectionValue(String enterpriseCode, String code, String time) throws ParseException {
        return R.ok(enterpriseService.getCollectionValue(enterpriseCode, code, time), "查询成功");
    }

    /***
     * @Description: 根据id查询计量器具详情
     * @Param:
     * @return:
     * @Author: zfw
     * @Date: 2020/12/30
     * @Date: 15:20
     */

    @GetMapping("/getInstrumentById")
    @SysLog("通过id查询计量器具详请")
    @ApiOperation(value = "通过id查询计量器具详请", notes = "通过id查询计量器具详请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "计量器具id", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = true, dataType = "String", paramType = "path")
    })
    public R getInstrumentById(String id, String time) {
        return R.ok(enterpriseService.getInstrumentById(id, time), "查询成功");
    }

    /***
     * @Description: 企业端查询登录的企业信息
     * @Param:
     * @return:
     * @Author: zfw
     * @Date: 2020/12/30
     * @Date: 15:20
     */

    @GetMapping("/getEnterpriseInfo")
    @SysLog("企业端查询登录的企业信息")
    @ApiOperation(value = "得到企业信息", notes = "得到企业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "String", paramType = "path")
    })
    public R getEnterpriseInfo(String enterpriseCode) {
        return R.ok(enterpriseService.getEnterpriseInfo(enterpriseCode), "查询成功");
    }

    /***
     * @Description: 企业端指标排序
     * @Param:
     * @return:
     * @Author: zfw
     * @Date: 2020/12/30
     * @Date: 15:20
     */

    @GetMapping("/getOrderByQuota")
    @SysLog("企业端指标排序")
    @ApiOperation(value = "企业端指标排序", notes = "企业端指标排序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressCode", value = "地区code", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "enterpriseCode", value = "社会信用代码", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "quotaName", value = "指标名称", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = true, dataType = "String", paramType = "path")
    })
    public R getOrderByQuota(String addressCode, String enterpriseCode, String quotaName, String time) throws ParseException {
        return R.ok(enterpriseService.getOrderByQuota(addressCode, enterpriseCode, quotaName, time), "查询成功");
    }

    @GetMapping("/getIndexName")
    @SysLog("企业工序名称")
    @ApiOperation(value = "企业工序名称", notes = "企业工序名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "时间", required = true, dataType = "String", paramType = "path"),
    })
    public R getOrderByQuota(String enterpriseCode, String time) throws ParseException {
        return R.ok(enterpriseService.getIndexName(enterpriseCode, time), "查询成功");
    }

    @GetMapping("/getDetailByDataCode")
    @SysLog("查询采集点明细")
    @ApiOperation(value = "查询采集点明细", notes = "查询采集点明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "时间", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "dataCode", value = "采集编码", required = true, dataType = "String", paramType = "path")
    })
    public R getDetailByDataCode(String enterpriseCode, String time, String dataCode) {
        return R.ok(enterpriseService.getDetailByDataCodeName(enterpriseCode, time, dataCode), "查询成功");
    }

    /**
     * 月数据
     *
     * @param enterpriseCode
     * @param time
     * @param dataCode
     * @return
     */
    @GetMapping("/getDetailMonthByDataCode")
    @SysLog("查询采集点明细（月数据）")
    @ApiOperation(value = "查询采集点明细", notes = "查询采集点明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "时间", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "dataCode", value = "采集编码", required = true, dataType = "String", paramType = "path")
    })
    public R getDetailMonthByDataCode(String enterpriseCode, String time, String dataCode) {
        return R.ok(enterpriseService.getDetailMonthByDataCodeName(enterpriseCode, time, dataCode), "查询成功");
    }


    //获取只有第一个月有数据的记录
    @GetMapping("/getDetailhaveoneonly")
    @SysLog("获取只有第一个月有数据的记录")
    @ApiOperation(value = "查询只有一月份的", notes = "按月查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "时间", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "dataCode", value = "采集编码", required = true, dataType = "String", paramType = "path")
    })
    public R getDetailhaveOneonly(String enterpriseCode, String time, String dataCode) {
        return R.ok(enterpriseService.getDetailHaveOne(enterpriseCode, time, dataCode), "查询成功");
    }

    //获取有数据的年 列表
    @GetMapping("/getyearsofhavedata")
    @SysLog("获取有数据的年列表")
    @ApiOperation(value = "获取有数据的年列表", notes = "获取有数据的年列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "dataCode", value = "采集编码", required = true, dataType = "String", paramType = "path")
    })
    public R getyears_havadata(String enterpriseCode, String dataCode) {
        return R.ok(enterpriseService.getyearshavada(enterpriseCode, dataCode));
    }

    @GetMapping("/selectall")
    @SysLog("查询所有数据明细")
    @ApiOperation(value = "查询所有", notes = "查询所有")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "dataCode", value = "采集编码", required = true, dataType = "String", paramType = "path")
    })
    public R selectall(String enterpriseCode, String dataCode) {
        return R.ok(enterpriseService.sall(enterpriseCode, dataCode));
    }

    //判断按月还是按日上传
    @GetMapping("/determinestatType")
    @SysLog("判断按月还是按日上传")
    @ApiOperation(value = "判断按月还是按日上传", notes = "判断按月还是按日上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "dataCode", value = "采集编码", required = true, dataType = "String", paramType = "path")
    })
    public R determinestattype(String enterpriseCode, String dataCode) {
        return R.ok(enterpriseService.stattype(enterpriseCode, dataCode), "查询成功");
    }

    @GetMapping("/getAccessEnergysTructure")
    @SysLog("延期企业填报时间段/老平台企业填报时间段")
    @ApiOperation(value = "延期企业填报时间段/老平台企业填报时间段", notes = "延期企业填报时间段/老平台企业填报时间段")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path")
    })
    public R getAccessEnergysTructure(String enterpriseCode) {
        return R.ok(enterpriseService.getAccessEnergysTructure(enterpriseCode));
    }

    //++++++++++++查询老平台迁移app+++++++++
    @GetMapping("/getOldDataByEpc")
    @SysLog("查询老平台app上报数据")
    @ApiOperation(value = "查询老平台app上报数据", notes = "查询老平台app上报数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path")
    })
    public R getOldDataByEpc(String enterpriseCode,String time) {
        return R.ok(enterpriseService.getOldDataByEpc(enterpriseCode,time));
    }

    //+++++++++++++++++++++++++++++++++++++++++++++++企业集团管理+++++++++++++++++++++++++++++++++++++++++++++++++
    @GetMapping("/isSuper")
    @SysLog("企业集团管理-是否为集团")
    @ApiOperation(value = "是否为集团", notes = "是否为集团")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path")
    })
    public R isSuper(String enterpriseCode) {
        return enterpriseService.isSuper(enterpriseCode);
    }

    @GetMapping("/superTotal")
    @SysLog("集团综合能耗")
    @ApiOperation(value = "集团综合能耗", notes = "集团综合能耗")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path")
    })
    public R superTotal(String enterpriseCode, String time) {
        return enterpriseService.superTotal(enterpriseCode, time);
    }

    @GetMapping("/superEnergyKinds")
    @SysLog("集团能源消费结构")
    @ApiOperation(value = "集团能源消费结构", notes = "集团能源消费结构")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path")
    })
    public R superEnergyKinds(String enterpriseCode, String time) {
        return enterpriseService.superEnergyKinds(enterpriseCode, time);
    }

    @GetMapping("/superElectricConsume")
    @SysLog("集团电力消耗")
    @ApiOperation(value = "集团电力消耗", notes = "集团电力消耗")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path")
    })
    public R superElectricConsume(String enterpriseCode, String time) {
        return enterpriseService.superElectricConsume(enterpriseCode, time);
    }

    @GetMapping("/superCollectData")
    @SysLog("集团管理-采集点,计量器具与异常数据")
    @ApiOperation(value = "采集点,计量器具与异常数据", notes = "采集点,计量器具与异常数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path")
    })
    public R superCollectData(String enterpriseCode, String time) {
        return enterpriseService.superCollectData(enterpriseCode, time);
    }

    @GetMapping("/superProduct")
    @SysLog("集团管理-产品产量")
    @ApiOperation(value = "产品产量", notes = "产品产量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path")
    })
    public R superProduct(String enterpriseCode, String time) {
        return enterpriseService.superProduct(enterpriseCode, time);
    }

    @GetMapping("/superProductValue")
    @SysLog("集团管理-产值能耗")
    @ApiOperation(value = "产值能耗", notes = "产值能耗")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path")
    })
    public R superProductValue(String enterpriseCode, String time) {
        return enterpriseService.superProductValueOut(enterpriseCode, time);
    }

    @GetMapping("/superExceptionList")
    @SysLog("集团异常信息")
    @ApiOperation(value = "集团异常信息", notes = "集团异常信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path")
    })
    public R superExceptionList(@RequestParam(value = "enterpriseCode", required = true) String enterpriseCode, @RequestParam(value = "time", required = true) String time, Page page, Integer type, String collectItemName, String state) {
        return enterpriseService.superExceptionList(enterpriseCode, time, page, type, collectItemName, state);
    }


    @GetMapping("/getAllEpc")
    @SysLog("查询集团所有企业")
    @ApiOperation(value = "查询集团所有企业", notes = "查询集团所有企业")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "企业code", required = true, dataType = "String", paramType = "path")
    })
    public R getAllEpc(String enterpriseCode) {
        //查询
        List<String> epcByIds = enterpriseService.getEpcById(enterpriseCode);
        epcByIds.add(enterpriseCode);
        String[] test = (String[]) epcByIds.toArray(new String[epcByIds.size()]);
        List<SuperEpcVo> allEpc = enterpriseService.getAllEpc(test);
        //获取父节点
        SuperEpcVo min = allEpc.stream().min(Comparator.comparing(SuperEpcVo::getIsSon)).orElse(null);
        List<SuperEpcVo> collect = allEpc.stream().filter(m -> m.getEpcCode().equals(min.getEpcCode()) ).map(
                (m) -> {
                    m.setChildren(getChildrenList(m, allEpc));
                    return m;
                }
        ).collect(Collectors.toList());
        return R.ok(collect);
    }

    /**
     * 获取子节点列表
     * @param tree
     * @param list
     * @return
     */
    public static List<SuperEpcVo> getChildrenList(SuperEpcVo tree, List<SuperEpcVo> list){
        List<SuperEpcVo> children = list.stream().filter(item -> Objects.equals(item.getSuperTreeCode(), tree.getEpcCode())).map(
                (item) -> {
                    item.setChildren(getChildrenList(item, list));
                    return item;
                }
        ).collect(Collectors.toList());
        return children;
    }

    /**
     * 判断企业停产状态是否改变
     */
    @GetMapping("/statusChange")
    public R getEnterpriseStatusChange(String enterpriseCode){
        return R.ok(enterpriseService.getEnterpriseStatusChange(enterpriseCode));
    }


    @GetMapping("/getMaterielProductByMonthListDesc")
    @SysLog("按月统计企业物料产品产量")
    @ApiOperation(value = "按月统计企业物料产品产量", notes = "按月统计企业物料产品产量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = false, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "baseCode", value = "核算单元", required = false, dataType = "string", paramType = "path")
    })
    public R getMaterielProductByMonthListDesc(String enterpriseCode, String time) throws ParseException {
        return R.ok(enterpriseService.getMaterielProductByMonthListDesc(enterpriseCode, time), "查询成功");
    }


    @GetMapping("/getEnergyTypeByProcedure")
    @SysLog("按分工序查询企业能源种类下拉框")
    @ApiOperation(value = "按分工序查询企业能源类型下拉框", notes = "按分工序查询企业能源类型下拉框")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseCode", value = "信用代码", required = true, dataType = "enterpriseCode", paramType = "path"),
            @ApiImplicitParam(name = "baseCode", value = "工序编码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "time", value = "周期", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "energyUseType", value = "能源用途", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "configureName", value = "配置项名称", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "configureCode", value = "配置项编码", required = true, dataType = "string", paramType = "path"),
    })
    public R getEnergyTypeByProcedure(String enterpriseCode, String baseCode, String time, String energyUseType, String configureName,String configureCode) throws ParseException {
        return R.ok(enterpriseService.getEnergyTypeByProcedure(enterpriseCode, baseCode, time, energyUseType, configureName,configureCode), "查询成功");
    }


    @SysLog("更新企业南网异常数据标记")
    @GetMapping("/updateDianliFlag")
    public R updateDianliFlag(String dianliFlag,String enterpriseCode){
        return R.ok(enterpriseService.updateDianliFlag(dianliFlag,enterpriseCode));
    }

}
