diff --git a/blade-biz-common/src/main/java/org/springblade/common/constant/orderpackage/OrderPackageSplitStateConstant.java b/blade-biz-common/src/main/java/org/springblade/common/constant/orderpackage/OrderPackageSplitStateConstant.java new file mode 100644 index 000000000..2d36f95a9 --- /dev/null +++ b/blade-biz-common/src/main/java/org/springblade/common/constant/orderpackage/OrderPackageSplitStateConstant.java @@ -0,0 +1,34 @@ +package org.springblade.common.constant.orderpackage; + +import lombok.Getter; + +/** + * 包件拆单明细的状态 + * 状态:1=待拆单,2=已拆单,3=驳回拆单,4=已撤回 + */ +@Getter +public enum OrderPackageSplitStateConstant { + + + wait("待拆单",1), + finish("已拆单",2), + reject("驳回拆单",3), + cancel("已撤回",4); + + private String name; + private Integer value; + + private OrderPackageSplitStateConstant(String name, Integer value) { + this.name = name; + this.value = value; + } + + public void setName(String name) { + this.name = name; + } + + public void setValue(Integer value) { + this.value = value; + } + +} diff --git a/blade-service-api/logpm-distribution-api/src/main/java/com/logpm/distribution/entity/DistributionSplitOrderDetailEntity.java b/blade-service-api/logpm-distribution-api/src/main/java/com/logpm/distribution/entity/DistributionSplitOrderDetailEntity.java index 554762d04..4f5d0199b 100644 --- a/blade-service-api/logpm-distribution-api/src/main/java/com/logpm/distribution/entity/DistributionSplitOrderDetailEntity.java +++ b/blade-service-api/logpm-distribution-api/src/main/java/com/logpm/distribution/entity/DistributionSplitOrderDetailEntity.java @@ -74,18 +74,33 @@ public class DistributionSplitOrderDetailEntity extends TenantEntity { @ApiModelProperty(value = "预留5") private String reserve5; + /** + * 任务ID + */ @ApiModelProperty(value = "任务ID") private Long splitOrderTaskId; + /** + * 包条 + */ @ApiModelProperty(value = "包条") private String orderPackageCode; + /** + * 状态:1=待拆单,2=已拆单,3=驳回拆单,4=已撤回 + */ @ApiModelProperty(value = "状态:1=待拆单,2=已拆单,3=驳回拆单,4=已撤回") private Integer state; + /** + * 撤回人员ID + */ @ApiModelProperty(value = "撤回人员ID") private Long cancelUser; + /** + * 撤回人员 + */ @ApiModelProperty(value = "撤回人员") private String cancelUsername; } diff --git a/blade-service-api/logpm-distribution-api/src/main/java/com/logpm/distribution/entity/DistributionSplitOrderTaskEntity.java b/blade-service-api/logpm-distribution-api/src/main/java/com/logpm/distribution/entity/DistributionSplitOrderTaskEntity.java index a2c5cd912..fed865c55 100644 --- a/blade-service-api/logpm-distribution-api/src/main/java/com/logpm/distribution/entity/DistributionSplitOrderTaskEntity.java +++ b/blade-service-api/logpm-distribution-api/src/main/java/com/logpm/distribution/entity/DistributionSplitOrderTaskEntity.java @@ -77,49 +77,91 @@ public class DistributionSplitOrderTaskEntity extends TenantEntity { @ApiModelProperty(value = "预留5") private String reserve5; + /** + * 被拆订单ID + */ @NotBlank(message = "被拆订单ID不能为空") @ApiModelProperty(value = "被拆订单ID") private Long stockArticleId; + /** + * 被拆订单 + */ @ApiModelProperty(value = "被拆订单") private String orderCode; + /** + * 商场名称 + */ @ApiModelProperty(value = "商场名称") private String mallName; + /** + * 状态:1=待审核,2=同意拆单,3=驳回拆单,4=撤回 + */ @ApiModelProperty(value = "状态:1=待审核,2=同意拆单,3=驳回拆单,4=撤回") private Integer state; + /** + * 原因 + */ @ApiModelProperty(value = "原因") private String reason; + /** + * 审核人 + */ @ApiModelProperty(value = "审核人") private Long reviewerUser; + /** + * 审核人名字 + */ @ApiModelProperty(value = "审核人名字") private String reviewerUsername; + /** + * 审核时间 + */ @ApiModelProperty(value = "审核时间") private String reviewTime; + /** + * 拆单数量 + */ @ApiModelProperty(value = "拆单数量") private Integer number; + /** + * 新的客户姓名 + */ @NotBlank(message = "新的客户姓名不能为空") @ApiModelProperty(value = "新的客户姓名") private String customerName; + /** + * 新的客户联系方式 + */ @NotBlank(message = "新的客户联系方式不能为空") @ApiModelProperty(value = "新的客户联系方式") private String customerTelephone; + /** + * 新的客户地址 + */ @NotBlank(message = "新的客户地址不能为空") @ApiModelProperty(value = "新的客户地址") private String customerAddress; + /** + * 新的订单 + */ @ApiModelProperty(value = "新的订单") private String newOrderCode; + /** + * 新的订单ID + */ @ApiModelProperty(value = "新的订单ID") private Long newStockArticleId; diff --git a/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/mapper/DistributionSplitOrderDetailMapper.java b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/mapper/DistributionSplitOrderDetailMapper.java new file mode 100644 index 000000000..7cd98e132 --- /dev/null +++ b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/mapper/DistributionSplitOrderDetailMapper.java @@ -0,0 +1,7 @@ +package com.logpm.distribution.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.logpm.distribution.entity.DistributionSplitOrderDetailEntity; + +public interface DistributionSplitOrderDetailMapper extends BaseMapper { +} diff --git a/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/mapper/DistributionSplitOrderLogMapper.java b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/mapper/DistributionSplitOrderLogMapper.java new file mode 100644 index 000000000..d88a5992f --- /dev/null +++ b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/mapper/DistributionSplitOrderLogMapper.java @@ -0,0 +1,8 @@ +package com.logpm.distribution.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.logpm.distribution.entity.DistributionSplitOrderLogEntity; + +public interface DistributionSplitOrderLogMapper extends BaseMapper { + +} diff --git a/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/IDistributionSplitOrderDetailService.java b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/IDistributionSplitOrderDetailService.java new file mode 100644 index 000000000..81bf217b0 --- /dev/null +++ b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/IDistributionSplitOrderDetailService.java @@ -0,0 +1,13 @@ +package com.logpm.distribution.service; + + +import com.logpm.distribution.entity.DistributionSplitOrderDetailEntity; +import org.springblade.core.mp.base.BaseService; + +/** + * 拆单任务详情接口 + */ +public interface IDistributionSplitOrderDetailService extends BaseService { + + +} diff --git a/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/IDistributionSplitOrderLogService.java b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/IDistributionSplitOrderLogService.java new file mode 100644 index 000000000..b91512d94 --- /dev/null +++ b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/IDistributionSplitOrderLogService.java @@ -0,0 +1,20 @@ +package com.logpm.distribution.service; + + +import com.logpm.distribution.entity.DistributionSplitOrderLogEntity; +import com.logpm.distribution.entity.DistributionSplitOrderTaskEntity; +import org.springblade.core.mp.base.BaseService; + +/** + * 拆单任务详情接口 + */ +public interface IDistributionSplitOrderLogService extends BaseService { + + default void record(DistributionSplitOrderTaskEntity taskEntity, String content, String username) { + DistributionSplitOrderLogEntity distributionSplitOrderLogEntity = new DistributionSplitOrderLogEntity(); + distributionSplitOrderLogEntity.setContent(content); + distributionSplitOrderLogEntity.setCreateUsername(username); + distributionSplitOrderLogEntity.setSplitOrderTaskId(taskEntity.getId()); + save(distributionSplitOrderLogEntity); + } +} diff --git a/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/IDistributionSplitOrderTaskService.java b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/IDistributionSplitOrderTaskService.java index 733e692f6..5aa6e60a6 100644 --- a/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/IDistributionSplitOrderTaskService.java +++ b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/IDistributionSplitOrderTaskService.java @@ -2,6 +2,7 @@ package com.logpm.distribution.service; import com.logpm.distribution.entity.DistributionSplitOrderTaskEntity; +import org.springblade.core.mp.base.BaseService; import org.springblade.core.mp.support.Query; import org.springblade.core.tool.api.R; @@ -10,7 +11,7 @@ import java.util.List; /** * 拆单任务接口 */ -public interface IDistributionSplitOrderTaskService { +public interface IDistributionSplitOrderTaskService extends BaseService { /** * 拆单任务列表 @@ -44,10 +45,10 @@ public interface IDistributionSplitOrderTaskService { /** * 单个包撤回任务 * @param taskId - * @param packageCode + * @param packageCodes * @return */ - R cancelPackageCode(Long taskId, String packageCode); + R cancelPackageCode(Long taskId, List packageCodes); /** * 审核任务 diff --git a/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/impl/DistributionSplitOrderDetailServiceImpl.java b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/impl/DistributionSplitOrderDetailServiceImpl.java new file mode 100644 index 000000000..194aeb82c --- /dev/null +++ b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/impl/DistributionSplitOrderDetailServiceImpl.java @@ -0,0 +1,15 @@ +package com.logpm.distribution.service.impl; + +import com.logpm.distribution.entity.DistributionSplitOrderDetailEntity; +import com.logpm.distribution.mapper.DistributionSplitOrderDetailMapper; +import com.logpm.distribution.service.IDistributionSplitOrderDetailService; +import groovy.util.logging.Slf4j; +import lombok.AllArgsConstructor; +import org.springblade.core.mp.base.BaseServiceImpl; +import org.springframework.stereotype.Service; + +@AllArgsConstructor +@Slf4j +@Service +public class DistributionSplitOrderDetailServiceImpl extends BaseServiceImpl implements IDistributionSplitOrderDetailService { +} diff --git a/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/impl/DistributionSplitOrderLogServiceImpl.java b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/impl/DistributionSplitOrderLogServiceImpl.java new file mode 100644 index 000000000..9c276afa8 --- /dev/null +++ b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/impl/DistributionSplitOrderLogServiceImpl.java @@ -0,0 +1,17 @@ +package com.logpm.distribution.service.impl; + +import com.logpm.distribution.entity.DistributionSplitOrderLogEntity; +import com.logpm.distribution.entity.DistributionSplitOrderTaskEntity; +import com.logpm.distribution.mapper.DistributionSplitOrderLogMapper; +import com.logpm.distribution.service.IDistributionSplitOrderLogService; +import groovy.util.logging.Slf4j; +import lombok.AllArgsConstructor; +import org.springblade.core.mp.base.BaseServiceImpl; +import org.springframework.stereotype.Service; + +@AllArgsConstructor +@Slf4j +@Service +public class DistributionSplitOrderLogServiceImpl extends BaseServiceImpl implements IDistributionSplitOrderLogService { + +} diff --git a/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/impl/DistributionSplitOrderTaskServiceImpl.java b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/impl/DistributionSplitOrderTaskServiceImpl.java index bfeea7ab8..11502ddd0 100644 --- a/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/impl/DistributionSplitOrderTaskServiceImpl.java +++ b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/impl/DistributionSplitOrderTaskServiceImpl.java @@ -1,69 +1,441 @@ package com.logpm.distribution.service.impl; +import com.baomidou.mybatisplus.core.toolkit.SerializationUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.logpm.distribution.entity.DistributionParcelListEntity; +import com.logpm.distribution.entity.DistributionSplitOrderDetailEntity; import com.logpm.distribution.entity.DistributionSplitOrderTaskEntity; import com.logpm.distribution.entity.DistributionStockArticleEntity; import com.logpm.distribution.mapper.DistributionSplitOrderTaskMapper; -import com.logpm.distribution.service.IDistributionSplitOrderTaskService; +import com.logpm.distribution.service.*; import groovy.util.logging.Slf4j; import lombok.AllArgsConstructor; +import org.springblade.common.constant.orderpackage.OrderPackageReservationStatusConstant; +import org.springblade.common.constant.orderpackage.OrderPackageSplitStateConstant; import org.springblade.core.mp.base.BaseServiceImpl; import org.springblade.core.mp.support.Query; +import org.springblade.core.redis.lock.LockType; +import org.springblade.core.redis.lock.RedisLockClient; +import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tool.api.R; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; @Service @AllArgsConstructor @Slf4j public class DistributionSplitOrderTaskServiceImpl extends BaseServiceImpl implements IDistributionSplitOrderTaskService { - private DistributionStockArticleServiceImpl distributionStockArticleService; - private DistributionParcelListServiceImpl distributionParcelListService; + private final IDistributionStockArticleService distributionStockArticleService; + private final IDistributionParcelListService distributionParcelListService; + private final IDistributionSplitOrderDetailService distributionSplitOrderDetailService; + private final IDistributionSplitOrderLogService distributionSplitOrderLogService; + private final RedisLockClient redisLockClient; + @Override public R> taskList(Query query, DistributionSplitOrderTaskEntity distributionSplitOrderTaskEntity) { return null; } + @Transactional @Override public R createTask(DistributionSplitOrderTaskEntity distributionSplitOrderTaskEntity, List packageCodeList) { + try { DistributionStockArticleEntity distributionStockArticle = distributionStockArticleService.getById(distributionSplitOrderTaskEntity.getStockArticleId()); if (distributionStockArticle == null) { return R.fail("原订单信息错误"); } - List packageCode = distributionParcelListService.list(Wrappers.lambdaQuery() + + List belongsToOrder = distributionParcelListService.list(Wrappers.lambdaQuery() .eq(DistributionParcelListEntity::getStockArticleId, distributionSplitOrderTaskEntity.getStockArticleId()) .in(DistributionParcelListEntity::getOrderPackageCode, packageCodeList) ); - if (packageCode.size() != packageCodeList.size()) { - return R.fail("订单包件信息错误"); + if (belongsToOrder.size() != packageCodeList.size()) { + List belongsToOrderPackage = belongsToOrder.stream().map(DistributionParcelListEntity::getOrderPackageCode).collect(Collectors.toList()); + return R.fail(String.format("订单包件信息错误,选择的包件(%s)不属于该订单", packageCodeList.stream() + .filter(packageCode -> !belongsToOrderPackage.contains(packageCode)) + .collect(Collectors.joining(",")) + )); + } + + List reservationsPackageList = belongsToOrder.stream() + .filter(distributionParcelListEntity -> OrderPackageReservationStatusConstant.yiyueyue.getValue().equals(distributionParcelListEntity.getOrderPackageReservationStatus())) + .collect(Collectors.toList()); + + if (!reservationsPackageList.isEmpty()) { + return R.fail(String.format("订单包件:%s,已被预约,无法拆单", reservationsPackageList.stream() + .map(DistributionParcelListEntity::getOrderPackageCode) + .collect(Collectors.joining(",")) + )); } + int leaseTime = 10000; + String key = "lock:splitOrderTask:create" + distributionSplitOrderTaskEntity.getStockArticleId(); + + boolean lockRes = redisLockClient.tryLock(key, LockType.FAIR, 5000, leaseTime, TimeUnit.MILLISECONDS); + if (!lockRes) { + return R.fail("拆单任务繁忙,请重试"); + } + long currentTimeMillis = System.currentTimeMillis(); - return R.data(""); + saveNewTask(distributionSplitOrderTaskEntity, distributionStockArticle, belongsToOrder); + distributionSplitOrderLogService.record(distributionSplitOrderTaskEntity, "拆单任务创建", AuthUtil.getUser().getUserName()); + + if (System.currentTimeMillis() - currentTimeMillis < leaseTime) { + redisLockClient.unLock(key, LockType.FAIR); + } + return R.data("success"); } catch (Exception e) { log.error("createTask error", e); return R.fail("createTask error"); } } + + public void saveNewTask(DistributionSplitOrderTaskEntity distributionSplitOrderTaskEntity, DistributionStockArticleEntity distributionStockArticle, List belongsToOrder){ + /* + * 避免审核期间的数据被操作,且无法去其他节点修改,所以第一时间分配新的订单 + */ + String newOrderCode = getNewOrderCode(distributionStockArticle.getOrderCode(), distributionStockArticle.getId()); + DistributionStockArticleEntity newStockArticle = SerializationUtils.clone(distributionStockArticle); + newStockArticle.setId(null); + newStockArticle.setOrderCode(newOrderCode); + newStockArticle.setTotalNumber(belongsToOrder.size()); + newStockArticle.setHandQuantity(belongsToOrder.size()); + distributionStockArticleService.save(newStockArticle); + + distributionStockArticle.setTotalNumber(distributionStockArticle.getTotalNumber() - belongsToOrder.size()); + distributionStockArticle.setHandQuantity(distributionStockArticle.getHandQuantity() - belongsToOrder.size()); + if (distributionStockArticle.getTotalNumber() == 0) { + distributionStockArticleService.removeById(distributionStockArticle.getId()); + }else{ + distributionStockArticleService.updateById(distributionStockArticle); + } + + List detailIds = belongsToOrder.stream().map(DistributionParcelListEntity::getId).collect(Collectors.toList()); + distributionParcelListService.update(Wrappers.lambdaUpdate() + .in(DistributionParcelListEntity::getId, detailIds) + .set(DistributionParcelListEntity::getStockArticleId, newStockArticle.getId()) + .set(DistributionParcelListEntity::getOrderCode, newStockArticle.getOrderCode()) + ); + + distributionSplitOrderTaskEntity.setNewOrderCode(newOrderCode); + distributionSplitOrderTaskEntity.setNewStockArticleId(newStockArticle.getId()); + distributionSplitOrderTaskEntity.setNumber(belongsToOrder.size()); + distributionSplitOrderTaskEntity.setMallName(newStockArticle.getMallName()); + save(distributionSplitOrderTaskEntity); + + List details = new ArrayList<>(belongsToOrder.size()); + belongsToOrder.forEach(distributionParcelListEntity -> { + DistributionSplitOrderDetailEntity detail = new DistributionSplitOrderDetailEntity(); + detail.setOrderPackageCode(distributionParcelListEntity.getOrderPackageCode()); + detail.setSplitOrderTaskId(distributionSplitOrderTaskEntity.getId()); + detail.setState(OrderPackageSplitStateConstant.wait.getValue()); + details.add(detail); + }); + distributionSplitOrderDetailService.saveBatch(details); + } + + @Transactional @Override public R updateTask(DistributionSplitOrderTaskEntity distributionSplitOrderTaskEntity, List packageCodeList) { - return null; + try { + DistributionSplitOrderTaskEntity task = getById(distributionSplitOrderTaskEntity.getId()); + if (task == null) { + return R.fail("拆单任务不存在"); + } + + if (!task.getStockArticleId().equals(distributionSplitOrderTaskEntity.getStockArticleId())) { + return R.fail("拆单任务订单ID不一致"); + } + + DistributionStockArticleEntity distributionStockArticle = distributionStockArticleService.getById(distributionSplitOrderTaskEntity.getStockArticleId()); + if (distributionStockArticle == null) { + return R.fail("原订单信息错误"); + } + + ArrayList StockArticleIds = new ArrayList<>(); + StockArticleIds.add(distributionSplitOrderTaskEntity.getStockArticleId()); + StockArticleIds.add(task.getNewStockArticleId()); + + List belongsToOrder = distributionParcelListService.list(Wrappers.lambdaQuery() + .in(DistributionParcelListEntity::getStockArticleId, StockArticleIds) + .in(DistributionParcelListEntity::getOrderPackageCode, packageCodeList) + ); + if (belongsToOrder.size() != packageCodeList.size()) { + List belongsToOrderPackage = belongsToOrder.stream().map(DistributionParcelListEntity::getOrderPackageCode).collect(Collectors.toList()); + return R.fail(String.format("订单包件信息错误,选择的包件(%s)不属于该订单", packageCodeList.stream() + .filter(packageCode -> !belongsToOrderPackage.contains(packageCode)) + .collect(Collectors.joining(",")) + )); + } + + List reservationsPackageList = belongsToOrder.stream() + .filter(distributionParcelListEntity -> OrderPackageReservationStatusConstant.yiyueyue.getValue().equals(distributionParcelListEntity.getOrderPackageReservationStatus())) + .collect(Collectors.toList()); + + if (!reservationsPackageList.isEmpty()) { + return R.fail(String.format("订单包件:%s,已被预约,无法拆单", reservationsPackageList.stream() + .map(DistributionParcelListEntity::getOrderPackageCode) + .collect(Collectors.joining(",")) + )); + } + + DistributionStockArticleEntity newStockArticle = distributionStockArticleService.getById(distributionSplitOrderTaskEntity.getNewStockArticleId()); + if (newStockArticle == null) { + return R.fail("新订单信息错误"); + } + + updateTask(distributionSplitOrderTaskEntity, distributionStockArticle, belongsToOrder, newStockArticle); + distributionSplitOrderLogService.record(distributionSplitOrderTaskEntity, "拆单任务修改", AuthUtil.getUser().getUserName()); + + return R.data("success"); + } catch (Exception e) { + log.error("updateTask error", e); + return R.fail("修改失败"); + } } + + private void updateTask(DistributionSplitOrderTaskEntity distributionSplitOrderTaskEntity, DistributionStockArticleEntity distributionStockArticle, List belongsToOrder, DistributionStockArticleEntity newStockArticle){ + + distributionSplitOrderTaskEntity.setNumber(belongsToOrder.size()); + updateById(distributionSplitOrderTaskEntity); + + newStockArticle.setTotalNumber(belongsToOrder.size()); + newStockArticle.setHandQuantity(belongsToOrder.size()); + distributionStockArticleService.save(newStockArticle); + + packageUpdate(distributionSplitOrderTaskEntity, distributionStockArticle, belongsToOrder); + + distributionStockArticle.setTotalNumber(distributionStockArticle.getTotalNumber() - belongsToOrder.size()); + distributionStockArticle.setHandQuantity(distributionStockArticle.getHandQuantity() - belongsToOrder.size()); + if (distributionStockArticle.getTotalNumber() == 0) { + distributionStockArticleService.removeById(distributionStockArticle.getId()); + }else{ + distributionStockArticleService.updateById(distributionStockArticle); + } + } + + private void packageUpdate(DistributionSplitOrderTaskEntity distributionSplitOrderTaskEntity, DistributionStockArticleEntity distributionStockArticle, List belongsToOrder) { + List taskJoinPackage = distributionSplitOrderDetailService.list(Wrappers.lambdaQuery() + .eq(DistributionSplitOrderDetailEntity::getSplitOrderTaskId, distributionSplitOrderTaskEntity.getId()) + ); + List taskJoinPackageList = taskJoinPackage.stream().map(DistributionSplitOrderDetailEntity::getOrderPackageCode).collect(Collectors.toList()); + + List add = belongsToOrder.stream().filter(distributionParcelListEntity -> + distributionParcelListEntity.getOrderCode().equals(distributionStockArticle.getOrderCode()) + && !taskJoinPackageList.contains(distributionParcelListEntity.getOrderPackageCode()) + ).collect(Collectors.toList()); + + if (!add.isEmpty()) { + List ids = add.stream().map(DistributionParcelListEntity::getId).collect(Collectors.toList()); + distributionParcelListService.update(Wrappers.lambdaUpdate() + .in(DistributionParcelListEntity::getId, ids) + .set(DistributionParcelListEntity::getStockArticleId, distributionSplitOrderTaskEntity.getNewStockArticleId()) + .set(DistributionParcelListEntity::getOrderCode, distributionSplitOrderTaskEntity.getNewOrderCode()) + ); + + List details = new ArrayList<>(add.size()); + add.forEach(distributionParcelListEntity -> { + DistributionSplitOrderDetailEntity detail = new DistributionSplitOrderDetailEntity(); + detail.setOrderPackageCode(distributionParcelListEntity.getOrderPackageCode()); + detail.setSplitOrderTaskId(distributionSplitOrderTaskEntity.getId()); + detail.setState(OrderPackageSplitStateConstant.wait.getValue()); + details.add(detail); + }); + distributionSplitOrderDetailService.saveBatch(details); + } + + List recover = belongsToOrder.stream().filter(distributionParcelListEntity -> + distributionParcelListEntity.getOrderCode().equals(distributionStockArticle.getOrderCode()) + && taskJoinPackageList.contains(distributionParcelListEntity.getOrderPackageCode()) + ).collect(Collectors.toList()); + + if (!recover.isEmpty()) { + List ids = recover.stream().map(DistributionParcelListEntity::getId).collect(Collectors.toList()); + distributionParcelListService.update(Wrappers.lambdaUpdate() + .in(DistributionParcelListEntity::getId, ids) + .set(DistributionParcelListEntity::getStockArticleId, distributionSplitOrderTaskEntity.getStockArticleId()) + .set(DistributionParcelListEntity::getOrderCode, distributionSplitOrderTaskEntity.getOrderCode()) + ); + distributionSplitOrderDetailService.update(Wrappers.lambdaUpdate() + .eq(DistributionSplitOrderDetailEntity::getSplitOrderTaskId, distributionSplitOrderTaskEntity.getId()) + .in(DistributionSplitOrderDetailEntity::getOrderPackageCode, recover.stream().map(DistributionParcelListEntity::getOrderPackageCode).collect(Collectors.toList())) + .set(DistributionSplitOrderDetailEntity::getState, OrderPackageSplitStateConstant.wait.getValue()) + ); + } + + List currentPackage = belongsToOrder.stream().map(DistributionParcelListEntity::getOrderPackageCode).collect(Collectors.toList()); + + List remove = taskJoinPackage.stream().filter(distributionSplitOrderDetailEntity -> + !currentPackage.contains(distributionSplitOrderDetailEntity.getOrderPackageCode()) + && !OrderPackageSplitStateConstant.wait.getValue().equals(distributionSplitOrderDetailEntity.getState()) + ).collect(Collectors.toList()); + + if (!remove.isEmpty()) { + List ids = remove.stream().map(DistributionSplitOrderDetailEntity::getId).collect(Collectors.toList()); + distributionSplitOrderDetailService.update(Wrappers.lambdaUpdate().in( + DistributionSplitOrderDetailEntity::getId, ids + ).set(DistributionSplitOrderDetailEntity::getState, OrderPackageSplitStateConstant.cancel.getValue())); + } + } + + @Transactional @Override public R cancelTask(Long taskId) { - return null; + try { + DistributionSplitOrderTaskEntity task = getById(taskId); + if (task == null) { + return R.fail("任务不存在"); + } + + DistributionStockArticleEntity oldStockArticle = distributionStockArticleService.getById(task.getStockArticleId()); + DistributionStockArticleEntity newStockArticle = distributionStockArticleService.getById(task.getNewStockArticleId()); + if (newStockArticle == null) { + return R.fail("订单错误,无法取消"); + } + if (oldStockArticle == null) { + oldStockArticle = distributionStockArticleService.getOne(Wrappers.lambdaQuery() + .eq(DistributionStockArticleEntity::getId, task.getStockArticleId()) + .eq(DistributionStockArticleEntity::getIsDeleted, 1) + ); + if (oldStockArticle == null) { + return R.fail("订单错误,无法取消"); + } + oldStockArticle.setIsDeleted(0); + } + + List packageList = distributionParcelListService.list(Wrappers.lambdaQuery() + .eq(DistributionParcelListEntity::getStockArticleId, task.getNewStockArticleId())); + if (packageList.isEmpty()) { + return R.fail("无可取消包件,无法取消"); + } + if (packageList.stream().anyMatch(distributionParcelListEntity -> OrderPackageReservationStatusConstant.yiyueyue.getValue().equals(distributionParcelListEntity.getOrderPackageReservationStatus()))) { + return R.fail("新的订单包件已预约,无法取消"); + } + + + distributionParcelListService.update(Wrappers.lambdaUpdate() + .eq(DistributionParcelListEntity::getStockArticleId, task.getNewStockArticleId()) + .set(DistributionParcelListEntity::getStockArticleId, task.getStockArticleId()) + .set(DistributionParcelListEntity::getOrderCode, task.getOrderCode()) + ); + + newStockArticle.setTotalNumber(0); + newStockArticle.setHandQuantity(0); + newStockArticle.setHandQuantity(1); + distributionStockArticleService.updateById(newStockArticle); + + oldStockArticle.setTotalNumber(oldStockArticle.getTotalNumber() + newStockArticle.getTotalNumber()); + oldStockArticle.setHandQuantity(oldStockArticle.getHandQuantity() + newStockArticle.getHandQuantity()); + distributionStockArticleService.updateById(oldStockArticle); + + task.setState(OrderPackageSplitStateConstant.cancel.getValue()); + updateById(task); + + distributionSplitOrderDetailService.update(Wrappers.lambdaUpdate() + .eq(DistributionSplitOrderDetailEntity::getSplitOrderTaskId, taskId) + .set(DistributionSplitOrderDetailEntity::getState, OrderPackageSplitStateConstant.cancel.getValue()) + .set(DistributionSplitOrderDetailEntity::getCancelUser, AuthUtil.getUser().getUserId()) + .set(DistributionSplitOrderDetailEntity::getCancelUsername, AuthUtil.getUser().getUserName()) + ); + distributionSplitOrderLogService.record(task, "取消拆单任务", AuthUtil.getUser().getUserName()); + + return R.success("success"); + } catch (Exception e) { + log.error("取消拆单任务失败", e); + return R.fail("取消拆单任务失败"); + } } + @Transactional @Override - public R cancelPackageCode(Long taskId, String packageCode) { - return null; + public R cancelPackageCode(Long taskId, List packageCodes) { + try { + DistributionSplitOrderTaskEntity task = getById(taskId); + if (task == null) { + return R.fail("拆单任务不存在"); + } + + if (task.getNumber() == packageCodes.size()) { + return cancelTask(taskId); + } + + List packageList = distributionParcelListService.list(Wrappers.lambdaQuery() + .eq(DistributionParcelListEntity::getStockArticleId, task.getNewStockArticleId()) + .in(DistributionParcelListEntity::getOrderPackageCode, packageCodes) + ); + if (packageList.stream().anyMatch(distributionParcelListEntity -> OrderPackageReservationStatusConstant.yiyueyue.getValue().equals(distributionParcelListEntity.getOrderPackageReservationStatus()))) { + return R.fail("新的订单包件已预约,无法取消"); + } + + List detail = distributionSplitOrderDetailService.list(Wrappers.lambdaQuery() + .eq(DistributionSplitOrderDetailEntity::getSplitOrderTaskId, taskId) + .in(DistributionSplitOrderDetailEntity::getOrderPackageCode, packageCodes) + ); + + long cancelCount = detail.stream().filter(distributionSplitOrderDetailEntity -> + OrderPackageSplitStateConstant.cancel.getValue().equals(distributionSplitOrderDetailEntity.getState()) + || OrderPackageSplitStateConstant.reject.getValue().equals(distributionSplitOrderDetailEntity.getState()) + ).count(); + if (cancelCount > 0) { + return R.fail("选择部分包件已取消,请重新选择"); + } + + DistributionStockArticleEntity oldStockArticle = distributionStockArticleService.getById(task.getStockArticleId()); + DistributionStockArticleEntity newStockArticle = distributionStockArticleService.getById(task.getNewStockArticleId()); + if (newStockArticle == null) { + return R.fail("订单错误,无法取消"); + } + if (oldStockArticle == null) { + oldStockArticle = distributionStockArticleService.getOne(Wrappers.lambdaQuery() + .eq(DistributionStockArticleEntity::getId, task.getStockArticleId()) + .eq(DistributionStockArticleEntity::getIsDeleted, 1) + ); + if (oldStockArticle == null) { + return R.fail("订单错误,无法取消"); + } + oldStockArticle.setIsDeleted(0); + } + + distributionParcelListService.update(Wrappers.lambdaUpdate() + .eq(DistributionParcelListEntity::getStockArticleId, task.getNewStockArticleId()) + .in(DistributionParcelListEntity::getOrderPackageCode, packageCodes) + .set(DistributionParcelListEntity::getStockArticleId, task.getStockArticleId()) + .set(DistributionParcelListEntity::getOrderCode, task.getOrderCode()) + ); + + distributionSplitOrderDetailService.update(Wrappers.lambdaUpdate() + .eq(DistributionSplitOrderDetailEntity::getId, detail.stream().map(DistributionSplitOrderDetailEntity::getId).collect(Collectors.toList())) + .set(DistributionSplitOrderDetailEntity::getState, OrderPackageSplitStateConstant.cancel.getValue()) + ); + + task.setNumber(task.getNumber() - packageCodes.size()); + updateById(task); + + newStockArticle.setTotalNumber(newStockArticle.getTotalNumber() - packageCodes.size()); + newStockArticle.setHandQuantity(newStockArticle.getHandQuantity() - packageCodes.size()); + distributionStockArticleService.updateById(newStockArticle); + + oldStockArticle.setTotalNumber(oldStockArticle.getTotalNumber() + newStockArticle.getTotalNumber()); + oldStockArticle.setHandQuantity(oldStockArticle.getHandQuantity() + newStockArticle.getHandQuantity()); + distributionStockArticleService.updateById(oldStockArticle); + + distributionSplitOrderLogService.record(task, String.format("取消包件:%s", String.join(",", packageCodes)), AuthUtil.getUser().getUserName()); + + return R.success("success"); + } catch (Exception e) { + log.error("拆单任务不存在", e); + return R.fail("系统错误"); + } } @Override @@ -75,4 +447,14 @@ public class DistributionSplitOrderTaskServiceImpl extends BaseServiceImpl getTaskDetail(Long taskId) { return null; } + + + private String getNewOrderCode(String oldOrderCode, Long oldStockArticleId) { + + long count = count(Wrappers.lambdaQuery().eq( + DistributionSplitOrderTaskEntity::getStockArticleId, oldStockArticleId + )); + + return oldOrderCode + "-拆" + (count + 1); + } }