package cn.cecep.talroad.controller.analysis;

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.domain.analysis.SRaRealTimeAnalysisRun;
import cn.cecep.talroad.service.analysis.ISRaRealTimeAnalysisRunService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.analysis.SRaRealTimeAnalysisRunVo;
import cn.cecep.talroad.vo.query.analysis.SRaRealTimeAnalysisRunQuery;
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.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.security.InvalidParameterException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * * 开发公司：中节能天融科技有限公司
 * 企业装备运行情况实时分析Controller 控制层
 *
 * @author szhpt
 * @date 2023-03-24
 */
@Api(tags = "关联分析-企业生产过程分析-企业装备运行情况实时分析")
@RestController
@RequestMapping("/run")
public class SRaRealTimeAnalysisRunController extends BasesController {
    @Autowired
    private ISRaRealTimeAnalysisRunService sRaRealTimeAnalysisRunService;

    /**
     * 查询企业装备运行情况实时分析列表
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/list")
    public AjaxResults<IPage<SRaRealTimeAnalysisRun>> list(SRaRealTimeAnalysisRunQuery sRaRealTimeAnalysisRun) {
        Page<SRaRealTimeAnalysisRun> page = new Page<>(sRaRealTimeAnalysisRun.getPageNum(), sRaRealTimeAnalysisRun.getPageSize());
        IPage<SRaRealTimeAnalysisRun> list = sRaRealTimeAnalysisRunService.page(page, SRaRealTimeAnalysisRunQuery.createLambdaQueryWrapper(sRaRealTimeAnalysisRun));
        return AjaxResults.success(list);
    }


    /**
     * 导出企业装备运行情况实时分析列表
     */
    @ApiOperation(value = "导出")
    @Log(title = "企业装备运行情况实时分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SRaRealTimeAnalysisRunQuery sRaRealTimeAnalysisRun) {
        List<SRaRealTimeAnalysisRun> list = sRaRealTimeAnalysisRunService.selectSRaRealTimeAnalysisRunList(sRaRealTimeAnalysisRun);
        ExcelUtil<SRaRealTimeAnalysisRun> util = new ExcelUtil<SRaRealTimeAnalysisRun>(SRaRealTimeAnalysisRun.class);
        util.exportExcel(response, list, "企业装备运行情况实时分析数据");
    }

    /**
     * 获取企业装备运行情况实时分析详细信息
     */
    @ApiOperation(value = "详情")
    @PostMapping(value = "/{dataId}")
    public AjaxResults<SRaRealTimeAnalysisRun> getInfo(@PathVariable("dataId") String dataId) {
        return AjaxResults.success(sRaRealTimeAnalysisRunService.getById(dataId));
    }

    /**
     * 新增企业装备运行情况实时分析
     */
    @ApiOperation(value = "新增")
    @Log(title = "企业装备运行情况实时分析", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody SRaRealTimeAnalysisRun sRaRealTimeAnalysisRun) {
        sRaRealTimeAnalysisRun.setCreateTime(new Date());
        return toAjax(sRaRealTimeAnalysisRunService.save(sRaRealTimeAnalysisRun));
    }

    /**
     * 修改企业装备运行情况实时分析
     */
    @ApiOperation(value = "修改")
    @Log(title = "企业装备运行情况实时分析", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody SRaRealTimeAnalysisRunVo sRaRealTimeAnalysisRunVo) {
        if (ObjectUtil.isEmpty(sRaRealTimeAnalysisRunVo)) {
            throw new InvalidParameterException("参数为空");
        }
        if (ObjectUtil.isEmpty(sRaRealTimeAnalysisRunVo.getDataId())) {
            throw new InvalidParameterException("参数id为空");
        }
        SRaRealTimeAnalysisRun sRaRealTimeAnalysisRun = sRaRealTimeAnalysisRunService.getById(sRaRealTimeAnalysisRunVo.getDataId());
        if (ObjectUtil.isEmpty(sRaRealTimeAnalysisRun)) {
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", sRaRealTimeAnalysisRunVo.getDataId()));
        }
        //赋值
        BeanUtil.copyProperties(sRaRealTimeAnalysisRunVo, sRaRealTimeAnalysisRun);
        return toAjax(sRaRealTimeAnalysisRunService.saveOrUpdate(sRaRealTimeAnalysisRun));
    }

    /**
     * 删除企业装备运行情况实时分析
     */
    @ApiOperation(value = "批量删除")
    @RequiresPermissions("talroad:run:remove")
    @Log(title = "企业装备运行情况实时分析", businessType = BusinessType.DELETE)
    @PostMapping("/{dataIds}")
    public AjaxResults<Boolean> remove(@PathVariable String[] dataIds) {
        return toAjax(sRaRealTimeAnalysisRunService.removeByIds(Arrays.asList(dataIds)));
    }

    /**
     * 运行状态options
     */
    @ApiOperation(value = "运行状态options")
    @GetMapping("/pullStatusOptions")
    public AjaxResults<List<Map<String, Object>>> pullStatusOptions() {
        return AjaxResults.success(sRaRealTimeAnalysisRunService.pullStatusOptions());
    }

    /**
     * 设备名称options
     */
    @ApiOperation(value = "设备名称options")
    @GetMapping("/pullRunDeviceNameOptions")
    public AjaxResults<List<Map<String, Object>>> pullDeviceNameOptions(@RequestParam(value = "factoryName", required = false) String factoryName) {
        return AjaxResults.success(sRaRealTimeAnalysisRunService.pullDeviceNameOptions(factoryName));
    }

    @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(sRaRealTimeAnalysisRunService.resultCurve(deviceId, monitoringStartTime, monitoringEndTime));
    }
    @ApiOperation(value = "企业装备运行情况-查看-数据分析曲线图")
    @GetMapping("/analysisCurve")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备编号", dataType = "String"),
            @ApiImplicitParam(name = "monitoringStartTime", value = "开始时间", dataType = "String"),
            @ApiImplicitParam(name = "monitoringEndTime", value = "结束时间", dataType = "String"),
            @ApiImplicitParam(name = "judgementBasis", value = "判断依据", dataType = "String"),
    })
    public AjaxResults<List<EChartsVO>> analysisCurve(
            @RequestParam(value = "deviceId") String deviceId,
            @RequestParam(value = "monitoringStartTime") String monitoringStartTime,
            @RequestParam(value = "monitoringEndTime") String monitoringEndTime,
            @RequestParam(value = "judgementBasis") String judgementBasis) {
        return AjaxResults.success(sRaRealTimeAnalysisRunService.analysisCurve(deviceId, monitoringStartTime, monitoringEndTime,judgementBasis));
    }
    @ApiOperation(value = "企业装备运行情况-查看-数据最新数据时间")
    @GetMapping("/analysisCurveEndTime")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备编号", dataType = "String"),
            @ApiImplicitParam(name = "monitoringStartTime", value = "开始时间", dataType = "String"),
            @ApiImplicitParam(name = "monitoringEndTime", value = "结束时间", dataType = "String"),
    })
    public AjaxResults<String> analysisCurveEndTime(
            @RequestParam(value = "deviceId") String deviceId,
            @RequestParam(value = "monitoringStartTime") String monitoringStartTime,
            @RequestParam(value = "monitoringEndTime") String monitoringEndTime
    ) {
        return AjaxResults.success("",sRaRealTimeAnalysisRunService.analysisCurveEndTime(deviceId, monitoringStartTime, monitoringEndTime));
    }
}
