Browse Source

Merge branch 'saas' into master_saas

yonge 3 years ago
parent
commit
be6caafbc5

+ 1 - 1
mec-biz/src/main/java/com/ym/mec/biz/dal/dao/GoodsDao.java

@@ -59,7 +59,7 @@ public interface GoodsDao extends BaseDAO<Integer, Goods> {
      * @author Joburgess
      * @date 2020.09.28
      */
-    void batchUpdate(@Param("goodsList") List<Goods> goodsList);
+    int batchUpdate(@Param("goodsList") List<Goods> goodsList);
 
     /**
      * 获取商品列表

+ 10 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/entity/StudentPaymentOrderDetail.java

@@ -64,6 +64,8 @@ public class StudentPaymentOrderDetail extends BaseEntity {
 	private String incomeItem;
 	
 	private BigDecimal income;
+	
+	private String minuendStockGoodsIdList;
 
 	@ApiModelProperty(value = "子商品列表",required = false)
 	private List<Goods> childGoodsList;
@@ -228,4 +230,12 @@ public class StudentPaymentOrderDetail extends BaseEntity {
 	public void setIncome(BigDecimal income) {
 		this.income = income;
 	}
+
+	public String getMinuendStockGoodsIdList() {
+		return minuendStockGoodsIdList;
+	}
+
+	public void setMinuendStockGoodsIdList(String minuendStockGoodsIdList) {
+		this.minuendStockGoodsIdList = minuendStockGoodsIdList;
+	}
 }

+ 2 - 1
mec-biz/src/main/java/com/ym/mec/biz/dal/enums/OrderDetailTypeEnum.java

@@ -29,7 +29,8 @@ public enum OrderDetailTypeEnum implements BaseEnum<String, OrderDetailTypeEnum>
     MAINTENANCE("MAINTENANCE","乐保服务"),
     CLOUD_TEACHER("CLOUD_TEACHER","云教练"),
     CLOUD_TEACHER_PLUS("CLOUD_TEACHER_PLUS","云教练"),
-    THEORY_COURSE("THEORY_COURSE", "乐理课");
+    THEORY_COURSE("THEORY_COURSE", "乐理课"),
+    REPAIR("REPAIR", "乐器维修");
 
     private String code;
 

+ 8 - 7
mec-biz/src/main/java/com/ym/mec/biz/service/GoodsService.java

@@ -1,20 +1,17 @@
 package com.ym.mec.biz.service;
 
+import java.util.List;
+
+import org.springframework.web.multipart.MultipartFile;
+
 import com.ym.mec.biz.dal.dto.GoodsSellDto;
 import com.ym.mec.biz.dal.entity.Goods;
 import com.ym.mec.biz.dal.entity.GoodsProcurement;
 import com.ym.mec.biz.dal.entity.SellOrder;
 import com.ym.mec.biz.dal.enums.AccountType;
 import com.ym.mec.biz.dal.page.GoodsQuery;
-import com.ym.mec.biz.dal.page.GoodsQueryInfo;
-import com.ym.mec.common.page.PageInfo;
 import com.ym.mec.common.service.BaseService;
 
-import org.apache.ibatis.annotations.Param;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.util.List;
-
 public interface GoodsService extends BaseService<Integer, Goods> {
 
     void addGoods(Goods goods, Integer operatorId);
@@ -126,4 +123,8 @@ public interface GoodsService extends BaseService<Integer, Goods> {
      * @return
      */
     Goods getDetail(Integer goodsId);
+    
+    List<Goods> getGoodsWithLocked(String goodsIds);
+    
+    int batchUpdate(List<Goods> goodsList);
 }

+ 12 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/impl/GoodsServiceImpl.java

@@ -20,6 +20,7 @@ import com.ym.mec.common.tenant.TenantContextHolder;
 import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
 import com.ym.mec.util.excel.POIUtil;
 import com.ym.mec.util.ini.IniFileUtil;
+
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.ss.usermodel.PictureData;
 import org.slf4j.Logger;
@@ -634,4 +635,15 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 		}
 		return goods;
 	}
+
+	@Override
+	public List<Goods> getGoodsWithLocked(String goodsIds) {
+		List<Integer> list = Arrays.stream(goodsIds.split(",")).map(Integer :: valueOf).collect(Collectors.toList());
+		return goodsDao.lockGoods(list);
+	}
+
+	@Override
+	public int batchUpdate(List<Goods> goodsList) {
+		return goodsDao.batchUpdate(goodsList);
+	}
 }

+ 31 - 6
mec-biz/src/main/java/com/ym/mec/biz/service/impl/PayServiceImpl.java

@@ -1,6 +1,7 @@
 package com.ym.mec.biz.service.impl;
 
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Date;
@@ -10,6 +11,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
 import org.apache.commons.lang3.StringUtils;
