OrderUtil.java 16 KB

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