package com.aucma.dms.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.poi.ExcelUtil;
import com.aucma.dms.domain.DmsRecordShutDown;
import com.aucma.dms.service.IDmsRecordShutDownService;
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 Open Platform
 * @date 2024-01-15
 */
@RestController
@RequestMapping("/dms/shutDown")
public class DmsRecordShutDownController extends BaseController
{
    @Autowired
    private IDmsRecordShutDownService dmsRecordShutDownService;

    /**
     * 查询停机记录列表
     */
    //@PreAuthorize("@ss.hasPermi('dms:shutDown:list')" )
    @GetMapping("/list")
    public TableDataInfo list(DmsRecordShutDown dmsRecordShutDown)
    {
        startPage();
        List<DmsRecordShutDown> list = dmsRecordShutDownService.selectDmsRecordShutDownList(dmsRecordShutDown);
        return getDataTable(list);
    }

    /**
     * 查询停机记录列表（不分页）
     */
    //@PreAuthorize("@ss.hasPermi('dms:shutDown:list')" )
    @GetMapping("/listAll")
    public AjaxResult listAll(DmsRecordShutDown dmsRecordShutDown)
    {
        List<DmsRecordShutDown> list = dmsRecordShutDownService.selectDmsRecordShutDownList(dmsRecordShutDown);
        return success(list);
    }

    /**
     * 导出停机记录列表
     */
    //@PreAuthorize("@ss.hasPermi('dms:shutDown:export')" )
    ////@Log(title = "停机记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DmsRecordShutDown dmsRecordShutDown)
    {
        List<DmsRecordShutDown> list = dmsRecordShutDownService.selectDmsRecordShutDownList(dmsRecordShutDown);
        ExcelUtil<DmsRecordShutDown> util = new ExcelUtil<DmsRecordShutDown>(DmsRecordShutDown.class);
        util.exportExcel(response, list, "停机记录数据");
    }

    /**
     * 获取停机记录详细信息
     */
    //@PreAuthorize("@ss.hasPermi('dms:shutDown:remove')" )
    @GetMapping(value = "/{recordShutDownId}")
    public AjaxResult getInfo(@PathVariable("recordShutDownId") Long recordShutDownId)
    {
        return success(dmsRecordShutDownService.selectDmsRecordShutDownByRecordShutDownId(recordShutDownId));
    }

    /**
     * 新增停机记录
     */
    //@PreAuthorize("@ss.hasPermi('dms:shutDown:add')" )
    ////@Log(title = "停机记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DmsRecordShutDown dmsRecordShutDown)
    {
        return toAjax(dmsRecordShutDownService.insertDmsRecordShutDown(dmsRecordShutDown));
    }

    /**
     * 修改停机记录
     */
    //@PreAuthorize("@ss.hasPermi('dms:shutDown:edit')" )
    ////@Log(title = "停机记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DmsRecordShutDown dmsRecordShutDown)
    {
        return toAjax(dmsRecordShutDownService.updateDmsRecordShutDown(dmsRecordShutDown));
    }

    /**
     * 删除停机记录
     */
    //@PreAuthorize("@ss.hasPermi('dms:shutDown:remove')" )
    ////@Log(title = "停机记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordShutDownIds}")
    public AjaxResult remove(@PathVariable Long[] recordShutDownIds)
    {
        return toAjax(dmsRecordShutDownService.deleteDmsRecordShutDownByRecordShutDownIds(recordShutDownIds));
    }

    /**
     * 查询PDA待处理停机记录列表
     */
    @PreAuthorize("@ss.hasPermi('dms:shutDown:query')")
    @GetMapping("/pda/list")
    public AjaxResult pdaList()
    {
        List<DmsRecordShutDown> list = dmsRecordShutDownService.selectPdaShutDownList();
        return AjaxResult.success(list);
    }

    /**
     * 批量更新停机原因
     */
    @PreAuthorize("@ss.hasPermi('dms:shutDown:edit')")
    @Log(title = "停机记录", businessType = BusinessType.UPDATE)
    @PutMapping("/batchUpdateReason")
    public AjaxResult batchUpdateReason(@RequestBody DmsRecordShutDown dmsRecordShutDown)
    {
        if (dmsRecordShutDown.getRecordShutDownIds() == null || dmsRecordShutDown.getRecordShutDownIds().length == 0)
        {
            return AjaxResult.error("请选择要维护的停机记录");
        }
        if (dmsRecordShutDown.getShutReasonId() == null)
        {
            return AjaxResult.error("请选择停机原因");
        }
        return toAjax(dmsRecordShutDownService.batchUpdateShutReason(dmsRecordShutDown.getRecordShutDownIds(), dmsRecordShutDown.getShutReasonId()));
    }
}