@@ -187,6 +189,7 @@ public class PayServiceImpl implements PayService {
         	if(sysPaymentConfig != null){
 
     			List<StudentPaymentOrderDetail> batchUpdateList = new ArrayList<StudentPaymentOrderDetail>();
+    			List<Goods> batchUpdateGoodsList = new ArrayList<Goods>();
         		
         		// 如果买了商品或组合商品,商品有内部库存,则将钱转入指定账户已云教练的方式作为收费项目(杭州、齐齐哈尔除外)
         		// 1.查询商品订单明细
@@ -195,11 +198,12 @@ public class PayServiceImpl implements PayService {
         			
         			String goodIds = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNotBlank(t.getGoodsIdList())).map(StudentPaymentOrderDetail :: getGoodsIdList).collect(Collectors.joining(","));
         			if(StringUtils.isNotBlank(goodIds)){
-        				List<Goods> goodsList = goodsService.findGoodsByIds(goodIds);
+        				List<Goods> goodsList = goodsService.getGoodsWithLocked(goodIds);
         				
         				Map<Integer,Goods> goodsMap = goodsList.stream().collect(Collectors.toMap(Goods :: getId, t -> t));
         				Goods goods = null;
         				BigDecimal totalAmout = studentPaymentOrder.getExpectAmount();
+        				List<Integer> minuendStockGoodsIdList = null;
         				
         				for(StudentPaymentOrderDetail spod : studentPaymentOrderDetailList){
         					
@@ -214,6 +218,8 @@ public class PayServiceImpl implements PayService {
         					
         					BigDecimal groupPurchaseAmount = BigDecimal.ZERO;
         					
+        					minuendStockGoodsIdList = new ArrayList<Integer>();
+        					
         					for(String goodsIdStr : spod.getGoodsIdList().split(",")){
         						if(StringUtils.isBlank(goodsIdStr)){
         							continue;
@@ -223,13 +229,18 @@ public class PayServiceImpl implements PayService {
         						if(goods != null){
         							// 是否是组合商品
         							if(StringUtils.isNotBlank(goods.getComplementGoodsIdList())){
-        								goodsList = goodsService.findGoodsByIds(goods.getComplementGoodsIdList());
+        								goodsList = goodsService.getGoodsWithLocked(goods.getComplementGoodsIdList());
         								totalGroupPurchaseAmount =  totalGroupPurchaseAmount.add(goodsList.stream().map(Goods :: getGroupPurchasePrice).reduce(BigDecimal.ZERO,BigDecimal :: add));
         								
         								for(Goods subGoods : goodsList){
         									//判断是否有内部库存
         									if(subGoods.getStockCount() > 0){
         										groupPurchaseAmount = groupPurchaseAmount.add(subGoods.getGroupPurchasePrice());
+        										subGoods.setStockCount(new AtomicInteger(subGoods.getStockCount()).decrementAndGet());
+        										subGoods.setUpdateTime(date);
+        										batchUpdateGoodsList.add(subGoods);
+        										
+        										minuendStockGoodsIdList.add(subGoods.getId());
         									}
         								}
         							}else{
@@ -237,6 +248,11 @@ public class PayServiceImpl implements PayService {
         								//判断是否有内部库存
         								if(goods.getStockCount() > 0){
     										groupPurchaseAmount = groupPurchaseAmount.add(goods.getGroupPurchasePrice());
+    										goods.setStockCount(new AtomicInteger(goods.getStockCount()).decrementAndGet());
+    										goods.setUpdateTime(date);
+    										batchUpdateGoodsList.add(goods);
+    										
+    										minuendStockGoodsIdList.add(goods.getId());
         								}
         							}
         						}
@@ -245,14 +261,19 @@ public class PayServiceImpl implements PayService {
 							if (groupPurchaseAmount.doubleValue() > 0) {
 	        					// 3.是否使用余额
 								if (balanceAmount.doubleValue() > 0) {
-									tempBalance = balanceAmount.multiply(spod.getPrice()).divide(totalAmout).multiply(groupPurchaseAmount)
-											.divide(totalGroupPurchaseAmount);
+									/*tempBalance = balanceAmount.multiply(spod.getPrice()).divide(totalAmout).multiply(groupPurchaseAmount)
+											.divide(totalGroupPurchaseAmount);*/
+									tempBalance = balanceAmount.multiply(spod.getPrice()).multiply(groupPurchaseAmount)
+											.divide(totalGroupPurchaseAmount.multiply(totalAmout), RoundingMode.HALF_UP);
 									subBalance = subBalance.add(tempBalance);
 								}
-								tempCashAmount = spod.getPrice().subtract(tempBalance).multiply(groupPurchaseAmount).divide(totalGroupPurchaseAmount);
+								tempCashAmount = spod.getPrice().subtract(tempBalance).multiply(groupPurchaseAmount).divide(totalGroupPurchaseAmount, RoundingMode.HALF_UP);
 								subCashAmount = subCashAmount.add(tempCashAmount);
 								spod.setIncomeItem(OrderDetailTypeEnum.CLOUD_TEACHER.name());
 								spod.setIncome(tempCashAmount);
+								if(minuendStockGoodsIdList.size() > 0){
+									spod.setMinuendStockGoodsIdList(minuendStockGoodsIdList.stream().map(t -> t.toString()).collect(Collectors.joining(",")));
+								}
 								batchUpdateList.add(spod);
 							}
         				}
@@ -263,6 +284,10 @@ public class PayServiceImpl implements PayService {
         			studentPaymentOrderDetailService.batchUpdate(batchUpdateList);
         		}
         		
+        		if(batchUpdateGoodsList.size() > 0){
+        			goodsService.batchUpdate(batchUpdateGoodsList);
+        		}
+        		
         		if(subCashAmount.doubleValue() > 0){
         			StudentPaymentRouteOrder studentPaymentRouteOrder = new StudentPaymentRouteOrder();
     	            studentPaymentRouteOrder.setOrderNo(orderNo);
@@ -272,7 +297,7 @@ public class PayServiceImpl implements PayService {
     	            studentPaymentRouteOrder.setRouteBalanceAmount(subBalance);
     	            studentPaymentRouteOrder.setMerNo(goodsSellReceiptMerNo);
     	            studentPaymentRouteOrder.setSaleAmount(BigDecimal.ZERO);
-    	            studentPaymentRouteOrder.setServiceAmount(amount);
+    	            studentPaymentRouteOrder.setServiceAmount(subCashAmount);
     	            studentPaymentRouteOrder.setCreateTime(date);
     	            studentPaymentRouteOrder.setUpdateTime(date);
     	            studentPaymentRouteOrderDao.insert(studentPaymentRouteOrder);

+ 22 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/impl/StudentRegistrationServiceImpl.java

@@ -24,6 +24,7 @@ import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
 import com.ym.mec.util.collection.MapUtil;
 import com.ym.mec.util.date.DateUtil;
 import com.ym.mec.util.http.HttpUtil;
+
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
@@ -35,9 +36,11 @@ import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
+
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.util.*;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
 import static com.ym.mec.biz.dal.entity.MusicGroupStudentFee.PaymentStatus.NON_PAYMENT;
@@ -112,6 +115,8 @@ public class StudentRegistrationServiceImpl extends BaseServiceImpl<Long, Studen
     @Autowired
     private GoodsDao goodsDao;
     @Autowired
+    private GoodsService goodsService;
+    @Autowired
     private MusicGroupPaymentCalenderDetailDao musicGroupPaymentCalenderDetailDao;
     @Autowired
     private MusicGroupPaymentCalenderDao musicGroupPaymentCalenderDao;
@@ -1251,6 +1256,23 @@ public class StudentRegistrationServiceImpl extends BaseServiceImpl<Long, Studen
             studentRegistrationDao.update(studentRegistration);
             //减去缴费人数(器乐收费,0元时不减缴费人数)
             studentPaymentOrderService.cutSubjectPlan(studentPaymentOrder);
+            
+			List<Goods> batchUpdateGoodsList = new ArrayList<Goods>();
+            //增加商品库存
+            List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = studentPaymentOrderDetailService.queryOrderDetail(studentPaymentOrder.getId());
+            String goodsIds = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNotBlank(t.getMinuendStockGoodsIdList())).map(t -> t.getMinuendStockGoodsIdList()).collect(Collectors.joining(","));
+            if(StringUtils.isNotBlank(goodsIds)){
+            	List<Goods> goodsList = goodsService.getGoodsWithLocked(goodsIds);
+            	for(Goods subGoods : goodsList){
+					subGoods.setStockCount(new AtomicInteger(subGoods.getStockCount()).incrementAndGet());
+					subGoods.setUpdateTime(nowDate);
+					batchUpdateGoodsList.add(subGoods);
+            	}
+            }
+            
+            if(batchUpdateGoodsList.size() > 0){
+            	goodsService.batchUpdate(batchUpdateGoodsList);
+            }
 
             //退还优惠券和余额
             studentPaymentOrderService.returnBalanceAndCoupon(studentPaymentOrder,"报名缴费失败");

+ 216 - 22
mec-biz/src/main/java/com/ym/mec/biz/service/impl/StudentRepairServiceImpl.java

@@ -1,18 +1,86 @@
 package com.ym.mec.biz.service.impl;
 
+import static com.ym.mec.biz.dal.enums.CouponDetailTypeEnum.ACCESSORIES;
+import static com.ym.mec.biz.dal.enums.CouponDetailTypeEnum.MUSICAL;
+import static com.ym.mec.biz.dal.enums.DealStatusEnum.CLOSE;
+import static com.ym.mec.biz.dal.enums.DealStatusEnum.ING;
+import static com.ym.mec.biz.dal.enums.DealStatusEnum.SUCCESS;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
+
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Isolation;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
+
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.ym.mec.auth.api.client.SysUserFeignService;
 import com.ym.mec.auth.api.entity.SysUser;
-import com.ym.mec.biz.dal.dao.*;
+import com.ym.mec.biz.dal.dao.MusicGroupDao;
+import com.ym.mec.biz.dal.dao.StudentDao;
+import com.ym.mec.biz.dal.dao.StudentGoodsSellDao;
+import com.ym.mec.biz.dal.dao.StudentRepairDao;
+import com.ym.mec.biz.dal.dao.SysConfigDao;
 import com.ym.mec.biz.dal.dto.BasicUserDto;
 import com.ym.mec.biz.dal.dto.GoodsSellDto;
 import com.ym.mec.biz.dal.dto.RepairGoodsDto;
-import com.ym.mec.biz.dal.entity.*;
-import com.ym.mec.biz.dal.enums.*;
+import com.ym.mec.biz.dal.entity.Goods;
+import com.ym.mec.biz.dal.entity.MusicGroup;
+import com.ym.mec.biz.dal.entity.SellOrder;
+import com.ym.mec.biz.dal.entity.StudentGoodsSell;
+import com.ym.mec.biz.dal.entity.StudentInstrument;
+import com.ym.mec.biz.dal.entity.StudentPaymentOrder;
+import com.ym.mec.biz.dal.entity.StudentPaymentOrderDetail;
+import com.ym.mec.biz.dal.entity.StudentRepair;
+import com.ym.mec.biz.dal.entity.SysUserCashAccount;
+import com.ym.mec.biz.dal.entity.SysUserCashAccountDetail;
+import com.ym.mec.biz.dal.enums.AccountType;
+import com.ym.mec.biz.dal.enums.CouponDetailTypeEnum;
+import com.ym.mec.biz.dal.enums.DealStatusEnum;
+import com.ym.mec.biz.dal.enums.GoodsType;
+import com.ym.mec.biz.dal.enums.GroupType;
+import com.ym.mec.biz.dal.enums.MessageTypeEnum;
+import com.ym.mec.biz.dal.enums.OrderDetailTypeEnum;
+import com.ym.mec.biz.dal.enums.OrderTypeEnum;
+import com.ym.mec.biz.dal.enums.PayStatus;
+import com.ym.mec.biz.dal.enums.PaymentChannelEnum;
+import com.ym.mec.biz.dal.enums.PlatformCashAccountDetailTypeEnum;
+import com.ym.mec.biz.dal.enums.SellTypeEnum;
 import com.ym.mec.biz.dal.page.RepairStudentQueryInfo;
-import com.ym.mec.biz.service.*;
+import com.ym.mec.biz.service.ContractService;
+import com.ym.mec.biz.service.GoodsService;
+import com.ym.mec.biz.service.PayService;
+import com.ym.mec.biz.service.SellOrderService;
+import com.ym.mec.biz.service.StudentGoodsSellService;
+import com.ym.mec.biz.service.StudentInstrumentService;
+import com.ym.mec.biz.service.StudentPaymentOrderDetailService;
+import com.ym.mec.biz.service.StudentPaymentOrderService;
+import com.ym.mec.biz.service.StudentPaymentRouteOrderService;
+import com.ym.mec.biz.service.StudentRepairService;
+import com.ym.mec.biz.service.SysConfigService;
+import com.ym.mec.biz.service.SysCouponCodeService;
+import com.ym.mec.biz.service.SysMessageService;
+import com.ym.mec.biz.service.SysPaymentConfigService;
+import com.ym.mec.biz.service.SysUserCashAccountDetailService;
+import com.ym.mec.biz.service.SysUserCashAccountService;
 import com.ym.mec.common.dal.BaseDAO;
 import com.ym.mec.common.exception.BizException;
 import com.ym.mec.common.page.PageInfo;
@@ -21,22 +89,6 @@ import com.ym.mec.common.service.IdGeneratorService;
 import com.ym.mec.common.service.impl.BaseServiceImpl;
 import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
 import com.ym.mec.util.collection.MapUtil;
-import org.apache.commons.collections.CollectionUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Isolation;
-import org.springframework.transaction.annotation.Propagation;
-import org.springframework.transaction.annotation.Transactional;
-
-import java.math.BigDecimal;
-import java.util.*;
-import java.util.stream.Collectors;
-
-import static com.ym.mec.biz.dal.enums.CouponDetailTypeEnum.*;
-import static com.ym.mec.biz.dal.enums.DealStatusEnum.*;
 
 @Service
 public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRepair> implements StudentRepairService {
@@ -54,6 +106,8 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
     @Autowired
     private StudentPaymentOrderService studentPaymentOrderService;
     @Autowired
+    private StudentPaymentOrderDetailService studentPaymentOrderDetailService;
+    @Autowired
     private SysPaymentConfigService sysPaymentConfigService;
     @Autowired
     private MusicGroupDao musicGroupDao;
@@ -252,6 +306,51 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         studentPaymentOrder.setRoutingOrganId(student.getOrganId());
         studentPaymentOrder.setTenantId(student.getTenantId());
         studentPaymentOrderService.insert(studentPaymentOrder);
+        
+        Date date = new Date();
+        //添加订单详情
+        List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = new ArrayList<StudentPaymentOrderDetail>();
+        // 添加studentPaymentOrderDetail
+        StudentPaymentOrderDetail studentPaymentOrderDetail = null;
+
+        BigDecimal totalPrice = BigDecimal.ZERO;
+        
+        if(goodsSellDtos != null){
+        	String goodsIdsStr = goodsSellDtos.stream().map(t -> t.getGoodsId().toString()).collect(Collectors.joining(","));
+        	List<Goods> goodsList = goodsService.findGoodsByIds(goodsIdsStr);
+        	BigDecimal goodsPrice = goodsList.stream().map(t -> t.getGroupPurchasePrice()).reduce(BigDecimal.ZERO, BigDecimal::add);
+        	for(Goods g : goodsList){
+        		studentPaymentOrderDetail = new StudentPaymentOrderDetail();
+                studentPaymentOrderDetail.setCreateTime(date);
+                if(g.getType() == GoodsType.INSTRUMENT){
+                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.MUSICAL);
+                }else if(g.getType() == GoodsType.ACCESSORIES){
+                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.ACCESSORIES);
+                }else if(g.getType() == GoodsType.TEACHING || g.getType() == GoodsType.STAFF){
+                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.TEACHING);
+                }else{
+                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.OTHER);
+                }
+                studentPaymentOrderDetail.setGoodsIdList(g.getId() + "");
+                
+                BigDecimal tempPrice = studentPaymentOrder.getExpectAmount().multiply(g.getGroupPurchasePrice()).divide(goodsPrice, RoundingMode.UP);
+                
+                if(totalPrice.add(tempPrice).compareTo(studentPaymentOrder.getExpectAmount()) > 0){
+                	studentPaymentOrderDetail.setPrice(studentPaymentOrder.getExpectAmount().subtract(totalPrice));
+                }else{
+                	studentPaymentOrderDetail.setPrice(tempPrice);
+                }
+                studentPaymentOrderDetail.setUpdateTime(date);
+                studentPaymentOrderDetail.setPaymentOrderId(studentPaymentOrder.getId());
+                
+                totalPrice = totalPrice.add(studentPaymentOrderDetail.getPrice());
+                studentPaymentOrderDetailList.add(studentPaymentOrderDetail);
+        	}
+        }
+        
+        if(studentPaymentOrderDetailList.size() > 0){
+        	studentPaymentOrderDetailService.batchAdd(studentPaymentOrderDetailList);
+        }
 
         studentPaymentOrder.setVersion(0);
         BigDecimal balance = BigDecimal.ZERO;
@@ -455,17 +554,23 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         repairInfo.setPayStatus(0);
         repairInfo.setCreateTime(date);
         repairInfo.setUpdateTime(date);
+        
+        List<RepairGoodsDto> repairGoodsDtos = null;
+        String goodIdList = null;
+        		
         String goodsJson = repairInfo.getGoodsJson();
         if (StringUtils.isNotEmpty(goodsJson)) {
-            List<RepairGoodsDto> repairGoodsDtos = JSONObject.parseArray(goodsJson, RepairGoodsDto.class);
+            repairGoodsDtos = JSONObject.parseArray(goodsJson, RepairGoodsDto.class);
             List<Integer> goodsIds = repairGoodsDtos.stream().map(e -> e.getId()).collect(Collectors.toList());
             Map<Integer, BigDecimal> map = getMap("goods", "id_", "discount_price_", goodsIds,student.getTenantId(), Integer.class, BigDecimal.class);
             repairGoodsDtos.forEach(e -> {
                 e.setGroupPurchasePrice(map.get(e.getId()));
             });
             repairInfo.setGoodsJson(JSONObject.toJSONString(repairGoodsDtos));
+            goodIdList = repairGoodsDtos.stream().map(t -> t.getId().toString()).collect(Collectors.joining(","));
         }
 
+        BigDecimal goodsPrice = BigDecimal.ZERO;
         if (StringUtils.isNoneBlank(repairInfo.getGoodsJson())) {
             JSONArray goods = JSON.parseArray(repairInfo.getGoodsJson());
             for (Object good : goods) {
@@ -473,6 +578,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
                 BigDecimal groupPurchasePrice = goodObject.getBigDecimal("groupPurchasePrice");
                 if (Objects.nonNull(groupPurchasePrice)) {
                     amount = amount.add(groupPurchasePrice);
+                    goodsPrice = goodsPrice.add(groupPurchasePrice);
                 }
             }
         }
@@ -509,6 +615,58 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 
         studentPaymentOrderService.insert(studentPaymentOrder);
         studentPaymentOrder.setVersion(0);
+  
+        
+        List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = new ArrayList<StudentPaymentOrderDetail>();
+        // 添加studentPaymentOrderDetail
+        StudentPaymentOrderDetail studentPaymentOrderDetail = new StudentPaymentOrderDetail();
+        studentPaymentOrderDetail.setCreateTime(date);
+        studentPaymentOrderDetail.setType(OrderDetailTypeEnum.REPAIR);
+        studentPaymentOrderDetail.setGoodsIdList(null);
+        studentPaymentOrderDetail.setPrice(studentPaymentOrder.getExpectAmount().multiply(repairInfo.getAmount()).divide(goodsPrice.add(repairInfo.getAmount()), RoundingMode.UP));
+        studentPaymentOrderDetail.setUpdateTime(date);
+        studentPaymentOrderDetail.setPaymentOrderId(studentPaymentOrder.getId());
+        
+        studentPaymentOrderDetailList.add(studentPaymentOrderDetail);
+
+        BigDecimal totalPrice = studentPaymentOrderDetail.getPrice();
+        BigDecimal repairPrice = studentPaymentOrderDetail.getPrice();
+        
+        if(StringUtils.isNotBlank(repairInfo.getGoodsJson()) && repairGoodsDtos != null){
+        	String goodsIds = repairGoodsDtos.stream().map(t -> t.getId().toString()).collect(Collectors.joining(","));
+        	List<Goods> goodsList = goodsService.findGoodsByIds(goodsIds);
+        	for(Goods g : goodsList){
+        		studentPaymentOrderDetail = new StudentPaymentOrderDetail();
+                studentPaymentOrderDetail.setCreateTime(date);
+                if(g.getType() == GoodsType.INSTRUMENT){
+                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.MUSICAL);
+                }else if(g.getType() == GoodsType.ACCESSORIES){
+                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.ACCESSORIES);
+                }else if(g.getType() == GoodsType.TEACHING || g.getType() == GoodsType.STAFF){
+                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.TEACHING);
+                }else{
+                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.OTHER);
+                }
+                studentPaymentOrderDetail.setGoodsIdList(g.getId() + "");
+                
+                BigDecimal tempPrice = studentPaymentOrder.getExpectAmount().subtract(repairPrice).multiply(g.getGroupPurchasePrice()).divide(goodsPrice, RoundingMode.UP);
+                
+                if(totalPrice.add(tempPrice).compareTo(studentPaymentOrder.getExpectAmount()) > 0){
+                	studentPaymentOrderDetail.setPrice(studentPaymentOrder.getExpectAmount().subtract(totalPrice));
+                }else{
+                	studentPaymentOrderDetail.setPrice(tempPrice);
+                }
+                studentPaymentOrderDetail.setUpdateTime(date);
+                studentPaymentOrderDetail.setPaymentOrderId(studentPaymentOrder.getId());
+                
+                totalPrice = totalPrice.add(studentPaymentOrderDetail.getPrice());
+                studentPaymentOrderDetailList.add(studentPaymentOrderDetail);
+        	}
+        }
+        
+        if(studentPaymentOrderDetailList.size() > 0){
+        	studentPaymentOrderDetailService.batchAdd(studentPaymentOrderDetailList);
+        }
 
         BigDecimal balance = BigDecimal.ZERO;
         if (repairInfo.getUseBalancePayment() && amount.compareTo(BigDecimal.ZERO) > 0) {
@@ -794,7 +952,25 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
             //购买的商品加入销售列表
             saveSellOrder(studentPaymentOrder.getOrderNo());
         } else if (studentPaymentOrder.getStatus() == DealStatusEnum.CLOSE || studentPaymentOrder.getStatus() == DealStatusEnum.FAILED) {
-            if (studentPaymentOrder.getBalancePaymentAmount() != null && studentPaymentOrder.getBalancePaymentAmount().compareTo(BigDecimal.ZERO) > 0) {
+            
+			List<Goods> batchUpdateGoodsList = new ArrayList<Goods>();
+            //增加商品库存
+            List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = studentPaymentOrderDetailService.queryOrderDetail(studentPaymentOrder.getId());
+            String goodsIds = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNotBlank(t.getMinuendStockGoodsIdList())).map(t -> t.getMinuendStockGoodsIdList()).collect(Collectors.joining(","));
+            if(StringUtils.isNotBlank(goodsIds)){
+            	List<Goods> goodsList = goodsService.getGoodsWithLocked(goodsIds);
+            	for(Goods subGoods : goodsList){
+					subGoods.setStockCount(new AtomicInteger(subGoods.getStockCount()).incrementAndGet());
+					subGoods.setUpdateTime(nowDate);
+					batchUpdateGoodsList.add(subGoods);
+            	}
+            }
+            
+            if(batchUpdateGoodsList.size() > 0){
+            	goodsService.batchUpdate(batchUpdateGoodsList);
+            }
+        	
+        	if (studentPaymentOrder.getBalancePaymentAmount() != null && studentPaymentOrder.getBalancePaymentAmount().compareTo(BigDecimal.ZERO) > 0) {
                 sysUserCashAccountService.updateBalance(studentPaymentOrder.getUserId(), studentPaymentOrder.getBalancePaymentAmount(), PlatformCashAccountDetailTypeEnum.REFUNDS, "乐器购买支付失败");
             }
             sysCouponCodeService.quit(studentPaymentOrder.getCouponCodeId());
@@ -1173,6 +1349,24 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
             if (this.update(repairInfo) <= 0) {
                 throw new BizException("维修单更新失败");
             }
+            
+			List<Goods> batchUpdateGoodsList = new ArrayList<Goods>();
+            //增加商品库存
+            List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = studentPaymentOrderDetailService.queryOrderDetail(studentPaymentOrder.getId());
+            String goodsIds = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNotBlank(t.getMinuendStockGoodsIdList())).map(t -> t.getMinuendStockGoodsIdList()).collect(Collectors.joining(","));
+            if(StringUtils.isNotBlank(goodsIds)){
+            	List<Goods> goodsList = goodsService.getGoodsWithLocked(goodsIds);
+            	for(Goods subGoods : goodsList){
+					subGoods.setStockCount(new AtomicInteger(subGoods.getStockCount()).incrementAndGet());
+					subGoods.setUpdateTime(nowDate);
+					batchUpdateGoodsList.add(subGoods);
+            	}
+            }
+            
+            if(batchUpdateGoodsList.size() > 0){
+            	goodsService.batchUpdate(batchUpdateGoodsList);
+            }
+            
             if (studentPaymentOrder.getBalancePaymentAmount() != null && studentPaymentOrder.getBalancePaymentAmount().compareTo(BigDecimal.ZERO) > 0) {
                 sysUserCashAccountService.updateBalance(studentPaymentOrder.getUserId(), studentPaymentOrder.getBalancePaymentAmount(), PlatformCashAccountDetailTypeEnum.REFUNDS, "乐器维修支付失败");
             }

+ 112 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SubjectChangeServiceImpl.java

@@ -15,6 +15,8 @@ import com.ym.mec.common.tenant.TenantContextHolder;
 import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
 import com.ym.mec.util.collection.MapUtil;
 import com.ym.mec.util.date.DateUtil;
+
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -24,7 +26,9 @@ import org.springframework.transaction.annotation.Isolation;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.*;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
 import static com.ym.mec.biz.dal.enums.DealStatusEnum.SUCCESS;
@@ -54,6 +58,8 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
     @Autowired
     private StudentPaymentOrderDetailDao studentPaymentOrderDetailDao;
     @Autowired
+    private StudentPaymentOrderDetailService studentPaymentOrderDetailService;
+    @Autowired
     private MusicGroupDao musicGroupDao;
     @Autowired
     private SellOrderDao sellOrderDao;
@@ -224,6 +230,66 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
         studentPaymentOrderService.update(studentPaymentOrder);
 
         studentPaymentOrder.setVersion(studentPaymentOrder.getVersion() + 1);
+        
+        //添加订单详情
+        List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = new ArrayList<StudentPaymentOrderDetail>();
+        // 添加studentPaymentOrderDetail
+        StudentPaymentOrderDetail studentPaymentOrderDetail = null;
+
+        BigDecimal totalPrice = BigDecimal.ZERO;
+        
+        List<String> changeAccessoriesList = null;
+        
+        if(StringUtils.isNotBlank(subjectChange.getChangeAccessories())){
+        	changeAccessoriesList = Arrays.asList(subjectChange.getChangeAccessories().split(","));
+        	
+        	List<String> originalAccessories = new ArrayList<String>();
+        	if(StringUtils.isNotBlank(subjectChange.getOriginalAccessories())){
+        		originalAccessories = Arrays.asList(subjectChange.getOriginalAccessories().split(","));
+        	}
+        	changeAccessoriesList.removeAll(originalAccessories);
+        	
+        	if(subjectChange.getChangeMusical() != subjectChange.getOriginalMusical()){
+        		changeAccessoriesList.add(subjectChange.getChangeMusical()+"");
+        	}
+        }
+        
+        if(changeAccessoriesList != null){
+            String goodsIdsStr = changeAccessoriesList.stream().map(t -> t.toString()).collect(Collectors.joining(","));
+        	List<Goods> goodsList = goodsService.findGoodsByIds(goodsIdsStr);
+        	BigDecimal goodsPrice = goodsList.stream().map(t -> t.getGroupPurchasePrice()).reduce(BigDecimal.ZERO, BigDecimal::add);
+        	for(Goods g : goodsList){
+        		studentPaymentOrderDetail = new StudentPaymentOrderDetail();
+                studentPaymentOrderDetail.setCreateTime(date);
+                if(g.getType() == GoodsType.INSTRUMENT){
+                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.MUSICAL);
+                }else if(g.getType() == GoodsType.ACCESSORIES){
+                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.ACCESSORIES);
+                }else if(g.getType() == GoodsType.TEACHING || g.getType() == GoodsType.STAFF){
+                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.TEACHING);
+                }else{
+                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.OTHER);
+                }
+                studentPaymentOrderDetail.setGoodsIdList(g.getId() + "");
+                
+                BigDecimal tempPrice = studentPaymentOrder.getExpectAmount().multiply(g.getGroupPurchasePrice()).divide(goodsPrice, RoundingMode.UP);
+                
+                if(totalPrice.add(tempPrice).compareTo(studentPaymentOrder.getExpectAmount()) > 0){
+                	studentPaymentOrderDetail.setPrice(studentPaymentOrder.getExpectAmount().subtract(totalPrice));
+                }else{
+                	studentPaymentOrderDetail.setPrice(tempPrice);
+                }
+                studentPaymentOrderDetail.setUpdateTime(date);
+                studentPaymentOrderDetail.setPaymentOrderId(studentPaymentOrder.getId());
+                
+                totalPrice = totalPrice.add(studentPaymentOrderDetail.getPrice());
+                studentPaymentOrderDetailList.add(studentPaymentOrderDetail);
+        	}
+            
+            if(studentPaymentOrderDetailList.size() > 0){
+            	studentPaymentOrderDetailService.batchAdd(studentPaymentOrderDetailList);
+            }
+        }
 
         if (amount.compareTo(BigDecimal.ZERO) == 0) {
             studentPaymentRouteOrderService.addRouteOrder(orderNo, subjectChange.getOrganId(), studentPaymentOrder.getExpectAmount());
@@ -697,6 +763,34 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
                 rechargeDetail.setPerAmount(studentPaymentOrder.getPerAmount().negate());
             }
             sysUserCashAccountDetailService.insert(paymentDetail);
