MaterialServiceImpl.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.concurrent.CompletableFuture;
  26. import java.util.stream.Collectors;
  27. @Service
  28. public class MaterialServiceImpl implements MaterialService {
  29. @Autowired
  30. private ObjectKeyUtil objectKeyUtil;
  31. @Autowired
  32. private MaterialDao materialDao;
  33. @Autowired
  34. private MaterialTagDao materialTagDao;
  35. @Autowired
  36. private MaterialCategoryDao materialCategoryDao;
  37. @Autowired
  38. private SysFileService sysFileService;
  39. private List<MaterialTag> getMaterialTagByIds(String tag_ids) {
  40. if (StrUtil.isEmpty(tag_ids)) return null;
  41. LambdaQueryWrapper<MaterialTag> wrapper = new LambdaQueryWrapper<>();
  42. wrapper.in(MaterialTag::getId, tag_ids.split(","));
  43. wrapper.orderByDesc(MaterialTag::getSort);
  44. return materialTagDao.selectList(wrapper);
  45. }
  46. /**
  47. * 获取素材列表
  48. */
  49. @Override
  50. public PageEntity selectMaterialList(Material material) {
  51. PageUtils.startPage(); // 分页
  52. List<Map<String, Object>> list = materialDao.selectMaterialList(material);
  53. // 1) 完成分页实体渲染
  54. PageEntity pageEntity = new PageInfoResult(list).toEntity();
  55. if (!list.isEmpty()) {
  56. // 2) 分页列表格式化
  57. list = list.stream().map(item -> {
  58. // 新增字段:标签列表
  59. String tag_ids = Convert.toStr(item.get("tag_ids"));
  60. List<MaterialTag> materialTagList = getMaterialTagByIds(tag_ids);
  61. item.put("tag_list", materialTagList);
  62. return item;
  63. }).collect(Collectors.toList());
  64. // 3) 分页实体重新赋值
  65. List<Object> objectList = list.stream().map(item -> (Object) item).collect(Collectors.toList());
  66. pageEntity.setList(objectList);
  67. }
  68. return pageEntity;
  69. }
  70. /**
  71. * 获取素材详情
  72. */
  73. @Override
  74. public Material selectMaterialDetail(Material material) {
  75. Material detail = materialDao.selectMaterialDetail(material);
  76. if (detail == null) throw new CustException("素材不存在");
  77. // 新增字段:标签列表
  78. List<MaterialTag> materialTagList = getMaterialTagByIds(detail.getTag_ids());
  79. detail.setTag_list(materialTagList);
  80. return detail;
  81. }
  82. /**
  83. * 创建素材
  84. */
  85. @Override
  86. @Transactional(rollbackFor = Exception.class)
  87. public Map<String, Object> insertMaterial(Material material) {
  88. MaterialCategory materialCategory = materialCategoryDao.selectById(material.getCategory_id());
  89. if (materialCategory == null) throw new CustException("素材分类不存在");
  90. ObjectKeyEntity objectKeyEntity = objectKeyUtil.urlToObjectKey(material.getImage_url());
  91. // 新需求:高清图也要返回压缩地址
  92. if (StrUtil.isNotEmpty(material.getImage_url())) {
  93. String image_url = UploadUtil.getImageThumbUrl(material.getImage_url(), objectKeyEntity.getTarget(), 315, null, null, true);
  94. material.setImage_url(image_url);
  95. }
  96. // 生成缩略图,并填充缩略图地址
  97. if (StrUtil.isNotEmpty(material.getImage_url())) {
  98. String image_thumb_url = UploadUtil.getImageThumbUrl(material.getImage_url(), objectKeyEntity.getTarget(), 315, 180);
  99. material.setImage_thumb_url(image_thumb_url);
  100. }
  101. materialDao.insertMaterial(material);
  102. return Map.of("material_id", material.getMaterial_id());
  103. }
  104. /**
  105. * 编辑素材
  106. */
  107. @Override
  108. @Transactional(rollbackFor = Exception.class)
  109. public Map<String, Object> updateMaterial(Material material) {
  110. Material detail = materialDao.selectById(material.getMaterial_id());
  111. if (detail == null) throw new CustException("素材不存在");
  112. if (material.getCategory_id() != null) {
  113. MaterialCategory materialCategory = materialCategoryDao.selectById(material.getCategory_id());
  114. if (materialCategory == null) throw new CustException("素材分类不存在");
  115. }
  116. // 编辑的时候,如果素材图片有修改,需要删除之前的图片
  117. // 生成缩略图,并填充缩略图地址
  118. if (StrUtil.isNotEmpty(material.getImage_url())) {
  119. ObjectKeyEntity objectKeyEntity = objectKeyUtil.urlToObjectKey(material.getImage_url());
  120. String image_thumb_url = UploadUtil.getImageThumbUrl(material.getImage_url(), objectKeyEntity.getTarget(), 315, 180);
  121. material.setImage_thumb_url(image_thumb_url);
  122. }
  123. materialDao.updateMaterial(material);
  124. return Map.of("material_id", material.getMaterial_id());
  125. };
  126. /**
  127. * 删除素材
  128. */
  129. @Override
  130. @Transactional(rollbackFor = Exception.class)
  131. public Map<String, Object> deleteMaterial(Material material) {
  132. Material detail = materialDao.selectById(material.getMaterial_id());
  133. if (detail == null) throw new CustException("素材不存在");
  134. // 删除的时候,同时删除对象存储中的素材图片 (异步)
  135. ObjectKeyEntity image_url_object = objectKeyUtil.urlToObjectKey(detail.getImage_url());
  136. ObjectKeyEntity fla_url_object = objectKeyUtil.urlToObjectKey(detail.getFla_url());
  137. ObjectKeyEntity psd_url_object = objectKeyUtil.urlToObjectKey(detail.getPsd_url());
  138. CompletableFuture.runAsync(() -> {
  139. sysFileService.deleteObject(image_url_object.getObject_key(), image_url_object.getTarget());
  140. sysFileService.deleteObject(fla_url_object.getObject_key(), fla_url_object.getTarget());
  141. sysFileService.deleteObject(psd_url_object.getObject_key(), psd_url_object.getTarget());
  142. });
  143. materialDao.deleteMaterial(material);
  144. return Map.of("material_id", material.getMaterial_id());
  145. }
  146. }