package com.ruoyi.project.ehs.controller;

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

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.project.common.domain.TCommonfile;
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.ehs.domain.TRcfile;
import com.ruoyi.project.ehs.service.ITRcfileService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * RC附件Controller
 *
 * @author ssy
 * @date 2023-10-24
 */
@RestController
@RequestMapping("/ehs/rcfile")
public class TRcfileController extends BaseController
{
    @Autowired
    private ITRcfileService tRcfileService;

    /**
     * 查询RC附件列表
     */
    @GetMapping("/list")
    public AjaxResult list(TRcfile tRcfile)
    {
        List<TRcfile> list = tRcfileService.selectTRcfileList(tRcfile);
        return AjaxResult.success(list);
    }

    /**
     * 导出RC附件列表
     */
    @Log(title = "RC附件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TRcfile tRcfile)
    {
        List<TRcfile> list = tRcfileService.selectTRcfileList(tRcfile);
        ExcelUtil<TRcfile> util = new ExcelUtil<TRcfile>(TRcfile.class);
        return util.exportExcel(list, "rcfile");
    }

    /**
     * 获取RC附件详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tRcfileService.selectTRcfileById(id));
    }

    /**
     * 新增RC附件
     */
    @Log(title = "RC附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TRcfile tRcfile)
    {
        return toAjax(tRcfileService.insertTRcfile(tRcfile));
    }

    /**
     * 通用附件上传
     */
    @Log(title = "通用附件上传", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, String pType, String pId,Long parentId) throws IOException
    {
        if (!file.isEmpty())
        {
            String url = FileUploadUtils.upload(RuoYiConfig.getFilePath("/"+ pType), file);
            long size = file.getSize()/1024;
            TRcfile tRcfile = new TRcfile();
            tRcfile.setFileUrl(url);
            tRcfile.setFileName(file.getOriginalFilename());
            tRcfile.setCreaterCode(String.valueOf(getUserId()));
            tRcfile.setpId(Long.parseLong(pId));
            tRcfile.setpType(pType);
            tRcfile.setFileSize(String.valueOf(size));
            tRcfile.setParentId(parentId);
            tRcfile.setFileType(1l);
            tRcfileService.insertTRcfile(tRcfile);

            return AjaxResult.success();
        }
        return AjaxResult.error("上传失败，请联系管理员");
    }
    /**
     * 修改RC附件
     */
    @Log(title = "RC附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TRcfile tRcfile)
    {
        return toAjax(tRcfileService.updateTRcfile(tRcfile));
    }

    /**
     * 删除RC附件
     */
    @Log(title = "RC附件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tRcfileService.deleteTRcfileByIds(ids));
    }
}
