package com.aucma.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.aucma.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.aucma.common.annotation.Log;
import com.aucma.common.core.controller.BaseController;
import com.aucma.common.core.domain.AjaxResult;
import com.aucma.common.enums.BusinessType;
import com.aucma.base.domain.BaseDeviceParamVal;
import com.aucma.base.service.IBaseDeviceParamValService;
import com.aucma.common.utils.poi.ExcelUtil;
import com.aucma.common.core.page.TableDataInfo;

/**
 * 设备工艺参数当前值Controller
 *
 * @author zch
 * @date 2025-10-15
 */
@RestController
@RequestMapping("/baseDeviceParamVal/val" )
public class BaseDeviceParamValController extends BaseController {
    @Autowired
    private IBaseDeviceParamValService baseDeviceParamValService;

    /**
     * 查询设备工艺参数当前值列表
     */
    @PreAuthorize("@ss.hasPermi('baseDeviceParamVal:val:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(BaseDeviceParamVal baseDeviceParamVal) {
        startPage();
        List<BaseDeviceParamVal> list = baseDeviceParamValService.selectBaseDeviceParamValList(baseDeviceParamVal);
        return getDataTable(list);
    }

    /**
     * 导出设备工艺参数当前值列表
     */
    @PreAuthorize("@ss.hasPermi('baseDeviceParamVal:val:export')" )
    @Log(title = "设备工艺参数当前值" , businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, BaseDeviceParamVal baseDeviceParamVal) {
        List<BaseDeviceParamVal> list = baseDeviceParamValService.selectBaseDeviceParamValList(baseDeviceParamVal);
        ExcelUtil<BaseDeviceParamVal> util = new ExcelUtil<BaseDeviceParamVal>(BaseDeviceParamVal. class);
        util.exportExcel(response, list, "设备工艺参数当前值数据" );
    }

    /**
     * 获取设备工艺参数当前值详细信息
     */
    @PreAuthorize("@ss.hasPermi('baseDeviceParamVal:val:query')" )
    @GetMapping(value = "/{recordId}" )
    public AjaxResult getInfo(@PathVariable("recordId" ) String recordId) {
        return success(baseDeviceParamValService.selectBaseDeviceParamValByRecordId(recordId));
    }

    /**
     * 新增设备工艺参数当前值
     */
    @PreAuthorize("@ss.hasPermi('baseDeviceParamVal:val:add')" )
    @Log(title = "设备工艺参数当前值" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseDeviceParamVal baseDeviceParamVal) {

        return toAjax(baseDeviceParamValService.insertBaseDeviceParamVal(baseDeviceParamVal));
    }

    /**
     * 修改设备工艺参数当前值
     */
    @PreAuthorize("@ss.hasPermi('baseDeviceParamVal:val:edit')" )
    @Log(title = "设备工艺参数当前值" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseDeviceParamVal baseDeviceParamVal) {
        return toAjax(baseDeviceParamValService.updateBaseDeviceParamVal(baseDeviceParamVal));
    }

    /**
     * 删除设备工艺参数当前值
     */
    @PreAuthorize("@ss.hasPermi('baseDeviceParamVal:val:remove')" )
    @Log(title = "设备工艺参数当前值" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordIds}" )
    public AjaxResult remove(@PathVariable String[] recordIds) {
        return toAjax(baseDeviceParamValService.deleteBaseDeviceParamValByRecordIds(recordIds));
    }

    /**
     * 查询设备工艺参数当前值列表
     */
    @PreAuthorize("@ss.hasPermi('baseDeviceParamVal:val:list')" )
    @GetMapping("/getList" )
    public AjaxResult getList(BaseDeviceParamVal baseDeviceParamVal) {
        List<BaseDeviceParamVal> list = baseDeviceParamValService.selectBaseDeviceParamValList(baseDeviceParamVal);
        return success(list);
    }

    /**
     * 查询按设备聚合的最新参数列表（不分页）
     */
    @PreAuthorize("@ss.hasPermi('baseDeviceParamVal:val:list')" )
    @GetMapping("/latest" )
    public AjaxResult latest(BaseDeviceParamVal baseDeviceParamVal) {
        List<BaseDeviceParamVal> list = baseDeviceParamValService.selectLatestBaseDeviceParamValList(baseDeviceParamVal);
        return success(list);
    }

    /**
     * 获取设备状态统计（基于三色灯参数）
     * 返回：设备数量、开机率、运行台数、停机台数、待机台数、未开机台数
     */
    @Anonymous
    @GetMapping("/statistics" )
    public AjaxResult statistics() {
        return success(baseDeviceParamValService.selectDeviceStatusStatistics());
    }

    /**
     * 获取每个设备的编号、名称、三色灯状态
     */
    @Anonymous
    @GetMapping("/deviceStatus" )
    public AjaxResult deviceStatus() {
        return success(baseDeviceParamValService.selectDeviceStatusList());
    }

    /**
     * 获取每个设备的开机时间当天最新值
     */
    @Anonymous
    @GetMapping("/deviceStartTime" )
    public AjaxResult deviceStartTime() {
        return success(baseDeviceParamValService.selectDeviceStartTimeList());
    }

    /**
     * 参数追溯列表（分页）
     */
    @PreAuthorize("@ss.hasPermi('baseDeviceParamVal:trace:list')" )
    @GetMapping("/trace/list" )
    public TableDataInfo traceList(
            @RequestParam("deviceCode") String deviceCode,
            @RequestParam(value = "paramCode", required = false) String paramCode,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime) {
        startPage();
        List<BaseDeviceParamVal> list = baseDeviceParamValService.selectTraceList(deviceCode, paramCode, startTime, endTime);
        return getDataTable(list);
    }

    /**
     * 导出参数追溯数据
     */
    @PreAuthorize("@ss.hasPermi('baseDeviceParamVal:trace:export')" )
    @Log(title = "参数追溯数据导出" , businessType = BusinessType.EXPORT)
    @PostMapping("/trace/export" )
    public void traceExport(HttpServletResponse response,
            @RequestParam("deviceCode") String deviceCode,
            @RequestParam(value = "paramCode", required = false) String paramCode,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime) {
        List<BaseDeviceParamVal> list = baseDeviceParamValService.selectTraceList(deviceCode, paramCode, startTime, endTime);
        ExcelUtil<BaseDeviceParamVal> util = new ExcelUtil<BaseDeviceParamVal>(BaseDeviceParamVal.class);
        util.exportExcel(response, list, "参数追溯数据");
    }

    /**
     * 获取SPC分析数据
     */
    @PreAuthorize("@ss.hasPermi('baseDeviceParamVal:trace:list')" )
    @GetMapping("/spcData" )
    public AjaxResult getSPCData(
            @RequestParam("deviceCode") String deviceCode,
            @RequestParam("paramCode") String paramCode,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime) {
        return success(baseDeviceParamValService.getSPCData(deviceCode, paramCode, startTime, endTime));
    }

}
