package com.ruoyi.web.controller.branch;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.security.access.prepost.PreAuthorize;
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.branch.domain.TFile;
import com.ruoyi.branch.service.ITFileService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 附件Controller
 *
 * @author ruoyi
 * @date 2023-07-13
 */
@RestController
@RequestMapping("/branch/file")
public class TFileController extends BaseController {
    @Autowired
    private ITFileService tFileService;

    /**
     * 查询附件列表
     */
    @GetMapping("/list")
    public AjaxResult list(TFile tFile) {
        return AjaxResult.success(tFileService.selectTFileList(tFile));
    }

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

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

    /**
     * 新增附件
     */

    @Log(title = "附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TFile tFile) {
        return toAjax(tFileService.insertTFile(tFile));
    }

    /**
     * 修改附件
     */
    @Log(title = "附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TFile tFile) {
        return toAjax(tFileService.updateTFile(tFile));
    }

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


    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file,String tableName,Long tableId) throws IOException {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getFilePath(tableName), file);
            TFile tFile = new TFile();
            tFile.setName(file.getOriginalFilename());
            tFile.setUrl(avatar);
            tFile.setTableId(tableId);
            tFile.setTableName(tableName);
            tFile.setCreatedate(new Date());
            tFile.setCreaterCode(getUserId().toString());
            tFileService.insertTFile(tFile);
            return AjaxResult.success(tFile.getId());
        }
        return AjaxResult.error("上传异常，请联系管理员");
    }
}
