package com.ruoyi.project.pssr.controller;

import com.ruoyi.common.utils.file.FileUploadUtils;
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.config.RuoYiConfig;
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.pssr.domain.TPssrInstrumentTest;
import com.ruoyi.project.pssr.mapper.TPssrInstrumentTestMapper;
import com.ruoyi.project.pssr.service.ITPssrFileService;
import com.ruoyi.project.pssr.service.ITPssrInstrumentTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 仪联锁测试Controller
 *
 * @author ssy
 * @date 2024-09-18
 */
@RestController
@RequestMapping("/pssr/instrumentTest")
public class TPssrInstrumentTestController extends BaseController {
    @Autowired
    private ITPssrFileService tPssrFileService;
    @Resource
    private TPssrInstrumentTestMapper tPssrInstrumentTestMapper;
    @Autowired
    private ITPssrInstrumentTestService tPssrInstrumentTestService;

    /**
     * 查询仪联锁测试列表
     */
    @PreAuthorize("@ss.hasPermi('pssr:instrumentTest:list')")
    @GetMapping("/list")
    public TableDataInfo list(TPssrInstrumentTest tPssrInstrumentTest) {
        startPage();
        List<TPssrInstrumentTest> list = tPssrInstrumentTestService.selectTPssrInstrumentTestList(tPssrInstrumentTest);
        return getDataTable(list);
    }

    /**
     * 导出仪联锁测试列表
     */
    @PreAuthorize("@ss.hasPermi('pssr:instrumentTest:export')")
    @Log(title = "仪联锁测试", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TPssrInstrumentTest tPssrInstrumentTest) {
        List<TPssrInstrumentTest> list = tPssrInstrumentTestService.selectTPssrInstrumentTestList(tPssrInstrumentTest);
        ExcelUtil<TPssrInstrumentTest> util = new ExcelUtil<TPssrInstrumentTest>(TPssrInstrumentTest.class);
        return util.exportExcel(list, "instrumentTest");
    }

    /**
     * 获取仪联锁测试详细信息
     */
    @PreAuthorize("@ss.hasPermi('pssr:instrumentTest:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tPssrInstrumentTestService.selectTPssrInstrumentTestById(id));
    }

    /**
     * 新增仪联锁测试
     */
    @PreAuthorize("@ss.hasPermi('pssr:instrumentTest:add')")
    @Log(title = "仪联锁测试", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPssrInstrumentTest tPssrInstrumentTest) {
        return toAjax(tPssrInstrumentTestService.insertTPssrInstrumentTest(tPssrInstrumentTest));
    }

    /**
     * 修改仪联锁测试
     */
    @PreAuthorize("@ss.hasPermi('pssr:instrumentTest:edit')")
    @Log(title = "仪联锁测试", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPssrInstrumentTest tPssrInstrumentTest)
    {
        return toAjax(tPssrInstrumentTestService.updateTPssrInstrumentTest(tPssrInstrumentTest));
    }

    /**
     * 修改仪联锁测试
     */
    @PreAuthorize("@ss.hasPermi('pssr:instrumentTest:edit')")
    @Log(title = "仪联锁测试", businessType = BusinessType.UPDATE)
    @PutMapping("/editBatch")
    public AjaxResult editb(@RequestBody TPssrInstrumentTest tPssrInstrumentTest)
    {
        return toAjax(tPssrInstrumentTestMapper.updateTPssrInstrumentTestByIds(tPssrInstrumentTest));
    }

    /**
     * 删除仪联锁测试
     */
    @PreAuthorize("@ss.hasPermi('pssr:instrumentTest:remove')")
    @Log(title = "仪联锁测试", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tPssrInstrumentTestService.deleteTPssrInstrumentTestByIds(ids));
    }

    @Log(title = "仪联锁测试附件上传", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, Long subId) throws IOException {
        if (!file.isEmpty()) {
            String url = FileUploadUtils.upload(RuoYiConfig.getFilePath("/pssr/yblscs"), file);
            TPssrInstrumentTest test = new TPssrInstrumentTest();
            test.setSubId(subId);
            test.setFileUrl(url);
            test.setFileName(file.getOriginalFilename());
            test.setApproveStatus(0L);
            test.setCreatedate(new Date());
            test.setCreaterCode(getUserId().toString());
            tPssrInstrumentTestService.insertTPssrInstrumentTest(test);
            return AjaxResult.success();
        }
        return AjaxResult.error("上传失败，请联系管理员");
    }
}
