package com.ruoyi.web.controller.rc;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.service.ISysDeptService;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.rc.domain.TCommonfile;
import com.ruoyi.rc.service.ITCommonfileService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 通用附件Controller
 * 
 * @author ruoyi
 * @date 2024-07-29
 */
@RestController
@RequestMapping("/rc/commonfile")
public class TCommonfileController extends BaseController
{
    @Autowired
    private ITCommonfileService tCommonfileService;

    /**
     * 通用附件上传
     */
    @Log(title = "通用附件上传", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, String pType, String pId) throws IOException
    {
        if (!file.isEmpty())
        {
            if ("deptinfo-plane".equals(pType) || "deptinfo-welcome".equals(pType) || "deptinfo-video".equals(pType)) {
                // 查询文件列表
                TCommonfile commonfile = new TCommonfile();
                commonfile.setpType(pType);
                commonfile.setpId(Long.parseLong(pId));
                List<TCommonfile> tCommonfiles = tCommonfileService.selectTCommonfileList(commonfile);
                // 遍历判断是否重名
                boolean isNameRepeat = false;
                for (TCommonfile tCommonfile : tCommonfiles) {
                    if (file.getOriginalFilename().equals(tCommonfile.getFileName())) {
                        isNameRepeat = true;
                    }
                }
                // 重名不上传
                if (isNameRepeat) {
                    return AjaxResult.error("上传失败，存在重名文件");
                }
            }
            String url = FileUploadUtils.upload(RuoYiConfig.getFilePath("/"+ pType), file);
            long size = file.getSize()/1024;
            TCommonfile tCommonfile = new TCommonfile();
            tCommonfile.setFileUrl(url);
            tCommonfile.setFileName(file.getOriginalFilename());
            tCommonfile.setCreaterCode(String.valueOf(getUserId()));
            tCommonfile.setpId(Long.parseLong(pId));
            tCommonfile.setpType(pType);
            tCommonfile.setFileSize(String.valueOf(size));
            tCommonfileService.insertTCommonfile(tCommonfile);

            return AjaxResult.success();
        }
        return AjaxResult.error("上传失败，请联系管理员");
    }

    /**
     * 查询通用附件列表
     */
    @GetMapping("/allFileList")
    public List<TCommonfile> allFileList(TCommonfile tCommonfile)
    {
        List<TCommonfile> list = tCommonfileService.selectAllFileList(tCommonfile);
        return list;
    }

    /**
     * 查询通用附件列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TCommonfile tCommonfile)
    {
        startPage();
        List<TCommonfile> list = tCommonfileService.selectTCommonfileList(tCommonfile);
        return getDataTable(list);
    }

    /**
     * 导出通用附件列表
     */
    @Log(title = "通用附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCommonfile tCommonfile)
    {
        List<TCommonfile> list = tCommonfileService.selectTCommonfileList(tCommonfile);
        ExcelUtil<TCommonfile> util = new ExcelUtil<TCommonfile>(TCommonfile.class);
        util.exportExcel(response, list, "通用附件数据");
    }

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

    /**
     * 新增通用附件
     */
    @Log(title = "通用附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCommonfile tCommonfile)
    {
        return toAjax(tCommonfileService.insertTCommonfile(tCommonfile));
    }

    /**
     * 修改通用附件
     */
    @Log(title = "通用附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCommonfile tCommonfile)
    {
        return toAjax(tCommonfileService.updateTCommonfile(tCommonfile));
    }

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