|
|
@@ -0,0 +1,209 @@
|
|
|
+package com.storlead.trade.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DS;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.storlead.framework.common.constant.CommonConstant;
|
|
|
+import com.storlead.framework.common.constant.DSConstants;
|
|
|
+import com.storlead.framework.common.result.Result;
|
|
|
+import com.storlead.trade.dto.SysDictCreateDTO;
|
|
|
+import com.storlead.trade.dto.SysDictDTO;
|
|
|
+import com.storlead.trade.dto.SysDictResponseDTO;
|
|
|
+import com.storlead.trade.dto.SysDictDetailResponseDTO;
|
|
|
+import com.storlead.trade.dto.SysDictUpdateDTO;
|
|
|
+import com.storlead.trade.entity.SysDictDetailEntity;
|
|
|
+import com.storlead.trade.entity.SysDictEntity;
|
|
|
+import com.storlead.trade.mapper.TradeSysDictDetailMapper;
|
|
|
+import com.storlead.trade.mapper.TradeSysDictMapper;
|
|
|
+import com.storlead.trade.service.SysDictDetailService;
|
|
|
+import com.storlead.trade.service.SysDictService;
|
|
|
+import com.storlead.trade.service.impl.support.TradeDataSourceServiceImpl;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 字典组 Service实现类
|
|
|
+ * 直接对 sys_dict 表做增删改查(逻辑删除,软删 is_delete=1;删除时级联软删明细)
|
|
|
+ *
|
|
|
+ * @author Generated
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service("tradeSysDictService")
|
|
|
+@DS(DSConstants.DATASOURCE_TRADE)
|
|
|
+public class SysDictServiceImpl
|
|
|
+ extends TradeDataSourceServiceImpl<TradeSysDictMapper, SysDictEntity>
|
|
|
+ implements SysDictService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ @Lazy
|
|
|
+ private SysDictDetailService sysDictDetailService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ @Lazy
|
|
|
+ private TradeSysDictDetailMapper tradeSysDictDetailMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<Object> getList(SysDictDTO dto) {
|
|
|
+ if (dto == null) {
|
|
|
+ dto = new SysDictDTO();
|
|
|
+ }
|
|
|
+ Page<SysDictEntity> page = new Page<>(dto.getPageIndex(), dto.getPageSize());
|
|
|
+ LambdaQueryWrapper<SysDictEntity> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ // 软删过滤
|
|
|
+ wrapper.eq(SysDictEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
|
|
|
+ // 默认按 sort 升序、再按 dictId 降序(顺序稳定)
|
|
|
+ wrapper.orderByAsc(SysDictEntity::getSort);
|
|
|
+ wrapper.orderByDesc(SysDictEntity::getDictId);
|
|
|
+
|
|
|
+ if (!ObjectUtils.isEmpty(dto.getDictId())) {
|
|
|
+ wrapper.eq(SysDictEntity::getDictId, dto.getDictId());
|
|
|
+ }
|
|
|
+ if (dto.getCode() != null && !dto.getCode().trim().isEmpty()) {
|
|
|
+ wrapper.eq(SysDictEntity::getCode, dto.getCode().trim());
|
|
|
+ }
|
|
|
+ if (dto.getName() != null && !dto.getName().trim().isEmpty()) {
|
|
|
+ wrapper.like(SysDictEntity::getName, dto.getName().trim());
|
|
|
+ }
|
|
|
+ if (dto.getDescription() != null && !dto.getDescription().trim().isEmpty()) {
|
|
|
+ wrapper.like(SysDictEntity::getDescription, dto.getDescription().trim());
|
|
|
+ }
|
|
|
+ if (dto.getEnabled() != null) {
|
|
|
+ wrapper.eq(SysDictEntity::getEnabled, dto.getEnabled());
|
|
|
+ }
|
|
|
+
|
|
|
+ IPage<SysDictEntity> iPage = this.page(page, wrapper);
|
|
|
+ // 转响应 DTO
|
|
|
+ IPage<SysDictResponseDTO> respPage = iPage.convert(this::toResponse);
|
|
|
+ return Result.result(respPage);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Result<Object> add(SysDictCreateDTO dto) {
|
|
|
+ if (dto == null) {
|
|
|
+ return Result.error("请求体不能为空");
|
|
|
+ }
|
|
|
+ SysDictEntity entity = new SysDictEntity();
|
|
|
+ entity.setCode(dto.getCode());
|
|
|
+ entity.setName(dto.getName());
|
|
|
+ entity.setDescription(dto.getDescription());
|
|
|
+ entity.setSort(dto.getSort());
|
|
|
+ boolean ok = this.save(entity);
|
|
|
+ if (!ok) {
|
|
|
+ return Result.error("新增字典组失败");
|
|
|
+ }
|
|
|
+ return Result.result(entity.getDictId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Result<Object> edit(SysDictUpdateDTO dto) {
|
|
|
+ if (dto == null || ObjectUtils.isEmpty(dto.getDictId())) {
|
|
|
+ return Result.error("dictId 不能为空");
|
|
|
+ }
|
|
|
+ SysDictEntity exist = this.getById(dto.getDictId());
|
|
|
+ if (exist == null || CommonConstant.DEL_FLAG_1.equals(exist.getIsDelete())) {
|
|
|
+ return Result.error("字典组不存在或已删除");
|
|
|
+ }
|
|
|
+ // 只更新非空字段,避免误清空
|
|
|
+ if (dto.getCode() != null) {
|
|
|
+ exist.setCode(dto.getCode());
|
|
|
+ }
|
|
|
+ if (dto.getName() != null) {
|
|
|
+ exist.setName(dto.getName());
|
|
|
+ }
|
|
|
+ if (dto.getDescription() != null) {
|
|
|
+ exist.setDescription(dto.getDescription());
|
|
|
+ }
|
|
|
+ if (dto.getSort() != null) {
|
|
|
+ exist.setSort(dto.getSort());
|
|
|
+ }
|
|
|
+ boolean ok = this.updateById(exist);
|
|
|
+ if (!ok) {
|
|
|
+ return Result.error("更新字典组失败");
|
|
|
+ }
|
|
|
+ return Result.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Result<Object> delete(List<Long> ids) {
|
|
|
+ if (CollectionUtils.isEmpty(ids)) {
|
|
|
+ return Result.error("id 列表不能为空");
|
|
|
+ }
|
|
|
+ // 软删主表
|
|
|
+ LambdaUpdateWrapper<SysDictEntity> wrapper = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper.in(SysDictEntity::getDictId, ids);
|
|
|
+ wrapper.set(SysDictEntity::getIsDelete, CommonConstant.DEL_FLAG_1);
|
|
|
+ boolean ok = this.update(wrapper);
|
|
|
+ if (!ok) {
|
|
|
+ return Result.error("删除字典组失败");
|
|
|
+ }
|
|
|
+ // 级联软删从表(sys_dict_detail 同组明细一并逻辑删除)
|
|
|
+ LambdaUpdateWrapper<SysDictDetailEntity> detailWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ detailWrapper.in(SysDictDetailEntity::getDictId, ids);
|
|
|
+ detailWrapper.set(SysDictDetailEntity::getIsDelete, CommonConstant.DEL_FLAG_1);
|
|
|
+ sysDictDetailService.update(detailWrapper);
|
|
|
+ return Result.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<SysDictDetailResponseDTO> getListByCode(String code) {
|
|
|
+ List<SysDictDetailResponseDTO> resp = new ArrayList<>();
|
|
|
+ if (code == null || code.trim().isEmpty()) {
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+ // 1. 主表按 code 定位字典组(取启用且未删除的最早一条)
|
|
|
+ LambdaQueryWrapper<SysDictEntity> dictWrapper = new LambdaQueryWrapper<>();
|
|
|
+ dictWrapper.eq(SysDictEntity::getCode, code.trim());
|
|
|
+ dictWrapper.eq(SysDictEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
|
|
|
+ dictWrapper.eq(SysDictEntity::getEnabled, true);
|
|
|
+ dictWrapper.orderByAsc(SysDictEntity::getDictId);
|
|
|
+ dictWrapper.last("LIMIT 1");
|
|
|
+ SysDictEntity dict = this.getOne(dictWrapper);
|
|
|
+ if (dict == null) {
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+ // 2. 从表查该组下全部可用明细(enabled=1 且未删除),按 sort 升序
|
|
|
+ LambdaQueryWrapper<SysDictDetailEntity> detailWrapper = new LambdaQueryWrapper<>();
|
|
|
+ detailWrapper.eq(SysDictDetailEntity::getDictId, dict.getDictId());
|
|
|
+ detailWrapper.eq(SysDictDetailEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
|
|
|
+ detailWrapper.eq(SysDictDetailEntity::getEnabled, true);
|
|
|
+ detailWrapper.orderByAsc(SysDictDetailEntity::getSort);
|
|
|
+ detailWrapper.orderByDesc(SysDictDetailEntity::getDictDetailId);
|
|
|
+ List<SysDictDetailEntity> details = sysDictDetailService.list(detailWrapper);
|
|
|
+ resp = details.stream().map(e -> {
|
|
|
+ if (e == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ SysDictDetailResponseDTO r = new SysDictDetailResponseDTO();
|
|
|
+ BeanUtils.copyProperties(e, r);
|
|
|
+ return r;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Entity → ResponseDTO
|
|
|
+ */
|
|
|
+ private SysDictResponseDTO toResponse(SysDictEntity e) {
|
|
|
+ if (e == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ SysDictResponseDTO r = new SysDictResponseDTO();
|
|
|
+ BeanUtils.copyProperties(e, r);
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+}
|