trade:优化运费模版的实现

This commit is contained in:
YunaiV 2023-10-02 18:30:14 +08:00
parent 6f4c04d944
commit fdb10aeced
10 changed files with 70 additions and 184 deletions

View File

@ -11,7 +11,10 @@ import java.util.List;
* 快递运费模板运费设置 Base VO提供给添加运费模板使用
*/
@Data
public class ExpressTemplateChargeBaseVO {
public class DeliveryExpressTemplateChargeBaseVO {
@Schema(description = "编号", example = "6592", hidden = true) // 由于想简单一点复用这个 VO 在更新操作所以 hidden false
private Long id;
@Schema(description = "区域编号列表", requiredMode = Schema.RequiredMode.REQUIRED, example = "[1,120000]")
@NotEmpty(message = "区域编号列表不能为空")

View File

@ -6,7 +6,6 @@ import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
@Schema(description = "管理后台 - 快递运费模板创建 Request VO")
@ -17,10 +16,10 @@ public class DeliveryExpressTemplateCreateReqVO extends DeliveryExpressTemplateB
@Schema(description = "区域运费列表")
@Valid
private List<ExpressTemplateChargeBaseVO> templateCharge;
private List<DeliveryExpressTemplateChargeBaseVO> charges;
@Schema(description = "包邮区域列表")
@Valid
private List<ExpressTemplateFreeBaseVO> templateFree;
private List<DeliveryExpressTemplateFreeBaseVO> frees;
}

View File

@ -17,9 +17,9 @@ public class DeliveryExpressTemplateDetailRespVO extends DeliveryExpressTemplate
private Long id;
@Schema(description = "运费模板运费设置", requiredMode = Schema.RequiredMode.REQUIRED)
private List<ExpressTemplateChargeBaseVO> templateCharge;
private List<DeliveryExpressTemplateChargeBaseVO> charges;
@Schema(description = "运费模板包邮区域", requiredMode = Schema.RequiredMode.REQUIRED)
private List<ExpressTemplateFreeBaseVO> templateFree;
private List<DeliveryExpressTemplateFreeBaseVO> frees;
}

View File

