package cn.cecep.talroad.controller.analysis;

import cn.cecep.talroad.common.core.domain.R;
import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.common.security.annotation.RequiresPermissions;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisCo;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.service.analysis.ISRaRealTimeAnalysisCoService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.analysis.SRaRealTimeAnalysisCoVo;
import cn.cecep.talroad.vo.query.analysis.SRaRealTimeAnalysisCoQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import freemarker.template.TemplateException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.security.InvalidParameterException;


/**
 * * 开发公司：中节能天融科技有限公司
 * co放散分析Controller 控制层
 *
 * @author szhpt
 * @date 2023-03-24
 */
@Api(tags = "关联分析-企业生产过程分析-co放散分析")
@RestController
@RequestMapping("/co")
public class SRaRealTimeAnalysisCoController extends BasesController {
    @Autowired
    private ISRaRealTimeAnalysisCoService sRaRealTimeAnalysisCoService;

    @Autowired
    @Qualifier("baseRegionService")
    private BaseRegionService baseRegionService;
    /**
     * 查询co放散分析列表
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/list")
    public AjaxResults<IPage<SRaRealTimeAnalysisCo>> list(SRaRealTimeAnalysisCoQuery sRaRealTimeAnalysisCo) {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();
        List<String> regionCodes=new ArrayList<>();
        R<List<String>> r = baseRegionService.getRegionCodeListChild(regionCode);
        if(r.getCode()==200){
            regionCodes=r.getData();
        }
        sRaRealTimeAnalysisCo.setRegionCodes(regionCodes);
        IPage<SRaRealTimeAnalysisCo> list = sRaRealTimeAnalysisCoService.selectSRaRealTimeAnalysisCoListPage(sRaRealTimeAnalysisCo);
        return AjaxResults.success(list);
    }


    /**
     * 导出co放散分析列表
     */
    @ApiOperation(value = "导出co放散分析列表")
    @Log(title = "co放散分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SRaRealTimeAnalysisCoQuery sRaRealTimeAnalysisCo) throws TemplateException, IOException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();
        List<String> regionCodes=new ArrayList<>();
        R<List<String>> r = baseRegionService.getRegionCodeListChild(regionCode);
        if(r.getCode()==200){
            regionCodes=r.getData();
        }
        sRaRealTimeAnalysisCo.setRegionCodes(regionCodes);
        List<SRaRealTimeAnalysisCo> list = sRaRealTimeAnalysisCoService.selectSRaRealTimeAnalysisCoList(sRaRealTimeAnalysisCo);
        ExcelUtil<SRaRealTimeAnalysisCo> util = new ExcelUtil<SRaRealTimeAnalysisCo>(SRaRealTimeAnalysisCo.class);
        util.exportExcel(response, list, "co放散分析数据");
    }

