StoreOrderPromotionsServices.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\dao\order\StoreOrderPromotionsDao;
  13. use app\services\BaseServices;
  14. use crmeb\traits\ServicesTrait;
  15. use think\exception\ValidateException;
  16. /**
  17. * Class StoreOrderPromotionsServices
  18. * @package app\services\order
  19. * @mixin StoreOrderPromotionsDao
  20. */
  21. class StoreOrderPromotionsServices extends BaseServices
  22. {
  23. use ServicesTrait;
  24. /**
  25. * StoreOrderPromotionsServices constructor.
  26. * @param StoreOrderPromotionsDao $dao
  27. */
  28. public function __construct(StoreOrderPromotionsDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 保存订单优惠详情
  34. * @param $oid
  35. * @param array $cartInfo
  36. * @param $uid
  37. * @return int
  38. */
  39. public function setPromotionsDetail(int $uid, int $oid, array $cartList, array $promotionsList)
  40. {
  41. $group = [];
  42. if ($promotionsList) {
  43. $time = time();
  44. foreach ($cartList as $key => $cart) {
  45. foreach ($promotionsList as $promotions) {
  46. $details = $promotions['details'] ?? [];
  47. $unique = $cart['product_attr_unique'] ?? '';
  48. if ($details && isset($details[$unique]['promotions_true_price'])) {
  49. $group[] = [
  50. 'oid' => $oid,
  51. 'uid' => $uid,
  52. 'product_id' => $cart['productInfo']['id'],
  53. 'promotions_id' => $promotions['id'],
  54. 'promotions_price' => bcmul((string)($details[$unique]['promotions_true_price'] ?? 0), (string)$cart['cart_num'], 2),
  55. 'add_time' => $time
  56. ];
  57. }
  58. }
  59. }
  60. }
  61. if ($group) {
  62. return $this->dao->saveAll($group);
  63. }
  64. return true;
  65. }
  66. /**
  67. * 获取订单商品实际参与优惠活动 以及优惠金额
  68. * @param int $oid
  69. * @return array
  70. */
  71. public function getOrderPromotionsDetail(int $oid)
  72. {
  73. $result = $this->dao->getPromotionsDetailList(['oid' => $oid], '*,sum(`promotions_price`) as promotions_price', ['promotions' => function($query) {
  74. $query->field('id,name,title,desc')->bind([
  75. 'promotions_type' => 'promotions_type',
  76. 'name' => 'name',
  77. 'title' => 'title',
  78. 'desc' => 'desc'
  79. ]);
  80. }], 'promotions_id');
  81. if ($result) {
  82. $typeArr = array_column($result, 'promotions_type');
  83. array_multisort($typeArr, SORT_ASC, $result);
  84. }
  85. return $result;
  86. }
  87. /**
  88. * 拆分订单同步拆分优惠活动记录
  89. * @param int $oid
  90. * @return bool
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\DbException
  93. * @throws \think\db\exception\ModelNotFoundException
  94. */
  95. public function splitOrderPromotions(int $oid)
  96. {
  97. /** @var StoreOrderServices $storeOrderServices */
  98. $storeOrderServices = app()->make(StoreOrderServices::class);
  99. $orderInfo = $storeOrderServices->getOne(['id' => $oid, 'is_del' => 0]);
  100. if (!$orderInfo) {
  101. throw new ValidateException('订单不存在');
  102. }
  103. $promotions_give = [];
  104. $promotions = [];
  105. if (isset($orderInfo['promotions_give']) && $orderInfo['promotions_give']) {
  106. $promotions_give = is_string($orderInfo['promotions_give']) ? json_decode($promotions_give, true) : $orderInfo['promotions_give'];
  107. }
  108. $promotions = $promotions_give['promotions'] ?? [];
  109. $pid = $orderInfo['pid'] > 0 ? $orderInfo['pid'] : $orderInfo['id'];
  110. //查询优惠记录
  111. $orderPromotions = $this->dao->getPromotionsDetailList(['order_id' => $oid]);
  112. //查询子订单
  113. $spliteOrder = $storeOrderServices->getColumn(['pid' => $pid, 'is_system_del' => 0], 'id,order_id');
  114. if ($spliteOrder && $orderPromotions && $promotions) {
  115. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  116. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  117. $data_all = $data = [];
  118. $data['uid'] = $orderInfo['uid'];
  119. $data['add_time'] = time();
  120. foreach ($spliteOrder as $order) {
  121. $cartInfo = $storeOrderCartInfoServices->getColumn(['oid' => $order['id']], 'id,product_id,cart_num,cart_info');
  122. if (!$cartInfo) {
  123. continue;
  124. }
  125. $data['oid'] = $order['id'];
  126. foreach ($cartInfo as $key => $cart) {
  127. $data['product_id'] = $cart['product_id'];
  128. $_info = is_string($cart['cart_info']) ? json_decode($cart['cart_info'], true) : $cart['cart_info'];
  129. $unique = $_info['product_attr_unique'] ?? '';
  130. foreach ($promotions as $key => $info) {
  131. if (isset($info['details'][$unique])) {
  132. $data['promotions_id'] = $info['id'];
  133. $data['promotions_price'] = floatval(bcmul((string)($info['details'][$unique]['promotions_true_price'] ?? 0), (string)$cart['cart_num'], 2));
  134. $data_all[] = $data;
  135. }
  136. }
  137. }
  138. }
  139. if ($data_all) {
  140. $this->transaction(function () use ($data_all, $spliteOrder) {
  141. $this->dao->delete(['oid' => array_column($spliteOrder, 'id')]);
  142. $this->dao->saveAll($data_all);
  143. });
  144. }
  145. }
  146. return true;
  147. }
  148. /**
  149. * 申请退款订单优惠详情处理
  150. * @param int $oid
  151. * @param array $cartInfo
  152. * @return bool
  153. */
  154. public function applyRefundOrderPromotions(int $oid, array $cartInfo = [])
  155. {
  156. /** @var StoreOrderServices $storeOrderServices */
  157. $storeOrderServices = app()->make(StoreOrderServices::class);
  158. $orderInfo = $storeOrderServices->getOne(['id' => $oid, 'is_del' => 0]);
  159. if (!$orderInfo) {
  160. return [];
  161. }
  162. $promotions_give = [];
  163. $promotions = [];
  164. if (isset($orderInfo['promotions_give']) && $orderInfo['promotions_give']) {
  165. $promotions_give = is_string($orderInfo['promotions_give']) ? json_decode($promotions_give, true) : $orderInfo['promotions_give'];
  166. }
  167. $promotions = $promotions_give['promotions'] ?? [];
  168. if (!$cartInfo || !$promotions) {
  169. return [];
  170. }
  171. $data_all = [];
  172. foreach ($promotions as $key => $info) {
  173. $data = [];
  174. foreach ($cartInfo as $key => $cart) {
  175. if (isset($info['details'][$cart['product_id']])) {
  176. $data['promotions_id'] = $info['id'];
  177. $data['promotions_type'] = $info['promotions_type'];
  178. $data['name'] = $info['name'];
  179. $data['title'] = $info['title'];
  180. $data['desc'] = $info['desc'];
  181. $data['promotions_price'] = bcadd((string)($data['promotions_price'] ?? 0), (string)bcmul((string)($info['details'][$cart['product_id']]['promotions_true_price'] ?? 0), (string)$cart['cart_num'], 2), 2);
  182. }
  183. }
  184. if ($data) {
  185. $data_all[] = $data;
  186. }
  187. }
  188. if ($data_all) {
  189. $typeArr = array_column($data_all, 'promotions_type');
  190. array_multisort($typeArr, SORT_ASC, $data_all);
  191. }
  192. return $data_all;
  193. }
  194. }