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.SRaRealTimeAnalysisCoal;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.service.analysis.ISRaRealTimeAnalysisCoalService;
import cn.cecep.talroad.vo.AmVideoDevFiVo;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.analysis.SRaRealTimeAnalysisCoalVo;
import cn.cecep.talroad.vo.query.analysis.SRaRealTimeAnalysisCoalQuery;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * * 开发公司：中节能天融科技有限公司
 * 装煤推焦次数分析Controller 控制层
 *
 * @author szhpt
 * @date 2023-03-24
 */
@Api(tags = "关联分析-企业生产过程分析-装煤推焦次数分析")
@RestController
@RequestMapping("/coal")
public class SRaRealTimeAnalysisCoalController extends BasesController {
    @Autowired
    private ISRaRealTimeAnalysisCoalService sRaRealTimeAnalysisCoalService;
    @Autowired
    @Qualifier("baseRegionService")
    private BaseRegionService baseRegionService;
    /**
     * 查询装煤推焦次数分析列表
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/list")
    public AjaxResults<IPage<SRaRealTimeAnalysisCoal>> list(SRaRealTimeAnalysisCoalQuery sRaRealTimeAnalysisCoal) {
        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();
        }
        sRaRealTimeAnalysisCoal.setRegionCodes(regionCodes);
        Page<SRaRealTimeAnalysisCoal> page = new Page<>(sRaRealTimeAnalysisCoal.getPageNum(), sRaRealTimeAnalysisCoal.getPageSize());
        IPage<SRaRealTimeAnalysisCoal> list = sRaRealTimeAnalysisCoalService.page(page, SRaRealTimeAnalysisCoalQuery.createLambdaQueryWrapper(sRaRealTimeAnalysisCoal));
        return AjaxResults.success(list);
    }
    /**
     * 查询装煤推焦次数统计
     */
    @ApiOperation(value = "装煤推焦次数统计分页查询")
    @GetMapping("/statisticsList")
    public AjaxResults<IPage<SRaRealTimeAnalysisCoal>> statisticsList(SRaRealTimeAnalysisCoalQuery sRaRealTimeAnalysisCoal) {
        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();
        }
        sRaRealTimeAnalysisCoal.setRegionCodes(regionCodes);
        IPage<SRaRealTimeAnalysisCoal> list = sRaRealTimeAnalysisCoalService.statisticsList(sRaRealTimeAnalysisCoal);
        return AjaxResults.success(list);
    }
    /**
     * 查询装煤推焦次数统计
     */
    @ApiOperation(value = "装煤推焦次数统计导出")
    @Log(title = "装煤推焦次数统计", businessType = BusinessType.EXPORT)
    @PostMapping("/statisticsExport")
    public void statisticsExport(HttpServletResponse response, @RequestBody SRaRealTimeAnalysisCoalQuery sRaRealTimeAnalysisCoal) {
        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();
        }
        sRaRealTimeAnalysisCoal.setRegionCodes(regionCodes);
        List<SRaRealTimeAnalysisCoal> list = sRaRealTimeAnalysisCoalService.statisticsExport(sRaRealTimeAnalysisCoal);
        if(sRaRealTimeAnalysisCoal.getType().equals("1")){
            List<SRaRealTimeAnalysisCoal.SRaRealTimeAnalysisFactoryCoal> factoryCoalList=list.stream().map(o -> {
                SRaRealTimeAnalysisCoal.SRaRealTimeAnalysisFactoryCoal vo = new SRaRealTimeAnalysisCoal.SRaRealTimeAnalysisFactoryCoal();
                BeanUtils.copyProperties(o, vo);
                vo.setAnalyseTime(sRaRealTimeAnalysisCoal.getAnalyseStartTime()+"至"+sRaRealTimeAnalysisCoal.getAnalyseEndTime());
                return vo;
            }).collect(Collectors.toList());
            ExcelUtil<SRaRealTimeAnalysisCoal.SRaRealTimeAnalysisFactoryCoal> util = new ExcelUtil<SRaRealTimeAnalysisCoal.SRaRealTimeAnalysisFactoryCoal>(SRaRealTimeAnalysisCoal.SRaRealTimeAnalysisFactoryCoal.class);
            util.exportExcel(response, factoryCoalList, "装煤推焦次数统计数据");
        }else {
            List<SRaRealTimeAnalysisCoal.SRaRealTimeAnalysisDeviceCoal> factoryCoalList=list.stream().map(o -> {
                SRaRealTimeAnalysisCoal.SRaRealTimeAnalysisDeviceCoal vo = new SRaRealTimeAnalysisCoal.SRaRealTimeAnalysisDeviceCoal();
                BeanUtils.copyProperties(o, vo);
                vo.setAnalyseTime(sRaRealTimeAnalysisCoal.getAnalyseStartTime()+"至"+sRaRealTimeAnalysisCoal.getAnalyseEndTime());
                return vo;
            }).collect(Collectors.toList());
            ExcelUtil<SRaRealTimeAnalysisCoal.SRaRealTimeAnalysisDeviceCoal> util = new ExcelUtil<SRaRealTimeAnalysisCoal.SRaRealTimeAnalysisDeviceCoal>(SRaRealTimeAnalysisCoal.SRaRealTimeAnalysisDeviceCoal.class);
            util.exportExcel(response, factoryCoalList, "装煤推焦次数统计数据");
        }

    }

    @ApiOperation(value = "装煤推焦次数统计-曲线图")
    @GetMapping("/resultCurve")
    public AjaxResults<EChartsVO> resultCurve(SRaRealTimeAnalysisCoalQuery sRaRealTimeAnalysisCoa) {
        return AjaxResults.success(sRaRealTimeAnalysisCoalService.resultCurve(sRaRealTimeAnalysisCoa));
    }

    /**
     * 导出装煤推焦次数分析列表
     */
    @ApiOperation(value = "导出")
    @Log(title = "装煤推焦次数分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SRaRealTimeAnalysisCoalQuery sRaRealTimeAnalysisCoal) {
        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();
        }
        sRaRealTimeAnalysisCoal.setRegionCodes(regionCodes);
        List<SRaRealTimeAnalysisCoal> list = sRaRealTimeAnalysisCoalService.selectSRaRealTimeAnalysisCoalList(sRaRealTimeAnalysisCoal);
        ExcelUtil<SRaRealTimeAnalysisCoal> util = new ExcelUtil<SRaRealTimeAnalysisCoal>(SRaRealTimeAnalysisCoal.class);
        util.exportExcel(response, list, "装煤推焦次数分析数据");
    }

    /**
     * 获取装煤推焦次数分析详细信息
     */
    @ApiOperation(value = "详情")
    @PostMapping(value = "/{dataId}")
    public AjaxResults<SRaRealTimeAnalysisCoal> getInfo(@PathVariable("dataId") String dataId) {
        return AjaxResults.success(sRaRealTimeAnalysisCoalService.getById(dataId));
    }

    /**
     * 新增装煤推焦次数分析
     */
    @ApiOperation(value = "新增")
    @Log(title = "装煤推焦次数分析", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody SRaRealTimeAnalysisCoal sRaRealTimeAnalysisCoal) {
        return toAjax(sRaRealTimeAnalysisCoalService.save(sRaRealTimeAnalysisCoal));
    }

    /**
     * 修改装煤推焦次数分析
     */
    @ApiOperation(value = "修改")
    @Log(title = "装煤推焦次数分析", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody SRaRealTimeAnalysisCoalVo sRaRealTimeAnalysisCoalVo) {
        if (ObjectUtil.isEmpty(sRaRealTimeAnalysisCoalVo)) {
            throw new InvalidParameterException("参数为空");
        }
        if (ObjectUtil.isEmpty(sRaRealTimeAnalysisCoalVo.getDataId())) {
            throw new InvalidParameterException("参数id为空");
        }
        SRaRealTimeAnalysisCoal sRaRealTimeAnalysisCoal = sRaRealTimeAnalysisCoalService.getById(sRaRealTimeAnalysisCoalVo.getDataId());
        if (ObjectUtil.isEmpty(sRaRealTimeAnalysisCoal)) {
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", sRaRealTimeAnalysisCoalVo.getDataId()));
        }
        //赋值
        BeanUtil.copyProperties(sRaRealTimeAnalysisCoalVo, sRaRealTimeAnalysisCoal);
        return toAjax(sRaRealTimeAnalysisCoalService.saveOrUpdate(sRaRealTimeAnalysisCoal));
    }

    /**
     * 删除装煤推焦次数分析
     */
    @ApiOperation(value = "批量删除")
    @RequiresPermissions("talroad:coal:remove")
    @Log(title = "装煤推焦次数分析", businessType = BusinessType.DELETE)
    @PostMapping("/{dataIds}")
    public AjaxResults<Boolean> remove(@PathVariable String[] dataIds) {
        return toAjax(sRaRealTimeAnalysisCoalService.removeByIds(Arrays.asList(dataIds)));
    }

    /**
     * 设备名称options
     */
    @ApiOperation(value = "设备名称options")
    @GetMapping("/pullDeviceNameOptions")
    public AjaxResults<List<Map<String, Object>>> pullDeviceNameOptions(@RequestParam(value = "factoryName", required = false) String factoryName) {
        return AjaxResults.success(sRaRealTimeAnalysisCoalService.pullDeviceNameOptions(factoryName));
    }

}
