package com.cpms.project.safety.controller;


import com.cpms.common.annotation.Log;
import com.cpms.common.config.RuoYiConfig;
import com.cpms.common.core.controller.BaseController;
import com.cpms.common.core.domain.AjaxResult;
import com.cpms.common.core.page.TableDataInfo;
import com.cpms.common.enums.BusinessType;
import com.cpms.common.utils.file.FileUploadUtils;
import com.cpms.common.utils.poi.ExcelUtil;
import com.cpms.project.safety.domain.TSafetyFiles;
import com.cpms.project.safety.service.ITSafetyFilesService;
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.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 安全设备设施检查附件Controller
 *
 * @author ruoyi
 * @date 2023-05-25
 */
@RestController
@RequestMapping("/safety/files")
public class TSafetyFilesController extends BaseController
{
    @Autowired
    private ITSafetyFilesService tSafetyFilesService;

    /**
     * 查询安全设备设施检查附件列表
     */
    @PreAuthorize("@ss.hasPermi('safety:files:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSafetyFiles tSafetyFiles)
    {
        startPage();
        List<TSafetyFiles> list = tSafetyFilesService.selectTSafetyFilesList(tSafetyFiles);
        return getDataTable(list);
    }
    /**
     * 导出安全设备设施检查附件列表
     */
    @PreAuthorize("@ss.hasPermi('safety:files:export')")
    @Log(title = "安全设备设施检查附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TSafetyFiles tSafetyFiles)
    {
        List<TSafetyFiles> list = tSafetyFilesService.selectTSafetyFilesList(tSafetyFiles);
        ExcelUtil<TSafetyFiles> util = new ExcelUtil<TSafetyFiles>(TSafetyFiles.class);
        util.exportExcel(list, "安全设备设施检查附件数据");
    }

    /**
     * 获取安全设备设施检查附件详细信息
     */
    @PreAuthorize("@ss.hasPermi('safety:files:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tSafetyFilesService.selectTSafetyFilesById(id));
    }

    /**
     * 新增安全设备设施检查附件
     */
    @PreAuthorize("@ss.hasPermi('safety:files:add')")
    @Log(title = "安全设备设施检查附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSafetyFiles tSafetyFiles)
    {
        return toAjax(tSafetyFilesService.insertTSafetyFiles(tSafetyFiles));
    }

    /**
     * 修改安全设备设施检查附件
     */
    @PreAuthorize("@ss.hasPermi('safety:files:edit')")
    @Log(title = "安全设备设施检查附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSafetyFiles tSafetyFiles)
    {
        return toAjax(tSafetyFilesService.updateTSafetyFiles(tSafetyFiles));
    }

    /**
     * 删除安全设备设施检查附件
     */
    @PreAuthorize("@ss.hasPermi('safety:files:remove')")
    @Log(title = "安全设备设施检查附件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tSafetyFilesService.deleteTSafetyFilesByIds(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("/safetyFile"), file);
            TSafetyFiles tSafetyFiles = new TSafetyFiles();
            tSafetyFiles.setFileName(file.getName());
            tSafetyFiles.setFileUrl(avatar);
            tSafetyFiles.setCreatedate(new Date());
            tSafetyFiles.setCreaterCode(getUserId().toString());
            tSafetyFilesService.insertTSafetyFiles(tSafetyFiles);
            return AjaxResult.success(tSafetyFiles.getId());
        }
        return AjaxResult.error("上传异常，请联系管理员");
    }
}
