OrderService.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package com.ruoyi.app.order;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.ruoyi.app.service.WalletService;
  7. import com.ruoyi.common.core.domain.entity.SysDictData;
  8. import com.ruoyi.common.utils.DictUtils;
  9. import com.ruoyi.common.utils.MessageUtils;
  10. import com.ruoyi.common.exception.ServiceException;
  11. import com.ruoyi.system.domain.*;
  12. import com.ruoyi.system.mapper.PosOrderPromotionMapper;
  13. import com.ruoyi.system.service.IInfoUserService;
  14. import com.ruoyi.system.service.IPosStoreService;
  15. import com.ruoyi.system.service.IUserBillingService;
  16. import com.ruoyi.system.service.IVipUserQuanyiService;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Service;
  19. import java.math.BigDecimal;
  20. import java.text.DecimalFormat;
  21. import java.util.List;
  22. /**
  23. * 订单账单生成服务(商家分成、骑手分成)
  24. */
  25. @Service
  26. public class OrderService {
  27. @Autowired
  28. private IUserBillingService userBillingService;
  29. @Autowired
  30. private IPosStoreService posStoreService;
  31. @Autowired
  32. private IInfoUserService infoUserService;
  33. @Autowired
  34. private WalletService walletService;
  35. @Autowired
  36. private IVipUserQuanyiService userQuanyiService;
  37. /**
  38. * 执行商户分成
  39. */
  40. public void setSanghuBilling(PosOrder posOrder) {
  41. long count = userBillingService.count(new LambdaQueryWrapper<UserBilling>()
  42. .eq(UserBilling::getUserId, posOrder.getShId())
  43. .eq(UserBilling::getType, "0")
  44. .eq(UserBilling::getDdId, posOrder.getDdId()));
  45. if (count <= 0) {
  46. UserBilling billing = new UserBilling();
  47. DecimalFormat df = new DecimalFormat("#.00");
  48. // 判断是否为摊位订单
  49. PosStore store = posStoreService.getById(posOrder.getMdId());
  50. boolean isStall = store != null && store.getIsStall() != null && store.getIsStall() == 1 && store.getNightMarketId() != null;
  51. InfoUser user;
  52. if (isStall) {
  53. user = infoUserService.getById(store.getNightMarketId());
  54. } else {
  55. user = infoUserService.getById(posOrder.getShId());
  56. }
  57. Double fcbili = user.getCommission() == null ? 0.00 : user.getCommission();
  58. JSONArray list = JSONArray.parseArray(posOrder.getFood());
  59. int fenc = 0;
  60. for (int i = 0; i < list.size(); i++) {
  61. JSONObject foods = list.getJSONObject(i);
  62. int price = foods.getInteger("price");
  63. int otherPrice = foods.getInteger("otherPrice");
  64. int num = foods.getInteger("number");
  65. int age = (price + otherPrice) * num;
  66. fenc += age;
  67. }
  68. String remark = "";
  69. if (posOrder.getMdYhId() != null && posOrder.getMdDiscountAmount() != null) {
  70. VipUserQuanyi userQuanyi = userQuanyiService.getById(posOrder.getMdYhId());
  71. if (userQuanyi != null && "1".equals(userQuanyi.getType())) {
  72. fenc = fenc - posOrder.getMdDiscountAmount();
  73. String yhmcMessage = MessageUtils.message("no.posorder.md.yh.mc.messag");
  74. yhmcMessage = StrUtil.format(yhmcMessage, posOrder.getMdYhName());
  75. remark += yhmcMessage;
  76. String yhAmount = MessageUtils.message("no.posorder.md.yh.jiner.messag");
  77. yhAmount = StrUtil.format(yhAmount, posOrder.getMdDiscountAmount());
  78. remark += yhAmount;
  79. }
  80. }
  81. if (posOrder.getMdSalesName() != null && posOrder.getMdSalesReduction() != null) {
  82. fenc = fenc - posOrder.getMdSalesReduction();
  83. String cxNameMessage = MessageUtils.message("no.posorder.md.cx.mc.messag");
  84. cxNameMessage = StrUtil.format(cxNameMessage, posOrder.getMdSalesName());
  85. remark += cxNameMessage;
  86. String cxAmount = MessageUtils.message("no.posorder.md.cx.jiner.messag");
  87. cxAmount = StrUtil.format(cxAmount, posOrder.getMdSalesReduction());
  88. remark += cxAmount;
  89. }
  90. billing.setIllustrate(remark);
  91. Double chouc = fenc * fcbili;
  92. Double shfc = fenc - chouc;
  93. billing.setUserId(posOrder.getShId());
  94. billing.setDdId(String.valueOf(posOrder.getDdId()));
  95. billing.setType("0");
  96. billing.setAmount(Double.valueOf(df.format(shfc)));
  97. billing.setDivvy(Double.valueOf(df.format(chouc)));
  98. billing.setState("0");
  99. billing.setMdId(posOrder.getMdId());
  100. billing.setUserType("1");
  101. billing.setDivvyRate(fcbili);
  102. if (isStall) {
  103. billing.setStoreId(Long.valueOf(store.getId()));
  104. walletService.addStallBalance(Long.valueOf(store.getId()), BigDecimal.valueOf(billing.getAmount()), billing);
  105. } else {
  106. walletService.addBalance(billing.getUserId(), BigDecimal.valueOf(billing.getAmount()), billing);
  107. }
  108. }
  109. }
  110. /**
  111. * 执行骑手分成
  112. */
  113. public void setQishouBilling(PosOrder posOrder) {
  114. long count = userBillingService.count(new LambdaQueryWrapper<UserBilling>()
  115. .eq(UserBilling::getUserId, posOrder.getQsId())
  116. .eq(UserBilling::getType, "0")
  117. .eq(UserBilling::getDdId, posOrder.getDdId()));
  118. if (count <= 0) {
  119. UserBilling billing = new UserBilling();
  120. DecimalFormat df = new DecimalFormat("#.00");
  121. InfoUser user = infoUserService.getById(posOrder.getQsId());
  122. if (user == null) {
  123. System.out.println("订单无骑手");
  124. } else {
  125. Double fcbili = user.getCommission() == null ? 0.00 : user.getCommission();
  126. Double fenc = posOrder.getFreight();
  127. Double chouc = fenc * fcbili;
  128. Double shfc = fenc - chouc;
  129. List<SysDictData> taxBl = DictUtils.getDictCache("sys_qs_freight_bl");
  130. if (taxBl != null && taxBl.get(0) != null) {
  131. Double tax = shfc * Double.parseDouble(taxBl.get(0).getDictValue());
  132. billing.setTax(BigDecimal.valueOf(tax));
  133. billing.setTaxRate(taxBl.get(0).getDictValue());
  134. shfc = shfc - tax;
  135. }
  136. billing.setUserId(posOrder.getQsId());
  137. billing.setDdId(String.valueOf(posOrder.getDdId()));
  138. billing.setType("0");
  139. billing.setAmount(Double.valueOf(df.format(shfc)));
  140. billing.setDivvy(Double.valueOf(df.format(chouc)));
  141. billing.setState("0");
  142. billing.setMdId(posOrder.getMdId());
  143. billing.setUserType("2");
  144. billing.setDivvyRate(user.getCommission());
  145. if ("1".equals(posOrder.getCollectPayment())) {
  146. billing.setBehalfAmount(Double.valueOf(posOrder.getAmount()));
  147. }
  148. walletService.addBalance(billing.getUserId(), BigDecimal.valueOf(billing.getAmount()), billing);
  149. }
  150. }
  151. }
  152. /**
  153. * 执行自配送运费入账(029):运费全额(平台不抽成,2026-09-08 确认)入账配送方。
  154. * user_id=sh_id——普通商家单=商家本人,夜市摊位单=夜市主(摊位订单 shId 即夜市主,research F3),
  155. * 夜市主与摊位主之间的运费分摊线下自理。type="5" 与商品分成(type=0)区分,dedup 键 (userId,type,ddId) 互不冲突;
  156. * 运费为空或 0 不生成零元账单。
  157. */
  158. public void setSelfDeliveryFreightBilling(PosOrder posOrder) {
  159. if (posOrder.getFreight() == null || posOrder.getFreight() <= 0) {
  160. return;
  161. }
  162. long count = userBillingService.count(new LambdaQueryWrapper<UserBilling>()
  163. .eq(UserBilling::getUserId, posOrder.getShId())
  164. .eq(UserBilling::getType, "5")
  165. .eq(UserBilling::getDdId, posOrder.getDdId()));
  166. if (count <= 0) {
  167. UserBilling billing = new UserBilling();
  168. DecimalFormat df = new DecimalFormat("#.00");
  169. billing.setUserId(posOrder.getShId());
  170. billing.setDdId(String.valueOf(posOrder.getDdId()));
  171. billing.setType("5");
  172. billing.setAmount(Double.valueOf(df.format(posOrder.getFreight())));
  173. billing.setDivvy(0.00);
  174. billing.setDivvyRate(0.00);
  175. billing.setState("0");
  176. billing.setMdId(posOrder.getMdId());
  177. billing.setUserType("1");
  178. billing.setIllustrate(MessageUtils.message("no.billing.selfdelivery.freight"));
  179. walletService.addBalance(billing.getUserId(), BigDecimal.valueOf(billing.getAmount()), billing);
  180. }
  181. }
  182. }