package com.nantian.cloud.jljc.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.client.utils.StringUtils;
import com.nantian.cloud.jljc.service.TReportService;
import com.nantian.cloud.jljc.service.TSuperviseTaskService;
import com.nantian.cloud.jljc.vo.EnergyInfoConsumptionVo;
import com.nantian.cloud.jljc.vo.Query;
import com.nantian.cloud.jljc.vo.comAnalysis.AEnergyInfoVo;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 节能首页
 *
 * @date 2020-09-21
 */
@Slf4j
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/jnHome")
@Api(value = "jnHome", tags = "节能首页")
public class JnHomeController {

    private final TReportService tReportService;
    private final TSuperviseTaskService tSuperviseTaskService;

    @GetMapping("/energyConsumption")
    @SysLog("获取综合能源消费（节能）")
    @ApiOperation(value = "获取综合能源消费", notes = "获取综合能源消费")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "area", value = "地区", required = true, dataType = "string", paramType = "path")
    })
    public R energyConsumption(String year, String area) {
        return getEnergyConsumption(year, area);
    }

    @PostMapping("/energyConsumptionPost")
    @SysLog("获取综合能源消费（节能）")
    @ApiOperation(value = "获取综合能源消费", notes = "获取综合能源消费")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "area", value = "地区", required = true, dataType = "string", paramType = "path")
    })
    public R energyConsumptionPost(Query query) {
        return getEnergyConsumption(query.getYear().toString(), query.getArea());
    }

    private R getEnergyConsumption(String year, String area) {
        if (StringUtils.isEmpty(year)) {
            return R.failed("查询失败年份不能为空！");
        }
        if (StringUtils.isEmpty(area)) {
            return R.failed("查询失败地区不能为空！");
        }
        return R.ok(tReportService.energyConsumptionByAreas(year, area));
    }

    @GetMapping("/energyConsumptionByEnergy")
    @SysLog("获取能源消费结构的综合能源消费（节能）")
    @ApiOperation(value = "获取能源消费结构的综合能源消费（折标系数异常的，采用参考折标计算）", notes = "获取能源消费结构的综合能源消费")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "area", value = "地区", required = true, dataType = "string", paramType = "path")
    })
    public R energyConsumptionByEnergy(Query query) {
        return getEnergyConsumptionByEnergyPost(query);
    }

    @PostMapping("/energyConsumptionByEnergyPost")
    @SysLog("获取能源消费结构的综合能源消费（节能）")
    @ApiOperation(value = "获取能源消费结构的综合能源消费（折标系数异常的，采用参考折标计算）", notes = "获取能源消费结构的综合能源消费")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "area", value = "地区", required = true, dataType = "string", paramType = "path")
    })
    public R energyConsumptionByEnergyPost(Query query) {
        return getEnergyConsumptionByEnergyPost(query);
    }

    private R getEnergyConsumptionByEnergyPost(Query query) {
        if (StringUtils.isEmpty(query.getYear().toString())) {
            return R.failed("查询失败年份不能为空！");
        }
        if (StringUtils.isEmpty(query.getArea())) {
            return R.failed("查询失败地区不能为空！");
        }
        List<EnergyInfoConsumptionVo> energyInfoConsumptionVos = tReportService.energyConsumptionByEnergy(query);
        if (energyInfoConsumptionVos.size() > 0) { //结果集大于0，进行排序
            energyInfoConsumptionVos.sort(new Comparator<EnergyInfoConsumptionVo>() {
                @Override
                public int compare(EnergyInfoConsumptionVo o1, EnergyInfoConsumptionVo o2) {
                    return (int)(o2.getNum()- o1.getNum()); //从大到小
                }
            });
        }
        return R.ok(energyInfoConsumptionVos);
    }


    @GetMapping("/noticeList")
    @SysLog("获取首页通知列表（节能）")
    @ApiOperation(value = "获取首页通知列表", notes = "获取首页通知列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "area", value = "地区", required = true, dataType = "string", paramType = "path")
    })
    public R noticeList(Query query) {

        ScUser u = SecurityUtils.getUser();
        String inspectorsid = u.getUsername(); //监察员的执法证编号
        String deptId = u.getDeptId().toString();
        if (StringUtils.isNotEmpty(query.getArea())) {
            query.setArea(query.getArea().replaceAll("0000","").replaceAll("00",""));
        }

        Map<String,Object> map = new HashMap<String,Object>();
        //获取能报情况首页通知（加数据权限），deptId为云南省只能审核已报送至省级的报表
        map.put("report",tReportService.noticeListByReport( query, deptId));
        //获取监察任务情况首页通知（加监察编号），deptId为云南省将查看所有监察任务
        map.put("jnjc", tSuperviseTaskService.noticeListByJnjc( query, deptId, inspectorsid));
        return R.ok(map);
    }


    @GetMapping("/exceptionInfo")
    @SysLog("获取异常数据（节能）")
    @ApiOperation(value = "获取异常数据", notes = "获取异常数据")
    @ApiImplicitParam(name = "Query", value = "查询条件(year年份、area地区)", required = true, dataType = "string", paramType = "path")
    public R getExceptionInfo(Query query) {

        if (StringUtils.isNotEmpty(query.getArea())) {
            query.setArea(query.getArea().replaceAll("0000","").replaceAll("00",""));
        }
        String deptId = SecurityUtils.getUser().getDeptId().toString();

        Map<String,Object> map = new HashMap<String,Object>();
        //获取能报报表1的异常情况（综合能源消费量异常）
        map.put("report1", tReportService.exceptionInfoByReport1( query, deptId));
        //获取能报报表2的异常情况（折标系数数值过大）
        map.put("report2", tReportService.exceptionInfoByReport2( query, deptId));
        return R.ok(map);
    }


    @GetMapping("/workbench")
    @SysLog("获取工作台信息（节能）")
    @ApiOperation(value = "获取工作台信息", notes = "获取工作台信息")
    @ApiImplicitParam(name = "Query", value = "查询条件(year年份、area地区)", required = true, dataType = "string", paramType = "path")
    public R getWorkbenchInfo(Query query) {
        ScUser u = SecurityUtils.getUser();

        //area，用于数据权限，只能查看本级及以下
        if (StringUtils.isNotEmpty(query.getArea())) {
            query.setArea(query.getArea().replaceAll("0000","").replaceAll("00",""));
        }
        query.setId(u.getUsername()); //监察员的执法证编号

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("report", tReportService.reportSummaryAndAudit(query)); //能报合计、审核情况（加数据权限）
        map.put("jnjcSummary", tSuperviseTaskService.jnjcSummary(query)); //监察合计情况（加数据权限）
        map.put("jnjcPersonal", tSuperviseTaskService.jnjcPersonal(query)); //监察员个人情况（加执法证编号）
        return R.ok(map);
    }


}