+            
+			List<Goods> batchUpdateGoodsList = new ArrayList<Goods>();
+			
+			List<String> changeAccessoriesList = null;
+	        
+	        if(StringUtils.isNotBlank(subjectChange.getOriginalAccessories())){
+	        	changeAccessoriesList = Arrays.asList(subjectChange.getOriginalAccessories().split(","));
+	        	
+	        	List<String> originalAccessories = new ArrayList<String>();
+	        	if(StringUtils.isNotBlank(subjectChange.getChangeAccessories())){
+	        		originalAccessories = Arrays.asList(subjectChange.getChangeAccessories().split(","));
+	        	}
+	        	changeAccessoriesList.removeAll(originalAccessories);
+	        	
+	        	if(subjectChange.getChangeMusical() != subjectChange.getOriginalMusical()){
+	        		changeAccessoriesList.add(subjectChange.getOriginalMusical()+"");
+	        	}
+	        }
+	        String goodsIdsStr = changeAccessoriesList.stream().map(t -> t.toString()).collect(Collectors.joining(","));
+            //增加商品库存
+            if(StringUtils.isNotBlank(goodsIdsStr)){
+            	List<Goods> goodsList = goodsService.getGoodsWithLocked(goodsIdsStr);
+            	for(Goods subGoods : goodsList){
+					subGoods.setStockCount(new AtomicInteger(subGoods.getStockCount()).incrementAndGet());
+					subGoods.setUpdateTime(nowDate);
+					batchUpdateGoodsList.add(subGoods);
+            	}
+            }
         }
 
         if (studentPaymentOrder.getStatus() == DealStatusEnum.CLOSE || studentPaymentOrder.getStatus() == DealStatusEnum.FAILED) {
@@ -704,6 +798,24 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
             if (this.update(subjectChange) <= 0) {
                 throw new BizException("声部更改订单更新失败");
             }
+            
+			List<Goods> batchUpdateGoodsList = new ArrayList<Goods>();
+            //增加商品库存
+            List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = studentPaymentOrderDetailService.queryOrderDetail(studentPaymentOrder.getId());
+            String goodsIds = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNotBlank(t.getMinuendStockGoodsIdList())).map(t -> t.getMinuendStockGoodsIdList()).collect(Collectors.joining(","));
+            if(StringUtils.isNotBlank(goodsIds)){
+            	List<Goods> goodsList = goodsService.getGoodsWithLocked(goodsIds);
+            	for(Goods subGoods : goodsList){
+					subGoods.setStockCount(new AtomicInteger(subGoods.getStockCount()).incrementAndGet());
+					subGoods.setUpdateTime(nowDate);
+					batchUpdateGoodsList.add(subGoods);
+            	}
+            }
+            
+            if(batchUpdateGoodsList.size() > 0){
+            	goodsService.batchUpdate(batchUpdateGoodsList);
+            }
+            
             if (studentPaymentOrder.getBalancePaymentAmount() != null && studentPaymentOrder.getBalancePaymentAmount().compareTo(BigDecimal.ZERO) > 0) {
                 sysUserCashAccountService.updateBalance(studentPaymentOrder.getUserId(), studentPaymentOrder.getBalancePaymentAmount(), PlatformCashAccountDetailTypeEnum.REFUNDS, "声部更改支付失败");
             }

