OrderSubmitServiceImpl.java 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. package com.caimei.service.impl;
  2. import com.caimei.mapper.CouponMapper;
  3. import com.caimei.mapper.OrderSubmitMapper;
  4. import com.caimei.mapper.ProductMapper;
  5. import com.caimei.model.ResponseJson;
  6. import com.caimei.model.po.*;
  7. import com.caimei.model.vo.*;
  8. import com.caimei.service.OrderSubmitService;
  9. import com.caimei.service.ShoppingCartService;
  10. import com.caimei.util.MathUtil;
  11. import com.caimei.util.OrderNoUtils;
  12. import com.caimei.util.ProductUtils;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.apache.commons.lang3.StringUtils;
  15. import org.springframework.beans.BeanUtils;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.annotation.Transactional;
  19. import org.springframework.transaction.interceptor.TransactionAspectSupport;
  20. import org.springframework.util.CollectionUtils;
  21. import javax.annotation.Resource;
  22. import java.math.BigDecimal;
  23. import java.text.SimpleDateFormat;
  24. import java.util.*;
  25. import java.util.concurrent.atomic.AtomicReference;
  26. /**
  27. * Description
  28. *
  29. * @author : plf
  30. * @date : 2021/4/25
  31. */
  32. @Slf4j
  33. @Service
  34. public class OrderSubmitServiceImpl implements OrderSubmitService {
  35. @Resource
  36. private OrderSubmitMapper orderSubmitMapper;
  37. @Resource
  38. private ShoppingCartService shoppingCartService;
  39. @Resource
  40. private ProductMapper productMapper;
  41. @Resource
  42. private CouponMapper couponMapper;
  43. @Value("${caimei.oldapi}")
  44. private String domain;
  45. @Override
  46. public ResponseJson<Map<String, Object>> orderConfirm(Integer userId, String cartIds, Integer productId, Integer productCount, Integer heUserId) {
  47. Map<String, Object> confirmData = new HashMap<>(5);
  48. log.info("<<<<< 结算订单 >>>>>");
  49. //商品总金额
  50. AtomicReference<BigDecimal> totalPrice = new AtomicReference<>(BigDecimal.ZERO);
  51. List<CartProductVo> totalProducts = new ArrayList<>();
  52. List<ShopVo> shopList = new ArrayList<>();
  53. if (productId != null && productId > 0) {
  54. //立即购买
  55. shopList.add(orderSubmitMapper.findShopByProductId(productId));
  56. } else {
  57. String[] cartId = cartIds.split(",");
  58. shopList = orderSubmitMapper.findShopByCartIds(cartId);
  59. }
  60. shopList.forEach(shop -> {
  61. shop.setLogo(ProductUtils.getImageURL("shopLogo", shop.getLogo(), 0, domain));
  62. List<CartProductVo> shopProducts = new ArrayList<>();
  63. if (productId != null && productId > 0) {
  64. CartProductVo cartProduct = orderSubmitMapper.findProductById(productId);
  65. cartProduct.setProductCount(productCount);
  66. cartProduct.setHeUserId(heUserId);
  67. shopProducts.add(cartProduct);
  68. } else {
  69. String[] cartId = cartIds.split(",");
  70. shopProducts = orderSubmitMapper.findByShopCartProduct(shop.getShopId(), cartId);
  71. }
  72. BigDecimal shopTotalPrice = BigDecimal.ZERO;
  73. for (CartProductVo product : shopProducts) {
  74. shoppingCartService.setPrice(product, userId);
  75. shopTotalPrice = MathUtil.add(shopTotalPrice, MathUtil.mul(product.getProductCount(), product.getPrice()));
  76. }
  77. shop.setShopTotalPrice(shopTotalPrice);
  78. shop.setProductList(shopProducts);
  79. totalProducts.addAll(shopProducts);
  80. totalPrice.set(MathUtil.add(totalPrice.get(), shopTotalPrice));
  81. });
  82. // 用户所有可用优惠券
  83. List<CouponVo> receiveCouponList = couponMapper.findReceiveCouponList(userId, null,null, 1);
  84. receiveCouponList.forEach(coupon -> {
  85. // 能参与该优惠券的总金额
  86. BigDecimal touchCouponAmount = BigDecimal.ZERO;
  87. if (coupon.getProductType() == 1) {
  88. //全商城商品
  89. touchCouponAmount = totalPrice.get();
  90. } else {
  91. // 参与优惠券的所有商品id
  92. String couponProductIds = couponMapper.getCouponProductIds(coupon.getCouponId());
  93. if (productId != null && productId > 0) {
  94. CartProductVo cartProduct = totalProducts.get(0);
  95. if (couponProductIds.contains(cartProduct.getProductId().toString())) {
  96. touchCouponAmount = totalPrice.get();
  97. }
  98. } else {
  99. for (CartProductVo product : totalProducts) {
  100. if (couponProductIds.contains(product.getProductId().toString())) {
  101. touchCouponAmount = MathUtil.add(touchCouponAmount, MathUtil.mul(product.getProductCount(), product.getPrice()));
  102. }
  103. }
  104. }
  105. }
  106. if (coupon.getNoThresholdFlag() == 1 || touchCouponAmount.compareTo(coupon.getTouchPrice()) > 0) {
  107. // 满足优惠条件
  108. coupon.setTouchFlag(1);
  109. } else {
  110. coupon.setTouchFlag(0);
  111. }
  112. });
  113. receiveCouponList.sort((o1, o2) -> {
  114. // 按满足优惠条件/优惠金额进行排序
  115. if (!o1.getTouchFlag().equals(o2.getTouchFlag()) ) {
  116. return o1.getTouchFlag() - o2.getTouchFlag();
  117. } else {
  118. return o1.getCouponAmount().compareTo(o2.getCouponAmount());
  119. }
  120. });
  121. confirmData.put("receiveCouponList", receiveCouponList);
  122. confirmData.put("totalPrice", totalPrice);
  123. confirmData.put("shopList", shopList);
  124. return ResponseJson.success(confirmData);
  125. }
  126. @Override
  127. @Transactional(rollbackFor = Exception.class)
  128. public ResponseJson<Map<String, String>> orderSubmit(Integer userId, Integer cartType, Integer addressId, Integer couponId, List<Map<String, Object>> orderInfo, Map<String, Object> payInfo) {
  129. /*
  130. * 逻辑处理 start
  131. */
  132. log.info("******************** 提交订单逻辑处理 start *******************");
  133. //机构用户
  134. UserPo user = orderSubmitMapper.findUser(userId);
  135. if (null == user) {
  136. return ResponseJson.error("用户信息异常", null);
  137. }
  138. // 商品总数量
  139. Integer productCount = 0;
  140. // 商品总金额 (商品单价乘以数量,再加上税费[默认0])
  141. BigDecimal productTotalFee = BigDecimal.ZERO;
  142. // 小计金额(商品折后单价乘以数量,再加上税费[默认0])
  143. BigDecimal orderTotalFee = BigDecimal.ZERO;
  144. // 订单总额(小计金额减去经理折扣后,再加上运费[默认0])
  145. BigDecimal payTotalFee = BigDecimal.ZERO;
  146. // 真实支付金额(订单总额减去抵扣的账户余额)
  147. BigDecimal payableAmount = BigDecimal.ZERO;
  148. /*
  149. * 初始化主订单参数
  150. */
  151. CmOrderPo order = new CmOrderPo();
  152. String curDateStr = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
  153. // 订单号
  154. String orderNo = null;
  155. orderNo = OrderNoUtils.getOrderNo("H");
  156. order.setOrderNo(orderNo);
  157. // 运营人员下单
  158. order.setBuyUserID(user.getUserID());
  159. order.setOrderType(2);
  160. order.setOrderSubmitType(0);
  161. order.setConfirmFlag("2");
  162. order.setUserID(user.getUserID().longValue());
  163. // 订单来源
  164. order.setOrderSource("6");
  165. order.setOrganizeID(0);
  166. order.setUpdateDate(curDateStr);
  167. order.setPayFlag("0");
  168. order.setOnlinePayFlag("0");
  169. order.setPreferential(BigDecimal.ZERO);
  170. order.setDiscountFee(BigDecimal.ZERO);
  171. // 订单提交时间
  172. order.setOrderTime(curDateStr);
  173. // 默认订单可以拆分
  174. order.setSplitFlag("1");
  175. // 发票类型
  176. order.setInvoiceFlag("0");
  177. order.setReceiptStatus("1");
  178. order.setPayStatus("1");
  179. order.setZeroCostFlag(0);
  180. order.setSendOutStatus("1");
  181. order.setRefundType("0");
  182. // 是否包含活动商品(受订单未支付自动关闭时间影响) 0 否 1 是
  183. order.setHasActProduct("0");
  184. // 订单状态 0 有效 其它无效
  185. order.setDelFlag("0");
  186. // 是否确认付款供应商
  187. order.setAffirmPaymentFlag("0");
  188. /*
  189. * 订单商品
  190. */
  191. List<CmOrderProductPo> orderProductList = new ArrayList<>();
  192. for (Map<String, Object> shopOrderInfo : orderInfo) {
  193. Integer shopId = (Integer) shopOrderInfo.get("shopId");
  194. if (null == shopId) {
  195. return ResponseJson.error("供应商数据异常", null);
  196. }
  197. // 一个子订单对应的商品信息
  198. List<Map<String, Integer>> productInfoList = (List<Map<String, Integer>>) shopOrderInfo.get("productInfo");
  199. if (null == productInfoList) {
  200. return ResponseJson.error(-1, "订单商品数据异常", null);
  201. }
  202. /*
  203. * 整理订单商品信息
  204. */
  205. if (!CollectionUtils.isEmpty(productInfoList)) {
  206. // 遍历所有商品
  207. for (Map<String, Integer> productTemp : productInfoList) {
  208. Integer productId = productTemp.get("productId");
  209. Integer productNum = productTemp.get("productNum");
  210. Integer heUserId = productTemp.get("heUserId");
  211. heUserId = heUserId == null ? 0 : heUserId;
  212. // 统计商品总数量
  213. productCount += productNum;
  214. // 获取商品信息
  215. CmHeHeProductPo product = orderSubmitMapper.getProduct(productId);
  216. if (null == product) {
  217. return ResponseJson.error("订单商品数据异常", null);
  218. }
  219. if (null == productNum || productNum == 0) {
  220. return ResponseJson.error("商品购买数量异常", null);
  221. }
  222. // 获取商品购买价格(活动阶梯价格)
  223. BigDecimal productPrice = product.getPrice();
  224. // 成本价
  225. BigDecimal costPrice = BigDecimal.ZERO;
  226. // 机构税费(单)
  227. BigDecimal addedValueTax = new BigDecimal(0);
  228. int priceType = 0;
  229. //不含税可开票商品计算税费
  230. Integer activityId = productMapper.findActivityByProductId(product.getProductId());
  231. if (activityId != null && activityId > 0) {
  232. //活动阶梯
  233. List<ActivityLadderVo> ladderList = productMapper.findActivityLadder(activityId, product.getProductId());
  234. if (ladderList != null && ladderList.size() > 0) {
  235. priceType = 1;
  236. for (ActivityLadderVo ladder : ladderList) {
  237. if (productNum >= ladder.getBuyNum()) {
  238. productPrice = ladder.getBuyPrice();
  239. }
  240. }
  241. }
  242. }
  243. Integer discount = productMapper.findProductDiscount(product.getProductId(), userId);
  244. if (null != discount && discount > 0) {
  245. productPrice = MathUtil.div(MathUtil.mul(productPrice, discount), 100);
  246. }
  247. //税费
  248. boolean addTaxFlag = (0 == product.getIncludedTax()) && (1 == product.getInvoiceType() || 2 == product.getInvoiceType());
  249. if (addTaxFlag) {
  250. addedValueTax = MathUtil.div(MathUtil.mul(productPrice, product.getClubTaxPoint()), BigDecimal.valueOf(100), 2);
  251. productPrice = MathUtil.add(productPrice, addedValueTax);
  252. product.setPrice(productPrice);
  253. }
  254. if (MathUtil.compare(productPrice, BigDecimal.ZERO) == 0) {
  255. return ResponseJson.error("商品购买价格不能为0", null);
  256. }
  257. // 单个商品的金额
  258. BigDecimal productFee = MathUtil.mul(productPrice, productNum);
  259. // 统计商品总金额
  260. productTotalFee = MathUtil.add(productTotalFee, productFee);
  261. // 判断是否选中固定成本价
  262. if (MathUtil.compare(product.getCostPrice(), 0) > 0 && 1 == product.getCostType()) {
  263. costPrice = product.getCostPrice();
  264. }
  265. // 判断是否选中比例成本价
  266. if (MathUtil.compare(product.getCostProportional(), 0) > 0 && 2 == product.getCostType()) {
  267. // 通过售价*比例得到成本价
  268. costPrice = BigDecimal.valueOf(MathUtil.div(MathUtil.mul(productPrice, product.getCostProportional()), 100).floatValue());
  269. }
  270. product.setCostPrice(costPrice);
  271. /*
  272. * 整理订单商品数据
  273. */
  274. CmOrderProductPo orderProduct = setOrderProduct(productNum, product, priceType, productPrice, addedValueTax);
  275. orderProduct.setHeUserId(heUserId);
  276. // 加入订单商品列表
  277. orderProductList.add(orderProduct);
  278. if (cartType == 1) {
  279. orderSubmitMapper.deleteCartByProductId(user.getUserID(), product.getProductId(), heUserId);
  280. log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>删除用户购物车数据productId:" + product.getProductId());
  281. }
  282. }
  283. }
  284. }
  285. //处理优惠券
  286. BigDecimal couponAmount = BigDecimal.ZERO;
  287. CouponVo coupon = null;
  288. // 已领取优惠券id
  289. Integer receiveCouponId = null;
  290. if (null != couponId && couponId > 0) {
  291. receiveCouponId = couponMapper.findReceiveCouponId(userId, couponId);
  292. List<CouponVo> receiveCouponList = couponMapper.findReceiveCouponList(userId, null, receiveCouponId, 1);
  293. if (receiveCouponList == null || receiveCouponList.size() <= 0) {
  294. // 设置手动回滚事务
  295. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  296. return ResponseJson.error("优惠券使用异常", null);
  297. } else {
  298. coupon = receiveCouponList.get(0);
  299. }
  300. couponAmount = coupon.getCouponAmount();
  301. }
  302. order.setCouponAmount(couponAmount);
  303. // 设置是否是二手订单
  304. order.setSecondHandOrderFlag("0");
  305. order.setPromotionFullReduction(BigDecimal.ZERO);
  306. // 商品总数量
  307. order.setProductCount(productCount);
  308. // 赠品数量
  309. order.setPresentCount(0);
  310. //促销赠品数量
  311. order.setPromotionalGiftsCount(0);
  312. // 0包邮 -1到付 1 有运费
  313. order.setFreePostFlag("0");
  314. order.setFreight(BigDecimal.ZERO);
  315. // 商品总额
  316. order.setProductTotalFee(productTotalFee);
  317. // 订单总额(商品金额+运费)
  318. payTotalFee = productTotalFee;
  319. orderTotalFee = productTotalFee;
  320. order.setOrderTotalFee(orderTotalFee);
  321. payTotalFee = MathUtil.sub(payTotalFee, couponAmount);
  322. // 订单状态
  323. order.setStatus("11");
  324. if (MathUtil.compare(payTotalFee, 0) < 0 && MathUtil.compare(productTotalFee, 0) >= 0 && MathUtil.compare(couponAmount, 0) > 0) {
  325. // 订单金额因为使用优惠券而小于0,将金额置为0
  326. payTotalFee = BigDecimal.ZERO;
  327. order.setCouponAmount(productTotalFee);
  328. order.setStatus("31");
  329. }
  330. order.setPayTotalFee(payTotalFee);
  331. payableAmount = payTotalFee;
  332. order.setConfirmTime(curDateStr);
  333. // 余额支付金额
  334. order.setBalancePayFee(BigDecimal.ZERO);
  335. // 实际支付金额(商品金额+运费-余额抵扣)
  336. order.setPayableAmount(payableAmount);
  337. // 售后条款
  338. order.setClauseID(1L);
  339. order.setClauseName("无条款");
  340. // 是否返佣订单
  341. order.setRebateFlag("0");
  342. // 判断前端传入orderShouldPayFee订单应付金额,和后台计算应付金额对比
  343. BigDecimal orderShouldPayFee = new BigDecimal(payInfo.get("orderShouldPayFee").toString());
  344. double v = MathUtil.sub(payableAmount, orderShouldPayFee).doubleValue();
  345. log.info(">>>>>payableAmount:" + payableAmount + " ,orderShouldPayFee:" + orderShouldPayFee);
  346. // 考虑前端计算不精确
  347. if (v < -0.1d || v > 0.1d) {
  348. // 设置手动回滚事务
  349. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  350. return ResponseJson.error("订单付款金额异常", null);
  351. }
  352. /**
  353. * 保存主订单数据
  354. */
  355. orderSubmitMapper.insertOrder(order);
  356. log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>新增主订单(insert[cm_order])orderId:" + order.getOrderID());
  357. /**
  358. * 设置订单商品订单号
  359. */
  360. for (CmOrderProductPo orderProduct : orderProductList) {
  361. orderProduct.setOrderID(order.getOrderID());
  362. orderProduct.setOrderNo(order.getOrderNo());
  363. orderProduct.setOrganizeID(order.getOrganizeID());
  364. }
  365. /**
  366. * 整理 子订单信息
  367. */
  368. // 收集子订单供应商ID字符串
  369. String shopOrderIds = "";
  370. for (Map<String, Object> shopOrderInfo : orderInfo) {
  371. Integer shopId = (Integer) shopOrderInfo.get("shopId");
  372. String shopNote = (String) shopOrderInfo.get("note");
  373. // 初始化子订单信息
  374. CmShopOrderPo shopOrder = saveShopOrder(order, orderProductList, shopId, shopNote);
  375. // 保存子订单号
  376. shopOrderIds += (("".equals(shopOrderIds) ? "" : ",") + shopOrder.getShopOrderID());
  377. // 设置订单商品子订单号
  378. for (CmOrderProductPo orderProduct : orderProductList) {
  379. if (shopId.longValue() == orderProduct.getShopID()) {
  380. orderProduct.setShopOrderID(shopOrder.getShopOrderID());
  381. orderProduct.setShopOrderNo(shopOrder.getShopOrderNo());
  382. }
  383. }
  384. }
  385. /**
  386. * 保存订单商品
  387. */
  388. List<OrderProductLadderPricePo> orderProductLadderPriceList = new ArrayList<>();
  389. for (CmOrderProductPo orderProduct : orderProductList) {
  390. // 保存订单商品数据
  391. orderSubmitMapper.insertOrderProduct(orderProduct);
  392. log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>保存订单商品(insert[cm_order_product])OrderProductID:" + orderProduct.getOrderProductID());
  393. if ("1".equals(orderProduct.getLadderPriceFlag())) {
  394. //使用阶梯价格的订单商品保存下单时的阶梯价格列表
  395. Integer activityId = productMapper.findActivityByProductId(orderProduct.getProductID());
  396. if (activityId != null && activityId > 0) {
  397. List<ActivityLadderVo> ladderPriceList = productMapper.findActivityLadder(activityId, orderProduct.getProductID());
  398. int ladderNum = 0;
  399. for (ActivityLadderVo ladder : ladderPriceList) {
  400. ladderNum++;
  401. OrderProductLadderPricePo orderProductLadderPrice = new OrderProductLadderPricePo();
  402. orderProductLadderPrice.setOrderProductId(orderProduct.getOrderProductID());
  403. orderProductLadderPrice.setBuyNum(ladder.getBuyNum());
  404. orderProductLadderPrice.setBuyPrice(ladder.getBuyPrice());
  405. orderProductLadderPrice.setCreateDate(new Date());
  406. orderProductLadderPrice.setLadderNum(ladderNum);
  407. orderProductLadderPriceList.add(orderProductLadderPrice);
  408. }
  409. }
  410. }
  411. }
  412. if (!CollectionUtils.isEmpty(orderProductLadderPriceList)) {
  413. orderProductLadderPriceList.forEach(ladderPrice -> {
  414. orderSubmitMapper.insertOrderProductLadderPrice(ladderPrice);
  415. });
  416. }
  417. // 更新主订单信息, 子订单ID:1000,1002
  418. order.setShopOrderIDs(shopOrderIds);
  419. orderSubmitMapper.updateOrder(order);
  420. /**
  421. * 保存 订单用户地址
  422. */
  423. // 获取地址信息
  424. AddressVo address = orderSubmitMapper.findByAddressId(addressId);
  425. if (null != address) {
  426. //保存地址信息
  427. BpOrderUserInfoPo userInfo = new BpOrderUserInfoPo();
  428. userInfo.setOrderId(order.getOrderID());
  429. userInfo.setUserId(user.getUserID().longValue());
  430. userInfo.setName(user.getName() == null ? user.getUserName() : user.getName());
  431. userInfo.setShouHuoRen(address.getShouHuoRen());
  432. userInfo.setMobile(address.getMobile());
  433. userInfo.setPostalCode(address.getPhone());
  434. userInfo.setPostalCode(address.getPostalCode());
  435. userInfo.setTownId(address.getTownId());
  436. userInfo.setProvince(address.getProvinceName());
  437. userInfo.setCity(address.getCityName());
  438. userInfo.setTown(address.getTownName());
  439. userInfo.setAddress(address.getAddress());
  440. orderSubmitMapper.insertUserInfo(userInfo);
  441. log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>保存订单用户地址(insert[bp_order_userinfo])orderId:" + order.getOrderID());
  442. } else {
  443. //设置手动回滚事务
  444. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  445. return ResponseJson.error("订单地址异常", null);
  446. }
  447. /*
  448. * 保存优惠券使用情况
  449. */
  450. if (null != receiveCouponId && receiveCouponId > 0) {
  451. // 修改优惠券使用情况
  452. orderSubmitMapper.updateReceiveCouponStatus(receiveCouponId, order.getOrderID());
  453. CouponOrderRecordPo orderRecord = new CouponOrderRecordPo();
  454. orderRecord.setOrderId(order.getOrderID().intValue());
  455. orderRecord.setReceiveCouponId(receiveCouponId);
  456. orderRecord.setCouponType(coupon.getCouponType());
  457. orderRecord.setCouponAmount(coupon.getCouponAmount().doubleValue());
  458. orderRecord.setTouchPrice(1 == coupon.getNoThresholdFlag() ? 0 : coupon.getTouchPrice().doubleValue());
  459. orderRecord.setCreateDate(new Date());
  460. //保存订单优惠记录
  461. orderSubmitMapper.insertCouponOrderRecord(orderRecord);
  462. }
  463. /*
  464. * 保存好友消费券赠送记录
  465. */
  466. Integer couponShareId = couponMapper.getCouponShareId(userId);
  467. if (null != couponShareId) {
  468. String consumeCouponIds = couponMapper.getCurrentCouponIds(5);
  469. couponMapper.updateCouponShareRecord(couponShareId, consumeCouponIds);
  470. }
  471. log.info("******************** 提交订单逻辑处理 end *******************");
  472. /*
  473. * 构造返回参数
  474. */
  475. Map<String, String> info = new HashMap<>(5);
  476. info.put("orderId", String.valueOf(order.getOrderID()));
  477. info.put("orderNo", String.valueOf(order.getOrderNo()));
  478. info.put("orderMark", "#" + order.getOrderID() + "#");
  479. //应付订单金额
  480. info.put("payTotalFee", String.valueOf(order.getPayTotalFee()));
  481. //真实需要付款金额
  482. info.put("payableAmount", String.valueOf(order.getPayableAmount()));
  483. return ResponseJson.success(info);
  484. }
  485. /**
  486. * 整理订单商品数据
  487. *
  488. * @param productNum
  489. * @param product
  490. * @param priceType 0机构价,1活动阶梯价
  491. * @param productPrice
  492. * @return
  493. */
  494. private CmOrderProductPo setOrderProduct(Integer productNum, CmHeHeProductPo product, int priceType, BigDecimal productPrice, BigDecimal addedValueTax) {
  495. CmOrderProductPo orderProduct = new CmOrderProductPo();
  496. orderProduct.setShopID(product.getShopId().longValue());
  497. orderProduct.setProductID(product.getProductId());
  498. orderProduct.setOrganizeProductID(product.getId());
  499. // 预留在保存保存子订单的时候添加
  500. orderProduct.setProductNo(null);
  501. orderProduct.setNum(productNum);
  502. orderProduct.setPresentNum(0);
  503. orderProduct.setProductUnit(product.getUnit());
  504. if (MathUtil.compare(product.getNormalPrice(), 0) > 0) {
  505. orderProduct.setNormalPrice(product.getNormalPrice());
  506. } else {
  507. orderProduct.setNormalPrice(BigDecimal.ZERO);
  508. }
  509. orderProduct.setCostPrice(product.getCostPrice());
  510. orderProduct.setTotalAmount(MathUtil.mul(product.getPrice(), productNum));
  511. orderProduct.setDiscount(new BigDecimal(100));
  512. // 经理折扣(优惠金额)
  513. orderProduct.setDiscountFee(new BigDecimal(0));
  514. //机构税率
  515. orderProduct.setTaxRate(product.getClubTaxPoint() == null ? new BigDecimal(0) : product.getClubTaxPoint());
  516. //供应商税率
  517. orderProduct.setSupplierTaxRate(BigDecimal.valueOf(0));
  518. orderProduct.setIncludedTax(product.getIncludedTax().toString());
  519. orderProduct.setInvoiceType(product.getInvoiceType().toString());
  520. BigDecimal singleShouldPayTotalTax = new BigDecimal(0);
  521. //不含税可开发票商品设置税费
  522. if (0 == product.getIncludedTax() && (1 == product.getInvoiceType() || 2 == product.getInvoiceType())) {
  523. //供应商税费(单)=成本价 * 供应商税率
  524. if (product.getShopTaxPoint() == null) {
  525. orderProduct.setSupplierTaxRate(product.getClubTaxPoint());
  526. } else {
  527. orderProduct.setSupplierTaxRate(product.getShopTaxPoint());
  528. }
  529. singleShouldPayTotalTax = MathUtil.div(MathUtil.mul(product.getCostPrice(), orderProduct.getSupplierTaxRate()), BigDecimal.valueOf(100));
  530. } else if (1 == product.getIncludedTax()) {
  531. //含税商品设置供应商税率
  532. if (product.getShopTaxPoint() == null) {
  533. orderProduct.setSupplierTaxRate(product.getClubTaxPoint());
  534. } else {
  535. orderProduct.setSupplierTaxRate(product.getShopTaxPoint());
  536. }
  537. } else if ((0 == product.getIncludedTax() && 3 == product.getInvoiceType()) || 2 == product.getIncludedTax()) {
  538. //不含税不可开票商品和未知商品,税率置为0
  539. orderProduct.setTaxRate(BigDecimal.ZERO);
  540. orderProduct.setSupplierTaxRate(BigDecimal.ZERO);
  541. }
  542. orderProduct.setAddedValueTax(addedValueTax);
  543. //机构税费(总)=机构税费(单) * 商品数量
  544. orderProduct.setTotalAddedValueTax(MathUtil.mul(addedValueTax, productNum));
  545. orderProduct.setSingleShouldPayTotalTax(singleShouldPayTotalTax);
  546. //供应商税费(总)=供应商税费(单) * 商品数量
  547. orderProduct.setShouldPayTotalTax(MathUtil.mul(singleShouldPayTotalTax, productNum));
  548. orderProduct.setTotalFee(MathUtil.mul(productPrice, productNum));
  549. orderProduct.setShouldPayFee(MathUtil.mul(productPrice, productNum));
  550. // 商品费=成本价快照*(购买数量 + 赠品数量)
  551. orderProduct.setShopProductAmount(MathUtil.mul(product.getCostPrice(), BigDecimal.valueOf(productNum)));
  552. //不含税可开票商品,单价/折后单价=售价-税费
  553. //阶梯价
  554. BigDecimal price1 = productPrice;
  555. //不含税可开票商品,单价/折后单价=售价-税费
  556. if (0 == product.getIncludedTax() && (1 == product.getInvoiceType() || 2 == product.getInvoiceType())) {
  557. price1 = MathUtil.sub(productPrice, orderProduct.getAddedValueTax());
  558. }
  559. orderProduct.setPrice(price1);
  560. orderProduct.setDiscountPrice(price1);
  561. //应付供应商(单)=成本价+供应商税费(单)
  562. BigDecimal singleShopFee = MathUtil.add(product.getCostPrice(), singleShouldPayTotalTax);
  563. orderProduct.setSingleShopFee(singleShopFee);
  564. // 应付供应商(总)=应付供应商(单) * 商品数量
  565. orderProduct.setShopFee(MathUtil.mul(singleShopFee, BigDecimal.valueOf(productNum)));
  566. orderProduct.setOtherFee(new BigDecimal(0));
  567. orderProduct.setSingleOtherFee(new BigDecimal(0));
  568. //应付采美(单)=单价+机构税费(单)-(成本(单)+供应商税费(单))
  569. BigDecimal singleCmFee = MathUtil.sub(MathUtil.add(product.getPrice(), orderProduct.getAddedValueTax()), MathUtil.add(product.getCostPrice(), singleShouldPayTotalTax));
  570. orderProduct.setSingleCmFee(singleCmFee);
  571. // 应付采美(总)=应付采美(单)*商品数量
  572. BigDecimal cmFee = MathUtil.mul(singleCmFee, BigDecimal.valueOf(productNum));
  573. orderProduct.setCmFee(cmFee);
  574. orderProduct.setTotalBeans(new BigDecimal(0));
  575. orderProduct.setUseBalanceAmount(0d);
  576. // 优惠金额
  577. orderProduct.setPreferential(new BigDecimal(0));
  578. orderProduct.setUseBalanceAmount(0d);
  579. // 订单商品供应商确认标志 0否 1是
  580. orderProduct.setConfirmProductFlag("0");
  581. orderProduct.setShopName(product.getShopName());
  582. orderProduct.setName(product.getName());
  583. orderProduct.setPayStatus("0");
  584. orderProduct.setBuyAgainFlag("0");
  585. // 未出库数量
  586. orderProduct.setNotOutStore(productNum);
  587. orderProduct.setActPreferential(new BigDecimal(0));
  588. orderProduct.setActType(null);
  589. if (priceType == 1) {
  590. orderProduct.setIsActProduct("1");
  591. orderProduct.setLadderPriceFlag("1");
  592. } else {
  593. orderProduct.setIsActProduct("1");
  594. orderProduct.setLadderPriceFlag("0");
  595. }
  596. orderProduct.setProductImage(ProductUtils.getImageURL("product", product.getMainImage(), 0, domain));
  597. orderProduct.setProductType("0");
  598. return orderProduct;
  599. }
  600. /**
  601. * 保存子订单,并返回子订单ids
  602. *
  603. * @param order
  604. * @param orderProductList
  605. * @param shopId
  606. * @param shopNote
  607. * @return
  608. */
  609. private CmShopOrderPo saveShopOrder(CmOrderPo order, List<CmOrderProductPo> orderProductList, Integer shopId, String shopNote) {
  610. /*
  611. * 初始化子订单信息
  612. */
  613. CmShopOrderPo shopOrder = new CmShopOrderPo();
  614. // 子订单编号
  615. String shopOrderNo = "";
  616. String maxShopOrderNo = orderSubmitMapper.findMaxShopOrderNo(order.getOrderID());
  617. if (StringUtils.isNotBlank(maxShopOrderNo)) {
  618. shopOrderNo = maxShopOrderNo;
  619. shopOrder.setShopOrderNo(OrderNoUtils.getShopOrderNo(order.getOrderNo(), Integer.parseInt(shopOrderNo.substring(shopOrderNo.length() - 2, shopOrderNo.length())) + 1));
  620. } else {
  621. shopOrder.setShopOrderNo(OrderNoUtils.getShopOrderNo(order.getOrderNo(), 1));
  622. }
  623. shopOrder.setShopID(shopId);
  624. shopOrder.setOrderID(order.getOrderID());
  625. shopOrder.setOrderNo(order.getOrderNo());
  626. shopOrder.setUserID(order.getUserID().intValue());
  627. shopOrder.setOrganizeID(order.getOrganizeID());
  628. /*
  629. * 统计子订单金额信息
  630. */
  631. // 订单总金额
  632. BigDecimal totalAmount = new BigDecimal(0);
  633. // 商品总金额
  634. BigDecimal productAmount = new BigDecimal(0);
  635. // 需要支付金额
  636. BigDecimal needPayAmount = new BigDecimal(0);
  637. // 优惠金额
  638. BigDecimal preferential = new BigDecimal(0);
  639. // 佣金
  640. BigDecimal brokerage = new BigDecimal(0);
  641. // 商品费
  642. BigDecimal shopProductAmount = new BigDecimal(0);
  643. // 供应商税费
  644. BigDecimal shopTaxFee = new BigDecimal(0);
  645. // 总购买数
  646. Integer buyNum = 0;
  647. // 计算子订单信息
  648. for (CmOrderProductPo orderProduct : orderProductList) {
  649. if (shopId.longValue() == orderProduct.getShopID()) {
  650. // 商品总金额
  651. productAmount = MathUtil.add(productAmount, orderProduct.getTotalAmount());
  652. // 订单总金额 包括税费
  653. totalAmount = MathUtil.add(totalAmount, orderProduct.getTotalFee());
  654. // 应付金额
  655. needPayAmount = MathUtil.add(needPayAmount, orderProduct.getShouldPayFee());
  656. // 总购买数
  657. buyNum += orderProduct.getNum();
  658. preferential = MathUtil.add(preferential, orderProduct.getPreferential());
  659. brokerage = MathUtil.add(brokerage, orderProduct.getCmFee());
  660. if (null != orderProduct.getShopProductAmount()) {
  661. shopProductAmount = MathUtil.add(shopProductAmount, orderProduct.getShopProductAmount());
  662. }
  663. if (null != orderProduct.getShouldPayTotalTax()) {
  664. shopTaxFee = MathUtil.add(shopTaxFee, orderProduct.getShouldPayTotalTax());
  665. }
  666. }
  667. }
  668. shopOrder.setPromotionFullReduction(BigDecimal.ZERO);
  669. if (MathUtil.compare(shopOrder.getPromotionFullReduction(), 0) > 0) {
  670. totalAmount = MathUtil.sub(totalAmount, shopOrder.getPromotionFullReduction());
  671. productAmount = MathUtil.sub(productAmount, shopOrder.getPromotionFullReduction());
  672. needPayAmount = MathUtil.sub(needPayAmount, shopOrder.getPromotionFullReduction());
  673. }
  674. // freePostFlag: 0包邮 -1到付 1 有运费
  675. shopOrder.setNote(shopNote);
  676. shopOrder.setOrderTime(order.getOrderTime());
  677. shopOrder.setCanRefundFlag(1);
  678. shopOrder.setCanRefundAmount(needPayAmount.doubleValue());
  679. shopOrder.setAccountAmount(BigDecimal.ZERO);
  680. // 佣金 采美应收
  681. shopOrder.setBrokerage(brokerage);
  682. shopOrder.setBuyStatus("1");
  683. shopOrder.setPresentNum(0);
  684. shopOrder.setUseBeanFlag(0);
  685. shopOrder.setUseBeanAmount(0);
  686. shopOrder.setUseBalanceFlag(0);
  687. shopOrder.setRefundStatus(0);
  688. shopOrder.setRefundsAmount(BigDecimal.ZERO);
  689. shopOrder.setPayStatus("1");
  690. shopOrder.setZeroCostFlag(0);
  691. shopOrder.setSendOutStatus("1");
  692. shopOrder.setPayFlag("0");
  693. // 订单状态标识,1:非退货退款订单、2:退货退款中、3退货退款完成
  694. shopOrder.setOrderStatusFlag("1");
  695. shopOrder.setDelFlag("0");
  696. shopOrder.setOrderBeanAmount(0);
  697. // 购买商品数
  698. shopOrder.setItemCount(buyNum);
  699. // 普通订单 1 协销订单0 与cm_order一样
  700. shopOrder.setOrderType(order.getOrderType());
  701. shopOrder.setOrderSubmitType(order.getOrderSubmitType());
  702. shopOrder.setTotalAmount(totalAmount);
  703. shopOrder.setProductAmount(productAmount);
  704. shopOrder.setNeedPayAmount(needPayAmount);
  705. shopOrder.setPreferential(preferential);
  706. shopOrder.setShopProductAmount(shopProductAmount);
  707. // 付给供应商运费
  708. shopOrder.setShopPostFee(BigDecimal.ZERO);
  709. // 付给供应商税费
  710. shopOrder.setShopTaxFee(shopTaxFee);
  711. // 已付款金额
  712. shopOrder.setPayedShopAmount(BigDecimal.ZERO);
  713. // 付第三方
  714. shopOrder.setShopOtherFee(BigDecimal.ZERO);
  715. // 付供应商 = 商品费 + 运费 + 税费
  716. shopOrder.setShouldPayShopAmount(MathUtil.add(shopProductAmount, shopTaxFee));
  717. // 订单能否拆分 1 为可拆分, 0为不可拆分
  718. if (buyNum > 1) {
  719. shopOrder.setSplitFlag("1");
  720. } else {
  721. shopOrder.setSplitFlag("0");
  722. }
  723. /*
  724. * 保存子订单信息到数据库
  725. */
  726. orderSubmitMapper.insertShopOrder(shopOrder);
  727. log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>新增子订单(insert[cm_shop_order])shopOrderId:" + shopOrder.getShopOrderID());
  728. return shopOrder;
  729. }
  730. }