import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import org.apache.commons.collections4.CollectionUtils;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import java.util.Collection;import java.util.List;import java.util.Map;import java.util.Set;import java.util.function.Function;import java.util.stream.Collectors;/** * <p> * 系统邮件配置明细表 服务实现类 * </p>@Servicepublic class SysEmailTemplateDetailServiceImpl extends ServiceImpl<SysEmailTemplateDetailMapper, SysEmailTemplateDetail> implements SysEmailTemplateDetailService { @Override public List<SysEmailTemplateDetail> selectByTemplateCode(Integer templateCode) { return lambdaQuery().eq(SysEmailTemplateDetail::getTemplateCode, templateCode) .eq(SysEmailTemplateDetail::getDeleteFlag, DeleteFlagEnum.NO_DEL.getType()) .list(); } @Override public List<SysEmailTemplateDetail> listAll() { return lambdaQuery().eq(SysEmailTemplateDetail::getDeleteFlag, DeleteFlagEnum.NO_DEL.getType()) .orderByAsc(SysEmailTemplateDetail::getId) .list(); } @Override public void deleteDetailByIds(Collection<Long> ids) { lambdaUpdate().set(SysEmailTemplateDetail::getDeleteFlag, DeleteFlagEnum.DEL.getType()) .in(SysEmailTemplateDetail::getId, ids) .update(); } @Override public void deleteDetailByTemplateCode(Integer templateCode) { lambdaUpdate().set(SysEmailTemplateDetail::getDeleteFlag, DeleteFlagEnum.DEL.getType()) .eq(SysEmailTemplateDetail::getTemplateCode, templateCode) .eq(SysEmailTemplateDetail::getDeleteFlag, DeleteFlagEnum.NO_DEL.getType()) .update(); } @Override @Transactional(rollbackFor = Exception.class) public void saveDetail(Integer templateCode, List<SysEmailTemplateAddDTO.Detail> detailList) { // 根据模板code查询明细 List<SysEmailTemplateDetail> dbDetailList = selectByTemplateCode(templateCode); if (CollectionUtils.isEmpty(dbDetailList)) { List<SysEmailTemplateDetail> sysEmailTemplateDetail = toSysEmailTemplateDetail(templateCode, detailList); saveBatch(sysEmailTemplateDetail); return; } // 比较数据 // 需要删除的id集合 Set<Long> needDeleteIdList = getNeedDeleteIdList(dbDetailList, detailList); // 需要新增或更新的数据集合 List<SysEmailTemplateDetail> needSaveList = getNeedSaveList(templateCode, dbDetailList, detailList); if (CollectionUtils.isNotEmpty(needDeleteIdList)) { deleteDetailByIds(needDeleteIdList); } if (CollectionUtils.isNotEmpty(needSaveList)) { saveOrUpdateBatch(needSaveList); } } /** * 获取需要删除的id集合 * * @param dbDetailList 数据库数据 * @param detailList 前端数据 * @return 删除的id集合 */ private Set<Long> getNeedDeleteIdList(List<SysEmailTemplateDetail> dbDetailList, List<SysEmailTemplateAddDTO.Detail> detailList) { Collection<Integer> webTypes = detailList.stream().map(SysEmailTemplateAddDTO.Detail::getAcceptorType).collect(Collectors.toList()); return dbDetailList.stream().filter(dbDetail -> webTypes.contains(dbDetail.getTemplateCode())).map(SysEmailTemplateDetail::getId).collect(Collectors.toSet()); } /** * 获取需要保存的数据集合 * * @param templateCode 模板code * @param dbDetailList 数据库数据 * @param detailList 前端数据 * @return 保存的数据集合 */ private List<SysEmailTemplateDetail> getNeedSaveList(Integer templateCode, List<SysEmailTemplateDetail> dbDetailList, List<SysEmailTemplateAddDTO.Detail> detailList) { Map<Integer, SysEmailTemplateDetail> map = dbDetailList.stream().collect(Collectors.toMap(SysEmailTemplateDetail::getAcceptorType, Function.identity())); return detailList.stream().map(webDetail -> { SysEmailTemplateDetail dbDetail = map.get(webDetail.getAcceptorType()); if (dbDetail == null) { dbDetail = new SysEmailTemplateDetail(); } toSysEmailTemplateDetail(templateCode, dbDetail, webDetail); return dbDetail; }).collect(Collectors.toList()); } /** * 转换为数据库数据 * * @param templateCode 模板code * @param detailList 数据集合 * @return 数据集合 */ private List<SysEmailTemplateDetail> toSysEmailTemplateDetail(Integer templateCode, List<SysEmailTemplateAddDTO.Detail> detailList) { return detailList.stream().map(detail -> { SysEmailTemplateDetail dbDetail = new SysEmailTemplateDetail(); toSysEmailTemplateDetail(templateCode, dbDetail, detail); return dbDetail; }).collect(Collectors.toList()); } /** * 转换为数据库数据 * * @param templateCode 模板code * @param dbDetail 数据库数据 * @param webDetail 前端数据 */ private void toSysEmailTemplateDetail(Integer templateCode, SysEmailTemplateDetail dbDetail, SysEmailTemplateAddDTO.Detail webDetail) { dbDetail.setTemplateCode(templateCode); dbDetail.setAcceptorType(webDetail.getAcceptorType()); dbDetail.setDeleteFlag(DeleteFlagEnum.NO_DEL.getType()); }} 一对多增删查改
2025-09-25
本文作者: linden
原文链接: 一对多增删查改
版权声明: 本站所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
免责声明: 文中如涉及第三方资源,均来自互联网,仅供学习研究,禁止商业使用,如有侵权,联系我们24小时内删除!
- « 上一篇一对多关系增删查改
- 下一篇 »cookie 工具类
评论0
暂时没有评论