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.web.domain.AjaxResults;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisRubbish;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.query.analysis.SRaRealTimeAnalysisRubbishQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.cecep.talroad.vo.query.analysis.SRaRealTimeAnalysisRubbishQuery;

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 java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

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 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.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisRubbish;
import cn.cecep.talroad.vo.analysis.SRaRealTimeAnalysisRubbishVo;
import cn.cecep.talroad.service.analysis.ISRaRealTimeAnalysisRubbishService;


/**
 * * 开发公司：中节能天融科技有限公司
 * 关联分析系统-企业生产过程分析-垃圾处理量分析Controller 控制层
 *
 * @author szhpt
 * @date 2023-04-03*/
@Api(tags = "关联分析系统-企业生产过程分析-垃圾处理量分析")
@RestController
@RequestMapping("/rubbish")
public class SRaRealTimeAnalysisRubbishController extends BasesController
{
    @Autowired
    private ISRaRealTimeAnalysisRubbishService sRaRealTimeAnalysisRubbishService;

    @Autowired
    @Qualifier("baseRegionService")
    private BaseRegionService baseRegionService;
    /**
     * 查询关联分析系统-企业生产过程分析-垃圾处理量分析列表
     */
    @ApiOperation(value = "查询关联分析系统-企业生产过程分析-垃圾处理量分析")
    @GetMapping("/list")
    public AjaxResults<IPage<SRaRealTimeAnalysisRubbish>> list(SRaRealTimeAnalysisRubbishQuery sRaRealTimeAnalysisRubbish)
    {
        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();
        }
        sRaRealTimeAnalysisRubbish.setRegionCodes(regionCodes);
        Page<SRaRealTimeAnalysisRubbish> page = new Page<>(sRaRealTimeAnalysisRubbish.getPageNum(), sRaRealTimeAnalysisRubbish.getPageSize());
        IPage<SRaRealTimeAnalysisRubbish> list = sRaRealTimeAnalysisRubbishService.page(page, SRaRealTimeAnalysisRubbishQuery.createLambdaQueryWrapper(sRaRealTimeAnalysisRubbish));
        return AjaxResults.success(list);
    }

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

    }

    @ApiOperation(value = "垃圾处理量统计-曲线图")
    @GetMapping("/resultCurve")
    public AjaxResults<EChartsVO> resultCurve(SRaRealTimeAnalysisRubbishQuery 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(sRaRealTimeAnalysisRubbishService.resultCurve(sRaRealTimeAnalysisCoa));
    }

    /**
     * 导出关联分析系统-企业生产过程分析-垃圾处理量分析列表
     */
    @ApiOperation(value = "导出关联分析系统-企业生产过程分析-垃圾处理量分析")
    @Log(title = "关联分析系统-企业生产过程分析-垃圾处理量分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SRaRealTimeAnalysisRubbishQuery sRaRealTimeAnalysisRubbish)
    {
        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();
        }
        sRaRealTimeAnalysisRubbish.setRegionCodes(regionCodes);
        List<SRaRealTimeAnalysisRubbish> list = sRaRealTimeAnalysisRubbishService.selectSRaRealTimeAnalysisRubbishList(sRaRealTimeAnalysisRubbish);
        ExcelUtil<SRaRealTimeAnalysisRubbish> util = new ExcelUtil<SRaRealTimeAnalysisRubbish>(SRaRealTimeAnalysisRubbish.class);
        util.exportExcel(response, list, "关联分析系统-企业生产过程分析-垃圾处理量分析数据");
    }

    /**
     * 获取关联分析系统-企业生产过程分析-垃圾处理量分析详细信息
     */
    @ApiOperation(value = "获取关联分析系统-企业生产过程分析-垃圾处理量分析详细信息")
    @PostMapping(value = "/{dataId}")
    public AjaxResults<SRaRealTimeAnalysisRubbish> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(sRaRealTimeAnalysisRubbishService.getById(id));
    }
    /**
     * 新增关联分析系统-企业生产过程分析-垃圾处理量分析
     */
    @ApiOperation(value = "新增关联分析系统-企业生产过程分析-垃圾处理量分析")
    @Log(title = "关联分析系统-企业生产过程分析-垃圾处理量分析", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody SRaRealTimeAnalysisRubbish sRaRealTimeAnalysisRubbish) {
        return toAjax(sRaRealTimeAnalysisRubbishService.save(sRaRealTimeAnalysisRubbish));
    }
    /**
     * 修改关联分析系统-企业生产过程分析-垃圾处理量分析
     */
    @ApiOperation(value = "修改关联分析系统-企业生产过程分析-垃圾处理量分析")
    @Log(title = "关联分析系统-企业生产过程分析-垃圾处理量分析", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody SRaRealTimeAnalysisRubbishVo sRaRealTimeAnalysisRubbishVo) {
        if(ObjectUtil.isEmpty(sRaRealTimeAnalysisRubbishVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(sRaRealTimeAnalysisRubbishVo.getDataId())){
            throw new InvalidParameterException("参数id为空");
        }
        SRaRealTimeAnalysisRubbish sRaRealTimeAnalysisRubbish = sRaRealTimeAnalysisRubbishService.getById(sRaRealTimeAnalysisRubbishVo.getDataId());
        if(ObjectUtil.isEmpty(sRaRealTimeAnalysisRubbish)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", sRaRealTimeAnalysisRubbishVo.getDataId()));
        }
        //赋值
        BeanUtil.copyProperties(sRaRealTimeAnalysisRubbishVo,sRaRealTimeAnalysisRubbish);
        return toAjax(sRaRealTimeAnalysisRubbishService.saveOrUpdate(sRaRealTimeAnalysisRubbish));
    }

    /**
     * 删除关联分析系统-企业生产过程分析-垃圾处理量分析
     */
    @ApiOperation(value = "删除关联分析系统-企业生产过程分析-垃圾处理量分析")
    @RequiresPermissions("talroad:rubbish:remove")
    @Log(title = "关联分析系统-企业生产过程分析-垃圾处理量分析", businessType = BusinessType.DELETE)
    @PostMapping("/{dataIds}")
    public AjaxResults<Boolean> remove(@PathVariable String[] dataIds) {
        return toAjax(sRaRealTimeAnalysisRubbishService.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(sRaRealTimeAnalysisRubbishService.pullDeviceNameOptions(factoryName));
    }
}
