OrderUtil.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. package com.caimei.modules.order.utils;
  2. import com.caimei.modules.order.entity.NewOrder;
  3. import com.caimei.modules.order.entity.NewOrderProduct;
  4. import com.caimei.modules.order.entity.NewShopOrder;
  5. import com.caimei.modules.product.entity.CmPromotion;
  6. import com.caimei.utils.MathUtil;
  7. import java.math.BigDecimal;
  8. import java.util.*;
  9. public class OrderUtil {
  10. /**
  11. * 获取主订单下的所有商品
  12. *
  13. * @param newOrder
  14. * @return
  15. */
  16. public static List<NewOrderProduct> getOrderProducts(NewOrder newOrder) {
  17. List<NewOrderProduct> orderProducts = new ArrayList<>();
  18. List<NewShopOrder> shopOrders = newOrder.getNewShopOrders();
  19. if (newOrder != null && shopOrders != null) {
  20. for (NewShopOrder so : shopOrders) {
  21. orderProducts.addAll(so.getNewOrderProducts());
  22. }
  23. }
  24. return orderProducts;
  25. }
  26. /**
  27. * 主订单拆分
  28. *
  29. * @param order
  30. */
  31. public static void orderSplit(NewOrder order,boolean payOnline) {
  32. int shopOrderNoIndex = 0;
  33. List<NewOrderProduct> orderProducts = order.getOrderProduct();
  34. // 促销
  35. List<CmPromotion> promotionsList = order.getPromotions();
  36. List<NewShopOrder> newShopOrders = new ArrayList<>();
  37. Map<String, NewShopOrder> map = new HashMap<>();
  38. // 把订单中的商品根据供应商不同进行分组,生成子订单
  39. for (NewOrderProduct o : orderProducts) {
  40. //子订单存在了 追加orderProduct 到 shopOrder
  41. if (map.containsKey(payOnline ? o.getSplitCode() + "-" + o.getShopID() : o.getShopID().toString())) {
  42. NewShopOrder shopOrder = map.get(payOnline ? o.getSplitCode() + "-" + o.getShopID() : o.getShopID().toString());
  43. shopOrder.getNewOrderProducts().add(o);
  44. shopOrder.setItemCount(o.getNum() + shopOrder.getItemCount()); // 子订单下商品数量统计 购买数量
  45. shopOrder.setPresentNum(o.getPresentNum() + shopOrder.getPresentNum());
  46. shopOrder.setPreferential(shopOrder.getPreferential() + o.getPreferential()); // 优惠金额
  47. shopOrder.setProductAmount(shopOrder.getProductAmount() + o.getTotalAmount()); // 商品总金额
  48. shopOrder.setDiscountFee(shopOrder.getDiscountFee() + o.getDiscountFee()); //经理折扣统计
  49. shopOrder.setTotalAmount(shopOrder.getTotalAmount() + o.getTotalFee());
  50. shopOrder.setNeedPayAmount(shopOrder.getNeedPayAmount() + o.getShouldPayFee());
  51. shopOrder.setTotalAddedValueTax(shopOrder.getTotalAddedValueTax() + o.getTotalAddedValueTax()); //税费
  52. shopOrder.setBrokerage(shopOrder.getBrokerage() + o.getCmFee()); //佣金 = 应付采美
  53. // 计算超级会员优惠
  54. if (null != o.getSvipPriceFlag() && 1 == o.getSvipPriceFlag()) {
  55. BigDecimal productTotalReduction = MathUtil.mul(o.getSvipReduction(), o.getNum(), 2);
  56. shopOrder.setSvipShopReduction(MathUtil.add(shopOrder.getSvipShopReduction(), productTotalReduction).doubleValue());
  57. }
  58. } else {
  59. // 子订单如果不存在 实例化shopOrder
  60. NewShopOrder shopOrder = new NewShopOrder();
  61. shopOrder.setDelFlag("0");
  62. shopOrder.setClubID(order.getClubID());
  63. shopOrder.setCanRefundAmount(0D);
  64. shopOrder.setRefundAmount(0D);
  65. shopOrder.setAccountAmount(0D);
  66. shopOrder.setAutoOverTimeMills(0L);
  67. shopOrder.setAutoReceiveTimeMills(0L);
  68. shopOrder.setPayFlag("0");
  69. shopOrder.setPayStatus("1");
  70. shopOrder.setZeroCostFlag(0);
  71. shopOrder.setSplitFlag("1"); //订单默认可拆分
  72. shopOrder.setOrderTime(order.getOrderTime());
  73. shopOrder.setShopOrderNo(getshopOrderNo(order.getOrderNo(), ++shopOrderNoIndex));
  74. shopOrder.setShopID(o.getShopID());
  75. shopOrder.setItemCount(o.getNum());
  76. shopOrder.setSendOutStatus("1");
  77. shopOrder.setOrderSubmitType(4);
  78. shopOrder.setFee(null);
  79. // 赠品单独列出了
  80. shopOrder.setPresentNum(0);
  81. shopOrder.setProductAmount(o.getTotalAmount());
  82. shopOrder.setDiscountFee(o.getDiscountFee());
  83. shopOrder.setTotalAmount(o.getTotalFee());
  84. shopOrder.setPreferential(o.getPreferential());
  85. shopOrder.setTotalAddedValueTax(o.getTotalAddedValueTax());
  86. List<NewOrderProduct> newOrderProducts = new ArrayList<>(); // 实例化订单商品集合
  87. newOrderProducts.add(o);
  88. shopOrder.setNewOrderProducts(newOrderProducts);
  89. shopOrder.setBrokerage(o.getCmFee());
  90. shopOrder.setDiscountAmount((o.getPrice() - o.getDiscountPrice()) * o.getNum());
  91. shopOrder.setTotalAmount(o.getTotalFee());
  92. shopOrder.setNeedPayAmount(o.getShouldPayFee());
  93. shopOrder.setOutStoreNum(0);
  94. shopOrder.setShopReceiptStatus("3".equals(order.getReceiptStatus()) ? 3 : 1);
  95. shopOrder.setRealPay(0d);
  96. shopOrder.setEachDiscount(0d);
  97. shopOrder.setReceiptAmount(0d);
  98. shopOrder.setSplitCode(o.getSplitCode());
  99. // 计算超级会员优惠
  100. if (null != o.getSvipPriceFlag() && 1 == o.getSvipPriceFlag()) {
  101. BigDecimal productTotalReduction = MathUtil.mul(o.getSvipReduction(), o.getNum(), 2);
  102. shopOrder.setSvipShopReduction(MathUtil.add(shopOrder.getSvipShopReduction(), productTotalReduction).doubleValue());
  103. } else {
  104. shopOrder.setSvipShopReduction(0d);
  105. }
  106. map.put(payOnline ? o.getSplitCode() + "-" + o.getShopID() : o.getShopID().toString(), shopOrder);
  107. newShopOrders.add(shopOrder); //在主订单中保存子订单的引用
  108. }
  109. }
  110. order.setNewShopOrders(newShopOrders);
  111. }
  112. /**
  113. * 根据 orderProducts里面的orderProduct 信息重新统计 shopOrder
  114. */
  115. public static void updateShopOrderInfo(NewShopOrder shopOrder, List<NewOrderProduct> orderProducts, List<CmPromotion> promotionsList, boolean onlinePay) {
  116. if (orderProducts == null || orderProducts.size() == 0) return;
  117. int itemCount = 0; //商品数量统计 (不含赠品)
  118. double productAmount = 0D;// 商品总金额 = 总价
  119. double discountAmount = 0D; //折扣金额
  120. double totalAmount = 0D; //总价 = 商品总金额
  121. double needPayAmount = 0D; // 需要支付金额 = 总金额 - 采美豆 - 余额 - 折扣-其它
  122. double preferential = 0D;
  123. int presentNum = 0;
  124. double totalAddedValueTax = 0D;
  125. int outStoreNum = 0; //未出库数量
  126. double discountFee = 0D; //经理折扣
  127. String shopOrderNo = null;
  128. Integer shopID = null;
  129. double brokerage = 0D;//佣金
  130. Double shopProductAmount = 0D; //商品费
  131. Double shopTaxFee = 0D; //付给供应商税费
  132. double promotionsTouchPrice = 0d;
  133. double promotionsReducedPrice = 0d;
  134. double shopTouchPrice = 0d;
  135. double shopReducedPrice = 0d;
  136. double productDiscount = 0d;
  137. double svipShopReduction = 0d;
  138. for (NewOrderProduct o : orderProducts) {
  139. // 没有折扣时促销才生效
  140. if (o.getDiscount() == 100d && !onlinePay) {
  141. for (CmPromotion promotion : promotionsList) {
  142. if ("3".equals(promotion.getType())) {
  143. shopOrder.setOrderPromotionsId(o.getOrderPromotionsId());
  144. if ("2".equals(promotion.getMode())) {
  145. promotionsTouchPrice = promotion.getTouchPrice();
  146. promotionsReducedPrice = promotion.getReducedPrice();
  147. shopTouchPrice += (o.getNum() * o.getPrice());
  148. }
  149. }
  150. // 单品
  151. if ("1".equals(promotion.getType())) {
  152. // 优惠价
  153. if ("1".equals(promotion.getMode())) {
  154. shopReducedPrice += (o.getPrice() - promotion.getTouchPrice()) * o.getNum();
  155. productDiscount += (o.getPrice() - promotion.getTouchPrice()) * o.getNum();
  156. }
  157. // 单品满减
  158. if ("2".equals(promotion.getMode())) {
  159. if (o.getPrice() * o.getNum() > promotion.getTouchPrice()) {
  160. shopReducedPrice += promotion.getReducedPrice();
  161. }
  162. }
  163. }
  164. }
  165. }
  166. // 计算超级会员优惠
  167. if (null != o.getSvipPriceFlag() && 1 == o.getSvipPriceFlag()) {
  168. BigDecimal productTotalReduction = MathUtil.mul(o.getSvipReduction(), o.getNum(), 2);
  169. svipShopReduction = MathUtil.add(shopOrder.getSvipShopReduction(), productTotalReduction).doubleValue();
  170. }
  171. if (shopOrderNo == null) shopOrderNo = o.getShopOrderNo();
  172. if (shopID == null) shopID = o.getShopID();
  173. itemCount += (o.getNum() == null ? 0 : o.getNum());
  174. totalAddedValueTax += (o.getTotalAddedValueTax() == null ? 0D : o.getTotalAddedValueTax());
  175. //(这前单价 - 折后单间) * 数量(含赠品) = 折扣金额
  176. discountAmount += (o.getDiscountFee() == null ? 0D : o.getDiscountFee());
  177. productAmount += (o.getTotalAmount() == null ? 0D : o.getTotalAmount());
  178. totalAmount += (o.getTotalFee() == null ? 0D : o.getTotalFee());
  179. needPayAmount += (o.getShouldPayFee() == null ? 0D : o.getShouldPayFee());
  180. preferential += (o.getPreferential() == null ? 0D : o.getPreferential());
  181. presentNum += (o.getPresentNum() == null ? 0 : o.getPresentNum());
  182. outStoreNum += o.getNum() - (o.getNotOutStore() == null ? 0 : o.getNotOutStore());
  183. discountFee += (o.getDiscountFee() == null ? 0D : o.getDiscountFee());
  184. brokerage += (o.getCmFee() == null ? 0D : o.getCmFee());
  185. shopProductAmount += (o.getShopProductAmount() == null ? 0D : o.getShopProductAmount());
  186. shopTaxFee += (o.getShouldPayTotalTax() == null ? 0D : o.getShouldPayTotalTax());
  187. }
  188. if (shopTouchPrice > promotionsTouchPrice) {
  189. shopReducedPrice += promotionsReducedPrice;
  190. }
  191. shopOrder.setPromotionFullReduction(shopReducedPrice);
  192. shopOrder.setNeedPayAmount(needPayAmount - shopReducedPrice + productDiscount);
  193. shopOrder.setTotalAddedValueTax(totalAddedValueTax);
  194. shopOrder.setItemCount(itemCount);
  195. shopOrder.setProductAmount(productAmount);
  196. shopOrder.setDiscountAmount(discountAmount);
  197. shopOrder.setTotalAmount(totalAmount);
  198. shopOrder.setShopOrderNo(shopOrderNo);
  199. shopOrder.setPreferential(preferential);
  200. shopOrder.setPresentNum(presentNum);
  201. shopOrder.setShopID(shopID);
  202. shopOrder.setOutStoreNum(outStoreNum);
  203. shopOrder.setDiscountFee(discountFee);
  204. shopOrder.setBrokerage(brokerage);
  205. shopOrder.setShopProductAmount(shopProductAmount);
  206. shopOrder.setShopPostFee(0D);
  207. shopOrder.setShopTaxFee(shopTaxFee);
  208. shopOrder.setShouldPayShopAmount(shopProductAmount + shopTaxFee);
  209. shopOrder.setPayedShopAmount(0D);
  210. shopOrder.setShopOtherFee(0D);
  211. shopOrder.setSvipShopReduction(svipShopReduction);
  212. if (presentNum == 0 && itemCount == 0)
  213. shopOrder.setDelFlag("1");// 这个子订单里面的商品全部被拆分走了 这个子订单就没有意义了
  214. }
  215. /**
  216. * 根据父订单单号 以及子订单的序列生成子订单编号
  217. *
  218. * @param orderNo
  219. * @param i
  220. * @return
  221. */
  222. public static String getshopOrderNo(String orderNo, int i) {
  223. if (i < 10) {
  224. return orderNo + "0" + i;
  225. } else {
  226. return orderNo + i;
  227. }
  228. }
  229. /**
  230. * 订单模块生成新订单号
  231. *
  232. * @param platform
  233. * @return
  234. */
  235. public static String getCmOrderNo(String platform) {
  236. Random rand = new Random();
  237. String code = "";
  238. for (int j = 0; j < 5; j++) {
  239. code += rand.nextInt(10) + "";
  240. }
  241. return platform + System.currentTimeMillis() / 1000 + code;
  242. }
  243. public static String geneCrmOrderNo() {
  244. return getCmOrderNo("C");
  245. }
  246. public static String geneWwwOrderNo() {
  247. return getCmOrderNo("W");
  248. }
  249. public static String geneAdminOrderNo() {
  250. return getCmOrderNo("P");
  251. }
  252. public static void main(String[] args) {
  253. System.out.println(geneWwwOrderNo());
  254. }
  255. public static Boolean isBuFenShouKuan(String code) {
  256. Boolean res = false;
  257. res = NewOrderStatus.isBuFenShouKuan(code);
  258. return res;
  259. }
  260. public static String getPayTypeStr(String payType) {
  261. // 1建设银行7297、2广发银行0115、3中信银行7172、4中信银行0897、5中信银行0897-财付通、
  262. // 6中信银行0897-支付宝、7线上-支付宝、8线上-微信支付、9线上-快钱支付,10口头返佣',
  263. if ("1".equals(payType)) {
  264. return "建设银行7297";
  265. }
  266. if ("2".equals(payType)) {
  267. return "广发银行0115";
  268. }
  269. if ("3".equals(payType)) {
  270. return "中信银行7172";
  271. }
  272. if ("4".equals(payType)) {
  273. return "中信银行0897";
  274. }
  275. if ("5".equals(payType)) {
  276. return "中信银行0897-财付通";
  277. }
  278. if ("6".equals(payType)) {
  279. return "中信银行0897-支付宝";
  280. }
  281. if ("7".equals(payType)) {
  282. return "线上-支付宝";
  283. }
  284. if ("8".equals(payType)) {
  285. return "线上-微信支付";
  286. }
  287. if ("9".equals(payType)) {
  288. return "线上-快钱支付";
  289. }
  290. if ("10".equals(payType)) {
  291. return "口头返佣";
  292. }
  293. if ("11".equals(payType)) {
  294. return "广发银行5461";
  295. }
  296. if ("12".equals(payType)) {
  297. return "企业网银";
  298. }
  299. if ("13".equals(payType)) {
  300. return "PC-微信支付";
  301. }
  302. if ("14".equals(payType)) {
  303. return "PC-支付宝";
  304. }
  305. if ("15".equals(payType)) {
  306. return "小程序-微信支付";
  307. }
  308. if ("16".equals(payType)) {
  309. return "余额抵扣";
  310. }
  311. if ("17".equals(payType)) {
  312. return "个人网银";
  313. }
  314. if ("18".equals(payType)) {
  315. return "建设银行1854";
  316. }
  317. if ("19".equals(payType)) {
  318. return "建设银行6256";
  319. }
  320. if ("20".equals(payType)) {
  321. return "建设银行3346";
  322. }
  323. return "";
  324. }
  325. /**
  326. * @return
  327. * @Author ye.qin
  328. * @Description //TODO 获取付款类型
  329. * @Date 2019\8\9 0009 10:35
  330. * @Param
  331. */
  332. public static String getPayType(String payType) {
  333. // 1建设银行7297, 2中信银行0897, 3中信银行7172, 4广发银行0115, 5广发银行5461
  334. if ("1".equals(payType))
  335. return "建设银行7297";
  336. if ("2".equals(payType))
  337. return "中信银行0897";
  338. if ("3".equals(payType))
  339. return "中信银行7172";
  340. if ("4".equals(payType))
  341. return "广发银行0115";
  342. if ("5".equals(payType))
  343. return "广发银行5461";
  344. return payType;
  345. }
  346. public static String getReceiptType(String receiptType) {
  347. if ("1".equals(receiptType))
  348. return "订单";
  349. if ("2".equals(receiptType))
  350. return "非订单";
  351. if ("3".equals(receiptType))
  352. return "返佣";
  353. return "";
  354. }
  355. public static String getReceiptStatus(String receiptStatus) {
  356. if ("1".equals(receiptStatus))
  357. return "待确认";
  358. if ("2".equals(receiptStatus))
  359. return "已确认";
  360. if ("3".equals(receiptStatus))
  361. return "审核通过";
  362. if ("4".equals(receiptStatus))
  363. return "审核未通过";
  364. if ("5".equals(receiptStatus))
  365. return "收款撤销";
  366. return "";
  367. }
  368. public static String getStatus(String status) {
  369. if ("0".equals(status))
  370. return "待确认";
  371. if ("11".equals(status))
  372. return "待收款待发货";
  373. if ("12".equals(status))
  374. return "待收款部分发货";
  375. if ("13".equals(status))
  376. return "待收款全发货";
  377. if ("21".equals(status))
  378. return "部分收款待发货";
  379. if ("22".equals(status))
  380. return "部分收款部分发货";
  381. if ("23".equals(status))
  382. return "部分收款全部发货";
  383. if ("31".equals(status))
  384. return "已收款待发货";
  385. if ("32".equals(status))
  386. return "已收款部分发货";
  387. if ("33".equals(status))
  388. return "已收款全发货";
  389. if ("4".equals(status))
  390. return "交易完成";
  391. if ("5".equals(status))
  392. return "订单完成";
  393. if ("6".equals(status))
  394. return "已关闭";
  395. if ("7".equals(status))
  396. return "交易全退";
  397. return "";
  398. }
  399. }