package com.ruoyi.project.production.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.production.domain.TFivesFile;
import com.ruoyi.project.production.service.ITFivesFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 5s管理附件Controller
 *
 * @author ruoyi
 * @date 2023-03-30
 */
@RestController
@RequestMapping("/production/file")
public class TFivesFileController extends BaseController
{
    @Autowired
    private ITFivesFileService tFivesFileService;

    /**
     * 查询5s管理附件列表
     */
   // @PreAuthorize("@ss.hasPermi('production:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(TFivesFile tFivesFile)
    {
        startPage();
        List<TFivesFile> list = tFivesFileService.selectTFivesFileList(tFivesFile);
        return getDataTable(list);
    }

    /**
     * 导出5s管理附件列表
     */
    //@PreAuthorize("@ss.hasPermi('production:file:export')")
    @Log(title = "5s管理附件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TFivesFile tFivesFile)
    {
        List<TFivesFile> list = tFivesFileService.selectTFivesFileList(tFivesFile);
        ExcelUtil<TFivesFile> util = new ExcelUtil<TFivesFile>(TFivesFile.class);
        return util.exportExcel(list, "file");
    }

    /**
     * 获取5s管理附件详细信息
     */
    //@PreAuthorize("@ss.hasPermi('production:file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tFivesFileService.selectTFivesFileById(id));
    }

    /**
     * 新增5s管理附件
     */
    //@PreAuthorize("@ss.hasPermi('production:file:add')")
    @Log(title = "5s管理附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TFivesFile tFivesFile)
    {
        return toAjax(tFivesFileService.insertTFivesFile(tFivesFile));
    }

    /**
     * 修改5s管理附件
     */
//    @PreAuthorize("@ss.hasPermi('production:file:edit')")
    @Log(title = "5s管理附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TFivesFile tFivesFile)
    {
        return toAjax(tFivesFileService.updateTFivesFile(tFivesFile));
    }

    /**
     * 删除5s管理附件
     */
   // @PreAuthorize("@ss.hasPermi('production:file:remove')")
    @Log(title = "5s管理附件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tFivesFileService.deleteTFivesFileByIds(ids));
    }

    @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("/5sFile"), file);
            TFivesFile tFivesFile = new TFivesFile();
            tFivesFile.setName(file.getName());
            tFivesFile.setUrl(avatar);
            tFivesFile.setCreatedate(new Date());
            tFivesFile.setCreaterCode(getUserId().toString());
            tFivesFileService.insertTFivesFile(tFivesFile);
            return AjaxResult.success(tFivesFile.getId());
        }
        return AjaxResult.error("上传异常，请联系管理员");
    }
}
