MaterialServiceImpl.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package com.backendsys.modules.material.service.impl;
  2. import cn.hutool.core.convert.Convert;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.backendsys.exception.CustException;
  5. import com.backendsys.modules.material.dao.MaterialCategoryDao;
  6. import com.backendsys.modules.material.dao.MaterialDao;
  7. import com.backendsys.modules.material.dao.MaterialTagDao;
  8. import com.backendsys.modules.material.entity.Material;
  9. import com.backendsys.modules.material.entity.MaterialCategory;
  10. import com.backendsys.modules.material.entity.MaterialTag;
  11. import com.backendsys.modules.material.service.MaterialService;
  12. import com.backendsys.modules.upload.service.SysFileService;
  13. import com.backendsys.modules.upload.utils.ObjectKey.ObjectKeyEntity;
  14. import com.backendsys.modules.upload.utils.ObjectKey.ObjectKeyUtil;
  15. import com.backendsys.modules.upload.utils.UploadUtil;
  16. import com.backendsys.utils.response.PageEntity;
  17. import com.backendsys.utils.response.PageInfoResult;
  18. import com.backendsys.utils.v2.PageUtils;
  19. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.transaction.annotation.Transactional;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Optional;
  26. import java.util.concurrent.CompletableFuture;
  27. import java.util.stream.Collectors;
  28. @Service
  29. public class MaterialServiceImpl implements MaterialService {
  30. @Autowired
  31. private ObjectKeyUtil objectKeyUtil;
  32. @Autowired
  33. private MaterialDao materialDao;
  34. @Autowired
  35. private MaterialTagDao materialTagDao;
  36. @Autowired
  37. private MaterialCategoryDao materialCategoryDao;
  38. @Autowired
  39. private SysFileService sysFileService;
  40. private List<MaterialTag> getMaterialTagByIds(String tag_ids) {
  41. if (StrUtil.isEmpty(tag_ids)) return null;
  42. LambdaQueryWrapper<MaterialTag> wrapper = new LambdaQueryWrapper<>();
  43. wrapper.in(MaterialTag::getId, tag_ids.split(","));
  44. wrapper.orderByDesc(MaterialTag::getSort);
  45. return materialTagDao.selectList(wrapper);
  46. }
  47. /**
  48. * 获取素材列表
  49. */
  50. @Override
  51. public PageEntity selectMaterialList(Material material) {
  52. PageUtils.startPage(); // 分页
  53. List<Map<String, Object>> list = materialDao.selectMaterialList(material);
  54. // 1) 完成分页实体渲染
  55. PageEntity pageEntity = new PageInfoResult(list).toEntity();
  56. if (!list.isEmpty()) {
  57. // 2) 分页列表格式化
  58. list = list.stream().map(item -> {
  59. // 新增字段:标签列表
  60. String tag_ids = Convert.toStr(item.get("tag_ids"));
  61. List<MaterialTag> materialTagList = getMaterialTagByIds(tag_ids);
  62. item.put("tag_list", materialTagList);
  63. return item;
  64. }).collect(Collectors.toList());
  65. // 3) 分页实体重新赋值
  66. List<Object> objectList = list.stream().map(item -> (Object) item).collect(Collectors.toList());
  67. pageEntity.setList(objectList);
  68. }
  69. return pageEntity;
  70. }
  71. /**
  72. * 获取素材详情
  73. */
  74. @Override
  75. public Material selectMaterialDetail(Material material) {
  76. Material detail = materialDao.selectMaterialDetail(material);
  77. if (detail == null) throw new CustException("素材不存在");
  78. // 新增字段:标签列表
  79. List<MaterialTag> materialTagList = getMaterialTagByIds(detail.getTag_ids());
  80. detail.setTag_list(materialTagList);
  81. return detail;
  82. }
  83. /**
  84. * 创建素材
  85. */
  86. @Override
  87. @Transactional(rollbackFor = Exception.class)
  88. public Map<String, Object> insertMaterial(Material material) {
  89. MaterialCategory materialCategory = materialCategoryDao.selectById(material.getCategory_id());
  90. if (materialCategory == null) throw new CustException("素材分类不存在");
  91. // [Filter] tag_id 过滤掉空值和 0
  92. String cleaned = Optional.ofNullable(material.getTag_ids())
  93. .orElse("")
  94. .replaceAll("\\b0\\b,?", "")
  95. .replaceAll(",{2,}", ",")
  96. .replaceAll("^,|,$", "");
  97. material.setTag_ids(cleaned);
  98. // 生成缩略图,并填充缩略图地址
  99. if (StrUtil.isNotEmpty(material.getImage_url())) {
  100. ObjectKeyEntity objectKeyEntity = objectKeyUtil.urlToObjectKey(material.getImage_url());
  101. if (objectKeyEntity != null) {
  102. String image_thumb_url = UploadUtil.getImageThumbUrl(material.getImage_url(), objectKeyEntity.getTarget(), 276, 155);
  103. material.setImage_thumb_url(image_thumb_url);
  104. }
  105. }
  106. materialDao.insertMaterial(material);
  107. return Map.of("material_id", material.getMaterial_id());
  108. }
  109. /**
  110. * 编辑素材
  111. */
  112. @Override
  113. @Transactional(rollbackFor = Exception.class)
  114. public Map<String, Object> updateMaterial(Material material) {
  115. Material detail = materialDao.selectById(material.getMaterial_id());
  116. if (detail == null) throw new CustException("素材不存在");
  117. if (material.getCategory_id() != null) {
  118. MaterialCategory materialCategory = materialCategoryDao.selectById(material.getCategory_id());
  119. if (materialCategory == null) throw new CustException("素材分类不存在");
  120. }
  121. // [Filter] tag_id 过滤掉空值和 0
  122. String cleaned = Optional.ofNullable(material.getTag_ids())
  123. .orElse("")
  124. .replaceAll("\\b0\\b,?", "")
  125. .replaceAll(",{2,}", ",")
  126. .replaceAll("^,|,$", "");
  127. material.setTag_ids(cleaned);
  128. // 编辑的时候,如果素材图片有修改,需要删除之前的图片
  129. // 生成缩略图,并填充缩略图地址
  130. if (StrUtil.isNotEmpty(material.getImage_url())) {
  131. ObjectKeyEntity objectKeyEntity = objectKeyUtil.urlToObjectKey(material.getImage_url());
  132. if (objectKeyEntity != null) {
  133. String image_thumb_url = UploadUtil.getImageThumbUrl(material.getImage_url(), objectKeyEntity.getTarget(), 276, 155);
  134. material.setImage_thumb_url(image_thumb_url);
  135. }
  136. }
  137. materialDao.updateMaterial(material);
  138. return Map.of("material_id", material.getMaterial_id());
  139. };
  140. /**
  141. * 删除素材
  142. */
  143. @Override
  144. @Transactional(rollbackFor = Exception.class)
  145. public Map<String, Object> deleteMaterial(Material material) {
  146. Material detail = materialDao.selectById(material.getMaterial_id());
  147. if (detail == null) throw new CustException("素材不存在");
  148. // 删除的时候,同时删除对象存储中的素材图片 (异步)
  149. ObjectKeyEntity image_url_object = objectKeyUtil.urlToObjectKey(detail.getImage_url());
  150. ObjectKeyEntity file_url_object = objectKeyUtil.urlToObjectKey(detail.getFile_url());
  151. CompletableFuture.runAsync(() -> {
  152. sysFileService.deleteObject(image_url_object.getObject_key(), image_url_object.getTarget());
  153. sysFileService.deleteObject(file_url_object.getObject_key(), file_url_object.getTarget());
  154. });
  155. materialDao.deleteMaterial(material);
  156. return Map.of("material_id", material.getMaterial_id());
  157. }
  158. }