package com.ims.eval.controller; import com.baomidou.mybatisplus.core.metadata.IPage; import com.ims.eval.config.CustomException; import com.ims.eval.entity.DataDictionary; import com.ims.eval.entity.EvaluationNotice; import com.ims.eval.entity.custom.Path; import com.ims.eval.entity.dto.result.R; import com.ims.eval.service.IDataDictionaryService; import com.ims.eval.service.IEvaluationNoticeService; import com.ims.eval.util.FileUploadUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.List; /** * 通告管理 * * @author hlf * @date 2023/5/17 14:48 * 文件说明: */ @Slf4j @RestController @RequestMapping("//evaluation-dept-notice") public class NoticeManagementController { @Autowired private IEvaluationNoticeService evaluationNoticeService; @Autowired private IDataDictionaryService dataDictionaryService; /** * 通告管理列表信息(分页) * * @param pageNum 当前记录起始索引 * @param pageSize 每页显示记录数 * @param noticeTitle 通告标题 * @return 结果 */ @GetMapping(value = "/list") public R list( @RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize, @RequestParam(value = "noticeTitle", required = false) String noticeTitle) { IPage list = evaluationNoticeService.listPage(pageNum, pageSize, noticeTitle); List evaluationNoticeList = list.getRecords(); for (EvaluationNotice obj : evaluationNoticeList) { StringBuilder names = new StringBuilder(); if (!"".equals(obj.getSendToContent()) && null != obj.getSendToContent()) { String[] strings = obj.getSendToContent().split(","); for (String str : strings) { DataDictionary dataDictionary = dataDictionaryService.getById(str); names.append(dataDictionary.getKeyName()).append(","); } obj.setDeptNames(names.toString().substring(0, names.toString().length() - 1)); } } return R.ok().data(list); } /** * 通告管理信息 * * @return 结果 */ @GetMapping(value = "/listAll") public R listAll() { List list = evaluationNoticeService.list(); return R.ok().data(list); } /** * 新增/修改通告管理信息 * * @param evaluationNotice 通告管理实体 * @return 结果 */ @PostMapping(value = "/save") public R addAll(EvaluationNotice evaluationNotice, @RequestParam(value = "files", required = false) MultipartFile[] files) throws IOException { try { boolean b = evaluationNoticeService.saveOrUpdate(evaluationNotice, files); if (b) { return R.ok().data(b); } else { return R.error().data("保存失败!"); } } catch (CustomException e) { return R.customError(e.getMessage()).data("失败!"); } } /** * 批量删除通告管理信息 * * @param ids * @return 结果 */ @PostMapping(value = "/removeAll/{ids}") public R deleteAll(@PathVariable("ids") String ids) { String[] strings = ids.split(","); boolean b = evaluationNoticeService.removeByIds(Arrays.asList(strings)); if (b) { return R.ok().data(b); } else { return R.error().data("删除失败!"); } } /** * 修改状态 * * @param id 主键 * @param releaseState 状态(未发布或已发布) * @return 结果 */ @PostMapping(value = "/modifiedState") public R modifiedState(@RequestParam(value = "id") String id, @RequestParam(value = "releaseState") String releaseState) { EvaluationNotice evaluationNotice = new EvaluationNotice(); evaluationNotice.setId(id); evaluationNotice.setReleaseState(releaseState); boolean b = evaluationNoticeService.updateById(evaluationNotice); if (b) { return R.ok().data(b); } else { return R.error().data("失败!"); } } /** * 上传附件 * * @param id 主键 * @param files 附件集 * @return 结果 * @throws IOException 异常 */ @PostMapping(value = "/uploadAttachment") public R uploadAttachment(@RequestParam(value = "id") String id, @RequestParam(value = "files", required = false) MultipartFile[] files) throws IOException { try { boolean b = false; EvaluationNotice evaluationNotice = evaluationNoticeService.getById(id); StringBuilder fileNames = new StringBuilder(); if (files.length > 0) { for (MultipartFile file : files) { // 上传文件路径 String filePath = Path.getNoticePath(); // 上传并返回新文件名称 String fileName = null; fileName = FileUploadUtil.upload(filePath, file); fileNames.append(fileName).append(","); } String noticeAnnex = evaluationNotice.getNoticeAnnex() + "," + fileNames.toString().substring(0, fileNames.toString().length() - 1); evaluationNotice.setNoticeAnnex(noticeAnnex); b = evaluationNoticeService.updateById(evaluationNotice); } if (b) { return R.ok().data(b); } else { return R.error().data("上传附件失败!"); } } catch (CustomException e) { return R.customError(e.getMessage()).data("失败!"); } } /** * 删除附件 * * @param id 主键 * @param noticeAnnex 附件名称 * @return 结果 */ @PostMapping(value = "/deleteAttachment") public R deleteAttachment(@RequestParam(value = "id") String id, @RequestParam(value = "noticeAnnex") String noticeAnnex) { try { boolean b; EvaluationNotice evaluationNotice = evaluationNoticeService.getById(id); String[] strings = evaluationNotice.getNoticeAnnex().split(","); StringBuilder fileNames = new StringBuilder(); for (String str : strings) { if (str.substring(str.lastIndexOf("/") + 1, str.length()).equals(noticeAnnex)) { String filePath = Path.getStatementPath() + str; File file = new File(filePath); b = file.delete(); } else { fileNames.append(str).append(","); } } evaluationNotice.setNoticeAnnex(fileNames.toString().substring(0, fileNames.toString().length() - 1)); b = evaluationNoticeService.updateById(evaluationNotice); if (b) { return R.ok().data(b); } else { return R.error().data("删除附件失败!"); } } catch (CustomException e) { return R.customError(e.getMessage()).data("失败!"); } } /** * 通告展示 * * @return 结果 */ @GetMapping(value = "/deptId") public R circularDisplay(@RequestParam(value = "id") String deptId) { List evaluationNoticeList = evaluationNoticeService.list(); for (EvaluationNotice evaluationNotice : evaluationNoticeList) { if ("All".equals(evaluationNotice.getSendTo())){ } } return R.ok().data(evaluationNoticeList); } }