| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- package com.jzg.service.impl;
- import cn.hutool.core.bean.BeanUtil;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.jzg.commons.constants.Constants;
- import com.jzg.commons.core.page.HttpResult;
- import com.jzg.commons.entity.dto.JyxConfigAdd;
- import com.jzg.commons.entity.dto.JyxConfigEnd;
- import com.jzg.commons.entity.dto.JyxConfigParam;
- import com.jzg.commons.entity.po.EsmInsCompany;
- import com.jzg.commons.entity.po.JyxBlame;
- import com.jzg.commons.entity.po.JyxConfig;
- import com.jzg.commons.entity.po.jyxSetting;
- import com.jzg.commons.entity.vo.JyxConfigVo;
- import com.jzg.commons.util.AssertionUtils;
- import com.jzg.commons.util.ConvertUtils;
- import com.jzg.commons.util.StringUtils;
- import com.jzg.constants.ConstantsParent;
- import com.jzg.mapper.JyxConfigMapper;
- import com.jzg.service.EsmInsCompanyService;
- import com.jzg.service.JyxBlameService;
- import com.jzg.service.JyxConfigService;
- import com.jzg.service.jyxSettingService;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.util.CollectionUtils;
- import org.springframework.util.ObjectUtils;
- import java.util.*;
- import java.util.concurrent.CompletableFuture;
- import java.util.function.Function;
- import java.util.stream.Collectors;
- import static java.util.stream.Collectors.collectingAndThen;
- import static java.util.stream.Collectors.toCollection;
- @Slf4j
- @Service
- public class JyxConfigServiceImpl extends ServiceImpl<JyxConfigMapper, JyxConfig> implements JyxConfigService {
- @Autowired
- private JyxConfigMapper jyxConfigMapper;
- @Autowired
- private jyxSettingService settingService;
- @Autowired
- private JyxBlameService blameService;
- @Autowired
- private EsmInsCompanyService esmInsCompanyService;
- @Override
- @Transactional(rollbackFor = Exception.class)
- public HttpResult saveAll(JyxConfigAdd configAdd) {
- try {
- AssertionUtils.isFail(Optional.ofNullable(baseMapper.selectOne(new LambdaQueryWrapper<JyxConfig>().eq(JyxConfig::getProductCode, configAdd.getProductCode())
- .eq(JyxConfig::getIsDelete, Constants.NOT_DELETED))).isPresent(), "产品编码重复");
- if (StringUtils.isEmpty(configAdd.getParentId())){
- configAdd.setParentId(Constants.DEFAULT_PARENT_ID);
- }else {
- AssertionUtils.isFail(Optional.ofNullable(baseMapper.selectOne(new LambdaQueryWrapper<JyxConfig>().eq(JyxConfig::getId, configAdd.getParentId())
- .eq(JyxConfig::getIsDelete, Constants.NOT_DELETED))).isEmpty(), "上级产品不存在或已删除,请重新选择");
- }
- JyxConfig jyxConfig = BeanUtil.copyProperties(configAdd, JyxConfig.class);
- int insert = jyxConfigMapper.insert(jyxConfig);
- if (insert <= 0) {
- return HttpResult.error("添加失败");
- }
- List<JyxBlame> blameList = configAdd.getBlameList();
- List<jyxSetting> settingList = configAdd.getSettingList();
- // 确保列表不为空再进行操作
- if (!CollectionUtils.isEmpty(blameList)) {
- blameList.forEach(blame -> blame.setJyxConfigId(jyxConfig.getId()));
- blameService.saveBatch(blameList);
- }
- if (!CollectionUtils.isEmpty(settingList)) {
- settingList.forEach(setting -> setting.setJyxConfigId(jyxConfig.getId()));
- settingService.saveBatch(settingList);
- }
- return HttpResult.ok("添加成功");
- }catch (Exception e){
- return HttpResult.error("添加失败" + e.getMessage());
- }
- }
- @Override
- public HttpResult selectById(String id) {
- if (id == null || id.trim().isEmpty()) {
- return HttpResult.error("ID不能为空");
- }
- try {
- JyxConfig config = jyxConfigMapper.selectById(id);
- if (config == null) {
- return HttpResult.error("查询失败");
- }
- JyxConfigVo jyxConfigVo = BeanUtil.copyProperties(config, JyxConfigVo.class);
- CompletableFuture<Void> blameFuture = CompletableFuture.runAsync(() -> {
- try {
- jyxConfigVo.setBlameList(selectBlameList(config.getId()));
- } catch (Exception e) {
- log.error("加载 blameList 失败", e);
- }
- });
- CompletableFuture<Void> settingFuture = CompletableFuture.runAsync(() -> {
- try {
- jyxConfigVo.setSettingList(selectSettingList(config.getId()));
- } catch (Exception e) {
- log.error("加载 settingList 失败", e);
- }
- });
- CompletableFuture.allOf(blameFuture, settingFuture).join();
- EsmInsCompany byId = esmInsCompanyService.getById(jyxConfigVo.getCompany());
- if(Objects.nonNull(byId)){
- jyxConfigVo.setCompanyName(byId.getNameSimple());
- }
- return HttpResult.ok(jyxConfigVo);
- } catch (Exception e) {
- log.error("查询配置失败", e);
- return HttpResult.error("系统错误");
- }
- }
- @Override
- @Transactional(rollbackFor = Exception.class)
- public HttpResult updateById(JyxConfigEnd configEnd) {
- try {
- JyxConfig jyxConfig = BeanUtil.copyProperties(configEnd, JyxConfig.class);
- if (jyxConfigMapper.updateById(jyxConfig) <= 0) {
- return HttpResult.error("修改失败");
- }
- //同步删除或者新增驾意险配置
- updateRelatedLists(configEnd, configEnd.getId());
- return HttpResult.ok("修改成功");
- } catch (Exception e) {
- log.error("更新配置失败", e);
- throw new RuntimeException("更新配置失败", e);
- }
- }
- private void updateRelatedLists(JyxConfigEnd configEnd, String jyxConfigId) {
- blameService.deleteByJyxConfigId(jyxConfigId);
- if (!CollectionUtils.isEmpty(configEnd.getBlameList())) {
- configEnd.getBlameList().forEach(blame -> blame.setJyxConfigId(jyxConfigId));
- blameService.saveBatch(configEnd.getBlameList());
- }
- settingService.deleteByJyxConfigId(jyxConfigId);
- if (!CollectionUtils.isEmpty(configEnd.getSettingList())) {
- configEnd.getSettingList().forEach(setting -> setting.setJyxConfigId(jyxConfigId));
- settingService.saveBatch(configEnd.getSettingList());
- }
- }
- @Override
- public List<JyxConfigVo> jyxList(JyxConfigParam configParam) {
- try {
- List<JyxConfigVo> jyxConfigVos = Optional.ofNullable(jyxConfigMapper.jyxList(configParam)).orElse(Collections.emptyList());
- if (StringUtils.isNotEmpty(configParam.getSeats()) || StringUtils.isNotEmpty(configParam.getParams())) {
- Set<String> ids = jyxConfigVos.stream().filter(v -> !ConstantsParent.PARENT_ID_ROOT.equals(v.getParentId())).map(JyxConfigVo::getParentId).collect(Collectors.toSet());
- List<JyxConfigVo> parentConfigs = jyxConfigMapper.selectParentIds(new ArrayList<>(ids));
- jyxConfigVos.addAll(parentConfigs);
- jyxConfigVos = jyxConfigVos.stream().collect(collectingAndThen(
- toCollection(() -> new TreeSet<>(Comparator.comparing(JyxConfigVo::getId))), ArrayList::new
- ));
- // configParam.setParentId(ConstantsParent.PARENT_ID_ROOT);
- // List<JyxConfigVo> additionalConfigs = Optional.ofNullable(jyxConfigMapper.jyxList(configParam)).orElse(Collections.emptyList());
- // if (!additionalConfigs.isEmpty()) {
- // Set<String> ids = additionalConfigs.stream().map(JyxConfigVo::getId).collect(Collectors.toSet());
- // List<JyxConfigVo> parentConfigs = jyxConfigMapper.selectParentIds(new ArrayList<>(ids));
- // jyxConfigVos.addAll(parentConfigs);
- // jyxConfigVos = jyxConfigVos.stream().collect(collectingAndThen(
- // toCollection(() -> new TreeSet<>(Comparator.comparing(JyxConfigVo::getId))), ArrayList::new
- // ));
- // }
- }
- if (jyxConfigVos.isEmpty()) {
- return Collections.emptyList();
- }
- // Set<String> parentIds = jyxConfigVos.stream().map(JyxConfigVo::getParentId).collect(Collectors.toSet());
- // List<JyxConfigVo> jyxConfigs = Optional.ofNullable(jyxConfigMapper.selectIds(new ArrayList<>(parentIds))).orElse(Collections.emptyList());
- Map<String, List<JyxConfigVo>> childrenMap = jyxConfigVos.stream()
- .filter(vo -> !ConstantsParent.PARENT_ID_ROOT.equals(vo.getParentId()))
- .collect(Collectors.groupingBy(vo -> String.valueOf(vo.getParentId()), Collectors.mapping(Function.identity(), Collectors.toList())));
- List<JyxConfigVo> configVos = jyxConfigVos.stream().filter(parentConfig -> ConstantsParent.PARENT_ID_ROOT.equals(parentConfig.getParentId())).collect(Collectors.toList());
- configVos.forEach(parentConfig -> {
- parentConfig.setChildren(childrenMap.getOrDefault(String.valueOf(parentConfig.getId()), Collections.emptyList()));
- });
- return configVos;
- } catch (Exception e) {
- // 记录日志或进行其他异常处理
- e.printStackTrace();
- return Collections.emptyList();
- }
- }
- @Override
- @Transactional(rollbackFor = Exception.class)
- public HttpResult deleteById(String id) {
- try {
- JyxConfig config = jyxConfigMapper.selectById(id);
- if (config == null) {
- return HttpResult.error("该驾意险配置不存在");
- }
- int deletedRows = jyxConfigMapper.deleteById(config);
- if (deletedRows <= 0) {
- return HttpResult.error("删除失败");
- }
- if (ConstantsParent.PARENT_ID_ROOT.equals(config.getParentId())) {
- LambdaQueryWrapper<JyxConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
- lambdaQueryWrapper.eq(JyxConfig::getParentId, config.getId());
- // 批量删除子配置
- List<String> childIds = jyxConfigMapper.selectList(lambdaQueryWrapper).stream()
- .map(JyxConfig::getId)
- .collect(Collectors.toList());
- if (!childIds.isEmpty()) {
- jyxConfigMapper.deleteBatchIds(childIds);
- blameService.lambdaUpdate().in(JyxBlame::getJyxConfigId, childIds).remove();
- settingService.lambdaUpdate().in(jyxSetting::getJyxConfigId, childIds).remove();
- }
- } else {
- blameService.lambdaUpdate().eq(JyxBlame::getJyxConfigId, config.getId()).remove();
- settingService.lambdaUpdate().eq(jyxSetting::getJyxConfigId, config.getId()).remove();
- }
- return HttpResult.ok("删除成功");
- } catch (Exception e) {
- // 记录异常信息到日志
- return HttpResult.error("删除失败: " + e.getMessage());
- }
- }
- @Override
- public List<JyxConfigVo> selectMainInsurance(String company) {
- List<JyxConfigVo> jyxConfigVos = jyxConfigMapper.selectMainInsurance(company);
- return jyxConfigVos;
- }
- public List<JyxBlame> selectBlameList(String id){
- return blameService.lambdaQuery().eq(JyxBlame::getJyxConfigId, id).list();
- }
- public List<jyxSetting> selectSettingList(String id){
- return settingService.lambdaQuery().eq(jyxSetting::getJyxConfigId, id).list();
- }
- }
|