package com.aucma.production.controller;

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 com.aucma.production.domain.AndonEvent;
import com.aucma.production.service.IAndonEventService;
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-10-28
 */
@RestController
@RequestMapping("/production/andonEvent" )
public class AndonEventController extends BaseController {
    @Autowired
    private IAndonEventService andonEventService;

    /**
     * 查询安灯事件列表
     */
    @PreAuthorize("@ss.hasPermi('production:andonEvent:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(AndonEvent andonEvent) {
        startPage();
        List<AndonEvent> list = andonEventService.selectAndonEventList(andonEvent);
        return getDataTable(list);
    }

    /**
     * 导出安灯事件列表
     */
    @PreAuthorize("@ss.hasPermi('production:andonEvent:export')" )
    @Log(title = "安灯事件" , businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, AndonEvent andonEvent) {
        List<AndonEvent> list = andonEventService.selectAndonEventList(andonEvent);
        ExcelUtil<AndonEvent> util = new ExcelUtil<AndonEvent>(AndonEvent. class);
        util.exportExcel(response, list, "安灯事件数据" );
    }

    /**
     * 获取安灯事件详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:andonEvent:query')" )
    @GetMapping(value = "/{eventId}" )
    public AjaxResult getInfo(@PathVariable("eventId" ) Long eventId) {
        return success(andonEventService.selectAndonEventByEventId(eventId));
    }

    /**
     * 新增安灯事件
     */
    @PreAuthorize("@ss.hasPermi('production:andonEvent:add')" )
    @Log(title = "安灯事件" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AndonEvent andonEvent) {
        andonEvent.setCreateBy(getUsername());
        andonEvent.setCreateTime(DateUtils.getNowDate());
        return toAjax(andonEventService.insertAndonEvent(andonEvent));
    }

    /**
     * 修改安灯事件
     */
    @PreAuthorize("@ss.hasPermi('production:andonEvent:edit')" )
    @Log(title = "安灯事件" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AndonEvent andonEvent) {
        andonEvent.setUpdateBy(getUsername());
        andonEvent.setUpdateTime(DateUtils.getNowDate());
        return toAjax(andonEventService.updateAndonEvent(andonEvent));
    }

    /**
     * 删除安灯事件
     */
    @PreAuthorize("@ss.hasPermi('production:andonEvent:remove')" )
    @Log(title = "安灯事件" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{eventIds}" )
    public AjaxResult remove(@PathVariable Long[] eventIds) {
        return toAjax(andonEventService.deleteAndonEventByEventIds(eventIds));
    }


    /**
     * 查询安灯事件列表
     */
    @PreAuthorize("@ss.hasPermi('production:andonEvent:list')" )
    @GetMapping("/getAndonEventList" )
    public AjaxResult getAndonEventList(AndonEvent andonEvent) {
        List<AndonEvent> list = andonEventService.selectAndonEventList(andonEvent);
        return success(list);
    }

    /**
     * 确认安灯事件
     */
    @PreAuthorize("@ss.hasPermi('production:andonEvent:edit')" )
    @Log(title = "安灯事件确认" , businessType = BusinessType.UPDATE)
    @PostMapping("/acknowledge/{eventId}" )
    public AjaxResult acknowledge(@PathVariable Long eventId) {
        return toAjax(andonEventService.acknowledgeEvent(eventId, getUsername()));
    }

    /**
     * 开始处理安灯事件
     */
    @PreAuthorize("@ss.hasPermi('production:andonEvent:edit')" )
    @Log(title = "安灯事件开始处理" , businessType = BusinessType.UPDATE)
    @PostMapping("/startProcess/{eventId}" )
    public AjaxResult startProcess(@PathVariable Long eventId) {
        return toAjax(andonEventService.startProcessEvent(eventId, getUsername()));
    }

    /**
     * 完成安灯事件
     */
    @PreAuthorize("@ss.hasPermi('production:andonEvent:edit')" )
    @Log(title = "安灯事件完成" , businessType = BusinessType.UPDATE)
    @PostMapping("/complete/{eventId}" )
    public AjaxResult complete(@PathVariable Long eventId, @RequestParam(required = false) String resolution) {
        return toAjax(andonEventService.completeEvent(eventId, resolution, getUsername()));
    }

    /**
     * 取消安灯事件
     */
    @PreAuthorize("@ss.hasPermi('production:andonEvent:edit')" )
    @Log(title = "安灯事件取消" , businessType = BusinessType.UPDATE)
    @PostMapping("/cancel/{eventId}" )
    public AjaxResult cancel(@PathVariable Long eventId, @RequestParam(required = false) String cancelReason) {
        return toAjax(andonEventService.cancelEvent(eventId, cancelReason, getUsername()));
    }

}