@ -11,7 +11,7 @@ import java.util.List;
* 快递运费模板包邮 Base VO提供给添加运费模板使用
*/
@Data
public class ExpressTemplateFreeBaseVO {
public class DeliveryExpressTemplateFreeBaseVO {
@Schema(description = "区域编号列表", requiredMode = Schema.RequiredMode.REQUIRED, example = "[1,120000]")
@NotEmpty(message = "区域编号列表不能为空")

View File

@ -21,36 +21,10 @@ public class DeliveryExpressTemplateUpdateReqVO extends DeliveryExpressTemplateB
@Schema(description = "区域运费列表")
@Valid
private List<ExpressTemplateChargeUpdateVO> templateCharge;
private List<DeliveryExpressTemplateChargeBaseVO> charges;
@Schema(description = "包邮区域列表")
@Valid
private List<ExpressTemplateFreeUpdateVO> templateFree;
private List<DeliveryExpressTemplateFreeBaseVO> frees;
@Schema(description = "管理后台 - 快递运费模板区域运费更新 Request VO")
@Data
public static class ExpressTemplateChargeUpdateVO extends ExpressTemplateChargeBaseVO {
@Schema(description = "编号", example = "6592")
private Long id;
// TODO @jason这几个字段应该不通过前端传递而是后端查询后去赋值的
@Schema(description = "配送模板编号", example = "1")
private Long templateId;
@Schema(description = "配送计费方式", example = "1")
private Integer chargeMode;
}
@Schema(description = "管理后台 - 快递运费模板包邮区域更新 Request VO")
@Data
public static class ExpressTemplateFreeUpdateVO extends ExpressTemplateFreeBaseVO {
@Schema(description = "编号", example = "6592")
private Long id;
@Schema(description = "配送模板编号", example = "1")
private Long templateId;
}
}

View File

@ -42,36 +42,32 @@ public interface DeliveryExpressTemplateConvert {
List<DeliveryExpressTemplateChargeDO> chargeList,
List<DeliveryExpressTemplateFreeDO> freeList) {
DeliveryExpressTemplateDetailRespVO respVO = convert2(bean);
respVO.setTemplateCharge(convertTemplateChargeList(chargeList));
respVO.setTemplateFree(convertTemplateFreeList(freeList));
respVO.setCharges(convertTemplateChargeList(chargeList));
respVO.setFrees(convertTemplateFreeList(freeList));
return respVO;
}
// ========== Template Charge ==========
DeliveryExpressTemplateChargeDO convertTemplateCharge(Long templateId, Integer chargeMode, ExpressTemplateChargeBaseVO vo);
DeliveryExpressTemplateChargeDO convertTemplateCharge(DeliveryExpressTemplateUpdateReqVO.ExpressTemplateChargeUpdateVO vo);
DeliveryExpressTemplateChargeDO convertTemplateCharge(Long templateId, Integer chargeMode, DeliveryExpressTemplateChargeBaseVO vo);
DeliveryExpressTemplateRespBO.Charge convertTemplateCharge(DeliveryExpressTemplateChargeDO bean);
default List<DeliveryExpressTemplateChargeDO> convertTemplateChargeList(Long templateId, Integer chargeMode, List<ExpressTemplateChargeBaseVO> list) {
default List<DeliveryExpressTemplateChargeDO> convertTemplateChargeList(Long templateId, Integer chargeMode, List<DeliveryExpressTemplateChargeBaseVO> list) {
return CollectionUtils.convertList(list, vo -> convertTemplateCharge(templateId, chargeMode, vo));
}
// ========== Template Free ==========
DeliveryExpressTemplateFreeDO convertTemplateFree(Long templateId, ExpressTemplateFreeBaseVO vo);
DeliveryExpressTemplateFreeDO convertTemplateFree(DeliveryExpressTemplateUpdateReqVO.ExpressTemplateFreeUpdateVO vo);
DeliveryExpressTemplateFreeDO convertTemplateFree(Long templateId, DeliveryExpressTemplateFreeBaseVO vo);
DeliveryExpressTemplateRespBO.Free convertTemplateFree(DeliveryExpressTemplateFreeDO bean);
List<ExpressTemplateChargeBaseVO> convertTemplateChargeList(List<DeliveryExpressTemplateChargeDO> list);
List<DeliveryExpressTemplateChargeBaseVO> convertTemplateChargeList(List<DeliveryExpressTemplateChargeDO> list);
List<ExpressTemplateFreeBaseVO> convertTemplateFreeList(List<DeliveryExpressTemplateFreeDO> list);
List<DeliveryExpressTemplateFreeBaseVO> convertTemplateFreeList(List<DeliveryExpressTemplateFreeDO> list);
default List<DeliveryExpressTemplateFreeDO> convertTemplateFreeList(Long templateId, List<ExpressTemplateFreeBaseVO> list) {
default List<DeliveryExpressTemplateFreeDO> convertTemplateFreeList(Long templateId, List<DeliveryExpressTemplateFreeBaseVO> list) {
return CollectionUtils.convertList(list, vo -> convertTemplateFree(templateId, vo));
}
@ -93,4 +89,5 @@ public interface DeliveryExpressTemplateConvert {
});
return result;
}
}

View File

