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.SRaRealTimeAnalysisFurnace;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.service.analysis.ISRaRealTimeAnalysisFurnaceService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.analysis.SRaRealTimeAnalysisFurnaceVo;
import cn.cecep.talroad.vo.query.analysis.SRaRealTimeAnalysisFurnaceQuery;
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("/furnace")
public class SRaRealTimeAnalysisFurnaceController extends BasesController {
    @Autowired
    private ISRaRealTimeAnalysisFurnaceService sRaRealTimeAnalysisFurnaceService;

    @Autowired
    @Qualifier("baseRegionService")
    private BaseRegionService baseRegionService;
    /**
     * 查询高炉产量分析列表
     */
    @ApiOperation(value = "查询高炉产量分析")
    @GetMapping("/list")
    public AjaxResults<IPage<SRaRealTimeAnalysisFurnace>> list(SRaRealTimeAnalysisFurnaceQuery sRaRealTimeAnalysisFurnace) {
        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();
        }
        sRaRealTimeAnalysisFurnace.setRegionCodes(regionCodes);
        Page<SRaRealTimeAnalysisFurnace> page = new Page<>(sRaRealTimeAnalysisFurnace.getPageNum(), sRaRealTimeAnalysisFurnace.getPageSize());
        IPage<SRaRealTimeAnalysisFurnace> list = sRaRealTimeAnalysisFurnaceService.page(page, SRaRealTimeAnalysisFurnaceQuery.createLambdaQueryWrapper(sRaRealTimeAnalysisFurnace));
        return AjaxResults.success(list);
    }

    @ApiOperation(value = "高炉产量统计分页查询")
    @GetMapping("/statisticsList")
    public AjaxResults<IPage<SRaRealTimeAnalysisFurnace>> statisticsList(SRaRealTimeAnalysisFurnaceQuery sRaRealTimeAnalysisFurnace) {
        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();
        }
        sRaRealTimeAnalysisFurnace.setRegionCodes(regionCodes);
        IPage<SRaRealTimeAnalysisFurnace> list = sRaRealTimeAnalysisFurnaceService.statisticsList(sRaRealTimeAnalysisFurnace);
        return AjaxResults.success(list);
    }
    @ApiOperation(value = "高炉产量统计导出")
    @Log(title = "高炉产量统计", businessType = BusinessType.EXPORT)
    @PostMapping("/statisticsExport")
    public void statisticsExport(HttpServletResponse response, @RequestBody SRaRealTimeAnalysisFurnaceQuery sRaRealTimeAnalysisFurnace) {
        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();
        }
        sRaRealTimeAnalysisFurnace.setRegionCodes(regionCodes);
        List<SRaRealTimeAnalysisFurnace> list = sRaRealTimeAnalysisFurnaceService.statisticsExport(sRaRealTimeAnalysisFurnace);
        if(sRaRealTimeAnalysisFurnace.getType().equals("1")){
            List<SRaRealTimeAnalysisFurnace.SRaRealTimeAnalysisFactoryFurnace> factoryCoalList=list.stream().map(o -> {
                SRaRealTimeAnalysisFurnace.SRaRealTimeAnalysisFactoryFurnace vo = new SRaRealTimeAnalysisFurnace.SRaRealTimeAnalysisFactoryFurnace();
                BeanUtils.copyProperties(o, vo);
                vo.setAnalyseTimeStr(sRaRealTimeAnalysisFurnace.getAnalyseStartTime()+"至"+sRaRealTimeAnalysisFurnace.getAnalyseEndTime());
                return vo;
            }).collect(Collectors.toList());
            ExcelUtil<SRaRealTimeAnalysisFurnace.SRaRealTimeAnalysisFactoryFurnace> util = new ExcelUtil<SRaRealTimeAnalysisFurnace.SRaRealTimeAnalysisFactoryFurnace>(SRaRealTimeAnalysisFurnace.SRaRealTimeAnalysisFactoryFurnace.class);
            util.exportExcel(response, factoryCoalList, "高炉产量统计");
        }else {
            List<SRaRealTimeAnalysisFurnace.SRaRealTimeAnalysisDeviceFurnace> factoryCoalList=list.stream().map(o -> {
                SRaRealTimeAnalysisFurnace.SRaRealTimeAnalysisDeviceFurnace vo = new SRaRealTimeAnalysisFurnace.SRaRealTimeAnalysisDeviceFurnace();
                BeanUtils.copyProperties(o, vo);
                vo.setAnalyseTimeStr(sRaRealTimeAnalysisFurnace.getAnalyseStartTime()+"至"+sRaRealTimeAnalysisFurnace.getAnalyseEndTime());
                return vo;
            }).collect(Collectors.toList());
            ExcelUtil<SRaRealTimeAnalysisFurnace.SRaRealTimeAnalysisDeviceFurnace> util = new ExcelUtil<SRaRealTimeAnalysisFurnace.SRaRealTimeAnalysisDeviceFurnace>(SRaRealTimeAnalysisFurnace.SRaRealTimeAnalysisDeviceFurnace.class);
            util.exportExcel(response, factoryCoalList, "高炉产量统计");
        }

    }

    @ApiOperation(value = "高炉产量统计-曲线图")
    @GetMapping("/resultCurve")
    public AjaxResults<EChartsVO> resultCurve(SRaRealTimeAnalysisFurnaceQuery 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(sRaRealTimeAnalysisFurnaceService.resultCurve(sRaRealTimeAnalysisCoa));
    }
    /**
     * 导出高炉产量分析列表
     */
    @ApiOperation(value = "导出高炉产量分析")
    @Log(title = "高炉产量分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SRaRealTimeAnalysisFurnaceQuery sRaRealTimeAnalysisFurnace) {
        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();
        }
        sRaRealTimeAnalysisFurnace.setRegionCodes(regionCodes);
        List<SRaRealTimeAnalysisFurnace> list = sRaRealTimeAnalysisFurnaceService.selectSRaRealTimeAnalysisFurnaceList(sRaRealTimeAnalysisFurnace);
        ExcelUtil<SRaRealTimeAnalysisFurnace> util = new ExcelUtil<SRaRealTimeAnalysisFurnace>(SRaRealTimeAnalysisFurnace.class);
        util.exportExcel(response, list, "高炉产量分析数据");
    }

    /**
     * 获取高炉产量分析详细信息
     */
    @ApiOperation(value = "获取高炉产量分析详细信息")
    @PostMapping(value = "/{dataId}")
    public AjaxResults<SRaRealTimeAnalysisFurnace> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(sRaRealTimeAnalysisFurnaceService.getById(id));
    }

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

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

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