package com.ruoyi.project.common.controller;

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

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
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.common.domain.TCommonfile;
import com.ruoyi.project.common.service.ITCommonfileService;
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 2020-12-11
 */
@RestController
@RequestMapping("/common/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())
        {
                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("/list")
    public TableDataInfo list(TCommonfile tCommonfile)
    {
        startPage();
        List<TCommonfile> list = tCommonfileService.selectTCommonfileList(tCommonfile);
        return getDataTable(list);
    }

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

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

    /**
     * 获取通用附件详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.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)
    {
        logger.info(JSON.toJSONString(tCommonfile));
        AjaxResult res = toAjax(tCommonfileService.updateTCommonfile(tCommonfile));

        return res;
    }

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