StoreOrderCartInfoServices.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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 getSplitCartNum(array $cart_id)
  22. * @method getOne(array $where, ?string $field = '*', array $with = []) 根据条件获取一条数据
  23. */
  24. class StoreOrderCartInfoServices extends BaseServices
  25. {
  26. /**
  27. * StoreOrderCartInfoServices constructor.
  28. * @param StoreOrderCartInfoDao $dao
  29. */
  30. public function __construct(StoreOrderCartInfoDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 清空订单商品缓存
  36. * @param int $oid
  37. * @return bool
  38. * @throws \Psr\SimpleCache\InvalidArgumentException
  39. */
  40. public function clearOrderCartInfo(int $oid)
  41. {
  42. return CacheService::delete(md5('store_order_cart_info_' . $oid));
  43. }
  44. /**
  45. * 获取指定订单下的商品详情
  46. * @param int $oid
  47. * @return array|bool|mixed
  48. * @throws \ReflectionException
  49. */
  50. public function getOrderCartInfo(int $oid)
  51. {
  52. $cartInfo = CacheService::get(md5('store_order_cart_info_' . $oid));
  53. if ($cartInfo) return $cartInfo;
  54. $cart_info = $this->dao->getColumn(['oid' => $oid], 'cart_info', 'cart_id');
  55. $info = [];
  56. foreach ($cart_info as $k => $v) {
  57. $_info = is_string($v) ? json_decode($v, true) : $v;
  58. if (!isset($_info['productInfo'])) $_info['productInfo'] = [];
  59. //缩略图处理
  60. if (isset($_info['productInfo']['attrInfo'])) {
  61. $_info['productInfo']['attrInfo'] = get_thumb_water($_info['productInfo']['attrInfo']);
  62. }
  63. $_info['productInfo'] = get_thumb_water($_info['productInfo']);
  64. $_info['refund_num'] = $this->dao->sum(['cart_id' => $_info['id']], 'refund_num');
  65. $info[$k]['cart_info'] = $_info;
  66. unset($_info);
  67. }
  68. CacheService::set(md5('store_order_cart_info_' . $oid), $info);
  69. return $info;
  70. }
  71. /**
  72. * 查找购物车里的所有商品标题
  73. * @param int $oid
  74. * @param false $goodsNum
  75. * @return bool|mixed|string
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function getCarIdByProductTitle(int $oid, bool $goodsNum = false)
  81. {
  82. if ($goodsNum) {
  83. $key = md5('store_order_cart_product_title_num' . $oid);
  84. } else {
  85. $key = md5('store_order_cart_product_title_' . $oid);
  86. }
  87. $title = CacheService::get($key);
  88. if (!$title) {
  89. $orderCart = $this->dao->getCartInfoList(['oid' => $oid], ['cart_info']);
  90. foreach ($orderCart as $item) {
  91. if (isset($item['cart_info']['productInfo']['store_name'])) {
  92. if ($goodsNum && isset($item['cart_info']['cart_num'])) {
  93. $title .= $item['cart_info']['productInfo']['store_name'] . ' * ' . $item['cart_info']['cart_num'] . ' | ';
  94. } else {
  95. $title .= $item['cart_info']['productInfo']['store_name'] . '|';
  96. }
  97. }
  98. }
  99. if ($title) {
  100. $title = substr($title, 0, strlen($title) - 1);
  101. }
  102. CacheService::set($key, $title);
  103. }
  104. return $title ?: '';
  105. }
  106. /**
  107. * 获取打印订单的商品信息
  108. * @param $oid
  109. * @return array
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\DbException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. */
  114. public function getCartInfoPrintProduct($oid)
  115. {
  116. $cartInfo = $this->dao->getCartInfoList(['oid' => $oid], ['cart_info']);
  117. $product = [];
  118. foreach ($cartInfo as $item) {
  119. $value = is_string($item['cart_info']) ? json_decode($item['cart_info'], true) : $item['cart_info'];
  120. $value['productInfo']['store_name'] = $value['productInfo']['store_name'] ?? "";
  121. $value['productInfo']['store_name'] = Str::substrUTf8($value['productInfo']['store_name'], 10, 'UTF-8', '');
  122. $product[] = $value;
  123. }
  124. return $product;
  125. }
  126. /**
  127. * 保存购物车info
  128. * @param $oid
  129. * @param $uid
  130. * @param array $cartInfo
  131. * @return \think\Collection
  132. * @throws \Exception
  133. */
  134. public function setCartInfo($oid, $uid, array $cartInfo)
  135. {
  136. $group = [];
  137. foreach ($cartInfo as $cart) {
  138. $group[] = [
  139. 'oid' => $oid,
  140. 'uid' => $uid,
  141. 'cart_id' => $cart['id'],
  142. 'product_id' => $cart['productInfo']['id'],
  143. 'cart_info' => json_encode($cart),
  144. 'cart_num' => $cart['cart_num'],
  145. 'surplus_num' => $cart['cart_num'],
  146. 'unique' => md5($cart['id'] . '' . $oid)
  147. ];
  148. }
  149. return $this->dao->saveAll($group);
  150. }
  151. /**
  152. * 订单创建成功之后计算订单(实际优惠、积分、佣金、上级、上上级)
  153. * @param $oid
  154. * @param array $cartInfo
  155. * @return bool
  156. */
  157. public function updateCartInfo($oid, array $cartInfo)
  158. {
  159. foreach ($cartInfo as $cart) {
  160. $group = [
  161. 'cart_info' => json_encode($cart)
  162. ];
  163. $this->dao->update(['oid' => $oid, 'cart_id' => $cart['id']], $group);
  164. }
  165. return true;
  166. }
  167. /**
  168. * 商品编号
  169. * @param $oid
  170. * @return array
  171. */
  172. public function getCartIdsProduct($oid)
  173. {
  174. return $this->dao->getColumn(['oid' => $oid], 'product_id', 'oid');
  175. }
  176. /**
  177. * 获取某个订单还可以拆分商品 split_status 0:未拆分1:部分拆分2:拆分完成
  178. * @param int $oid
  179. * @param string $field
  180. * @param string $key
  181. * @return array
  182. */
  183. public function getSplitCartList(int $oid, string $field = '*', string $key = 'cart_id')
  184. {
  185. $cartInfo = $this->dao->getColumn([['oid', '=', $oid], ['split_status', 'IN', [0, 1]]], $field, $key);
  186. foreach ($cartInfo as &$item) {
  187. if ($field == 'cart_info') {
  188. $item = is_string($item) ? json_decode($item, true) : $item;
  189. } else {
  190. if (isset($item['cart_info'])) $item['cart_info'] = is_string($item['cart_info']) ? json_decode($item['cart_info'], true) : $item['cart_info'];
  191. if (isset($item['cart_num']) && !$item['cart_num']) {//兼容之前老数据
  192. $item['cart_num'] = $item['cart_info']['cart_num'] ?? 0;
  193. }
  194. }
  195. }
  196. return $cartInfo;
  197. }
  198. /**
  199. * 获取可退款商品
  200. * @param int $oid
  201. * @param string $field
  202. * @param string $key
  203. * @return array
  204. */
  205. public function getRefundCartList(int $oid, string $field = '*', string $key = '')
  206. {
  207. $cartInfo = array_merge($this->dao->getColumn(['oid' => $oid], $field, 'id'));
  208. foreach ($cartInfo as $key => &$item) {
  209. if ($field == 'cart_info') {
  210. $item = is_string($item) ? json_decode($item, true) : $item;
  211. } else {
  212. if (isset($item['cart_info'])) $item['cart_info'] = is_string($item['cart_info']) ? json_decode($item['cart_info'], true) : $item['cart_info'];
  213. if (isset($item['cart_num']) && !$item['cart_num']) {//兼容之前老数据
  214. $item['cart_num'] = $item['cart_info']['cart_num'] ?? 0;
  215. }
  216. }
  217. $surplus = (int)bcsub((string)$item['cart_num'], (string)$item['refund_num'], 0);
  218. if ($surplus > 0) {
  219. $item['surplus_num'] = $surplus;
  220. } else {
  221. unset($cartInfo['key']);
  222. }
  223. }
  224. return $cartInfo;
  225. }
  226. }