package com.ruoyi.project.pssr.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.patrol.patrol.service.ITPatrolMainService;
import com.ruoyi.project.patrol.patrol.service.ITPatrolPlanService;
import com.ruoyi.project.pssr.domain.TPssrPatrol;
import com.ruoyi.project.pssr.mapper.TPssrPatrolMapper;
import com.ruoyi.project.pssr.service.ITPssrFileService;
import com.ruoyi.project.pssr.service.ITPssrPatrolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 巡检Controller
 *
 * @author ssy
 * @date 2024-09-18
 */
@RestController
@RequestMapping("/pssr/patrol")
public class TPssrPatrolController extends BaseController {
    @Resource
     private TPssrPatrolMapper tPssrPatrolMapper;
    @Autowired
    private ITPssrFileService tPssrFileService;
    @Autowired
    private ITPssrPatrolService tPssrPatrolService;
    @Autowired
    private ITPatrolPlanService tPatrolPlanService;
    @Autowired
    private ITPatrolMainService tPatrolMainService;

    /**
     * 查询巡检列表
     */
    @PreAuthorize("@ss.hasPermi('pssr:patrol:list')")
    @GetMapping("/list")
    public TableDataInfo list(TPssrPatrol tPssrPatrol) {
        startPage();
        List<TPssrPatrol> list = tPssrPatrolService.selectTPssrPatrolList(tPssrPatrol);
        list.forEach(item->{
            item.setPlanName(tPatrolPlanService.selectTPatrolPlanById(item.getPlanId()).getPlanName());
        });
        return getDataTable(list);
    }

    /**
     * 导出巡检列表
     */
    @PreAuthorize("@ss.hasPermi('pssr:patrol:export')")
    @Log(title = "巡检", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TPssrPatrol tPssrPatrol) {
        List<TPssrPatrol> list = tPssrPatrolService.selectTPssrPatrolList(tPssrPatrol);
        ExcelUtil<TPssrPatrol> util = new ExcelUtil<TPssrPatrol>(TPssrPatrol.class);
        return util.exportExcel(list, "patrol");
    }

    /**
     * 获取巡检详细信息
     */
    @PreAuthorize("@ss.hasPermi('pssr:patrol:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tPssrPatrolService.selectTPssrPatrolById(id));
    }

    /**
     * 新增巡检
     */
    @PreAuthorize("@ss.hasPermi('pssr:patrol:add')")
    @Log(title = "巡检", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPssrPatrol tPssrPatrol) {
        return toAjax(tPssrPatrolService.insertTPssrPatrol(tPssrPatrol, getUserId().toString()));
    }

    /**
     * 修改巡检
     */
    @PreAuthorize("@ss.hasPermi('pssr:patrol:edit')")
    @Log(title = "巡检", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPssrPatrol tPssrPatrol) {
        return toAjax(tPssrPatrolService.updateTPssrPatrol(tPssrPatrol));
    }

    /**
     * 修改巡检
     */
    @PreAuthorize("@ss.hasPermi('pssr:patrol:edit')")
    @Log(title = "巡检", businessType = BusinessType.UPDATE)
    @PutMapping("/editBatch")
    public AjaxResult editb(@RequestBody TPssrPatrol tPssrPatrol) {
        return toAjax(tPssrPatrolMapper.updateTPssrPatrolByIds(tPssrPatrol));
    }


    /**
     * 删除巡检
     */
    @PreAuthorize("@ss.hasPermi('pssr:patrol:remove')")
    @Log(title = "巡检", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tPssrPatrolService.deleteTPssrPatrolByIds(ids));
    }
}
