|
@@ -2,7 +2,6 @@ package com.fjhx.sale.flow;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
|
|
-import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
@@ -15,6 +14,9 @@ import com.fjhx.file.utils.ObsFileUtil;
|
|
|
import com.fjhx.flow.core.FlowDelegate;
|
|
|
import com.fjhx.purchase.entity.purchase.enums.PurchaseDataResourceEnum;
|
|
|
import com.fjhx.purchase.entity.purchase.enums.PurchaseStatusEnum;
|
|
|
+import com.fjhx.purchase.entity.subscribe.enums.SubscribeDetailStatusEnum;
|
|
|
+import com.fjhx.purchase.entity.subscribe.po.SubscribeDetail;
|
|
|
+import com.fjhx.purchase.service.subscribe.SubscribeDetailService;
|
|
|
import com.fjhx.sale.entity.contract.po.ContractProduct;
|
|
|
import com.fjhx.sale.entity.purchase.dto.EhsdPurchaseDto;
|
|
|
import com.fjhx.sale.entity.purchase.po.*;
|
|
@@ -31,6 +33,7 @@ import java.math.BigDecimal;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* EHSD采购流程
|
|
@@ -65,6 +68,8 @@ public class EhsdPurchaseFlow extends FlowDelegate {
|
|
|
|
|
|
@Autowired
|
|
|
private CodingRuleService codingRuleService;
|
|
|
+ @Autowired
|
|
|
+ private SubscribeDetailService subscribeDetailService;
|
|
|
|
|
|
@Override
|
|
|
public String getFlowKey() {
|
|
@@ -73,12 +78,12 @@ public class EhsdPurchaseFlow extends FlowDelegate {
|
|
|
|
|
|
/**
|
|
|
* 发起流程
|
|
|
- * @param flowId 流程ID
|
|
|
+ *
|
|
|
+ * @param flowId 流程ID
|
|
|
* @param submitData 采购数据
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- @DSTransactional
|
|
|
public Long start(Long flowId, JSONObject submitData) {
|
|
|
EhsdPurchaseDto purchase = submitData.toJavaObject(EhsdPurchaseDto.class);
|
|
|
//赋值城市省份信息
|
|
@@ -86,38 +91,56 @@ public class EhsdPurchaseFlow extends FlowDelegate {
|
|
|
purchase.setSellCityId(purchase.getCityId());
|
|
|
purchase.setSellCountryId(purchase.getCountryId());
|
|
|
purchase.setSellProvinceId(purchase.getProvinceId());
|
|
|
- purchase.setCode(codingRuleService.createCode(CodingRuleEnum.EHSD_PURCHASE.getKey(),null));
|
|
|
+ purchase.setCode(codingRuleService.createCode(CodingRuleEnum.EHSD_PURCHASE.getKey(), null));
|
|
|
// purchase.setCode(CodeEnum.PURCHASE.getCode());
|
|
|
purchase.setStatus(PurchaseStatusEnum.UNDER_REVIEW.getKey());
|
|
|
purchase.setUserName(SecurityUtils.getUsername());
|
|
|
+ purchase.setFlowId(flowId);
|
|
|
purchaseService.save(purchase);
|
|
|
List<EhsdPurchaseProduct> purchaseProductList = purchase.getPurchaseProductList();
|
|
|
List<EhsdPurchaseProject> purchaseProjectList = purchase.getPurchaseProjectList();
|
|
|
List<EhsdPurchaseArrival> purchaseArrivalList = purchase.getPurchaseArrivalList();
|
|
|
List<EhsdPurchaseProductMountings> addMountingsList = new ArrayList<>();
|
|
|
- if(CollectionUtils.isNotEmpty(purchaseProductList)){
|
|
|
- for(EhsdPurchaseProduct s : purchaseProductList){//产品
|
|
|
+ if (CollectionUtils.isNotEmpty(purchaseProductList)) {
|
|
|
+ for (EhsdPurchaseProduct s : purchaseProductList) {//产品
|
|
|
long id = IdWorker.getId();
|
|
|
s.setId(id);
|
|
|
s.setPurchaseId(purchase.getId());
|
|
|
List<EhsdPurchaseProductMountings> purchaseProductMountingsList = s.getPurchaseProductMountingsList();
|
|
|
- if(CollectionUtils.isNotEmpty(purchaseProductMountingsList)){//产品配件
|
|
|
+ if (CollectionUtils.isNotEmpty(purchaseProductMountingsList)) {//产品配件
|
|
|
purchaseProductMountingsList.forEach(obj -> obj.setPurchaseProductId(id));
|
|
|
addMountingsList.addAll(purchaseProductMountingsList);
|
|
|
}
|
|
|
- ObsFileUtil.saveFile(s.getFileList(),s.getId());
|
|
|
+ ObsFileUtil.saveFile(s.getFileList(), s.getId());
|
|
|
+
|
|
|
+ if ("0".equals(purchase.getDataResource())) {
|
|
|
+ //计算采购数量是否大于申购数量
|
|
|
+ List<EhsdPurchaseProduct> purchaseDetailList1 = purchaseProductService.list(q -> q.eq(EhsdPurchaseProduct::getSubscribeDetailId,
|
|
|
+ s.getSubscribeDetailId()));
|
|
|
+ //求和
|
|
|
+ BigDecimal purchaseCount = purchaseDetailList1.stream()
|
|
|
+ .map(EhsdPurchaseProduct::getQuantity)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ //计算历史采购数量+本次采购数量之和
|
|
|
+ BigDecimal count = purchaseCount.add(s.getQuantity());
|
|
|
+ //判断采购数量是否大于申购数量
|
|
|
+ SubscribeDetail subscribeDetail = subscribeDetailService.getById(s.getSubscribeDetailId());
|
|
|
+ if (count.compareTo(subscribeDetail.getCount()) > 0) {
|
|
|
+ throw new ServiceException("采购数量不能大于申购数量");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
purchaseProductService.saveBatch(purchaseProductList);
|
|
|
purchaseProductMountingsService.saveBatch(addMountingsList);
|
|
|
}
|
|
|
- if(CollectionUtils.isNotEmpty(purchaseProjectList)){//到货
|
|
|
- for(EhsdPurchaseProject s : purchaseProjectList){
|
|
|
+ if (CollectionUtils.isNotEmpty(purchaseProjectList)) {//到货
|
|
|
+ for (EhsdPurchaseProject s : purchaseProjectList) {
|
|
|
s.setPurchaseId(purchase.getId());
|
|
|
}
|
|
|
purchaseProjectService.saveBatch(purchaseProjectList);
|
|
|
}
|
|
|
- if(CollectionUtils.isNotEmpty(purchaseArrivalList)){//收费项目
|
|
|
- for(EhsdPurchaseArrival s : purchaseArrivalList){
|
|
|
+ if (CollectionUtils.isNotEmpty(purchaseArrivalList)) {//收费项目
|
|
|
+ for (EhsdPurchaseArrival s : purchaseArrivalList) {
|
|
|
s.setPurchaseId(purchase.getId());
|
|
|
}
|
|
|
purchaseArrivalService.saveBatch(purchaseArrivalList);
|
|
@@ -127,54 +150,78 @@ public class EhsdPurchaseFlow extends FlowDelegate {
|
|
|
|
|
|
/**
|
|
|
* 结束流程
|
|
|
- * @param flowId 流程ID
|
|
|
+ *
|
|
|
+ * @param flowId 流程ID
|
|
|
* @param businessId 业务ID
|
|
|
* @param submitData 数据
|
|
|
*/
|
|
|
@Override
|
|
|
- @DSTransactional
|
|
|
public void end(Long flowId, Long businessId, JSONObject submitData) {
|
|
|
//通过业务ID查询采购数据
|
|
|
EhsdPurchase purchase = purchaseService.getById(businessId);
|
|
|
- if(ObjectUtils.isEmpty(purchase)){
|
|
|
+ if (ObjectUtils.isEmpty(purchase)) {
|
|
|
throw new ServiceException("采购单不存在,或已被删除");
|
|
|
}
|
|
|
//查询采购产品
|
|
|
- List<EhsdPurchaseProduct> purchaseProductList = purchaseProductService.list(Wrappers.<EhsdPurchaseProduct>query().lambda().eq(EhsdPurchaseProduct::getPurchaseId,businessId));
|
|
|
+ List<EhsdPurchaseProduct> purchaseProductList = purchaseProductService.list(Wrappers.<EhsdPurchaseProduct>query().lambda().eq(EhsdPurchaseProduct::getPurchaseId, businessId));
|
|
|
List<ContractProduct> upContractProduct = new ArrayList<>();
|
|
|
List<SampleProduct> upSampleProduct = new ArrayList<>();
|
|
|
- for(EhsdPurchaseProduct p:purchaseProductList){
|
|
|
- if(ObjectUtils.isNotEmpty(p.getDataResourceId())&&
|
|
|
- p.getDataResource()== PurchaseDataResourceEnum.DATA_RESOURCE_1.getKey()){//如果采购的是外销合同
|
|
|
+ for (EhsdPurchaseProduct p : purchaseProductList) {
|
|
|
+ if (ObjectUtils.isNotEmpty(p.getDataResourceId()) &&
|
|
|
+ p.getDataResource() == PurchaseDataResourceEnum.DATA_RESOURCE_1.getKey()) {//如果采购的是外销合同
|
|
|
ContractProduct contractProduct = contractProductService.getById(p.getDataResourceId());
|
|
|
BigDecimal expendQuantity = contractProduct.getExpendQuantity().subtract(p.getQuantity());
|
|
|
- if(expendQuantity.compareTo(BigDecimal.ZERO)< 0){//小于0不让继续执行
|
|
|
+ if (expendQuantity.compareTo(BigDecimal.ZERO) < 0) {//小于0不让继续执行
|
|
|
throw new ServiceException("采购数量不得大于合同剩余采购数量,请驳回审批修改");
|
|
|
}
|
|
|
contractProduct.setExpendQuantity(expendQuantity);
|
|
|
upContractProduct.add(contractProduct);
|
|
|
}
|
|
|
- if(ObjectUtils.isNotEmpty(p.getDataResourceId())&&
|
|
|
- p.getDataResource()== PurchaseDataResourceEnum.DATA_RESOURCE_2.getKey()){//如果采购的是样品单
|
|
|
+ if (ObjectUtils.isNotEmpty(p.getDataResourceId()) &&
|
|
|
+ p.getDataResource() == PurchaseDataResourceEnum.DATA_RESOURCE_2.getKey()) {//如果采购的是样品单
|
|
|
SampleProduct sampleProduct = sampleProductService.getById(p.getDataResourceId());
|
|
|
BigDecimal expendQuantity = sampleProduct.getExpendQuantity().subtract(p.getQuantity());
|
|
|
- if(expendQuantity.compareTo(BigDecimal.ZERO)< 0){//小于0不让继续执行
|
|
|
+ if (expendQuantity.compareTo(BigDecimal.ZERO) < 0) {//小于0不让继续执行
|
|
|
throw new ServiceException("采购数量不得大于合同剩余采购数量,请驳回审批修改");
|
|
|
}
|
|
|
sampleProduct.setExpendQuantity(expendQuantity);
|
|
|
upSampleProduct.add(sampleProduct);
|
|
|
}
|
|
|
}
|
|
|
- if(CollectionUtils.isNotEmpty(upContractProduct)){//扣减销售合同数量
|
|
|
+ if (CollectionUtils.isNotEmpty(upContractProduct)) {//扣减销售合同数量
|
|
|
contractProductService.updateBatchById(upContractProduct);
|
|
|
}
|
|
|
- if(CollectionUtils.isNotEmpty(upSampleProduct)){//扣减样品单数量
|
|
|
+ if (CollectionUtils.isNotEmpty(upSampleProduct)) {//扣减样品单数量
|
|
|
sampleProductService.updateBatchById(upSampleProduct);
|
|
|
}
|
|
|
//修改采购状态为审批通过
|
|
|
purchase.setStatus(PurchaseStatusEnum.PASS.getKey());
|
|
|
purchase.setApprovedDate(new Date());
|
|
|
purchaseService.updateById(purchase);
|
|
|
+
|
|
|
+ if ("0".equals(purchase.getDataResource())) {
|
|
|
+ //修改申购明细状态
|
|
|
+ List<EhsdPurchaseProduct> purchaseDetailList = purchaseProductService.list(q -> q.eq(EhsdPurchaseProduct::getProductId, businessId));
|
|
|
+ List<Long> subscribeDetailIds = purchaseDetailList.stream().map(EhsdPurchaseProduct::getSubscribeDetailId).collect(Collectors.toList());
|
|
|
+ List<SubscribeDetail> subscribeDetails = subscribeDetailService.listByIds(subscribeDetailIds);
|
|
|
+ for (SubscribeDetail subscribeDetail : subscribeDetails) {
|
|
|
+ //获取申购明细下的所有采购记录 计算已采购数
|
|
|
+ List<EhsdPurchaseProduct> purchaseDetails = purchaseProductService.list(q -> q.eq(EhsdPurchaseProduct::getSubscribeDetailId,
|
|
|
+ subscribeDetail.getId()).eq(EhsdPurchaseProduct::getProductId, subscribeDetail.getBussinessId()));
|
|
|
+ BigDecimal count = purchaseDetails.stream()
|
|
|
+ .map(EhsdPurchaseProduct::getQuantity)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ if (count.compareTo(subscribeDetail.getCount()) >= 0) {
|
|
|
+ //修改为已采购
|
|
|
+ subscribeDetail.setStatus(SubscribeDetailStatusEnum.PURCHASED.getKey());
|
|
|
+ } else {
|
|
|
+ //修改为部分采购
|
|
|
+ subscribeDetail.setStatus(SubscribeDetailStatusEnum.LITT_PAID_AMOUNT.getKey());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ subscribeDetailService.updateBatchById(subscribeDetails);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
}
|