+ 11 - 4
mec-biz/src/main/resources/config/mybatis/StudentPaymentOrderDetailMapper.xml

@@ -21,6 +21,7 @@
         <result column="is_renew_" property="isRenew"/>
         <result column="income_item_" property="incomeItem"/>
         <result column="income_" property="income"/>
+        <result column="minuend_stock_goods_id_list_" property="minuendStockGoodsIdList"/>
         <result column="user_id_" property="userId"/>
         <collection property="goodsList" ofType="com.ym.mec.biz.dal.entity.Goods">
             <result column="goods_id_" property="id"/>
@@ -64,10 +65,10 @@
             keyColumn="id" keyProperty="id">
         INSERT INTO student_payment_order_detail
         (type_,goods_id_list_,price_,create_time_,update_time_,payment_order_id_,kit_group_purchase_type_,
-         student_instrument_id_,is_renew_,income_item_,income_,tenant_id_,remit_fee_)
+         student_instrument_id_,is_renew_,income_item_,income_,minuend_stock_goods_id_list_,tenant_id_,remit_fee_)
         VALUES(#{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{goodsIdList},#{price},now(),now(),
         #{paymentOrderId},#{kitGroupPurchaseType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
-               #{studentInstrumentId},#{isRenew},#{incomeItem},#{income},#{tenantId},#{remitFee})
+               #{studentInstrumentId},#{isRenew},#{incomeItem},#{income},#{minuendStockGoodsIdList},#{tenantId},#{remitFee})
     </insert>
 
     <!-- 根据主键查询一条记录 -->
@@ -102,6 +103,9 @@
             <if test="income != null">
                 income_ = #{income},
             </if>
+            <if test="minuendStockGoodsIdList != null">
+                minuend_stock_goods_id_list_ = #{minuendStockGoodsIdList},
+            </if>
         </set>
         WHERE id_ = #{id} and tenant_id_ = #{tenantId}
     </update>
@@ -138,6 +142,9 @@
 	            <if test="item.income != null">
 	                income_ = #{item.income},
 	            </if>
+	            <if test="item.minuendStockGoodsIdList != null">
+	                minuend_stock_goods_id_list_ = #{item.minuendStockGoodsIdList},
+	            </if>
 	        </set>
 	        WHERE id_ = #{item.id} and tenant_id_ = #{item.tenantId}
         </foreach>
@@ -165,13 +172,13 @@
     <insert id="batchAdd" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id_">
         INSERT INTO student_payment_order_detail
         (type_,goods_id_list_,price_,remit_fee_,create_time_,update_time_,payment_order_id_,
-         kit_group_purchase_type_,student_instrument_id_,is_renew_,income_item_,income_,tenant_id_)
+         kit_group_purchase_type_,student_instrument_id_,is_renew_,income_item_,income_,minuend_stock_goods_id_list_,tenant_id_)
         VALUE
         <foreach collection="studentPaymentOrderDetailList" item="orderDetail" separator=",">
             (#{orderDetail.type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
             #{orderDetail.goodsIdList},#{orderDetail.price},#{orderDetail.remitFee},now(),now(),#{orderDetail.paymentOrderId},
             #{orderDetail.kitGroupPurchaseType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
-            #{orderDetail.studentInstrumentId},#{orderDetail.isRenew},#{orderDetail.incomeItem},#{orderDetail.income},#{orderDetail.tenantId})
+            #{orderDetail.studentInstrumentId},#{orderDetail.isRenew},#{orderDetail.incomeItem},#{orderDetail.income},#{orderDetail.minuendStockGoodsIdList},#{orderDetail.tenantId})
         </foreach>
     </insert>