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.SRaRealTimeAnalysisStop;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.service.analysis.ISRaRealTimeAnalysisStopService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.analysis.SRaRealTimeAnalysisStopVo;
import cn.cecep.talroad.vo.query.analysis.SRaRealTimeAnalysisStopQuery;
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 freemarker.template.TemplateException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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.io.IOException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * * 开发公司：中节能天融科技有限公司
 * 企业装备停限产分析报(DCS)Controller 控制层
 *
 * @author szhpt
 * @date 2023-03-24
 */
@Api(tags = "关联分析-企业生产过程分析-企业装备停限产分析报(DCS)")
@RestController
@RequestMapping("/stop")
public class SRaRealTimeAnalysisStopController extends BasesController {
    @Autowired
    private ISRaRealTimeAnalysisStopService sRaRealTimeAnalysisStopService;

    @Autowired
    @Qualifier("baseRegionService")
    private BaseRegionService baseRegionService;

    /**
     * 查询企业装备停限产分析报(DCS)列表
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/list")
    public AjaxResults<IPage<SRaRealTimeAnalysisStop>> list(SRaRealTimeAnalysisStopQuery sRaRealTimeAnalysisStop) {
        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();
        }
        sRaRealTimeAnalysisStop.setRegionCodes(regionCodes);
        IPage<SRaRealTimeAnalysisStop> list = sRaRealTimeAnalysisStopService.selectSRaRealTimeAnalysisStopListPage(sRaRealTimeAnalysisStop);
        return AjaxResults.success(list);
    }


    /**
     * 导出企业装备停限产分析报(DCS)列表
     */
    @ApiOperation(value = "导出")
    @Log(title = "企业装备停限产分析报(DCS)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SRaRealTimeAnalysisStopQuery sRaRealTimeAnalysisStop) {
        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();
        }
        sRaRealTimeAnalysisStop.setRegionCodes(regionCodes);
        List<SRaRealTimeAnalysisStop> list = sRaRealTimeAnalysisStopService.selectSRaRealTimeAnalysisStopList(sRaRealTimeAnalysisStop);
        ExcelUtil<SRaRealTimeAnalysisStop> util = new ExcelUtil<SRaRealTimeAnalysisStop>(SRaRealTimeAnalysisStop.class);
        util.exportExcel(response, list, "企业装备停限产分析报表数据");
    }

    /**
     * 导出企业装备停限产分析报(DCS)word报告
     */
    @ApiOperation(value = "企业装备停限产分析报(DCS)word报告")
    @Log(title = "企业装备停限产分析报(DCS)word报告", businessType = BusinessType.EXPORT)
    @PostMapping("/exportWordReport")
    public void exportWordReport(HttpServletResponse response, @RequestBody SRaRealTimeAnalysisStopQuery sRaRealTimeAnalysisStop) throws TemplateException, IOException {
        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();
        }
        sRaRealTimeAnalysisStop.setRegionCodes(regionCodes);
        sRaRealTimeAnalysisStopService.exportWordReport(response,sRaRealTimeAnalysisStop);
    }

    /**
     * 获取企业装备停限产分析报(DCS)详细信息
     */
    @ApiOperation(value = "详情")
    @PostMapping(value = "/{dataId}")
    public AjaxResults<SRaRealTimeAnalysisStop> getInfo(@PathVariable("dataId") String dataId) {
        return AjaxResults.success(sRaRealTimeAnalysisStopService.getById(dataId));
    }

    /**
     * 新增企业装备停限产分析报(DCS)
     */
    @ApiOperation(value = "新增")
    @Log(title = "企业装备停限产分析报(DCS)", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody SRaRealTimeAnalysisStop sRaRealTimeAnalysisStop) {
        return toAjax(sRaRealTimeAnalysisStopService.save(sRaRealTimeAnalysisStop));
    }

    /**
     * 修改企业装备停限产分析报(DCS)
     */
    @ApiOperation(value = "修改")
    @Log(title = "企业装备停限产分析报(DCS)", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody SRaRealTimeAnalysisStopVo sRaRealTimeAnalysisStopVo) {
        if (ObjectUtil.isEmpty(sRaRealTimeAnalysisStopVo)) {
            throw new InvalidParameterException("参数为空");
        }
        if (ObjectUtil.isEmpty(sRaRealTimeAnalysisStopVo.getDataId())) {
            throw new InvalidParameterException("参数id为空");
        }
        SRaRealTimeAnalysisStop sRaRealTimeAnalysisStop = sRaRealTimeAnalysisStopService.getById(sRaRealTimeAnalysisStopVo.getDataId());
        if (ObjectUtil.isEmpty(sRaRealTimeAnalysisStop)) {
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", sRaRealTimeAnalysisStopVo.getDataId()));
        }
        //赋值
        BeanUtil.copyProperties(sRaRealTimeAnalysisStopVo, sRaRealTimeAnalysisStop);
        return toAjax(sRaRealTimeAnalysisStopService.saveOrUpdate(sRaRealTimeAnalysisStop));
    }

    /**
     * 删除企业装备停限产分析报(DCS)
     */
    @ApiOperation(value = "批量删除")
    @RequiresPermissions("talroad:stop:remove")
    @Log(title = "企业装备停限产分析报(DCS)", businessType = BusinessType.DELETE)
    @PostMapping("/{dataIds}")
    public AjaxResults<Boolean> remove(@PathVariable String[] dataIds) {
        return toAjax(sRaRealTimeAnalysisStopService.removeByIds(Arrays.asList(dataIds)));
    }

    /**
     * 工序options
     */
    @ApiOperation(value = "工序options")
    @GetMapping("/pullWorkingProcedureOptions")
    public AjaxResults<List<Map<String, Object>>> pullWorkingProcedureOptions(@RequestParam(value = "industryCategoryCode", required = false) String industryCategoryCode) {
        return AjaxResults.success(sRaRealTimeAnalysisStopService.pullWorkingProcedureOptions(industryCategoryCode));
    }

    @ApiOperation(value = "企业装备停限产分析报表-查看-结果曲线图")
    @GetMapping("/resultCurve")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备编号", dataType = "String"),
            @ApiImplicitParam(name = "monitoringStartTime", value = "开始时间", dataType = "String"),
            @ApiImplicitParam(name = "monitoringEndTime", value = "结束时间", dataType = "String"),
    })
    public AjaxResults<EChartsVO> resultCurve(
            @RequestParam(value = "deviceId") String deviceId,
            @RequestParam(value = "monitoringStartTime") String monitoringStartTime,
            @RequestParam(value = "monitoringEndTime") String monitoringEndTime) {
        return AjaxResults.success(sRaRealTimeAnalysisStopService.resultCurve(deviceId, monitoringStartTime, monitoringEndTime));
    }
}
