package com.aucma.base.controller;

import com.aucma.base.domain.ProcessAlert;
import com.aucma.base.service.IProcessAlertService;
import com.aucma.common.annotation.Log;
import com.aucma.common.core.controller.BaseController;
import com.aucma.common.core.domain.AjaxResult;
import com.aucma.common.core.page.TableDataInfo;
import com.aucma.common.enums.BusinessType;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 工艺预警Controller
 *
 * @author Yinq
 * @date 2025-12-30
 */
@RestController
@RequestMapping("/base/processAlert")
public class ProcessAlertController extends BaseController {
    @Autowired
    private IProcessAlertService processAlertService;

    @PreAuthorize("@ss.hasPermi('base:processAlert:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProcessAlert processAlert) {
        startPage();
        List<ProcessAlert> list = processAlertService.selectProcessAlertList(processAlert);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('base:processAlert:export')")
    @Log(title = "工艺预警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProcessAlert processAlert) {
        List<ProcessAlert> list = processAlertService.selectProcessAlertList(processAlert);
        ExcelUtil<ProcessAlert> util = new ExcelUtil<ProcessAlert>(ProcessAlert.class);
        util.exportExcel(response, list, "工艺预警数据");
    }

    @PreAuthorize("@ss.hasPermi('base:processAlert:query')")
    @GetMapping(value = "/{alertId}")
    public AjaxResult getInfo(@PathVariable("alertId") Long alertId) {
        return success(processAlertService.selectProcessAlertByAlertId(alertId));
    }

    @PreAuthorize("@ss.hasPermi('base:processAlert:add')")
    @Log(title = "工艺预警", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProcessAlert processAlert) {
        processAlert.setCreateBy(getUsername());
        return toAjax(processAlertService.insertProcessAlert(processAlert));
    }

    @PreAuthorize("@ss.hasPermi('base:processAlert:edit')")
    @Log(title = "工艺预警", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProcessAlert processAlert) {
        processAlert.setUpdateBy(getUsername());
        return toAjax(processAlertService.updateProcessAlert(processAlert));
    }

    @PreAuthorize("@ss.hasPermi('base:processAlert:remove')")
    @Log(title = "工艺预警", businessType = BusinessType.DELETE)
    @DeleteMapping("/{alertIds}")
    public AjaxResult remove(@PathVariable Long[] alertIds) {
        return toAjax(processAlertService.deleteProcessAlertByAlertIds(alertIds));
    }

    @PreAuthorize("@ss.hasPermi('base:processAlert:edit')")
    @Log(title = "工艺预警处理", businessType = BusinessType.UPDATE)
    @PutMapping("/handle")
    public AjaxResult handleAlert(@RequestBody ProcessAlert processAlert) {
        processAlert.setUpdateBy(getUsername());
        processAlert.setHandleUser(getUsername());
        processAlert.setHandleTime(DateUtils.getNowDate());
        return toAjax(processAlertService.updateProcessAlert(processAlert));
    }
    
    /**
     * 检查参数阈值并生成预警
     */
    @PreAuthorize("@ss.hasPermi('base:processAlert:add')")
    @Log(title = "检查阈值预警", businessType = BusinessType.INSERT)
    @PostMapping("/checkThreshold")
    public AjaxResult checkThreshold() {
        int count = processAlertService.checkThresholdAlerts();
        return success("检查完成，生成预警 " + count + " 条");
    }

    /**
     * 批量标记预警为已处理
     */
    @PreAuthorize("@ss.hasPermi('base:processAlert:edit')")
    @Log(title = "批量标记预警已处理", businessType = BusinessType.UPDATE)
    @PutMapping("/batchMarkProcessed")
    public AjaxResult batchMarkProcessed(@RequestBody Long[] alertIds) {
        int count = processAlertService.batchMarkAsProcessed(alertIds, getUsername());
        return success("成功标记 " + count + " 条预警为已处理");
    }
}
