package com.nantian.cloud.dbcapp.carbon.contorller;


import com.nantian.cloud.dbcapp.carbon.domain.carbonDetection.EnterpriseDataVo;
import com.nantian.cloud.dbcapp.carbon.domain.carbonDetection.Erprise;
import com.nantian.cloud.dbcapp.carbon.domain.carbonDetection.InstrumentInfoVo;
import com.nantian.cloud.dbcapp.carbon.domain.carbonDetection.Machine;
import com.nantian.cloud.dbcapp.carbon.service.CarbonEnterpriseService;
import com.nantian.sc.cloud.common.core.util.R;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author BurgessLee
 * @date 2022年7月22日14:42:17
 * @desc 碳监测-在线监测
 */
@RestController
@RequestMapping("/api/ent")
@Slf4j
@CrossOrigin
@Api(value = "碳检测", tags = {"EnterpriseController"}, description = "碳监测-在线监测")
public class CarbonEnterpriseController {

    @Autowired
    private CarbonEnterpriseService carbonEnterpriseService;

    /**
     * 企业树
     *
     * @return
     */
    @GetMapping("/entTree")
    @ApiOperation(value = "碳监测-在线监测-企业树")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")})
    public R entTree() {
        return R.ok( carbonEnterpriseService.entTree());
    }