    /**
     * 导出co放散分析报告word文档
     */
    @ApiOperation(value = "导出co放散分析报告word文档")
    @Log(title = "导出co放散分析报告word文档", businessType = BusinessType.EXPORT)
    @PostMapping("/exportWordReport")
    public void exportWordReport(HttpServletResponse response, @RequestBody SRaRealTimeAnalysisCoQuery sRaRealTimeAnalysisCo) throws TemplateException, IOException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();
        List<String> regionCodes=new ArrayList<>();
        R<List<String>> r = baseRegionService.getRegionCodeListChild(regionCode);
        if(r.getCode()==200){
            regionCodes=r.getData();
        }
        sRaRealTimeAnalysisCo.setRegionCodes(regionCodes);
        sRaRealTimeAnalysisCoService.exportCoDataToWord(sRaRealTimeAnalysisCo, response);
    }

    /**
     * 获取co放散分析详细信息
     */
    @ApiOperation(value = "详情")
    @PostMapping(value = "/{dataId}")
    public AjaxResults<SRaRealTimeAnalysisCo> getInfo(@PathVariable("dataId") String dataId) {
        return AjaxResults.success(sRaRealTimeAnalysisCoService.getById(dataId));
    }

    /**
     * 新增co放散分析
     */
    @ApiOperation(value = "新增")
    @Log(title = "co放散分析", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody SRaRealTimeAnalysisCo sRaRealTimeAnalysisCo) {
        sRaRealTimeAnalysisCo.setCreateTime(new Date());
        return toAjax(sRaRealTimeAnalysisCoService.save(sRaRealTimeAnalysisCo));
    }

    /**
     * 修改co放散分析
     */
    @ApiOperation(value = "修改")
    @Log(title = "co放散分析", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody SRaRealTimeAnalysisCoVo sRaRealTimeAnalysisCoVo) {
        if (ObjectUtil.isEmpty(sRaRealTimeAnalysisCoVo)) {
            throw new InvalidParameterException("参数为空");
        }
        if (ObjectUtil.isEmpty(sRaRealTimeAnalysisCoVo.getDataId())) {
            throw new InvalidParameterException("参数id为空");
        }
        SRaRealTimeAnalysisCo sRaRealTimeAnalysisCo = sRaRealTimeAnalysisCoService.getById(sRaRealTimeAnalysisCoVo.getDataId());
        if (ObjectUtil.isEmpty(sRaRealTimeAnalysisCo)) {
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", sRaRealTimeAnalysisCoVo.getDataId()));
        }
        //赋值
        BeanUtil.copyProperties(sRaRealTimeAnalysisCoVo, sRaRealTimeAnalysisCo);
        return toAjax(sRaRealTimeAnalysisCoService.saveOrUpdate(sRaRealTimeAnalysisCo));
    }

    /**
     * 删除co放散分析
     */
    @ApiOperation(value = "删除")
    @RequiresPermissions("talroad:co:remove")
    @Log(title = "co放散分析", businessType = BusinessType.DELETE)
    @PostMapping("/{dataIds}")
    public AjaxResults<Boolean> remove(@PathVariable String[] dataIds) {
        return toAjax(sRaRealTimeAnalysisCoService.removeByIds(Arrays.asList(dataIds)));
    }

    /**
     * 工序options
     */
    @ApiOperation(value = "工序options")
    @GetMapping("/pullWorkingProcedureOptions")
    public AjaxResults<List<Map<String, Object>>> pullWorkingProcedureOptions(@RequestParam(value = "industryCategoryCode", required = false) String industryCategoryCode) {
        return AjaxResults.success(sRaRealTimeAnalysisCoService.pullWorkingProcedureOptions(industryCategoryCode));
    }

    @ApiOperation(value = "CO放散分析-查看-结果曲线图")
    @GetMapping("/resultCurve")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备编号", dataType = "String"),
            @ApiImplicitParam(name = "monitoringStartTime", value = "开始时间", dataType = "String"),
            @ApiImplicitParam(name = "monitoringEndTime", value = "结束时间", dataType = "String"),
    })
    public AjaxResults<EChartsVO> resultCurve(
            @RequestParam(value = "deviceId") String deviceId,
            @RequestParam(value = "monitoringStartTime") String monitoringStartTime,
            @RequestParam(value = "monitoringEndTime") String monitoringEndTime
    ) {
        return AjaxResults.success(sRaRealTimeAnalysisCoService.resultCurve(deviceId, monitoringStartTime, monitoringEndTime));
    }

    @ApiOperation(value = "CO放散分析-查看-煤气排放量曲线图")
    @GetMapping("/coalEmissionsChart")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备编号", dataType = "String"),
            @ApiImplicitParam(name = "monitoringStartTime", value = "开始时间", dataType = "String"),
            @ApiImplicitParam(name = "monitoringEndTime", value = "结束时间", dataType = "String"),
    })
    public AjaxResults<EChartsVO> coalEmissionsChart(
            @RequestParam(value = "deviceId") String deviceId,
            @RequestParam(value = "monitoringStartTime") String monitoringStartTime,
            @RequestParam(value = "monitoringEndTime") String monitoringEndTime
    ) {
        return AjaxResults.success(sRaRealTimeAnalysisCoService.coalEmissionsChart(deviceId, monitoringStartTime, monitoringEndTime));
    }

    @ApiOperation(value = "企业装备运行情况-查看-获取结束日期")
    @GetMapping("/analysisCurveEndTime")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备编号", dataType = "String"),
            @ApiImplicitParam(name = "monitoringStartTime", value = "开始时间", dataType = "String"),
            @ApiImplicitParam(name = "monitoringEndTime", value = "结束时间", dataType = "String"),
    })
    public AjaxResults<String> analysisCurveEndTime(
            @RequestParam(value = "deviceId") String deviceId,
            @RequestParam(value = "monitoringStartTime") String monitoringStartTime,
            @RequestParam(value = "monitoringEndTime") String monitoringEndTime
    ) {
        return AjaxResults.success("",sRaRealTimeAnalysisCoService.analysisCurveEndTime(deviceId, monitoringStartTime, monitoringEndTime));
    }
}
