package com.ruoyi.web.controller.ems;

import com.huashe.common.domain.AjaxResult;
import com.huashe.common.utils.uuid.Seq;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ems.domain.OpInspectionTask;
import com.ruoyi.ems.service.IOpInspectionPlanService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 巡检计划Controller
 *
 * @author ruoyi
 * @date 2024-08-29
 */
@RestController
@RequestMapping("/ems/inspectionPlan")
@Api(value = "OpInspectionPlanController", description = "巡检计划")
public class OpInspectionPlanController extends BaseController {
    @Autowired
    private IOpInspectionPlanService inspectionTaskService;

    /**
     * 查询巡检任务列表
     */
    @PreAuthorize("@ss.hasPermi('ems:inspection-task:list')")
    @GetMapping("/list")
    public TableDataInfo list(OpInspectionTask inspectionTask) {
        startPage();
        List<OpInspectionTask> list = inspectionTaskService.selectOpInspectionPlanList(inspectionTask);
        return getDataTable(list);
    }

    /**
     * 导出巡检任务列表
     */
    @PreAuthorize("@ss.hasPermi('ems:inspection-task:export')")
    @Log(title = "巡检任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OpInspectionTask inspectionTask) {
        List<OpInspectionTask> list = inspectionTaskService.selectOpInspectionPlanList(inspectionTask);
        ExcelUtil<OpInspectionTask> util = new ExcelUtil<OpInspectionTask>(OpInspectionTask.class);
        util.exportExcel(response, list, "巡检任务数据");
    }

    /**
     * 获取巡检任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('ems:inspection-task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(inspectionTaskService.selectOpInspectionPlanById(id));
    }

    /**
     * 新增巡检任务
     */
    @PreAuthorize("@ss.hasPermi('ems:inspection-task:add')")
    @Log(title = "巡检任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OpInspectionTask inspectionTask) {
        inspectionTask.setTaskCode(Seq.getId());
        return toAjax(inspectionTaskService.insertOpInspectionPlan(inspectionTask));
    }

    /**
     * 修改巡检任务
     */
    @PreAuthorize("@ss.hasPermi('ems:inspection-task:edit')")
    @Log(title = "巡检任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OpInspectionTask inspectionTask) {
        return toAjax(inspectionTaskService.updateOpInspectionPlan(inspectionTask));
    }

    /**
     * 删除巡检任务
     */
    @PreAuthorize("@ss.hasPermi('ems:inspection-task:remove')")
    @Log(title = "巡检任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(inspectionTaskService.deleteOpInspectionPlanByIds(ids));
    }
}
