Forráskód Böngészése

fix: 商品进销存

Joburgess 5 éve
szülő
commit
4a32ce5df7

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

@@ -63,6 +63,15 @@ public interface GoodsDao extends BaseDAO<Integer, Goods> {
     List<Goods> getGoodies(@Param("goodsIds") List<Integer> goodsIds);
 
     /**
+     * @describe 锁定指定商品
+     * @author Joburgess
+     * @date 2020.10.12
+     * @param goodsIds:
+     * @return java.util.List<com.ym.mec.biz.dal.entity.Goods>
+     */
+    List<Goods> lockGoods(@Param("goodsIds") List<Integer> goodsIds);
+
+    /**
      * @describe 根据货号查找商品
      * @author Joburgess
      * @date 2020.10.10

+ 18 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/dao/GoodsProcurementDao.java

@@ -14,4 +14,22 @@ public interface GoodsProcurementDao extends BaseDAO<Long, GoodsProcurement> {
     List<GoodsProcurement> queryGoodsProcurements(Map<String, Object> params);
     int countGoodsProcurements(Map<String, Object> params);
 
+    /**
+     * @describe 获取有内部库存的清单
+     * @author Joburgess
+     * @date 2020.10.12
+     * @param goodsId:
+     * @return com.ym.mec.biz.dal.entity.GoodsProcurement
+     */
+    GoodsProcurement getWithStockSurplusProcurement(@Param("goodsId") Integer goodsId);
+
+    /**
+     * @describe 获取有税务库存的清单
+     * @author Joburgess
+     * @date 2020.10.12
+     * @param goodsId:
+     * @return com.ym.mec.biz.dal.entity.GoodsProcurement
+     */
+    GoodsProcurement getWithTaxStockSurplusProcurement(@Param("goodsId") Integer goodsId);
+
 }

+ 28 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/dto/GoodsBatchNoDto.java

@@ -0,0 +1,28 @@
+package com.ym.mec.biz.dal.dto;
+
+/**
+ * @Author Joburgess
+ * @Date 2020.10.12
+ */
+public class GoodsBatchNoDto {
+
+    private Integer goodsId;
+
+    private String batchNo;
+
+    public Integer getGoodsId() {
+        return goodsId;
+    }
+
+    public void setGoodsId(Integer goodsId) {
+        this.goodsId = goodsId;
+    }
+
+    public String getBatchNo() {
+        return batchNo;
+    }
+
+    public void setBatchNo(String batchNo) {
+        this.batchNo = batchNo;
+    }
+}

+ 37 - 26
mec-biz/src/main/java/com/ym/mec/biz/dal/entity/GoodsProcurement.java

