Browse Source

fix:添加标签删除判断

liujunchi 2 years ago
parent
commit
b193cf5c51

+ 17 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/dao/MusicTagDao.java

@@ -49,4 +49,21 @@ public interface MusicTagDao extends BaseMapper<MusicTag> {
 	 * @return 子集集合
 	 */
 	List<MusicTagVo> getChildrenByParentIdList(@Param("longList") List<Long> longList, @Param("state") YesOrNoEnum state);
+
+	/**
+	 * 检查标签是否被用过
+	 *
+	 * @param longs
+	 * @return
+	 */
+    Integer checkTagBeUsedMusicSheet(@Param("longs") List<Long> longs);
+
+
+	/**
+	 * 检查标签是否被用过
+	 *
+	 * @param longs
+	 * @return
+	 */
+    Integer checkTagBeUsedMusicAblum(@Param("longs") List<Long> longs);
 }

+ 29 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/impl/MusicTagServiceImpl.java

@@ -10,6 +10,7 @@ import com.yonge.cooleshow.biz.dal.dto.MusicTagSaveDto;
 import com.yonge.cooleshow.biz.dal.dto.search.MusicTagSearch;
 import com.yonge.cooleshow.biz.dal.entity.MusicTag;
 import com.yonge.cooleshow.biz.dal.enums.YesOrNoEnum;
+import com.yonge.cooleshow.biz.dal.service.MusicSheetService;
 import com.yonge.cooleshow.biz.dal.service.MusicTagService;
 import com.yonge.cooleshow.biz.dal.vo.MusicTagVo;
 import com.yonge.cooleshow.common.exception.BizException;
@@ -34,6 +35,8 @@ public class MusicTagServiceImpl extends ServiceImpl<MusicTagDao, MusicTag> impl
     @Autowired
     private SysUserFeignService sysUserFeignService;
 
+    private MusicSheetService musicSheetService;
+
 	@Override
     public MusicTagVo detail(Long id) {
         return baseMapper.detail(id);
@@ -78,6 +81,12 @@ public class MusicTagServiceImpl extends ServiceImpl<MusicTagDao, MusicTag> impl
     @Override
     @Transactional(rollbackFor = Exception.class)
     public boolean del(Long musicTagId) {
+
+        // 有关联的曲目或专辑不让删除
+        if (checkTagBeUserd(musicTagId)) {
+            throw  new BizException("当前标签或子集标签已被使用,不可删除");
+        }
+
         // 删除下面的子集标签
         return this.lambdaUpdate()
                    .eq(MusicTag::getParentTagId,musicTagId)
@@ -87,6 +96,26 @@ public class MusicTagServiceImpl extends ServiceImpl<MusicTagDao, MusicTag> impl
 
     }
 
+    /**
+     * 检查标签是否被使用过
+     *
+     * @param musicTagId 标签id
+     * @return
+     */
+    private boolean checkTagBeUserd(Long musicTagId) {
+
+        List<MusicTag> list = this.lambdaQuery()
+                                  .eq(MusicTag::getParentTagId, musicTagId)
+                                  .eq(MusicTag::getDelFlag, false)
+                                  .list();
+        List<Long> longs = list.stream().map(MusicTag::getId).collect(Collectors.toList());
+        if (baseMapper.checkTagBeUsedMusicAblum(longs) >0) return true;
+        if (baseMapper.checkTagBeUsedMusicSheet(longs) >0) return true;
+
+        return false;
+
+    }
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public boolean state(Long musicTagId) {

+ 27 - 0
cooleshow-user/user-biz/src/main/resources/config/mybatis/MusicTagMapper.xml

@@ -96,4 +96,31 @@
             </if>
         </where>
     </select>
+
+    <select id="checkTagBeUsedMusicSheet" resultType="java.lang.Integer">
+
+        select count(1) from music_sheet ms
+        <where>
+            ms.del_flag_ = 0
+            <if test="longs != null and longs.size() != 0">
+                and
+                <foreach collection="longs" item="item" open="" close="" >
+                    find_in_set(ms.music_tag_)
+                </foreach>
+            </if>
+        </where>
+    </select>
+
+    <select id="checkTagBeUsedMusicAblum" resultType="java.lang.Integer">
+
+        select count(1) from music_album ms
+        <where>
+            <if test="longs != null and longs.size() != 0">
+                and
+                <foreach collection="longs" item="item" open="" close="" >
+                    find_in_set(ms.album_tag_)
+                </foreach>
+            </if>
+        </where>
+    </select>
 </mapper>