package com.ruoyi.project.common.controller; import com.alibaba.fastjson.JSON; import com.ruoyi.common.thread.TaskThread; import com.ruoyi.common.utils.file.FileUploadUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.aspectj.lang.annotation.Log; import com.ruoyi.framework.aspectj.lang.enums.BusinessType; import com.ruoyi.framework.config.RuoYiConfig; import com.ruoyi.framework.web.controller.BaseController; import com.ruoyi.framework.web.domain.AjaxResult; import com.ruoyi.framework.web.page.TableDataInfo; import com.ruoyi.project.common.domain.TCommonfile; import com.ruoyi.project.common.service.ITCommonfileService; import com.ruoyi.project.monitor.domain.SysJob; import com.ruoyi.project.monitor.service.ISysJobService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.time.LocalDate; import java.util.List; /** * 通用附件Controller * * @author ruoyi * @date 2020-12-11 */ @RestController @RequestMapping("/common/commonfile") public class TCommonfileController extends BaseController { @Autowired private ITCommonfileService tCommonfileService; @Autowired private ISysJobService jobService; /** * 通用附件上传 */ @Log(title = "通用附件上传", businessType = BusinessType.UPDATE) @PostMapping("/uploadFile") public AjaxResult uploadFile(@RequestParam("file") MultipartFile file,String pType,String pId,@RequestParam(value = "pValue",required = false)String pValue) throws IOException { if (!file.isEmpty()) { // 获取当前日期 LocalDate currentDate = LocalDate.now(); // 获取年份 int year = currentDate.getYear(); // 获取月份 int month = currentDate.getMonthValue(); String url = FileUploadUtils.upload(RuoYiConfig.getFilePath("/"+ pType + "/" + year), 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.setpValue(pValue); tCommonfile.setFileSize(String.valueOf(size)); tCommonfileService.insertTCommonfile(tCommonfile); return AjaxResult.success(); } return AjaxResult.error("上传失败,请联系管理员"); } /** * 查询通用附件列表 */ @GetMapping("/list") public TableDataInfo list(TCommonfile tCommonfile) { startPage(); List list = tCommonfileService.selectTCommonfileList(tCommonfile); return getDataTable(list); } /** * 查询通用附件列表(pType模糊查询) */ @GetMapping("/allFileListFuzzy") public List allFileListFuzzy(TCommonfile tCommonfile) { List list = tCommonfileService.selectAllFileListFuzzy(tCommonfile); return list; } /** * 查询通用附件列表 */ @GetMapping("/allFileList") public List allFileList(TCommonfile tCommonfile) { List list = tCommonfileService.selectAllFileList(tCommonfile); return list; } /** * 导出通用附件列表 */ @Log(title = "通用附件", businessType = BusinessType.EXPORT) @GetMapping("/export") public AjaxResult export(TCommonfile tCommonfile) { List list = tCommonfileService.selectTCommonfileList(tCommonfile); ExcelUtil util = new ExcelUtil(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)); SysJob job = new SysJob(); job.setJobId(10040l); job.setJobGroup("SYSTEM"); TaskThread thread = new TaskThread(job,jobService); Thread t = new Thread(thread); t.start(); return res; } /** * 删除通用附件 */ @Log(title = "通用附件", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public AjaxResult remove(@PathVariable Long[] ids) { return toAjax(tCommonfileService.deleteTCommonfileByIds(ids)); } }