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.bean.BeanUtils;
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.SRaRealTimeAnalysisCement;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.service.analysis.ISRaRealTimeAnalysisCementService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.analysis.SRaRealTimeAnalysisCementVo;
import cn.cecep.talroad.vo.query.analysis.SRaRealTimeAnalysisCementQuery;
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.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("/cement")
public class SRaRealTimeAnalysisCementController extends BasesController {
    @Autowired
    private ISRaRealTimeAnalysisCementService isRaRealTimeAnalysisCementService;

    @Autowired
    @Qualifier("baseRegionService")
    private BaseRegionService baseRegionService;
    /**
     * 查询水泥熟料产量分析列表
     */
    @ApiOperation(value = "查询水泥熟料产量分析")
    @GetMapping("/list")
    public AjaxResults<IPage<SRaRealTimeAnalysisCement>> list(SRaRealTimeAnalysisCementQuery sRaRealTimeAnalysisCement) {
        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();
        }
        sRaRealTimeAnalysisCement.setRegionCodes(regionCodes);
        Page<SRaRealTimeAnalysisCement> page = new Page<>(sRaRealTimeAnalysisCement.getPageNum(), sRaRealTimeAnalysisCement.getPageSize());
        IPage<SRaRealTimeAnalysisCement> list = isRaRealTimeAnalysisCementService.page(page, SRaRealTimeAnalysisCementQuery.createLambdaQueryWrapper(sRaRealTimeAnalysisCement));
        return AjaxResults.success(list);
    }

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

    }

    @ApiOperation(value = "水泥熟料产量统计-曲线图")
    @GetMapping("/resultCurve")
    public AjaxResults<EChartsVO> resultCurve(SRaRealTimeAnalysisCementQuery 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(isRaRealTimeAnalysisCementService.resultCurve(sRaRealTimeAnalysisCoa));
    }
    /**
     * 导出水泥熟料产量分析列表
     */
    @ApiOperation(value = "导出水泥熟料产量分析")
    @Log(title = "水泥熟料产量分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SRaRealTimeAnalysisCementQuery sRaRealTimeAnalysisCement) {
        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();
        }
        sRaRealTimeAnalysisCement.setRegionCodes(regionCodes);
        List<SRaRealTimeAnalysisCement> list = isRaRealTimeAnalysisCementService.selectSRaRealTimeAnalysisCementList(sRaRealTimeAnalysisCement);
        ExcelUtil<SRaRealTimeAnalysisCement> util = new ExcelUtil<SRaRealTimeAnalysisCement>(SRaRealTimeAnalysisCement.class);
        util.exportExcel(response, list, "水泥熟料产量分析数据");
    }

    /**
     * 获取水泥熟料产量分析详细信息
     */
    @ApiOperation(value = "获取水泥熟料产量分析详细信息")
    @PostMapping(value = "/{dataId}")
    public AjaxResults<SRaRealTimeAnalysisCement> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(isRaRealTimeAnalysisCementService.getById(id));
    }

    /**
     * 新增水泥熟料产量分析
     */
    @ApiOperation(value = "新增水泥熟料产量分析")
    @Log(title = "水泥熟料产量分析", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody SRaRealTimeAnalysisCement SRaRealTimeAnalysisCement) {
        return toAjax(isRaRealTimeAnalysisCementService.save(SRaRealTimeAnalysisCement));
    }

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

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