|
@@ -0,0 +1,191 @@
|
|
|
+package com.yonge.cooleshow.biz.dal.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.yonge.cooleshow.biz.dal.entity.TenantAlbumCategory;
|
|
|
+import com.yonge.cooleshow.biz.dal.entity.TenantAlbumCategoryDetail;
|
|
|
+import com.yonge.cooleshow.biz.dal.entity.TenantAlbumMusic;
|
|
|
+import com.yonge.cooleshow.biz.dal.mapper.TenantAlbumCategoryMapper;
|
|
|
+import com.yonge.cooleshow.biz.dal.mapper.TenantAlbumMusicMapper;
|
|
|
+import com.yonge.cooleshow.biz.dal.service.TenantAlbumCategoryDetailService;
|
|
|
+import com.yonge.cooleshow.biz.dal.service.TenantAlbumCategoryService;
|
|
|
+import com.yonge.cooleshow.biz.dal.wrapper.TenantAlbumCategoryWrapper;
|
|
|
+import com.yonge.cooleshow.common.enums.ETenantAlbumCategoryType;
|
|
|
+import com.yonge.toolset.base.exception.BizException;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 机构专辑分类
|
|
|
+ * 2023-10-10 09:48:46
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class TenantAlbumCategoryServiceImpl extends ServiceImpl<TenantAlbumCategoryMapper, TenantAlbumCategory> implements TenantAlbumCategoryService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TenantAlbumCategoryDetailService tenantAlbumCategoryDetailService;
|
|
|
+ @Autowired
|
|
|
+ private TenantAlbumMusicMapper tenantAlbumMusicMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询详情
|
|
|
+ *
|
|
|
+ * @param id 详情ID
|
|
|
+ * @return TenantAlbumCategory
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public TenantAlbumCategoryWrapper.TenantAlbumCategory detail(Long id) {
|
|
|
+ TenantAlbumCategory albumCategory = baseMapper.selectById(id);
|
|
|
+ if (albumCategory == null) {
|
|
|
+ throw new BizException("专辑分类不存在");
|
|
|
+ }
|
|
|
+ TenantAlbumCategoryWrapper.TenantAlbumCategory tenantAlbumCategory =
|
|
|
+ TenantAlbumCategoryWrapper.TenantAlbumCategory.from(JSON.toJSONString(albumCategory));
|
|
|
+
|
|
|
+ List<TenantAlbumCategoryDetail> list = tenantAlbumCategoryDetailService.lambdaQuery()
|
|
|
+ .eq(TenantAlbumCategoryDetail::getTenantAlbumCategoryId, id)
|
|
|
+ .list();
|
|
|
+ tenantAlbumCategory.setValues(list);
|
|
|
+ return tenantAlbumCategory;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页查询
|
|
|
+ *
|
|
|
+ * @param page IPage<TenantAlbumCategory>
|
|
|
+ * @param query TenantAlbumCategoryWrapper.TenantAlbumCategoryQuery
|
|
|
+ * @return IPage<TenantAlbumCategory>
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public IPage<TenantAlbumCategory> selectPage(IPage<TenantAlbumCategory> page,
|
|
|
+ TenantAlbumCategoryWrapper.TenantAlbumCategoryQuery query) {
|
|
|
+
|
|
|
+ return page.setRecords(baseMapper.selectPage(page, query));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加
|
|
|
+ *
|
|
|
+ * @param tenantAlbumCategory TenantAlbumCategoryWrapper.TenantAlbumCategory
|
|
|
+ * @return Boolean
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public Boolean add(TenantAlbumCategoryWrapper.TenantAlbumCategory tenantAlbumCategory) {
|
|
|
+ Integer count = this.lambdaQuery()
|
|
|
+ .eq(TenantAlbumCategory::getName, tenantAlbumCategory.getName())
|
|
|
+ .eq(TenantAlbumCategory::getCategoryType, tenantAlbumCategory.getCategoryType())
|
|
|
+ .eq(TenantAlbumCategory::getDelFlag, 0)
|
|
|
+ .count();
|
|
|
+ if (count > 0) {
|
|
|
+ throw new BizException("专辑分类名称已存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ TenantAlbumCategory albumCategory = JSON.parseObject(tenantAlbumCategory.jsonString(),
|
|
|
+ TenantAlbumCategory.class);
|
|
|
+ this.save(albumCategory);
|
|
|
+ List<TenantAlbumCategoryDetail> values = tenantAlbumCategory.getValues();
|
|
|
+ if (!values.isEmpty()) {
|
|
|
+ values.forEach(next -> next.setTenantAlbumCategoryId(albumCategory.getId()));
|
|
|
+ tenantAlbumCategoryDetailService.saveBatch(values);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新
|
|
|
+ *
|
|
|
+ * @param tenantAlbumCategory TenantAlbumCategoryWrapper.TenantAlbumCategory
|
|
|
+ * @return Boolean
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public Boolean update(TenantAlbumCategoryWrapper.TenantAlbumCategory tenantAlbumCategory) {
|
|
|
+
|
|
|
+ Integer count = this.lambdaQuery()
|
|
|
+ .eq(TenantAlbumCategory::getName, tenantAlbumCategory.getName())
|
|
|
+ .eq(TenantAlbumCategory::getCategoryType, tenantAlbumCategory.getCategoryType())
|
|
|
+ .eq(TenantAlbumCategory::getDelFlag, 0)
|
|
|
+ .ne(TenantAlbumCategory::getId, tenantAlbumCategory.getId())
|
|
|
+ .count();
|
|
|
+ if (count > 0) {
|
|
|
+ throw new BizException("专辑分类名称已存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验是否存在引用删除
|
|
|
+ List<TenantAlbumCategoryDetail> detailList = tenantAlbumCategoryDetailService.lambdaQuery()
|
|
|
+ .eq(TenantAlbumCategoryDetail::getTenantAlbumCategoryId, tenantAlbumCategory.getId())
|
|
|
+ .list();
|
|
|
+
|
|
|
+ List<TenantAlbumCategoryDetail> values = tenantAlbumCategory.getValues();
|
|
|
+ List<Long> newUpdateIds = values.stream().map(TenantAlbumCategoryDetail::getId).filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<TenantAlbumCategoryDetail> removeDetailIdList =
|
|
|
+ detailList.stream().filter(next -> !newUpdateIds.contains(next.getId())).collect(Collectors.toList());
|
|
|
+ if (!removeDetailIdList.isEmpty()) {
|
|
|
+ TenantAlbumCategory albumCategory = this.getById(tenantAlbumCategory.getId());
|
|
|
+ checkTenantAlbumCategoryDetailUsed(albumCategory.getCategoryType(), removeDetailIdList);
|
|
|
+ }
|
|
|
+
|
|
|
+ tenantAlbumCategoryDetailService.lambdaUpdate()
|
|
|
+ .eq(TenantAlbumCategoryDetail::getTenantAlbumCategoryId, tenantAlbumCategory.getId())
|
|
|
+ .remove();
|
|
|
+ this.updateById(JSON.parseObject(tenantAlbumCategory.jsonString(), TenantAlbumCategory.class));
|
|
|
+ if (!values.isEmpty()) {
|
|
|
+ values.forEach(next -> next.setTenantAlbumCategoryId(tenantAlbumCategory.getId()));
|
|
|
+ tenantAlbumCategoryDetailService.saveBatch(values);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public Boolean delete(Long id) {
|
|
|
+ TenantAlbumCategory albumCategory = this.getById(id);
|
|
|
+ if (albumCategory == null) {
|
|
|
+ throw new BizException("删除的数据不存在");
|
|
|
+ }
|
|
|
+ List<TenantAlbumCategoryDetail> list = tenantAlbumCategoryDetailService.lambdaQuery()
|
|
|
+ .eq(TenantAlbumCategoryDetail::getTenantAlbumCategoryId, albumCategory.getId())
|
|
|
+ .list();
|
|
|
+ checkTenantAlbumCategoryDetailUsed(albumCategory.getCategoryType(), list);
|
|
|
+
|
|
|
+ tenantAlbumCategoryDetailService.lambdaUpdate()
|
|
|
+ .eq(TenantAlbumCategoryDetail::getTenantAlbumCategoryId, id)
|
|
|
+ .remove();
|
|
|
+ this.removeById(id);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验专辑分类中的值是否有被应用
|
|
|
+ */
|
|
|
+ private void checkTenantAlbumCategoryDetailUsed(ETenantAlbumCategoryType categoryType,
|
|
|
+ List<TenantAlbumCategoryDetail> list) {
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<String> idStrList = list.stream().map(next -> next.getId().toString()).collect(Collectors.toList());
|
|
|
+ QueryWrapper<TenantAlbumMusic> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.lambda()
|
|
|
+ .in(ETenantAlbumCategoryType.CATEGORY_TYPE.equals(categoryType),
|
|
|
+ TenantAlbumMusic::getType, idStrList)
|
|
|
+ .in(ETenantAlbumCategoryType.CATEGORY_LEVEL.equals(categoryType),
|
|
|
+ TenantAlbumMusic::getType, idStrList);
|
|
|
+ Integer useCount = tenantAlbumMusicMapper.selectCount(queryWrapper);
|
|
|
+ // 删除的ID被应用的数量大于0
|
|
|
+ if (useCount > 0) {
|
|
|
+ throw new BizException("专辑分类存在" + (ETenantAlbumCategoryType.CATEGORY_TYPE.equals(categoryType) ? "类型" : "级别") + "引用");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|