package com.nantian.cloud.dbcapp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.dbcapp.service.EnterpriseEnergyDataService;
import com.nantian.cloud.dbcapp.vo.ApplyRecord;
import com.nantian.cloud.dbcapp.vo.EnterpriseEnergyDataVo;
import com.nantian.cloud.dbcapp.vo.ExceptionInfoVo;
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.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/enterpriseEnergyData")
@Api(value = "enterpriseEnergyData", tags = "能耗数据")
public class EnterpriseEnergyDataController {

    @Autowired
    private EnterpriseEnergyDataService enterpriseEnergyDataService;

    @GetMapping("/realTime")
    @SysLog("实时库数据导入energy_record库")
    @ApiOperation(value = "实时库数据导入energy_record库", notes = "实时库数据导入energy_record库")
    public R getRealTime() {
        return enterpriseEnergyDataService.getRealTime();
    }

    /**
     * 数据质量只公式必传点
     * 当月应送未送企业名单去除注销企业
     */
    @RequestMapping("/noEnergyDataExcel")
    @SysLog("获取当月应送未送企业名单（含实时库）（名单）")
    @ApiOperation(value = "获取当月应送未送企业名单（含实时库）", notes = "获取当月应送未送企业名单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "epcStr", value = "指定企业编码", required = false, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "1按数据质量(全列),2不按数据质量(全列),3按数据质量(只列数据不齐点)", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "statDate", value = "数据统计月份（如2021-07）", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "sendDate", value = "规定上送时间（如2021-08-05 23:59:59）", required = true, dataType = "string", paramType = "path")
    })
    public R noEnergyDataExcel(HttpServletResponse response, String epcStr, String type, String statDate, String sendDate) throws IOException {
        return enterpriseEnergyDataService.noEnergyDataExcel(response, epcStr, type, statDate, sendDate);
    }

    @RequestMapping("/noEnergyDataInfoExcel")
    @SysLog("获取单月应送未送名单明细（含实时库）（名单）")
    @ApiOperation(value = "获取单月应送未送名单明细（含实时库）", notes = "获取单月应送未送名单明细（含实时库）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "epcStr", value = "指定企业编码", required = false, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "1按数据质量(全列),2不按数据质量(全列),3按数据质量(只列数据不齐点)", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "statDate", value = "数据统计月份（如2021-07）", required = true, dataType = "string", paramType = "path")
    })
    public R noEnergyDataInfoExcel(HttpServletResponse response, String epcStr, String type, String statDate) throws IOException {
        return enterpriseEnergyDataService.noEnergyDataInfoExcel(response, epcStr, type, statDate);
    }

    @RequestMapping("/energyDataByEpc")
    @SysLog("查询企业采集数据上传情况（名单）")
    @ApiOperation(value = "查询企业采集数据上传情况", notes = "查询企业采集数据上传情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "epc", value = "企业编码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "statDate", value = "数据统计月份（如2021-07）", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "sendDate", value = "规定上送时间（如2021-08-05 23:59:59）", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "除去采集编码名称，填1，为是", required = true, dataType = "string", paramType = "path")
    })
    public R energyDataByEpc(String epc, String statDate, String sendDate, String type) {
        return enterpriseEnergyDataService.energyDataByEpc(epc, statDate, sendDate, type);
    }

    @GetMapping("/energyPage")
    @SysLog("查询企业采集数据（名单）")
    @ApiOperation(value = "查询企业采集数据", notes = "查询企业采集数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "类型，1历史，2实时", required = true, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "EnterpriseEnergyDataVo", value = "采集数据", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "start", value = "开始时间", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "end", value = "结束时间", required = true, dataType = "string", paramType = "path")
    })
    public R energyPage(Page page, String type, EnterpriseEnergyDataVo energyDataVo, String start, String end) {
        return enterpriseEnergyDataService.energyPage(page, type, energyDataVo, start, end);
    }

    /**
     * 注销企业不列没有传数据的采集点
     */
    @GetMapping("/noDataCollect")
    @SysLog("企业没有传数据的采集点（名单）")
    @ApiOperation(value = "企业没有传数据的采集点", notes = "企业没有传数据的采集点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "epc", value = "企业编码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "statDate", value = "数据统计月份（如2021-07）", required = true, dataType = "string", paramType = "path")
    })
    public R noDataCollect( String epc, String statDate) {
        return enterpriseEnergyDataService.noDataCollectByEpc(epc,statDate);
    }

    @GetMapping("/enterpriseTceExcel")
    @SysLog("查询企业综合能耗、是否超出耗能级别表（名单）")
    @ApiOperation(value = "查询企业综合能耗、是否超出耗能级别表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "统计年份（如2021）", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "epcStr", value = "指定企业（默认全部企业）", required = false, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "类型（1取系统结算数据，2取原始数据结算的结果）", required = true, dataType = "string", paramType = "path"),
    })
    public R enterpriseTceExcel( HttpServletResponse response, String year, String epcStr, String type) throws IOException {
        return enterpriseEnergyDataService.enterpriseTceExcel(response,year,epcStr,type);
    }

    @GetMapping("/enterpriseQuotaDetailExcel")
    @SysLog("企业指标明细表（名单）")
    @ApiOperation(value = "企业指标明细表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "统计年份（如2021）", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "epcStr", value = "指定企业（默认全部企业）", required = false, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "类型（1取系统结算数据，2取原始数据结算的结果）", required = true, dataType = "string", paramType = "path"),
    })
    public R enterpriseQuotaDetailExcel( HttpServletResponse response, String year, String epcStr, String type) throws IOException {
        return enterpriseEnergyDataService.enterpriseQuotaDetailExcel(response,year,epcStr,type);
    }

    @GetMapping("/electricExcel")
    @SysLog("企业电力消耗明细（名单）")
    @ApiOperation(value = "企业电力消耗明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "统计年份（如2021）", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "epcStr", value = "指定企业（默认全部企业）", required = false, dataType = "string", paramType = "path"),
    })
    public R electricExcel( HttpServletResponse response, String year, String epcStr) throws IOException {
        return enterpriseEnergyDataService.enterpriseElectricExcel(response,year,epcStr);
    }

    @GetMapping("/energyExcel")
    @SysLog("企业用能数据传送明细（名单）")
    @ApiOperation(value = "企业用能数据传送明细(如，配置了煤，没有送煤的数据)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "统计年份（如2021）", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "epcStr", value = "指定企业（默认全部企业）", required = false, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "energyCode", value = "用能种类编码（如：0100为煤，0900为焦炭）", required = false, dataType = "string", paramType = "path"),
    })
    public R energyExcel( HttpServletResponse response, String year, String epcStr, String energyCode) throws IOException {
        return enterpriseEnergyDataService.enterpriseEnergyExcel(response,year,epcStr,energyCode);
    }

    @Transactional
    @PostMapping("/updateWaveException")
    @SysLog("批量处理波动异常（提交政府审批）")
    @ApiOperation(value = "批量处理波动异常（提交政府审批）", notes = "批量处理波动异常（提交政府审批）")
    public R updateWaveException(@RequestBody List<ExceptionInfoVo> data) {
        return enterpriseEnergyDataService.updateWaveExceptionList(data);
    }

    /*//走采集接口处理传输异常
    @PostMapping("/updateTransException")
    @ApiOperation(value = "处理传输异常（数据补录至实时库）", notes = "处理传输异常（数据补录至实时库）")
    public R updateTransException(@RequestBody ExceptionInfoVo data) {
        return enterpriseEnergyDataService.updateTransException(data);
    }*/

    @PostMapping("/confirmException")
    @SysLog("批量确认异常数据无误")
    @ApiOperation(value = "批量确认异常数据无误", notes = "批量确认异常数据无误")
    public R confirmException(@RequestBody List<ExceptionInfoVo> data) {
        return enterpriseEnergyDataService.confirmException(data);
    }

    /*@PostMapping("/insertDataMaster")
    @SysLog("企业数据补录至实时主库")
    @ApiOperation(value = "企业数据补录至实时主库", notes = "企业数据补录至实时主库")
    public R insertDataMaster(@RequestBody ExceptionInfoVo data) {
        return enterpriseEnergyDataService.insertDataMaster(data);
    }*/

    @Transactional
    @PostMapping("/modifyData")
    @SysLog("（政府审批）企业删改数据")
    @ApiOperation(value = "（政府审批）企业删改数据", notes = "企业删改数据")
    public R modifyData(@RequestBody List<ApplyRecord> applyRecordList) {
        return enterpriseEnergyDataService.modifyData(applyRecordList);
    }

    /*@Transactional
    @PostMapping("/updateException")
    @SysLog("企业处理异常")
    @ApiOperation(value = "修改异常", notes = "修改异常")
    public R updateException(@RequestBody List<ExceptionInfoVo> data) {
        return enterpriseEnergyDataService.updateException(data);
    }*/


    /**
     * 能源计量审查信息推到计量管理与首页
     */
    //@Inner(value = false)
    @SysLog("能源计量审查信息推到计量管理与首页")
    @ApiOperation(value = "能源计量审查信息推到计量管理与首页",notes = "能源计量审查信息推到计量管理与首页")
    @GetMapping("/etlGaugesManage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planId", value = "审查计划id", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "epc", value = "企业社会信用编码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "类型 1计量器具，2计量人员", required = true, dataType = "string", paramType = "path")
    })
    public R etlGaugesManage(String planId, String epc, String type) {
        return enterpriseEnergyDataService.etlGaugesManage(planId,epc,type);
    }


    @SysLog("更新企业信息中间表")
    @ApiOperation(value = "更新企业信息中间表",notes = "更新企业信息中间表")
    @GetMapping("/renewEnterprise")
    public R etlGaugesManage() {
        enterpriseEnergyDataService.insertEnterpriseTmp();
        return R.ok();
    }

}
