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.*;
import cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisConverter;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.service.analysis.ISRaRealTimeAnalysisConverterService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.analysis.SRaRealTimeAnalysisConverterVo;
import cn.cecep.talroad.vo.query.analysis.SRaRealTimeAnalysisConverterQuery;
import cn.cecep.talroad.vo.query.analysis.SRaRealTimeAnalysisConverterQuery;
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.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("/converter")
public class SRaRealTimeAnalysisConverterController extends BasesController {
    @Autowired
    private ISRaRealTimeAnalysisConverterService sRaRealTimeAnalysisConverterService;

    @Autowired
    @Qualifier("baseRegionService")
    private BaseRegionService baseRegionService;
    /**
     * 查询转炉产量分析列表
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/list")
    public AjaxResults<IPage<SRaRealTimeAnalysisConverter>> list(SRaRealTimeAnalysisConverterQuery sRaRealTimeAnalysisConverter) {
        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();
        }
        sRaRealTimeAnalysisConverter.setRegionCodes(regionCodes);
        Page<SRaRealTimeAnalysisConverter> page = new Page<>(sRaRealTimeAnalysisConverter.getPageNum(), sRaRealTimeAnalysisConverter.getPageSize());
        IPage<SRaRealTimeAnalysisConverter> list = sRaRealTimeAnalysisConverterService.page(page, SRaRealTimeAnalysisConverterQuery.createLambdaQueryWrapper(sRaRealTimeAnalysisConverter));
        return AjaxResults.success(list);
    }
    @ApiOperation(value = "转炉产量统计分页查询")
    @GetMapping("/statisticsList")
    public AjaxResults<IPage<SRaRealTimeAnalysisConverter>> statisticsList(SRaRealTimeAnalysisConverterQuery sRaRealTimeAnalysisConverter) {
        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();
        }
        sRaRealTimeAnalysisConverter.setRegionCodes(regionCodes);
        IPage<SRaRealTimeAnalysisConverter> list = sRaRealTimeAnalysisConverterService.statisticsList(sRaRealTimeAnalysisConverter);
        return AjaxResults.success(list);
    }
    @ApiOperation(value = "转炉产量统计导出")
    @Log(title = "转炉产量统计", businessType = BusinessType.EXPORT)
    @PostMapping("/statisticsExport")
    public void statisticsExport(HttpServletResponse response, @RequestBody SRaRealTimeAnalysisConverterQuery sRaRealTimeAnalysisConverter) {
        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();
        }
        sRaRealTimeAnalysisConverter.setRegionCodes(regionCodes);
        List<SRaRealTimeAnalysisConverter> list = sRaRealTimeAnalysisConverterService.statisticsExport(sRaRealTimeAnalysisConverter);
        if(sRaRealTimeAnalysisConverter.getType().equals("1")){
            List<SRaRealTimeAnalysisConverter.SRaRealTimeAnalysisFactoryConverter> factoryCoalList=list.stream().map(o -> {
                SRaRealTimeAnalysisConverter.SRaRealTimeAnalysisFactoryConverter vo = new SRaRealTimeAnalysisConverter.SRaRealTimeAnalysisFactoryConverter();
                BeanUtils.copyProperties(o, vo);
                vo.setAnalyseTimeStr(sRaRealTimeAnalysisConverter.getAnalyseStartTime()+"至"+sRaRealTimeAnalysisConverter.getAnalyseEndTime());
                return vo;
            }).collect(Collectors.toList());
            ExcelUtil<SRaRealTimeAnalysisConverter.SRaRealTimeAnalysisFactoryConverter> util = new ExcelUtil<SRaRealTimeAnalysisConverter.SRaRealTimeAnalysisFactoryConverter>(SRaRealTimeAnalysisConverter.SRaRealTimeAnalysisFactoryConverter.class);
            util.exportExcel(response, factoryCoalList, "转炉产量统计");
        }else {
            List<SRaRealTimeAnalysisConverter.SRaRealTimeAnalysisDeviceConverter> factoryCoalList=list.stream().map(o -> {
                SRaRealTimeAnalysisConverter.SRaRealTimeAnalysisDeviceConverter vo = new SRaRealTimeAnalysisConverter.SRaRealTimeAnalysisDeviceConverter();
                BeanUtils.copyProperties(o, vo);
                vo.setAnalyseTimeStr(sRaRealTimeAnalysisConverter.getAnalyseStartTime()+"至"+sRaRealTimeAnalysisConverter.getAnalyseEndTime());
                return vo;
            }).collect(Collectors.toList());
            ExcelUtil<SRaRealTimeAnalysisConverter.SRaRealTimeAnalysisDeviceConverter> util = new ExcelUtil<SRaRealTimeAnalysisConverter.SRaRealTimeAnalysisDeviceConverter>(SRaRealTimeAnalysisConverter.SRaRealTimeAnalysisDeviceConverter.class);
            util.exportExcel(response, factoryCoalList, "转炉产量统计");
        }

    }

    @ApiOperation(value = "转炉产量统计-曲线图")
    @GetMapping("/resultCurve")
    public AjaxResults<EChartsVO> resultCurve(SRaRealTimeAnalysisConverterQuery sRaRealTimeAnalysisCoa) {
        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();
        }
        sRaRealTimeAnalysisCoa.setRegionCodes(regionCodes);
        return AjaxResults.success(sRaRealTimeAnalysisConverterService.resultCurve(sRaRealTimeAnalysisCoa));
    }

    /**
     * 导出转炉产量分析列表
     */
    @ApiOperation(value = "导出")
    @Log(title = "转炉产量分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SRaRealTimeAnalysisConverterQuery sRaRealTimeAnalysisConverter) {
        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();
        }
        sRaRealTimeAnalysisConverter.setRegionCodes(regionCodes);
        List<SRaRealTimeAnalysisConverter> list = sRaRealTimeAnalysisConverterService.selectSRaRealTimeAnalysisConverterList(sRaRealTimeAnalysisConverter);
        ExcelUtil<SRaRealTimeAnalysisConverter> util = new ExcelUtil<SRaRealTimeAnalysisConverter>(SRaRealTimeAnalysisConverter.class);
        util.exportExcel(response, list, "转炉产量分析数据");
    }

    /**
     * 获取转炉产量分析详细信息
     */
    @ApiOperation(value = "详情")
    @PostMapping(value = "/{dataId}")
    public AjaxResults<SRaRealTimeAnalysisConverter> getInfo(@PathVariable("dataId") String dataId) {
        return AjaxResults.success(sRaRealTimeAnalysisConverterService.getById(dataId));
    }

    /**
     * 新增转炉产量分析
     */
    @ApiOperation(value = "新增")
    @Log(title = "转炉产量分析", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody SRaRealTimeAnalysisConverter sRaRealTimeAnalysisConverter) {
        return toAjax(sRaRealTimeAnalysisConverterService.save(sRaRealTimeAnalysisConverter));
    }

    /**
     * 修改转炉产量分析
     */
    @ApiOperation(value = "修改")
    @Log(title = "转炉产量分析", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody SRaRealTimeAnalysisConverterVo sRaRealTimeAnalysisConverterVo) {
        if (ObjectUtil.isEmpty(sRaRealTimeAnalysisConverterVo)) {
            throw new InvalidParameterException("参数为空");
        }
        if (ObjectUtil.isEmpty(sRaRealTimeAnalysisConverterVo.getDataId())) {
            throw new InvalidParameterException("参数id为空");
        }
        SRaRealTimeAnalysisConverter sRaRealTimeAnalysisConverter = sRaRealTimeAnalysisConverterService.getById(sRaRealTimeAnalysisConverterVo.getDataId());
        if (ObjectUtil.isEmpty(sRaRealTimeAnalysisConverter)) {
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", sRaRealTimeAnalysisConverterVo.getDataId()));
        }
        //赋值
        BeanUtil.copyProperties(sRaRealTimeAnalysisConverterVo, sRaRealTimeAnalysisConverter);
        return toAjax(sRaRealTimeAnalysisConverterService.saveOrUpdate(sRaRealTimeAnalysisConverter));
    }

    /**
     * 删除转炉产量分析
     */
    @ApiOperation(value = "批量删除")
    @RequiresPermissions("talroad:converter:remove")
    @Log(title = "转炉产量分析", businessType = BusinessType.DELETE)
    @PostMapping("/{dataIds}")
    public AjaxResults<Boolean> remove(@PathVariable String[] dataIds) {
        return toAjax(sRaRealTimeAnalysisConverterService.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(sRaRealTimeAnalysisConverterService.pullDeviceNameOptions(factoryName));
    }
}
