StoreOrderCartInfoServices.php 9.5 KB

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