JyxConfigServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package com.jzg.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.jzg.commons.constants.Constants;
  6. import com.jzg.commons.core.page.HttpResult;
  7. import com.jzg.commons.entity.dto.JyxConfigAdd;
  8. import com.jzg.commons.entity.dto.JyxConfigEnd;
  9. import com.jzg.commons.entity.dto.JyxConfigParam;
  10. import com.jzg.commons.entity.po.EsmInsCompany;
  11. import com.jzg.commons.entity.po.JyxBlame;
  12. import com.jzg.commons.entity.po.JyxConfig;
  13. import com.jzg.commons.entity.po.jyxSetting;
  14. import com.jzg.commons.entity.vo.JyxConfigVo;
  15. import com.jzg.commons.util.AssertionUtils;
  16. import com.jzg.commons.util.ConvertUtils;
  17. import com.jzg.commons.util.StringUtils;
  18. import com.jzg.constants.ConstantsParent;
  19. import com.jzg.mapper.JyxConfigMapper;
  20. import com.jzg.service.EsmInsCompanyService;
  21. import com.jzg.service.JyxBlameService;
  22. import com.jzg.service.JyxConfigService;
  23. import com.jzg.service.jyxSettingService;
  24. import lombok.extern.slf4j.Slf4j;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Service;
  27. import org.springframework.transaction.annotation.Transactional;
  28. import org.springframework.util.CollectionUtils;
  29. import org.springframework.util.ObjectUtils;
  30. import java.util.*;
  31. import java.util.concurrent.CompletableFuture;
  32. import java.util.function.Function;
  33. import java.util.stream.Collectors;
  34. import static java.util.stream.Collectors.collectingAndThen;
  35. import static java.util.stream.Collectors.toCollection;
  36. @Slf4j
  37. @Service
  38. public class JyxConfigServiceImpl extends ServiceImpl<JyxConfigMapper, JyxConfig> implements JyxConfigService {
  39. @Autowired
  40. private JyxConfigMapper jyxConfigMapper;
  41. @Autowired
  42. private jyxSettingService settingService;
  43. @Autowired
  44. private JyxBlameService blameService;
  45. @Autowired
  46. private EsmInsCompanyService esmInsCompanyService;
  47. @Override
  48. @Transactional(rollbackFor = Exception.class)
  49. public HttpResult saveAll(JyxConfigAdd configAdd) {
  50. try {
  51. AssertionUtils.isFail(Optional.ofNullable(baseMapper.selectOne(new LambdaQueryWrapper<JyxConfig>().eq(JyxConfig::getProductCode, configAdd.getProductCode())
  52. .eq(JyxConfig::getIsDelete, Constants.NOT_DELETED))).isPresent(), "产品编码重复");
  53. if (StringUtils.isEmpty(configAdd.getParentId())){
  54. configAdd.setParentId(Constants.DEFAULT_PARENT_ID);
  55. }else {
  56. AssertionUtils.isFail(Optional.ofNullable(baseMapper.selectOne(new LambdaQueryWrapper<JyxConfig>().eq(JyxConfig::getId, configAdd.getParentId())
  57. .eq(JyxConfig::getIsDelete, Constants.NOT_DELETED))).isEmpty(), "上级产品不存在或已删除,请重新选择");
  58. }
  59. JyxConfig jyxConfig = BeanUtil.copyProperties(configAdd, JyxConfig.class);
  60. int insert = jyxConfigMapper.insert(jyxConfig);
  61. if (insert <= 0) {
  62. return HttpResult.error("添加失败");
  63. }
  64. List<JyxBlame> blameList = configAdd.getBlameList();
  65. List<jyxSetting> settingList = configAdd.getSettingList();
  66. // 确保列表不为空再进行操作
  67. if (!CollectionUtils.isEmpty(blameList)) {
  68. blameList.forEach(blame -> blame.setJyxConfigId(jyxConfig.getId()));
  69. blameService.saveBatch(blameList);
  70. }
  71. if (!CollectionUtils.isEmpty(settingList)) {
  72. settingList.forEach(setting -> setting.setJyxConfigId(jyxConfig.getId()));
  73. settingService.saveBatch(settingList);
  74. }
  75. return HttpResult.ok("添加成功");
  76. }catch (Exception e){
  77. return HttpResult.error("添加失败" + e.getMessage());
  78. }
  79. }
  80. @Override
  81. public HttpResult selectById(String id) {
  82. if (id == null || id.trim().isEmpty()) {
  83. return HttpResult.error("ID不能为空");
  84. }
  85. try {
  86. JyxConfig config = jyxConfigMapper.selectById(id);
  87. if (config == null) {
  88. return HttpResult.error("查询失败");
  89. }
  90. JyxConfigVo jyxConfigVo = BeanUtil.copyProperties(config, JyxConfigVo.class);
  91. CompletableFuture<Void> blameFuture = CompletableFuture.runAsync(() -> {
  92. try {
  93. jyxConfigVo.setBlameList(selectBlameList(config.getId()));
  94. } catch (Exception e) {
  95. log.error("加载 blameList 失败", e);
  96. }
  97. });
  98. CompletableFuture<Void> settingFuture = CompletableFuture.runAsync(() -> {
  99. try {
  100. jyxConfigVo.setSettingList(selectSettingList(config.getId()));
  101. } catch (Exception e) {
  102. log.error("加载 settingList 失败", e);
  103. }
  104. });
  105. CompletableFuture.allOf(blameFuture, settingFuture).join();
  106. EsmInsCompany byId = esmInsCompanyService.getById(jyxConfigVo.getCompany());
  107. if(Objects.nonNull(byId)){
  108. jyxConfigVo.setCompanyName(byId.getNameSimple());
  109. }
  110. return HttpResult.ok(jyxConfigVo);
  111. } catch (Exception e) {
  112. log.error("查询配置失败", e);
  113. return HttpResult.error("系统错误");
  114. }
  115. }
  116. @Override
  117. @Transactional(rollbackFor = Exception.class)
  118. public HttpResult updateById(JyxConfigEnd configEnd) {
  119. try {
  120. JyxConfig jyxConfig = BeanUtil.copyProperties(configEnd, JyxConfig.class);
  121. if (jyxConfigMapper.updateById(jyxConfig) <= 0) {
  122. return HttpResult.error("修改失败");
  123. }
  124. //同步删除或者新增驾意险配置
  125. updateRelatedLists(configEnd, configEnd.getId());
  126. return HttpResult.ok("修改成功");
  127. } catch (Exception e) {
  128. log.error("更新配置失败", e);
  129. throw new RuntimeException("更新配置失败", e);
  130. }
  131. }
  132. private void updateRelatedLists(JyxConfigEnd configEnd, String jyxConfigId) {
  133. blameService.deleteByJyxConfigId(jyxConfigId);
  134. if (!CollectionUtils.isEmpty(configEnd.getBlameList())) {
  135. configEnd.getBlameList().forEach(blame -> blame.setJyxConfigId(jyxConfigId));
  136. blameService.saveBatch(configEnd.getBlameList());
  137. }
  138. settingService.deleteByJyxConfigId(jyxConfigId);
  139. if (!CollectionUtils.isEmpty(configEnd.getSettingList())) {
  140. configEnd.getSettingList().forEach(setting -> setting.setJyxConfigId(jyxConfigId));
  141. settingService.saveBatch(configEnd.getSettingList());
  142. }
  143. }
  144. @Override
  145. public List<JyxConfigVo> jyxList(JyxConfigParam configParam) {
  146. try {
  147. List<JyxConfigVo> jyxConfigVos = Optional.ofNullable(jyxConfigMapper.jyxList(configParam)).orElse(Collections.emptyList());
  148. if (StringUtils.isNotEmpty(configParam.getSeats()) || StringUtils.isNotEmpty(configParam.getParams())) {
  149. Set<String> ids = jyxConfigVos.stream().filter(v -> !ConstantsParent.PARENT_ID_ROOT.equals(v.getParentId())).map(JyxConfigVo::getParentId).collect(Collectors.toSet());
  150. List<JyxConfigVo> parentConfigs = jyxConfigMapper.selectParentIds(new ArrayList<>(ids));
  151. jyxConfigVos.addAll(parentConfigs);
  152. jyxConfigVos = jyxConfigVos.stream().collect(collectingAndThen(
  153. toCollection(() -> new TreeSet<>(Comparator.comparing(JyxConfigVo::getId))), ArrayList::new
  154. ));
  155. // configParam.setParentId(ConstantsParent.PARENT_ID_ROOT);
  156. // List<JyxConfigVo> additionalConfigs = Optional.ofNullable(jyxConfigMapper.jyxList(configParam)).orElse(Collections.emptyList());
  157. // if (!additionalConfigs.isEmpty()) {
  158. // Set<String> ids = additionalConfigs.stream().map(JyxConfigVo::getId).collect(Collectors.toSet());
  159. // List<JyxConfigVo> parentConfigs = jyxConfigMapper.selectParentIds(new ArrayList<>(ids));
  160. // jyxConfigVos.addAll(parentConfigs);
  161. // jyxConfigVos = jyxConfigVos.stream().collect(collectingAndThen(
  162. // toCollection(() -> new TreeSet<>(Comparator.comparing(JyxConfigVo::getId))), ArrayList::new
  163. // ));
  164. // }
  165. }
  166. if (jyxConfigVos.isEmpty()) {
  167. return Collections.emptyList();
  168. }
  169. // Set<String> parentIds = jyxConfigVos.stream().map(JyxConfigVo::getParentId).collect(Collectors.toSet());
  170. // List<JyxConfigVo> jyxConfigs = Optional.ofNullable(jyxConfigMapper.selectIds(new ArrayList<>(parentIds))).orElse(Collections.emptyList());
  171. Map<String, List<JyxConfigVo>> childrenMap = jyxConfigVos.stream()
  172. .filter(vo -> !ConstantsParent.PARENT_ID_ROOT.equals(vo.getParentId()))
  173. .collect(Collectors.groupingBy(vo -> String.valueOf(vo.getParentId()), Collectors.mapping(Function.identity(), Collectors.toList())));
  174. List<JyxConfigVo> configVos = jyxConfigVos.stream().filter(parentConfig -> ConstantsParent.PARENT_ID_ROOT.equals(parentConfig.getParentId())).collect(Collectors.toList());
  175. configVos.forEach(parentConfig -> {
  176. parentConfig.setChildren(childrenMap.getOrDefault(String.valueOf(parentConfig.getId()), Collections.emptyList()));
  177. });
  178. return configVos;
  179. } catch (Exception e) {
  180. // 记录日志或进行其他异常处理
  181. e.printStackTrace();
  182. return Collections.emptyList();
  183. }
  184. }
  185. @Override
  186. @Transactional(rollbackFor = Exception.class)
  187. public HttpResult deleteById(String id) {
  188. try {
  189. JyxConfig config = jyxConfigMapper.selectById(id);
  190. if (config == null) {
  191. return HttpResult.error("该驾意险配置不存在");
  192. }
  193. int deletedRows = jyxConfigMapper.deleteById(config);
  194. if (deletedRows <= 0) {
  195. return HttpResult.error("删除失败");
  196. }
  197. if (ConstantsParent.PARENT_ID_ROOT.equals(config.getParentId())) {
  198. LambdaQueryWrapper<JyxConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  199. lambdaQueryWrapper.eq(JyxConfig::getParentId, config.getId());
  200. // 批量删除子配置
  201. List<String> childIds = jyxConfigMapper.selectList(lambdaQueryWrapper).stream()
  202. .map(JyxConfig::getId)
  203. .collect(Collectors.toList());
  204. if (!childIds.isEmpty()) {
  205. jyxConfigMapper.deleteBatchIds(childIds);
  206. blameService.lambdaUpdate().in(JyxBlame::getJyxConfigId, childIds).remove();
  207. settingService.lambdaUpdate().in(jyxSetting::getJyxConfigId, childIds).remove();
  208. }
  209. } else {
  210. blameService.lambdaUpdate().eq(JyxBlame::getJyxConfigId, config.getId()).remove();
  211. settingService.lambdaUpdate().eq(jyxSetting::getJyxConfigId, config.getId()).remove();
  212. }
  213. return HttpResult.ok("删除成功");
  214. } catch (Exception e) {
  215. // 记录异常信息到日志
  216. return HttpResult.error("删除失败: " + e.getMessage());
  217. }
  218. }
  219. @Override
  220. public List<JyxConfigVo> selectMainInsurance(String company) {
  221. List<JyxConfigVo> jyxConfigVos = jyxConfigMapper.selectMainInsurance(company);
  222. return jyxConfigVos;
  223. }
  224. public List<JyxBlame> selectBlameList(String id){
  225. return blameService.lambdaQuery().eq(JyxBlame::getJyxConfigId, id).list();
  226. }
  227. public List<jyxSetting> selectSettingList(String id){
  228. return settingService.lambdaQuery().eq(jyxSetting::getJyxConfigId, id).list();
  229. }
  230. }