@@ -26,11 +26,11 @@ public class GoodsProcurement {
 	@ApiModelProperty(value = "采购价二")
 	private java.math.BigDecimal agreeCostPrice;
 	
-	@ApiModelProperty(value = "总进货数量")
-	private Integer totalQuantity;
+	@ApiModelProperty(value = "内部库存")
+	private Integer stockCount;
 	
-	@ApiModelProperty(value = "税务数量")
-	private Integer taxQuantity;
+	@ApiModelProperty(value = "税务库存")
+	private Integer taxStockCount;
 	
 	@ApiModelProperty(value = "进货人编号")
 	private Integer operatorId;
@@ -38,8 +38,11 @@ public class GoodsProcurement {
 	@ApiModelProperty(value = "批次号")
 	private String batchNo;
 
-	@ApiModelProperty(value = "售出数量")
-	private int soldNum;
+	@ApiModelProperty(value = "内部库存售出数量")
+	private int stockSoldNum;
+
+	@ApiModelProperty(value = "税务库存售出数量")
+	private int taxStockSoldNum;
 	
 	/**  */
 	private java.util.Date createTime;
@@ -95,22 +98,6 @@ public class GoodsProcurement {
 		return this.agreeCostPrice;
 	}
 			
-	public void setTotalQuantity(Integer totalQuantity){
-		this.totalQuantity = totalQuantity;
-	}
-	
-	public Integer getTotalQuantity(){
-		return this.totalQuantity;
-	}
-			
-	public void setTaxQuantity(Integer taxQuantity){
-		this.taxQuantity = taxQuantity;
-	}
-	
-	public Integer getTaxQuantity(){
-		return this.taxQuantity;
-	}
-			
 	public void setOperatorId(Integer operatorId){
 		this.operatorId = operatorId;
 	}
@@ -143,12 +130,36 @@ public class GoodsProcurement {
 		return this.updateTime;
 	}
 
-	public int getSoldNum() {
-		return soldNum;
+	public Integer getStockCount() {
+		return stockCount;
+	}
+
+	public void setStockCount(Integer stockCount) {
+		this.stockCount = stockCount;
+	}
+
+	public Integer getTaxStockCount() {
+		return taxStockCount;
+	}
+
+	public void setTaxStockCount(Integer taxStockCount) {
+		this.taxStockCount = taxStockCount;
+	}
+
+	public int getStockSoldNum() {
+		return stockSoldNum;
+	}
+
+	public void setStockSoldNum(int stockSoldNum) {
+		this.stockSoldNum = stockSoldNum;
+	}
+
+	public int getTaxStockSoldNum() {
+		return taxStockSoldNum;
 	}
 
-	public void setSoldNum(int soldNum) {
-		this.soldNum = soldNum;
+	public void setTaxStockSoldNum(int taxStockSoldNum) {
+		this.taxStockSoldNum = taxStockSoldNum;
 	}
 
 	@Override

+ 5 - 2
mec-biz/src/main/java/com/ym/mec/biz/service/GoodsService.java

@@ -1,6 +1,8 @@
 package com.ym.mec.biz.service;
 
+import com.ym.mec.biz.dal.dto.GoodsSellDto;
 import com.ym.mec.biz.dal.entity.Goods;
+import com.ym.mec.biz.dal.enums.AccountType;
 import com.ym.mec.common.service.BaseService;
 
 import org.apache.ibatis.annotations.Param;
@@ -69,8 +71,9 @@ public interface GoodsService extends BaseService<Integer, Goods> {
      * @describe 扣减商品库存
      * @author Joburgess
      * @date 2020.10.12
-     * @param goodsIds:
+     * @param goodsIds: 销售商品编号列表
+     * @param accountType: 收款账户类型
      * @return java.lang.String
      */
-    String subtractStock(List<Integer> goodsIds);
+    List<Goods> subtractStock(List<Integer> goodsIds, AccountType accountType);
 }

+ 49 - 14
mec-biz/src/main/java/com/ym/mec/biz/service/impl/GoodsServiceImpl.java

@@ -7,11 +7,10 @@ import com.ym.mec.auth.api.entity.SysUser;
 import com.ym.mec.biz.dal.dao.GoodsDao;
 import com.ym.mec.biz.dal.dao.GoodsProcurementDao;
 import com.ym.mec.biz.dal.dao.SysConfigDao;
+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.enums.GoodsType;
-import com.ym.mec.biz.dal.enums.MessageTypeEnum;
-import com.ym.mec.biz.dal.enums.TemplateTypeEnum;
+import com.ym.mec.biz.dal.enums.*;
 import com.ym.mec.biz.service.GoodsService;
 import com.ym.mec.biz.service.SysMessageService;
 import com.ym.mec.biz.service.UploadFileService;
@@ -23,6 +22,7 @@ import com.ym.mec.common.service.impl.BaseServiceImpl;
 import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
 import com.ym.mec.util.excel.IniFileUtil;
 import com.ym.mec.util.excel.POIUtil;
+import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.ss.usermodel.PictureData;
 import org.slf4j.Logger;
@@ -38,6 +38,8 @@ import org.springframework.web.multipart.MultipartFile;
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
 @Service
@@ -84,8 +86,8 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 		gp.setSupplyChannel(goods.getSupplyChannel());
 		gp.setDiscountPrice(goods.getDiscountPrice());
 		gp.setAgreeCostPrice(goods.getAgreeCostPrice());
-		gp.setTotalQuantity(goods.getStockCount());
-		gp.setTaxQuantity(goods.getTaxStockCount());
+		gp.setStockCount(goods.getStockCount());
+		gp.setTaxStockCount(goods.getTaxStockCount());
 		gp.setOperatorId(operatorId);
 		gp.setBatchNo(batchNo);
 
@@ -218,8 +220,8 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 			gp.setSupplyChannel(goods.getSupplyChannel());
 			gp.setDiscountPrice(goods.getDiscountPrice());
 			gp.setAgreeCostPrice(goods.getAgreeCostPrice());
-			gp.setTotalQuantity(goods.getStockCount());
-			gp.setTaxQuantity(goods.getTaxStockCount());
+			gp.setStockCount(goods.getStockCount());
+			gp.setTaxStockCount(goods.getTaxStockCount());
 			gp.setOperatorId(operatorId);
 			gp.setBatchNo(batchNo);
 			goodsProcurements.add(gp);
@@ -264,17 +266,50 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 	}
 
 	@Override
-	@Transactional(rollbackFor = Exception.class)
-	public String subtractStock(List<Integer> goodsIds) {
-		List result = new ArrayList();
+	@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
+	public List<Goods> subtractStock(List<Integer> goodsIds, AccountType accountType) {
 		if(CollectionUtils.isEmpty(goodsIds)){
-			return JSONObject.toJSONString(result);
+			return Collections.emptyList();
 		}
-		List<Goods> tempGoods = goodsDao.getGoodies(goodsIds);
-		List<Integer> realGoodIds = new ArrayList<>();
-		for (Goods tempGood : tempGoods) {
 
+		Map<Integer, Long> goodsSellNumMap = goodsIds.stream().collect(Collectors.groupingBy(s -> s, Collectors.counting()));
+
+		List<Goods> tempGoodsList = goodsDao.lockGoods(new ArrayList<>(goodsIds));
+		Map<Integer, Integer> singleGoodsSellNumMap = new HashMap<>();
+		Map<Integer, Goods> idTempGoodsMap = tempGoodsList.stream().collect(Collectors.toMap(Goods::getId, g -> g));
+		List<Integer> singleGoodsIds = new ArrayList<>();
+		for (Integer goodsId : goodsIds) {
+			Goods tempGoods = idTempGoodsMap.get(goodsId);
+			if(StringUtils.isBlank(tempGoods.getComplementGoodsIdList())){
+				singleGoodsIds.add(tempGoods.getId());
+			}else{
+				List<Integer> complementGoodsIds = Arrays.stream(tempGoods.getComplementGoodsIdList().split(",")).map(s -> Integer.valueOf(s)).collect(Collectors.toList());
+				singleGoodsIds.addAll(complementGoodsIds);
+			}
 		}
+		List<Goods> singleGoodsList = goodsDao.getGoodies(singleGoodsIds);
+		Map<Integer, Goods> idSingleGoodsMap = singleGoodsList.stream().collect(Collectors.toMap(Goods::getId, g -> g));
+		for (Integer singleGoodsId : singleGoodsIds) {
+			Goods goods = idSingleGoodsMap.get(singleGoodsId);
+			GoodsProcurement goodsProcurement = null;
+			if(StockType.INTERNAL.equals(goods.getStockType())||(StockType.ALL.equals(goods.getStockType())&&AccountType.INTERNAL.equals(accountType))){
+				goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(goods.getId());
+				goods.setStockCount(new AtomicInteger(goods.getStockCount()).decrementAndGet());
+				goodsProcurement.setStockCount(new AtomicInteger(goodsProcurement.getStockCount()).decrementAndGet());
+				goodsProcurement.setStockSoldNum(new AtomicInteger(goodsProcurement.getStockSoldNum()).incrementAndGet());
+			}else if(StockType.EXTERNAL.equals(goods.getStockType())||(StockType.ALL.equals(goods.getStockType())&&AccountType.EXTERNAL.equals(accountType))){
+				goodsProcurement = goodsProcurementDao.getWithTaxStockSurplusProcurement(goods.getId());
+				goods.setTaxStockCount(new AtomicInteger(goods.getTaxStockCount()).decrementAndGet());
+				goodsProcurement.setTaxStockCount(new AtomicInteger(goodsProcurement.getTaxStockCount()).decrementAndGet());
+				goodsProcurement.setTaxStockSoldNum(new AtomicInteger(goodsProcurement.getTaxStockSoldNum()).incrementAndGet());
+			}
+			if(Objects.isNull(goodsProcurement)){
+				throw new BizException("商品类型不明");
+			}
+			goodsDao.update(goods);
+			goodsProcurementDao.update(goodsProcurement);
+		}
+
 		return null;
 	}
 }

+ 7 - 0
mec-biz/src/main/resources/config/mybatis/GoodsMapper.xml

@@ -296,6 +296,13 @@
         </foreach>
     </select>
 
+    <select id="lockGoods" resultMap="Goods">
+        SELECT * FROM goods WHERE id_ IN
+        <foreach collection="goodsIds" item="goodsId" open="(" close=")" separator=",">
+            #{goodsId}
+        </foreach>
+    </select>
+
     <select id="findBySn" resultMap="Goods">
         SELECT * FROM goods WHERE sn_ = #{sn}
     </select>

+ 26 - 10
mec-biz/src/main/resources/config/mybatis/GoodsProcurementMapper.xml

@@ -13,10 +13,12 @@
 		<result column="supply_channel_" property="supplyChannel" />
 		<result column="discount_price_" property="discountPrice" />
 		<result column="agree_cost_price_" property="agreeCostPrice" />
-		<result column="total_quantity_" property="totalQuantity" />
-		<result column="tax_quantity_" property="taxQuantity" />
+		<result column="stock_count_" property="stockCount" />
+		<result column="tax_stock_count_" property="taxStockCount" />
 		<result column="operator_id_" property="operatorId" />
 		<result column="batch_no_" property="batchNo" />
+		<result column="stock_sold_num_" property="stockSoldNum"/>
+		<result column="tax_stock_sold_num_" property="taxStockSoldNum"/>
 		<result column="create_time_" property="createTime" />
 		<result column="update_time_" property="updateTime" />
 	</resultMap>
@@ -33,15 +35,15 @@
 	
 	<!-- 向数据库增加一条记录 -->
 	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.GoodsProcurement" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
-		INSERT INTO goods_procurement (goods_id_,goods_category_id_,supply_channel_,discount_price_,agree_cost_price_,total_quantity_,tax_quantity_,operator_id_,batch_no_,create_time_,update_time_)
-		VALUES(#{goodsId},#{goodsCategoryId},#{supplyChannel},#{discountPrice},#{agreeCostPrice},#{totalQuantity},#{taxQuantity},#{operatorId},#{batchNo},NOW(),NOW())
+		INSERT INTO goods_procurement (goods_id_,goods_category_id_,supply_channel_,discount_price_,agree_cost_price_,stock_count_,tax_stock_count_,operator_id_,batch_no_,create_time_,update_time_)
+		VALUES(#{goodsId},#{goodsCategoryId},#{supplyChannel},#{discountPrice},#{agreeCostPrice},#{stockCount},#{taxStockCount},#{operatorId},#{batchNo},NOW(),NOW())
 	</insert>
 
 	<insert id="batchInsert" parameterType="com.ym.mec.biz.dal.entity.GoodsProcurement" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
-		INSERT INTO goods_procurement (goods_id_,goods_category_id_,supply_channel_,discount_price_,agree_cost_price_,total_quantity_,tax_quantity_,operator_id_,batch_no_,create_time_,update_time_)
+		INSERT INTO goods_procurement (goods_id_,goods_category_id_,supply_channel_,discount_price_,agree_cost_price_,stock_count_,tax_stock_count_,operator_id_,batch_no_,create_time_,update_time_)
 		VALUE
 		<foreach collection="goodsProcurements" item="gp" separator=",">
-			(#{gp.goodsId},#{gp.goodsCategoryId},#{gp.supplyChannel},#{gp.discountPrice},#{gp.agreeCostPrice},#{gp.totalQuantity},#{gp.taxQuantity},#{gp.operatorId},#{gp.batchNo},NOW(),NOW())
+			(#{gp.goodsId},#{gp.goodsCategoryId},#{gp.supplyChannel},#{gp.discountPrice},#{gp.agreeCostPrice},#{gp.stockCount},#{gp.taxStockCount},#{gp.operatorId},#{gp.batchNo},NOW(),NOW())
 		</foreach>
 	</insert>
 	
@@ -61,8 +63,8 @@
 			<if test="goodsCategoryId != null">
 				goods_category_id_ = #{goodsCategoryId},
 			</if>
-			<if test="taxQuantity != null">
-				tax_quantity_ = #{taxQuantity},
+			<if test="taxStockCount != null">
+				tax_stock_count_ = #{taxStockCount},
 			</if>
 			<if test="supplyChannel != null">
 				supply_channel_ = #{supplyChannel},
@@ -70,8 +72,8 @@
 			<if test="discountPrice != null">
 				discount_price_ = #{discountPrice},
 			</if>
-			<if test="totalQuantity != null">
-				total_quantity_ = #{totalQuantity},
+			<if test="stockCount != null">
+				stock_count_ = #{stockCount},
 			</if>
 			<if test="goodsId != null">
 				goods_id_ = #{goodsId},
@@ -79,6 +81,12 @@
 			<if test="batchNo != null">
 				batch_no_ = #{batchNo},
 			</if>
+			<if test="stockSoldNum != null">
+				stock_sold_num_ = #{stockSoldNum},
+			</if>
+			<if test="taxStockSoldNum != null">
+				tax_stock_sold_num_ = #{taxStockSoldNum},
+			</if>
 				update_time_ = NOW(),
 		</set> WHERE id_ = #{id}
 	</update>
@@ -124,4 +132,12 @@
 	<select id="queryCount" resultType="int">
 		SELECT COUNT(*) FROM goods_procurement
 	</select>
+
+	<select id="getWithStockSurplusProcurement" resultMap="GoodsProcurement">
+		SELECT * FROM goods_procurement WHERE goods_id_ = #{goodsId} AND stock_count_>stock_sold_num_
+	</select>
+
+	<select id="getWithStockSurplusProcurement" resultMap="GoodsProcurement">
+		SELECT * FROM goods_procurement WHERE goods_id_ = #{goodsId} AND tax_stock_count_>tax_stock_sold_num_
+	</select>
 </mapper>