StoreOrderCartInfoServices.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 crmeb\utils\Str;
  13. use app\services\BaseServices;
  14. use crmeb\services\CacheService;
  15. use app\dao\order\StoreOrderCartInfoDao;
  16. /**
  17. * Class StoreOrderCartInfoServices
  18. * @package app\services\order
  19. * @method array getCartColunm(array $where, string $field, ?string $key) 获取购物车信息以数组返回
  20. * @method array getCartInfoList(array $where, array $field) 获取购物车详情列表
  21. * @method getOne(array $where, ?string $field = '*', array $with = []) 根据条件获取一条数据
  22. */
  23. class StoreOrderCartInfoServices extends BaseServices
  24. {
  25. /**
  26. * StorePinkServices constructor.
  27. * @param StorePinkDao $dao
  28. */
  29. public function __construct(StoreOrderCartInfoDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 获取指定订单下的商品详情
  35. * @param int $oid
  36. * @return array|mixed
  37. */
  38. public function getOrderCartInfo(int $oid)
  39. {
  40. $cartInfo = CacheService::get(md5('store_order_cart_info_' . $oid));
  41. if ($cartInfo) return $cartInfo;
  42. $cart_info = $this->dao->getColumn(['oid' => $oid], 'cart_info', 'cart_id');
  43. $info = [];
  44. foreach ($cart_info as $k => $v) {
  45. $_info = is_string($v) ? json_decode($v, true) : $v;
  46. if (!isset($_info['productInfo'])) $_info['productInfo'] = [];
  47. $info[$k]['cart_info'] = $_info;
  48. unset($_info);
  49. }
  50. CacheService::set(md5('store_order_cart_info_' . $oid), $info);
  51. return $info;
  52. // return CacheService::get(md5('store_order_cart_info_' . $oid), function () use ($oid) {
  53. // $cart_info = $this->dao->getColumn(['oid' => $oid], 'cart_info', 'cart_id');
  54. // $info = [];
  55. // foreach ($cart_info as $k => $v) {
  56. // $_info = is_string($v) ? json_decode($v, true) : $v;
  57. // if (!isset($_info['productInfo'])) $_info['productInfo'] = [];
  58. // $info[$k]['cart_info'] = $_info;
  59. // unset($_info);
  60. // }
  61. // return $info;
  62. // }) ?: [];
  63. }
  64. /**
  65. * 查找购物车里的所有商品标题
  66. * @param $cartId
  67. * @return bool|string
  68. */
  69. public function getCarIdByProductTitle($cartId, $goodsNum = false)
  70. {
  71. $title = '';
  72. $orderCart = $this->dao->getCartInfoList(['cart_id' => $cartId], ['cart_info']);
  73. foreach ($orderCart as $item) {
  74. if (isset($item['cart_info']['productInfo']['store_name'])) {
  75. if ($goodsNum && isset($item['cart_info']['cart_num'])) {
  76. $title .= $item['cart_info']['productInfo']['store_name'] . ' * '.$item['cart_info']['cart_num'].' | ';
  77. }else{
  78. $title .= $item['cart_info']['productInfo']['store_name'] . '|';
  79. }
  80. }
  81. }
  82. if ($title) {
  83. $title = substr($title, 0, strlen($title) - 1);
  84. }
  85. return $title;
  86. }
  87. /**
  88. * 获取打印订单的商品信息
  89. * @param array $cartId
  90. * @return array
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\DbException
  93. * @throws \think\db\exception\ModelNotFoundException
  94. */
  95. public function getCartInfoPrintProduct(array $cartId)
  96. {
  97. $cartInfo = $this->dao->getCartInfoList(['cart_id' => $cartId], ['cart_info']);
  98. $product = [];
  99. foreach ($cartInfo as $item) {
  100. $value = is_string($item['cart_info']) ? json_decode($item['cart_info'], true) : $item['cart_info'];
  101. $value['productInfo']['store_name'] = $value['productInfo']['store_name'] ?? "";
  102. $value['productInfo']['store_name'] = Str::substrUTf8($value['productInfo']['store_name'], 10, 'UTF-8', '');
  103. $product[] = $value;
  104. }
  105. return $product;
  106. }
  107. /**
  108. * 获取产品返佣金额
  109. * @param array $cartId
  110. * @param bool $type true = 一级返佣, fasle = 二级返佣
  111. * @return string
  112. */
  113. public function getProductBrokerage(array $cartId, bool $type = true)
  114. {
  115. $cartInfo = $this->dao->getCartInfoList(['cart_id' => $cartId], ['cart_info']);
  116. $oneBrokerage = '0';//一级返佣金额
  117. $twoBrokerage = '0';//二级返佣金额
  118. $sumProductPrice = '0';//非指定返佣商品总金额
  119. foreach ($cartInfo as $value) {
  120. $cartNum = $value['cart_info']['cart_num'] ?? 0;
  121. if (isset($value['cart_info']['productInfo'])) {
  122. $productInfo = $value['cart_info']['productInfo'];
  123. //指定返佣金额
  124. if (isset($productInfo['is_sub']) && $productInfo['is_sub'] == 1) {
  125. $oneBrokerage = bcadd($oneBrokerage, bcmul($cartNum, $productInfo['attrInfo']['brokerage'] ?? 0, 2), 2);
  126. $twoBrokerage = bcadd($twoBrokerage, bcmul($cartNum, $productInfo['attrInfo']['brokerage_two'] ?? 0, 2), 2);
  127. } else {
  128. //比例返佣
  129. if (isset($productInfo['attrInfo'])) {
  130. $sumProductPrice = bcadd($sumProductPrice, bcmul($cartNum, $productInfo['attrInfo']['price'] ?? 0, 2), 2);
  131. } else {
  132. $sumProductPrice = bcadd($sumProductPrice, bcmul($cartNum, $productInfo['price'] ?? 0, 2), 2);
  133. }
  134. }
  135. }
  136. }
  137. if ($type) {
  138. //获取后台一级返佣比例
  139. $storeBrokerageRatio = sys_config('store_brokerage_ratio');
  140. //一级返佣比例 小于等于零时直接返回 不返佣
  141. if ($storeBrokerageRatio <= 0) {
  142. return $oneBrokerage;
  143. }
  144. //计算获取一级返佣比例
  145. $brokerageRatio = bcdiv($storeBrokerageRatio, 100, 4);
  146. $brokeragePrice = bcmul($sumProductPrice, $brokerageRatio, 2);
  147. //固定返佣 + 比例返佣 = 一级总返佣金额
  148. return bcadd($oneBrokerage, $brokeragePrice, 2);
  149. } else {
  150. //获取二级返佣比例
  151. $storeBrokerageTwo = sys_config('store_brokerage_two');
  152. //二级返佣比例小于等于0 直接返回
  153. if ($storeBrokerageTwo <= 0) {
  154. return $twoBrokerage;
  155. }
  156. //计算获取二级返佣比例
  157. $brokerageRatio = bcdiv($storeBrokerageTwo, 100, 4);
  158. $brokeragePrice = bcmul($sumProductPrice, $brokerageRatio, 2);
  159. //固定返佣 + 比例返佣 = 二级总返佣金额
  160. return bcadd($twoBrokerage, $brokeragePrice, 2);
  161. }
  162. }
  163. /**
  164. * 保存购物车info
  165. * @param $oid
  166. * @param array $cartInfo
  167. * @return int
  168. */
  169. public function setCartInfo($oid, array $cartInfo)
  170. {
  171. $group = [];
  172. foreach ($cartInfo as $cart) {
  173. $group[] = [
  174. 'oid' => $oid,
  175. 'cart_id' => $cart['id'],
  176. 'product_id' => $cart['productInfo']['id'],
  177. 'cart_info' => json_encode($cart),
  178. 'unique' => md5($cart['id'] . '' . $oid)
  179. ];
  180. }
  181. return $this->dao->saveAll($group);
  182. }
  183. /**
  184. * 商品编号
  185. * @param $cartId
  186. * @return array
  187. */
  188. public function getCartIdsProduct($cartId)
  189. {
  190. return $this->dao->getColumn([['cart_id', 'in', $cartId]], 'product_id', 'oid');
  191. }
  192. }