|
@@ -1,11 +1,142 @@
|
|
|
package com.sqx.modules.lovers.service.impl;
|
|
package com.sqx.modules.lovers.service.impl;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.sqx.modules.lovers.dao.LoversSetContentDao;
|
|
import com.sqx.modules.lovers.dao.LoversSetContentDao;
|
|
|
|
|
+import com.sqx.modules.lovers.dto.LoversSetContentDTO;
|
|
|
|
|
+import com.sqx.modules.lovers.dto.LoversSetContentDetailDTO;
|
|
|
import com.sqx.modules.lovers.entity.LoversSetContent;
|
|
import com.sqx.modules.lovers.entity.LoversSetContent;
|
|
|
|
|
+import com.sqx.modules.lovers.service.LoversSetContentDetailService;
|
|
|
import com.sqx.modules.lovers.service.LoversSetContentService;
|
|
import com.sqx.modules.lovers.service.LoversSetContentService;
|
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@Service
|
|
@Service
|
|
|
|
|
+@RequiredArgsConstructor
|
|
|
public class LoversSetContentServiceImpl extends ServiceImpl<LoversSetContentDao, LoversSetContent> implements LoversSetContentService {
|
|
public class LoversSetContentServiceImpl extends ServiceImpl<LoversSetContentDao, LoversSetContent> implements LoversSetContentService {
|
|
|
|
|
+
|
|
|
|
|
+ private final LoversSetContentDetailService loversSetContentDetailService;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public void add(List<LoversSetContentDTO> loversSetContentDTOS) {
|
|
|
|
|
+ List<LoversSetContent> loversSetContents = loversSetContentDTOS.stream().map(e -> {
|
|
|
|
|
+ LoversSetContent loversSetContent = new LoversSetContent();
|
|
|
|
|
+ BeanUtil.copyProperties(e, loversSetContent);
|
|
|
|
|
+ loversSetContent.setDelFlag("0");
|
|
|
|
|
+ return loversSetContent;
|
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
|
+
|
|
|
|
|
+ this.saveBatch(loversSetContents);
|
|
|
|
|
+
|
|
|
|
|
+ // 保存套餐内容详情
|
|
|
|
|
+ List<LoversSetContentDetailDTO> allDetails = getLoversSetContentDetailDTOS(loversSetContentDTOS, loversSetContents);
|
|
|
|
|
+ loversSetContentDetailService.add(allDetails);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public void update(List<LoversSetContentDTO> loversSetContentDTOS) {
|
|
|
|
|
+ Long loversSetId = loversSetContentDTOS.get(0).getLoversSetId();
|
|
|
|
|
+ LambdaQueryWrapper<LoversSetContent> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ queryWrapper.eq(LoversSetContent::getLoversSetId, loversSetId);
|
|
|
|
|
+ List<LoversSetContent> oldList = this.list(queryWrapper);
|
|
|
|
|
+
|
|
|
|
|
+ // 提取DTO中的ID列表
|
|
|
|
|
+ List<Long> dtoIds = loversSetContentDTOS.stream()
|
|
|
|
|
+ .filter(dto -> ObjectUtil.isNotNull(dto.getId()))
|
|
|
|
|
+ .map(LoversSetContentDTO::getId)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ // 待删除数据id列表
|
|
|
|
|
+ List<Long> deleteIds = oldList.stream()
|
|
|
|
|
+ .filter(old -> !dtoIds.contains(old.getId()))
|
|
|
|
|
+ .map(LoversSetContent::getId)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+
|
|
|
|
|
+ // 区分更新和新增数据
|
|
|
|
|
+ List<LoversSetContent> updateList = new ArrayList<>();
|
|
|
|
|
+ List<LoversSetContent> addList = new ArrayList<>();
|
|
|
|
|
+ List<LoversSetContentDTO> updateDTOList = new ArrayList<>();
|
|
|
|
|
+ List<LoversSetContentDTO> addDTOList = new ArrayList<>();
|
|
|
|
|
+ List<Long> existingIds = oldList.stream().map(LoversSetContent::getId).collect(Collectors.toList());
|
|
|
|
|
+ for (LoversSetContentDTO dto : loversSetContentDTOS) {
|
|
|
|
|
+ LoversSetContent entity = new LoversSetContent();
|
|
|
|
|
+ BeanUtil.copyProperties(dto, entity);
|
|
|
|
|
+
|
|
|
|
|
+ if (ObjectUtil.isNotNull(dto.getId()) && existingIds.contains(dto.getId())) {
|
|
|
|
|
+ // 更新操作
|
|
|
|
|
+ updateList.add(entity);
|
|
|
|
|
+ updateDTOList.add(dto);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 新增操作
|
|
|
|
|
+ entity.setId(null); // 清除可能存在的无效ID
|
|
|
|
|
+ entity.setDelFlag("0");
|
|
|
|
|
+ addList.add(entity);
|
|
|
|
|
+ addDTOList.add(dto);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 删除数据
|
|
|
|
|
+ if (CollUtil.isNotEmpty(deleteIds)) {
|
|
|
|
|
+ deleteByIds(deleteIds);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 更新数据
|
|
|
|
|
+ if (!updateList.isEmpty()) {
|
|
|
|
|
+ this.updateBatchById(updateList);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 新增数据
|
|
|
|
|
+ if (!addList.isEmpty()) {
|
|
|
|
|
+ this.saveBatch(addList);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 合并更新和新增的内容详情
|
|
|
|
|
+ List<LoversSetContentDetailDTO> unionDetails = CollUtil.union(
|
|
|
|
|
+ getLoversSetContentDetailDTOS(updateDTOList, updateList),
|
|
|
|
|
+ getLoversSetContentDetailDTOS(addDTOList, addList)
|
|
|
|
|
+ )
|
|
|
|
|
+ .stream()
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ loversSetContentDetailService.update(unionDetails);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public void deleteByIds(List<Long> ids) {
|
|
|
|
|
+ removeByIds(ids);
|
|
|
|
|
+ // 删除内容详情
|
|
|
|
|
+ loversSetContentDetailService.deleteByContentIds(ids);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public void deleteByLoversSetIds(List<Long> loversSetIds) {
|
|
|
|
|
+ LambdaQueryWrapper<LoversSetContent> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ queryWrapper.in(LoversSetContent::getLoversSetId, loversSetIds);
|
|
|
|
|
+ List<LoversSetContent> list = list(queryWrapper);
|
|
|
|
|
+ List<Long> ids = list.stream().map(LoversSetContent::getId).collect(Collectors.toList());
|
|
|
|
|
+
|
|
|
|
|
+ deleteByIds(ids);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<LoversSetContentDetailDTO> getLoversSetContentDetailDTOS(List<LoversSetContentDTO> loversSetContentDTOS, List<LoversSetContent> loversSetContents) {
|
|
|
|
|
+ List<LoversSetContentDetailDTO> allDetails = new ArrayList<>();
|
|
|
|
|
+ for (int i = 0; i < loversSetContentDTOS.size(); i++) {
|
|
|
|
|
+ LoversSetContentDTO dto = loversSetContentDTOS.get(i);
|
|
|
|
|
+ LoversSetContent entity = loversSetContents.get(i);
|
|
|
|
|
+ List<LoversSetContentDetailDTO> details = dto.getLoversSetContentDetailDTOS();
|
|
|
|
|
+ details.forEach(detail -> detail.setLoversSetContentId(entity.getId()));
|
|
|
|
|
+ allDetails.addAll(details);
|
|
|
|
|
+ }
|
|
|
|
|
+ return allDetails;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|