MaterialServiceImpl.java 8.7 KB

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