SyncTescoSystemService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package com.jsh.erp.service;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.jsh.erp.datasource.entities.DepotHeadVo4Body;
  5. import com.jsh.erp.datasource.entities.MaterialVo4Unit;
  6. import com.jsh.erp.datasource.vo.DepotHeadXsddRequestVO;
  7. import com.jsh.erp.datasource.vo.DepotItemXsddRequestVO;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.stereotype.Service;
  11. import javax.annotation.Resource;
  12. import java.math.BigDecimal;
  13. import java.util.ArrayList;
  14. import java.util.Date;
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.stream.Collectors;
  18. import java.util.stream.Stream;
  19. /**
  20. * @Author MS.BLUE
  21. * @Date 2025-04-12
  22. */
  23. @Service
  24. public class SyncTescoSystemService {
  25. private Logger logger = LoggerFactory.getLogger(SyncTescoSystemService.class);
  26. @Resource
  27. private MaterialService materialService;
  28. @Resource
  29. private DepotHeadService depotHeadService;
  30. @Resource
  31. private SequenceService sequenceService;
  32. public String syncSystemSku(String param) {
  33. logger.info("获取商品系统sku,返回信息集采系统 param:{}", param);
  34. JSONObject result = new JSONObject();
  35. JSONArray dataArray = new JSONArray();
  36. try {
  37. // 参数校验
  38. if (param == null || param.isEmpty()) {
  39. result.put("code", 1);
  40. result.put("msg", "参数不能为空");
  41. return result.toJSONString();
  42. }
  43. // 解析传入的JSON参数
  44. JSONArray barCodeArray = JSONArray.parseArray(param);
  45. if (barCodeArray == null || barCodeArray.isEmpty()) {
  46. result.put("code", 1);
  47. result.put("msg", "参数格式错误");
  48. return result.toJSONString();
  49. }
  50. // 提取 barCode 到字符串数组
  51. List<String> barCodeList = new ArrayList<>();
  52. for (int i = 0; i < barCodeArray.size(); i++) {
  53. JSONObject barCodeObj = barCodeArray.getJSONObject(i);
  54. String barCode = barCodeObj.getString("barCode");
  55. barCodeList.add(barCode);
  56. }
  57. // 查询商品信息
  58. List<MaterialVo4Unit> materialList = materialService.getMaterialByBarCode(barCodeList);
  59. if (materialList != null && !materialList.isEmpty()) {
  60. result.put("code", 0);
  61. for (MaterialVo4Unit material : materialList) {
  62. JSONObject item = new JSONObject();
  63. item.put("barCode", material.getmBarCode());
  64. item.put("systemSku", material.getSystemSku());
  65. dataArray.add(item);
  66. }
  67. } else {
  68. result.put("code", 1);
  69. result.put("msg", "未找到对应商品信息");
  70. }
  71. } catch (Exception e) {
  72. result.put("code", 1);
  73. result.put("msg", "系统异常,请稍后重试");
  74. }
  75. // 将数据数组放入返回结果
  76. result.put("data", dataArray);
  77. logger.info(" result:{}", param);
  78. return result.toJSONString();
  79. }
  80. /**
  81. *
  82. 集采下单成功->封装同步erp销售订单请求体
  83. {
  84. orderSn:"订单编号",
  85. customer:{//客户信息
  86. name:"客户名称",
  87. contact:"客户联系方式",
  88. address:"客户地址"
  89. },
  90. item:[{//订单商品信息
  91. erp_sku:"erp_sku编号",
  92. unitPrice:"商品单价",
  93. quantity:"商品下单数量",
  94. price:"商品实付价格"
  95. }]
  96. }
  97. * @param param
  98. * @return
  99. */
  100. public String syncOrder(String param) {
  101. logger.info("同步集采订单-》销售订单 param:{}", param);
  102. JSONObject result = new JSONObject();
  103. try {
  104. // 解析传入的JSON参数
  105. JSONObject paramJson = JSONObject.parseObject(param);
  106. if (paramJson == null || paramJson.isEmpty()) {
  107. result.put("code", 1);
  108. result.put("msg", "参数不能为空");
  109. return result.toJSONString();
  110. }
  111. // 校验订单编号
  112. String orderSn = paramJson.getString("orderSn");
  113. if (orderSn == null || orderSn.isEmpty()) {
  114. result.put("code", 1);
  115. result.put("msg", "订单编号不能为空");
  116. return result.toJSONString();
  117. }
  118. // 校验客户信息
  119. JSONObject customer = paramJson.getJSONObject("customer");
  120. if (customer == null || customer.isEmpty()) {
  121. result.put("code", 1);
  122. result.put("msg", "客户信息不能为空");
  123. return result.toJSONString();
  124. }
  125. // 校验订单商品信息
  126. JSONArray itemArray = paramJson.getJSONArray("item");
  127. if (itemArray == null || itemArray.isEmpty()) {
  128. result.put("code", 1);
  129. result.put("msg", "订单商品信息不能为空");
  130. return result.toJSONString();
  131. }
  132. buildOrderAndItem(paramJson, itemArray);
  133. // 返回成功结果
  134. result.put("code", 0);
  135. result.put("msg", "订单同步成功");
  136. } catch (Exception e) {
  137. logger.error("同步订单发生异常", e);
  138. result.put("code", 1);
  139. result.put("msg", "系统异常,请稍后重试");
  140. }
  141. return result.toJSONString();
  142. }
  143. private void buildOrderAndItem(JSONObject orderJson, JSONArray itemJsonArray) throws Exception {
  144. DepotHeadXsddRequestVO order = new DepotHeadXsddRequestVO();
  145. order.setType("其他");
  146. order.setSubType("销售订单");
  147. String orderSn = orderJson.getString("orderSn");
  148. String number = sequenceService.buildOnlyNumber();
  149. String defaultNumber = "XSDD" + number;
  150. order.setDefaultNumber(defaultNumber);
  151. order.setNumber(defaultNumber);
  152. order.setCreateTime(new Date());
  153. order.setOperTime(new Date());
  154. order.setOrganId(1L);// 供应商
  155. order.setCreator(1L);// 创建人
  156. order.setAccountId(1L);// 账户
  157. order.setChangeAmount(BigDecimal.ZERO);
  158. order.setBackAmount(BigDecimal.ZERO);
  159. order.setTotalPrice(orderJson.getBigDecimal("totalPrice"));
  160. order.setPayType("现付");
  161. order.setDiscount(BigDecimal.ZERO);
  162. order.setDiscountMoney(BigDecimal.ZERO);
  163. order.setDiscountLastMoney(orderJson.getBigDecimal("totalPrice"));
  164. order.setStatus("0");
  165. order.setPurchaseStatus("0");
  166. order.setSource("2");// 来源集采商城
  167. order.setTenantId(null);
  168. order.setDeleteFlag("0");
  169. List<DepotItemXsddRequestVO> itemList = new ArrayList<>();
  170. List<String> systemSkuList = itemJsonArray.stream().map(JSONObject.class::cast).map(json -> json.getString("erp_sku")).collect(Collectors.toList());
  171. Map<String, JSONObject> skuToJsonMap = itemJsonArray.stream().map(JSONObject.class::cast).collect(Collectors.toMap(json -> json.getString("erp_sku"), json -> json));
  172. List<MaterialVo4Unit> materialList = materialService.getMaterialBySystemSku(systemSkuList);
  173. Map<String, List<MaterialVo4Unit>> skuToMaterialMap = materialList.stream().collect(Collectors.groupingBy(MaterialVo4Unit::getSystemSku));
  174. for (Map.Entry<String, JSONObject> entry : skuToJsonMap.entrySet()) {
  175. String erpSku = entry.getKey(); // 获取 erp_sku
  176. JSONObject itemJson = entry.getValue(); // 获取对应的 JSONObject
  177. Integer quantity = itemJson.getInteger("quantity");
  178. BigDecimal unitPrice = itemJson.getBigDecimal("unitPrice");
  179. BigDecimal price = itemJson.getBigDecimal("price");
  180. if (quantity == null || unitPrice == null || price == null) {
  181. logger.warn("商品信息不完整,erp_sku: {}", erpSku);
  182. continue;
  183. }
  184. List<MaterialVo4Unit> materials = skuToMaterialMap.get(erpSku);
  185. if (materials != null && !materials.isEmpty()) {
  186. BigDecimal remainingQuantity = itemJson.getBigDecimal("quantity"); // 剩余数量
  187. for (MaterialVo4Unit material : materials) {
  188. BigDecimal inventory = material.getInventory(); // 当前商品库存
  189. if (inventory.compareTo(BigDecimal.ZERO) <= 0) {
  190. continue; // 如果库存为0,跳过
  191. }
  192. DepotItemXsddRequestVO item = new DepotItemXsddRequestVO();
  193. item.setHeaderId(order.getId());
  194. item.setMaterialId(material.getId());
  195. item.setMaterialExtendId(material.getMeId());
  196. item.setMaterialUnit(material.getUnit());
  197. item.setSku(material.getSku());
  198. item.setDepotId(material.getDepotId());
  199. item.setTaxRate(BigDecimal.ONE);
  200. item.setTaxMoney(BigDecimal.ZERO);
  201. item.setBatchNumber("");
  202. item.setUnitPrice(unitPrice); // 设置单价
  203. if (inventory.compareTo(remainingQuantity) >= 0) {
  204. // 如果当前商品库存大于等于剩余数量,直接使用剩余数量
  205. item.setOperNumber(remainingQuantity);
  206. // 计算实付价格:单价 × 剩余数量
  207. BigDecimal taxLastMoney = itemJson.getBigDecimal("unitPrice").multiply(remainingQuantity);
  208. item.setTaxLastMoney(taxLastMoney);
  209. item.setAllPrice(taxLastMoney);
  210. itemList.add(item);
  211. break; // 结束循环
  212. } else {
  213. // 如果当前商品库存小于剩余数量,使用当前商品库存
  214. item.setOperNumber(inventory);
  215. // 计算实付价格:单价 × 剩余数量
  216. BigDecimal taxLastMoney = itemJson.getBigDecimal("unitPrice").multiply(remainingQuantity);
  217. item.setTaxLastMoney(taxLastMoney);
  218. item.setAllPrice(taxLastMoney);
  219. itemList.add(item);
  220. remainingQuantity = remainingQuantity.subtract(inventory); // 减少剩余数量
  221. }
  222. }
  223. } else {
  224. // 如果未找到对应的 MaterialVo4Unit,可以记录日志或处理异常
  225. logger.warn("未找到 erp_sku: {} 对应的商品信息", erpSku);
  226. }
  227. }
  228. depotHeadService.syncOrderToXsdd(order, itemList);
  229. }
  230. }