    /**
     * 查询企业基本信息接口
     *
     * @param enterpriseName
     * @return
     */
    @GetMapping("/getEntInfo")
    @ApiOperation(value = "碳监测-在线监测-查询企业基本信息接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")})
    public R getEntInfo(@ApiParam(name = "enterpriseName", value = "企业名称", required = true) String enterpriseName) {
        Erprise enterpriseVo = carbonEnterpriseService.selectEnterpriseVo(enterpriseName);
        return R.ok(enterpriseVo);
    }

    /**
     * 查询机组基本信息
     *
     * @param enterpriseName
     * @return
     */
    @GetMapping("/getMechs")
    @ApiOperation(value = "碳监测-在线监测-查询机组基本信息")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")})
    public R getMechs(@ApiParam(name = "enterpriseName", value = "企业名称", required = true) String enterpriseName) {
        List<Machine> machineVos = carbonEnterpriseService.selectMachineVos(enterpriseName);
        return R.ok(machineVos);
    }

    /**
     * 查询数据统计基本数据信息
     *
     * @param enterpriseName 企业名称
     * @return
     */
    @GetMapping("/getBaseAccData")
    @ApiOperation(value = "碳监测-在线监测-查询基本数据信息")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")})
    public R getBaseAccData(@ApiParam(name = "enterpriseName", value = "企业名称", required = true) String enterpriseName,
                                 @ApiParam(name = "year", value = "年份", example = "2022", required = true) String year,
                                 @ApiParam(name = "month", value = "月份", example = "1", required = true) String month) {
        List<EnterpriseDataVo> enterpriseDataVos = new ArrayList<>();
        try {
            enterpriseDataVos = carbonEnterpriseService.getBaseAccData(enterpriseName, year, month);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(enterpriseDataVos);
    }

    /**
     * 查询计量器具基本信息
     *
     * @param enterpriseName 企业名称
     * @return
     */
    @GetMapping("/getInstrumentInfo")
    @ApiOperation(value = "碳监测-在线监测-查询计量器具基本信息")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")})
    public R<List<InstrumentInfoVo>> getInstrumentInfo(@ApiParam(name = "enterpriseName", value = "企业名称", required = true) String enterpriseName) {
        List<InstrumentInfoVo> instrumentInfoVos = new ArrayList<>();
        try {
            instrumentInfoVos = carbonEnterpriseService.getInstrumentInfo(enterpriseName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(instrumentInfoVos);
    }

    /**
     * 查询机组数据详情
     *
     * @param enterpriseName 企业名称
     * @param mechNames      机组名称
     * @return
     */
    @GetMapping("/getMechInfo")
    @ApiOperation(value = "碳监测-在线监测-查询计量器具基本信息")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")})
    public R getMechInfo(@ApiParam(name = "enterpriseName", value = "企业名称", required = true) String enterpriseName,
                              @ApiParam(name = "mechNames", value = "机组名称", required = true) String mechNames,
                              @ApiParam(name = "dates", value = "日期", example = "2022-07-23", required = true) String dates) {
        return R.ok(carbonEnterpriseService.getMechInfo(dates, enterpriseName, mechNames));
    }

    /**
     * 获取基础的月度数据
     *
     * @param enterpriseName 企业名称
     * @param mechNames      机组名称
     * @return
     */
    @PostMapping("/getMechData")
    @ApiOperation(value = "碳监测-在线监测-获取基础的基础数据信息")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")})
    public R getMechData(
            @ApiParam(name = "yearMonth", value = "年月", required = true) String yearMonth,
            @ApiParam(name = "enterpriseName", value = "企业名称", required = true) String enterpriseName,
            @ApiParam(name = "mechNames", value = "机组名称", required = false) String mechNames) {
        return R.ok(carbonEnterpriseService.getMechData(yearMonth, enterpriseName, mechNames));
    }

    /**
     * 某月碳排放数据溯源
     *
     * @param enterpriseName
     * @return
     */
    @PostMapping("/carbonDay")
    @ApiOperation(value = "某月碳排放数据溯源")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")})
    public R carbonMonthSource(@ApiParam(name = "enterpriseName", value = "企业名称", required = true) String enterpriseName,
                                    @ApiParam(name = "yearMonth", value = "年月", required = true) String yearMonth) {
        return R.ok(carbonEnterpriseService.carbonMonthSource(enterpriseName, yearMonth));
    }



    /**
     * 某年碳排放数据溯源
     *
     * @param enterpriseName
     * @return
     */
    @PostMapping("/carbonMonth")
    @ApiOperation(value = "某年碳排放数据溯源")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")})
    public R carbonYearSource(@ApiParam(name = "enterpriseName", value = "企业名称", required = true) String enterpriseName,
                                   @ApiParam(name = "year", value = "年份", required = true) String year) {
        return R.ok(carbonEnterpriseService.carbonYearSource(enterpriseName, year));
    }

    /**
     * 某年配额总量
     *
     * @param enterpriseName
     * @return
     */
    @PostMapping("/distribute")
    @ApiOperation(value = "某年配额总量")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")})
    public R distribute(@ApiParam(name = "enterpriseName", value = "企业名称", required = true) String enterpriseName,
                             @ApiParam(name = "year", value = "年份", required = true) String year) {
        return R.ok(carbonEnterpriseService.distribute(enterpriseName, year));
    }

    /**
     * 某年配额详细
     *
     * @param enterpriseName
     * @return
     */
    @PostMapping("/distributeDetail")
    @ApiOperation(value = "某年配额详细")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")})
    public R distributeDetail(@ApiParam(name = "enterpriseName", value = "企业名称", required = true) String enterpriseName,
                                   @ApiParam(name = "machineName", value = "机组名称", required = true) String machineName,
                                   @ApiParam(name = "year", value = "年份", required = true) String year,
                                   @ApiParam(name = "month", value = "月份", required = true) Integer month) {
        return R.ok(carbonEnterpriseService.distributeDetail(enterpriseName, machineName, year, month));
    }

    /**
     * 某年累计配额总量
     *
     * @param enterpriseName
     * @return
     */
    @PostMapping("/distributeTotal")
    @ApiOperation(value = "某年累计配额总量")
    @ApiResponses({@ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")})
    public R distributeTotal(@ApiParam(name = "enterpriseName", value = "企业名称", required = true) String enterpriseName,
                             @ApiParam(name = "year", value = "年份", required = true) String year) {
        return R.ok(carbonEnterpriseService.distributeTotal(enterpriseName, year));
    }
}
