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.DmsRepairWorkOrder;
import com.aucma.dms.service.IDmsRepairWorkOrderService;
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 Cascade AI
 * @date 2025-01-10
 */
@RestController
@RequestMapping("/dms/repairWorkOrder")
public class DmsRepairWorkOrderController extends BaseController {
    
    @Autowired
    private IDmsRepairWorkOrderService dmsRepairWorkOrderService;

    /**
     * 查询维修工单列表
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairWorkOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(DmsRepairWorkOrder dmsRepairWorkOrder) {
        startPage();
        List<DmsRepairWorkOrder> list = dmsRepairWorkOrderService.selectDmsRepairWorkOrderList(dmsRepairWorkOrder);
        return getDataTable(list);
    }

    /**
     * 导出维修工单列表
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairWorkOrder:export')")
    @Log(title = "维修工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DmsRepairWorkOrder dmsRepairWorkOrder) {
        List<DmsRepairWorkOrder> list = dmsRepairWorkOrderService.selectDmsRepairWorkOrderList(dmsRepairWorkOrder);
        ExcelUtil<DmsRepairWorkOrder> util = new ExcelUtil<DmsRepairWorkOrder>(DmsRepairWorkOrder.class);
        util.exportExcel(response, list, "维修工单数据");
    }

    /**
     * 获取维修工单详细信息
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairWorkOrder:query')")
    @GetMapping(value = "/{workOrderId}")
    public AjaxResult getInfo(@PathVariable("workOrderId") Long workOrderId) {
        return AjaxResult.success(dmsRepairWorkOrderService.selectDmsRepairWorkOrderByWorkOrderId(workOrderId));
    }

    /**
     * 新增维修工单
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairWorkOrder:add')")
    @Log(title = "维修工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DmsRepairWorkOrder dmsRepairWorkOrder) {
        return toAjax(dmsRepairWorkOrderService.insertDmsRepairWorkOrder(dmsRepairWorkOrder));
    }

    /**
     * 修改维修工单
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairWorkOrder:edit')")
    @Log(title = "维修工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DmsRepairWorkOrder dmsRepairWorkOrder) {
        return toAjax(dmsRepairWorkOrderService.updateDmsRepairWorkOrder(dmsRepairWorkOrder));
    }

    /**
     * 删除维修工单
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairWorkOrder:remove')")
    @Log(title = "维修工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workOrderIds}")
    public AjaxResult remove(@PathVariable Long[] workOrderIds) {
        return toAjax(dmsRepairWorkOrderService.deleteDmsRepairWorkOrderByWorkOrderIds(workOrderIds));
    }

    /**
     * 派工
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairWorkOrder:dispatch')")
    @Log(title = "维修工单派工", businessType = BusinessType.UPDATE)
    @PostMapping("/dispatch")
    public AjaxResult dispatch(@RequestBody DmsRepairWorkOrder dmsRepairWorkOrder) {
        return toAjax(dmsRepairWorkOrderService.dispatchWorkOrder(dmsRepairWorkOrder));
    }

    /**
     * 开始维修
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairWorkOrder:start')")
    @Log(title = "开始维修", businessType = BusinessType.UPDATE)
    @PostMapping("/start")
    public AjaxResult start(@RequestBody DmsRepairWorkOrder dmsRepairWorkOrder) {
        return toAjax(dmsRepairWorkOrderService.startWorkOrder(dmsRepairWorkOrder.getWorkOrderId()));
    }

    /**
     * 完成维修
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairWorkOrder:complete')")
    @Log(title = "完成维修", businessType = BusinessType.UPDATE)
    @PostMapping("/complete")
    public AjaxResult complete(@RequestBody DmsRepairWorkOrder dmsRepairWorkOrder) {
        return toAjax(dmsRepairWorkOrderService.completeWorkOrder(dmsRepairWorkOrder));
    }

    /**
     * 审批
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairWorkOrder:approve')")
    @Log(title = "维修工单审批", businessType = BusinessType.UPDATE)
    @PostMapping("/approve")
    public AjaxResult approve(@RequestBody DmsRepairWorkOrder dmsRepairWorkOrder) {
        return toAjax(dmsRepairWorkOrderService.approveWorkOrder(dmsRepairWorkOrder));
    }
}