@ -2,11 +2,10 @@ package cn.iocoder.yudao.module.trade.service.delivery;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate.DeliveryExpressTemplateCreateReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate.DeliveryExpressTemplateDetailRespVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate.DeliveryExpressTemplatePageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate.DeliveryExpressTemplateUpdateReqVO;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateChargeDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateFreeDO;
@ -50,21 +49,21 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
validateTemplateNameUnique(createReqVO.getName(), null);
// 插入
DeliveryExpressTemplateDO deliveryExpressTemplate = INSTANCE.convert(createReqVO);
expressTemplateMapper.insert(deliveryExpressTemplate);
DeliveryExpressTemplateDO template = INSTANCE.convert(createReqVO);
expressTemplateMapper.insert(template);
// 插入运费模板计费表
if (CollUtil.isNotEmpty(createReqVO.getTemplateCharge())) {
if (CollUtil.isNotEmpty(createReqVO.getCharges())) {
expressTemplateChargeMapper.insertBatch(
INSTANCE.convertTemplateChargeList(deliveryExpressTemplate.getId(), createReqVO.getChargeMode(), createReqVO.getTemplateCharge())
INSTANCE.convertTemplateChargeList(template.getId(), createReqVO.getChargeMode(), createReqVO.getCharges())
);
}
// 插入运费模板包邮表
if (CollUtil.isNotEmpty(createReqVO.getTemplateFree())) {
if (CollUtil.isNotEmpty(createReqVO.getFrees())) {
expressTemplateFreeMapper.insertBatch(
INSTANCE.convertTemplateFreeList(deliveryExpressTemplate.getId(), createReqVO.getTemplateFree())
INSTANCE.convertTemplateFreeList(template.getId(), createReqVO.getFrees())
);
}
return deliveryExpressTemplate.getId();
return template.getId();
}
@Override
@ -76,76 +75,54 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
validateTemplateNameUnique(updateReqVO.getName(), updateReqVO.getId());
// 更新运费从表
updateExpressTemplateCharge(updateReqVO);
updateExpressTemplateCharge(updateReqVO.getId(), updateReqVO.getChargeMode(), updateReqVO.getCharges());
// 更新包邮从表
updateExpressTemplateFree(updateReqVO);
updateExpressTemplateFree(updateReqVO.getId(), updateReqVO.getFrees());
// 更新模板主表
DeliveryExpressTemplateDO updateObj = INSTANCE.convert(updateReqVO);
expressTemplateMapper.updateById(updateObj);
}
private void updateExpressTemplateFree(DeliveryExpressTemplateUpdateReqVO updateReqVO) {
// 1.1 获得新增/修改的区域列表
List<DeliveryExpressTemplateFreeDO> oldFreeList = expressTemplateFreeMapper.selectListByTemplateId(updateReqVO.getId());
List<DeliveryExpressTemplateUpdateReqVO.ExpressTemplateFreeUpdateVO> newFreeList = updateReqVO.getTemplateFree();
List<DeliveryExpressTemplateFreeDO> addFreeList = new ArrayList<>(newFreeList.size()); // 新增包邮区域列表
List<DeliveryExpressTemplateFreeDO> updateFreeList = new ArrayList<>(newFreeList.size()); // 更新包邮区域列表
for (DeliveryExpressTemplateUpdateReqVO.ExpressTemplateFreeUpdateVO item : newFreeList) {
if (Objects.nonNull(item.getId())) {
updateFreeList.add(INSTANCE.convertTemplateFree(item));
} else {
item.setTemplateId(updateReqVO.getId());
addFreeList.add(INSTANCE.convertTemplateFree(item));
}
}
// 1.2 新增
if (CollUtil.isNotEmpty(addFreeList)) {
expressTemplateFreeMapper.insertBatch(addFreeList);
}
// 1.3 修改
if (CollUtil.isNotEmpty(updateFreeList)) {
expressTemplateFreeMapper.updateBatch(updateFreeList);
}
private void updateExpressTemplateFree(Long templateId, List<DeliveryExpressTemplateFreeBaseVO> frees) {
// 第一步对比新老数据获得添加修改删除的列表
List<DeliveryExpressTemplateFreeDO> oldList = expressTemplateFreeMapper.selectListByTemplateId(templateId);
List<DeliveryExpressTemplateFreeDO> newList = INSTANCE.convertTemplateFreeList(templateId, frees);
List<List<DeliveryExpressTemplateFreeDO>> diffList = CollectionUtils.diffList(oldList, newList,
(oldVal, newVal) -> ObjectUtil.equal(oldVal.getId(), newVal.getTemplateId()));
// 2. 删除
Set<Long> deleteFreeIds = convertSet(oldFreeList, DeliveryExpressTemplateFreeDO::getId);
deleteFreeIds.removeAll(convertSet(updateFreeList, DeliveryExpressTemplateFreeDO::getId));
if (CollUtil.isNotEmpty(deleteFreeIds)) {
expressTemplateFreeMapper.deleteBatchIds(deleteFreeIds);
// 第二步批量添加修改删除
if (CollUtil.isNotEmpty(diffList.get(0))) {
expressTemplateFreeMapper.insertBatch(diffList.get(0));
}
if (CollUtil.isNotEmpty(diffList.get(1))) {
expressTemplateFreeMapper.updateBatch(diffList.get(1));
}
if (CollUtil.isNotEmpty(diffList.get(2))) {
expressTemplateFreeMapper.deleteBatchIds(convertList(diffList.get(2), DeliveryExpressTemplateFreeDO::getId));
}
}
private void updateExpressTemplateCharge(DeliveryExpressTemplateUpdateReqVO updateReqVO) {
// 1.1 获得新增/修改的区域列表
List<DeliveryExpressTemplateChargeDO> oldChargeList = expressTemplateChargeMapper.selectListByTemplateId(updateReqVO.getId());
List<DeliveryExpressTemplateUpdateReqVO.ExpressTemplateChargeUpdateVO> newChargeList = updateReqVO.getTemplateCharge();
List<DeliveryExpressTemplateChargeDO> addList = new ArrayList<>(newChargeList.size()); // 新增运费区域列表
List<DeliveryExpressTemplateChargeDO> updateList = new ArrayList<>(newChargeList.size()); // 更新运费区域列表
for (DeliveryExpressTemplateUpdateReqVO.ExpressTemplateChargeUpdateVO item : newChargeList) {
if (item.getId() != null) {
// 计费模式以主表为准
item.setChargeMode(updateReqVO.getChargeMode());
updateList.add(INSTANCE.convertTemplateCharge(item));
} else {
item.setTemplateId(updateReqVO.getId());
item.setChargeMode(updateReqVO.getChargeMode());
addList.add(INSTANCE.convertTemplateCharge(item));
private void updateExpressTemplateCharge(Long templateId, Integer chargeMode, List<DeliveryExpressTemplateChargeBaseVO> charges) {
// 第一步对比新老数据获得添加修改删除的列表
List<DeliveryExpressTemplateChargeDO> oldList = expressTemplateChargeMapper.selectListByTemplateId(templateId);
List<DeliveryExpressTemplateChargeDO> newList = INSTANCE.convertTemplateChargeList(templateId, chargeMode, charges);
List<List<DeliveryExpressTemplateChargeDO>> diffList = diffList(oldList, newList, (oldVal, newVal) -> {
boolean same = ObjectUtil.equal(oldVal.getId(), newVal.getId());
if (same) {
newVal.setChargeMode(chargeMode); // 更新下收费模式
}
}
// 1.2 新增
if (CollUtil.isNotEmpty(addList)) {
expressTemplateChargeMapper.insertBatch(addList);
}
// 1.3 修改
if (CollUtil.isNotEmpty(updateList)) {
expressTemplateChargeMapper.updateBatch(updateList);
}
return same;
});
// 2. 删除
Set<Long> deleteChargeIds = convertSet(oldChargeList, DeliveryExpressTemplateChargeDO::getId);
deleteChargeIds.removeAll(convertSet(updateList, DeliveryExpressTemplateChargeDO::getId));
if (CollUtil.isNotEmpty(deleteChargeIds)) {
expressTemplateChargeMapper.deleteBatchIds(deleteChargeIds);
// 第二步批量添加修改删除
if (CollUtil.isNotEmpty(diffList.get(0))) {
expressTemplateChargeMapper.insertBatch(diffList.get(0));
}
if (CollUtil.isNotEmpty(diffList.get(1))) {
expressTemplateChargeMapper.updateBatch(diffList.get(1));
}
if (CollUtil.isNotEmpty(diffList.get(2))) {
expressTemplateChargeMapper.deleteBatchIds(convertList(diffList.get(2), DeliveryExpressTemplateChargeDO::getId));
}
}
@ -238,14 +215,4 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
return INSTANCE.convertMap(areaId, templateList, chargeList, freeList);
}
private DeliveryExpressTemplateRespBO.Charge findMatchExpressTemplateCharge(
List<DeliveryExpressTemplateChargeDO> templateChargeList, Integer areaId) {
return INSTANCE.convertTemplateCharge(findFirst(templateChargeList, item -> item.getAreaIds().contains(areaId)));
}
private DeliveryExpressTemplateRespBO.Free findMatchExpressTemplateFree(
List<DeliveryExpressTemplateFreeDO> templateFreeList, Integer areaId) {
return INSTANCE.convertTemplateFree(findFirst(templateFreeList, item -> item.getAreaIds().contains(areaId)));
}
}

View File

@ -6,17 +6,17 @@ import cn.iocoder.yudao.framework.ip.core.Area;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.framework.ip.core.utils.IPUtils;
import cn.iocoder.yudao.module.system.controller.admin.ip.vo.AreaNodeRespVO;
import cn.iocoder.yudao.module.system.controller.admin.ip.vo.AreaNodeSimpleRespVO;
import cn.iocoder.yudao.module.system.convert.ip.AreaConvert;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@ -34,28 +34,6 @@ public class AreaController {
return success(AreaConvert.INSTANCE.convertList(area.getChildren()));
}
@GetMapping("/get-children")
@Operation(summary = "获得地区的下级区域")
@Parameter(name = "id", description = "区域编号", required = true, example = "150000")
public CommonResult<List<AreaNodeSimpleRespVO>> getChildren(@RequestParam("id") Integer id) {
Area area = AreaUtils.getArea(id);
Assert.notNull(area, String.format("获取不到 id : %d 的区域", id));
return success(AreaConvert.INSTANCE.convertList2(area.getChildren()));
}
// 4)方法改成 getAreaChildrenList 获得子节点们5url 可以已改成 children-list
//@芋艿 是不是叫 getAreaListByIds 更合适 因为不一定是子节点 用于前端树选择获取缓存数据 <el-tree-select :cache-data="areaCache">
@GetMapping("/get-by-ids")
@Operation(summary = "通过区域 ids 获得地区列表")
@Parameter(name = "ids", description = "区域编号 ids", required = true, example = "1,150000")
public CommonResult<List<AreaNodeSimpleRespVO>> getAreaListByIds(@RequestParam("ids") Set<Integer> ids) {
List<Area> areaList = new ArrayList<>(ids.size());
for (Integer areaId : ids) {
areaList.add(AreaUtils.getArea(areaId));
}
return success(AreaConvert.INSTANCE.convertList2(areaList));
}
@GetMapping("/get-by-ip")
@Operation(summary = "获得 IP 对应的地区名")
@Parameter(name = "ip", description = "IP", required = true)

View File

@ -1,19 +0,0 @@
package cn.iocoder.yudao.module.system.controller.admin.ip.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Schema(description = "管理后台 - 简洁的地区节点 Response VO")
@Data
public class AreaNodeSimpleRespVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "110000")
private Integer id;
@Schema(description = "名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "北京")
private String name;
@Schema(description = "是否叶子节点", example = "false")
private Boolean leaf;
}

View File

@ -1,16 +1,12 @@
package cn.iocoder.yudao.module.system.convert.ip;
import cn.iocoder.yudao.framework.ip.core.Area;
import cn.iocoder.yudao.framework.ip.core.enums.AreaTypeEnum;
import cn.iocoder.yudao.module.system.controller.admin.ip.vo.AreaNodeRespVO;
import cn.iocoder.yudao.module.system.controller.admin.ip.vo.AreaNodeSimpleRespVO;
import cn.iocoder.yudao.module.system.controller.app.ip.vo.AppAreaNodeRespVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import java.util.List;
import java.util.Objects;
@Mapper
public interface AreaConvert {
@ -19,15 +15,6 @@ public interface AreaConvert {
List<AreaNodeRespVO> convertList(List<Area> list);
List<AreaNodeSimpleRespVO> convertList2(List<Area> list);
@Mapping(source = "type", target = "leaf")
AreaNodeSimpleRespVO convert(Area area);
default Boolean convertAreaType(Integer type) {
return Objects.equals(AreaTypeEnum.DISTRICT.getType(), type);
}
List<AppAreaNodeRespVO> convertList3(List<Area> list);
}