package com.ruoyi.project.production.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.production.domain.TSaiFile;
import com.ruoyi.project.production.service.ITSaiFileService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * SAI文件Controller
 *
 * @author ruoyi
 * @date 2023-04-06
 */
@RestController
@RequestMapping("/production/saiFile")
public class TSaiFileController extends BaseController
{
    @Autowired
    private ITSaiFileService tSaiFileService;

    /**
     * 上传
     */
    @Log(title = "文件上传", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getFilePath("/saiFile"), file);
            return AjaxResult.success(avatar);
        }
        return AjaxResult.error("上传异常，请联系管理员");
    }

    /**
     * 查询SAI文件列表
     */
//    @PreAuthorize("@ss.hasPermi('production:saiFile:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSaiFile tSaiFile)
    {
        startPage();
        List<TSaiFile> list = tSaiFileService.selectTSaiFileList(tSaiFile);
        return getDataTable(list);
    }

    /**
     * 导出SAI文件列表
     */
//    @PreAuthorize("@ss.hasPermi('production:saiFile:export')")
    @Log(title = "SAI文件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TSaiFile tSaiFile)
    {
        List<TSaiFile> list = tSaiFileService.selectTSaiFileList(tSaiFile);
        ExcelUtil<TSaiFile> util = new ExcelUtil<TSaiFile>(TSaiFile.class);
        return util.exportExcel(list, "saiFile");
    }

    /**
     * 获取SAI文件详细信息
     */
//    @PreAuthorize("@ss.hasPermi('production:saiFile:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tSaiFileService.selectTSaiFileById(id));
    }

    /**
     * 新增SAI文件
     */
//    @PreAuthorize("@ss.hasPermi('production:saiFile:add')")
    @Log(title = "SAI文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSaiFile tSaiFile)
    {
        return toAjax(tSaiFileService.insertTSaiFile(tSaiFile));
    }

    /**
     * 修改SAI文件
     */
//    @PreAuthorize("@ss.hasPermi('production:saiFile:edit')")
    @Log(title = "SAI文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSaiFile tSaiFile)
    {
        return toAjax(tSaiFileService.updateTSaiFile(tSaiFile));
    }

    /**
     * 删除SAI文件
     */
//    @PreAuthorize("@ss.hasPermi('production:saiFile:remove')")
    @Log(title = "SAI文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tSaiFileService.deleteTSaiFileByIds(ids));
    }
}
