package com.ruoyi.project.production.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.interceptor.annotation.RepeatSubmit;
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.production.domain.TFivesMonthlyInspection;
import com.ruoyi.project.production.service.ITFivesMonthlyInspectionService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 区域月度检查Controller
 *
 * @author ruoyi
 * @date 2023-03-30
 */
@RestController
@RequestMapping("/production/inspection")
public class TFivesMonthlyInspectionController extends BaseController {
    @Autowired
    private ITFivesMonthlyInspectionService tFivesMonthlyInspectionService;

    /**
     * 查询区域月度检查列表
     */
    @PreAuthorize("@ss.hasPermi('production:inspection:list')")
    @GetMapping("/list")
    public TableDataInfo list(TFivesMonthlyInspection tFivesMonthlyInspection) {
        startPage();
        List<TFivesMonthlyInspection> list = tFivesMonthlyInspectionService.selectTFivesMonthlyInspectionList(tFivesMonthlyInspection);
        return getDataTable(list);
    }

    /**
     * 导出区域月度检查列表
     */
    @PreAuthorize("@ss.hasPermi('production:inspection:export')")
    @Log(title = "区域月度检查", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TFivesMonthlyInspection tFivesMonthlyInspection) {
        List<TFivesMonthlyInspection> list = tFivesMonthlyInspectionService.selectTFivesMonthlyInspectionList(tFivesMonthlyInspection);
        ExcelUtil<TFivesMonthlyInspection> util = new ExcelUtil<TFivesMonthlyInspection>(TFivesMonthlyInspection.class);
        return util.exportExcel(list, "inspection");
    }

    /**
     * 获取区域月度检查详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:inspection:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tFivesMonthlyInspectionService.selectTFivesMonthlyInspectionById(id));
    }

    /**
     * 新增区域月度检查
     */
    @PreAuthorize("@ss.hasPermi('production:inspection:add')")
    @Log(title = "区域月度检查", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@RequestBody TFivesMonthlyInspection tFivesMonthlyInspection) {
        TFivesMonthlyInspection tfmi = new TFivesMonthlyInspection();
        tfmi.setPosition(tFivesMonthlyInspection.getPosition());
        tfmi.setOwner(tFivesMonthlyInspection.getOwner());
        tfmi.setRegionOrder(tFivesMonthlyInspection.getRegionOrder());
        tfmi.setYear(tFivesMonthlyInspection.getYear());
        List<TFivesMonthlyInspection> selected = tFivesMonthlyInspectionService.selectTFivesMonthlyInspectionList(tfmi);
        if (CollectionUtils.isNotEmpty(selected)) {
            return AjaxResult.error("当前数据已存在");
        }
        tFivesMonthlyInspection.setCreatedate(new Date());
        tFivesMonthlyInspection.setCreaterCode(getUserId().toString());
        return toAjax(tFivesMonthlyInspectionService.insertTFivesMonthlyInspection(tFivesMonthlyInspection));
    }

    /**
     * 修改区域月度检查
     */
    @PreAuthorize("@ss.hasPermi('production:inspection:edit')")
    @Log(title = "区域月度检查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TFivesMonthlyInspection tFivesMonthlyInspection) {
        return toAjax(tFivesMonthlyInspectionService.updateTFivesMonthlyInspection(tFivesMonthlyInspection));
    }

    /**
     * 删除区域月度检查
     */
    @PreAuthorize("@ss.hasPermi('production:inspection:remove')")
    @Log(title = "区域月度检查", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tFivesMonthlyInspectionService.deleteTFivesMonthlyInspectionByIds(ids));
    }
}
