package com.ruoyi.project.sems.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.sems.domain.TSpecFile;
import com.ruoyi.project.sems.service.ITSpecFileService;
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;

/**
 * 特种设备文件Controller
 *
 * @author ruoyi
 * @date 2021-08-02
 */
@RestController
@RequestMapping("/sems/specfile")
public class TSpecFileController extends BaseController
{
    @Autowired
    private ITSpecFileService tSpecFileService;

    /**
     * 查询特种设备文件列表
     */
    @PreAuthorize("@ss.hasPermi('sems:specfile:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSpecFile tSpecFile)
    {
        startPage();
        List<TSpecFile> list = tSpecFileService.selectTSpecFileList(tSpecFile);
        return getDataTable(list);
    }

    /**
     * 导出特种设备文件列表
     */
    @PreAuthorize("@ss.hasPermi('sems:specfile:export')")
    @Log(title = "特种设备文件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TSpecFile tSpecFile)
    {
        List<TSpecFile> list = tSpecFileService.selectTSpecFileList(tSpecFile);
        ExcelUtil<TSpecFile> util = new ExcelUtil<TSpecFile>(TSpecFile.class);
        return util.exportExcel(list, "specfile");
    }

    /**
     * 获取特种设备文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('sems:specfile:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tSpecFileService.selectTSpecFileById(id));
    }

    /**
     * 新增特种设备文件
     */
    @PreAuthorize("@ss.hasPermi('sems:specfile:add')")
    @Log(title = "特种设备文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSpecFile tSpecFile)
    {
        return toAjax(tSpecFileService.insertTSpecFile(tSpecFile));
    }

    /**
     * 修改特种设备文件
     */
    @PreAuthorize("@ss.hasPermi('sems:specfile:edit')")
    @Log(title = "特种设备文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSpecFile tSpecFile)
    {
        return toAjax(tSpecFileService.updateTSpecFile(tSpecFile));
    }

    /**
     * 删除特种设备文件
     */
    @PreAuthorize("@ss.hasPermi('sems:specfile:remove')")
    @Log(title = "特种设备文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tSpecFileService.deleteTSpecFileByIds(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("/specFile"), file);
            return AjaxResult.success(avatar);
        }
        return AjaxResult.error("上传异常，请联系管理员");
    }

}
