StoreOrderCartInfoServices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. use think\exception\ValidateException;
  17. /**
  18. * Class StoreOrderCartInfoServices
  19. * @package app\services\order
  20. * @method array getCartColunm(array $where, string $field, ?string $key) 获取购物车信息以数组返回
  21. * @method array getCartInfoList(array $where, array $field) 获取购物车详情列表
  22. * @method getSplitCartNum(array $cart_id)
  23. * @method getOne(array $where, ?string $field = '*', array $with = []) 根据条件获取一条数据
  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|mixed
  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 $cartId
  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, $cartId, $goodsNum = false)
  82. {
  83. $key = md5('store_order_cart_product_title_' . $oid . '_' . is_array($cartId) ? implode('_', $cartId) : $cartId);
  84. $title = CacheService::get($key);
  85. if (!$title) {
  86. $orderCart = $this->dao->getCartInfoList(['oid' => $oid, 'cart_id' => $cartId], ['cart_info']);
  87. foreach ($orderCart as $item) {
  88. if (isset($item['cart_info']['productInfo']['store_name'])) {
  89. if ($goodsNum && isset($item['cart_info']['cart_num'])) {
  90. $title .= $item['cart_info']['productInfo']['store_name'] . ' * ' . $item['cart_info']['cart_num'] . ' | ';
  91. } else {
  92. $title .= $item['cart_info']['productInfo']['store_name'] . '|';
  93. }
  94. }
  95. }
  96. if ($title) {
  97. $title = substr($title, 0, strlen($title) - 1);
  98. }
  99. CacheService::set($key, $title);
  100. }
  101. return $title ?: '';
  102. }
  103. /**
  104. * 获取打印订单的商品信息
  105. * @param array $cartId
  106. * @return array
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. */
  111. public function getCartInfoPrintProduct(array $cartId)
  112. {
  113. $cartInfo = $this->dao->getCartInfoList(['cart_id' => $cartId], ['cart_info']);
  114. $product = [];
  115. foreach ($cartInfo as $item) {
  116. $value = is_string($item['cart_info']) ? json_decode($item['cart_info'], true) : $item['cart_info'];
  117. $value['productInfo']['store_name'] = $value['productInfo']['store_name'] ?? "";
  118. $value['productInfo']['store_name'] = Str::substrUTf8($value['productInfo']['store_name'], 10, 'UTF-8', '');
  119. $product[] = $value;
  120. }
  121. return $product;
  122. }
  123. /**
  124. * 获取产品返佣金额
  125. * @param array $cartId
  126. * @param bool $type true = 一级返佣, fasle = 二级返佣
  127. * @return string
  128. */
  129. public function getProductBrokerage(array $cartId, bool $type = true)
  130. {
  131. $cartInfo = $this->dao->getCartInfoList(['cart_id' => $cartId], ['cart_info']);
  132. $oneBrokerage = '0';//一级返佣金额
  133. $twoBrokerage = '0';//二级返佣金额
  134. $sumProductPrice = '0';//非指定返佣商品总金额
  135. foreach ($cartInfo as $value) {
  136. $cartNum = $value['cart_info']['cart_num'] ?? 0;
  137. if (isset($value['cart_info']['productInfo'])) {
  138. $productInfo = $value['cart_info']['productInfo'];
  139. //指定返佣金额
  140. if (isset($productInfo['is_sub']) && $productInfo['is_sub'] == 1) {
  141. $oneBrokerage = bcadd($oneBrokerage, bcmul($cartNum, $productInfo['attrInfo']['brokerage'] ?? 0, 2), 2);
  142. $twoBrokerage = bcadd($twoBrokerage, bcmul($cartNum, $productInfo['attrInfo']['brokerage_two'] ?? 0, 2), 2);
  143. } else {
  144. //比例返佣
  145. if (isset($productInfo['attrInfo'])) {
  146. $sumProductPrice = bcadd($sumProductPrice, bcmul($cartNum, $productInfo['attrInfo']['price'] ?? 0, 2), 2);
  147. } else {
  148. $sumProductPrice = bcadd($sumProductPrice, bcmul($cartNum, $productInfo['price'] ?? 0, 2), 2);
  149. }
  150. }
  151. }
  152. }
  153. if ($type) {
  154. //获取后台一级返佣比例
  155. $storeBrokerageRatio = sys_config('store_brokerage_ratio');
  156. //一级返佣比例 小于等于零时直接返回 不返佣
  157. if ($storeBrokerageRatio <= 0) {
  158. return $oneBrokerage;
  159. }
  160. //计算获取一级返佣比例
  161. $brokerageRatio = bcdiv($storeBrokerageRatio, 100, 4);
  162. $brokeragePrice = bcmul($sumProductPrice, $brokerageRatio, 2);
  163. //固定返佣 + 比例返佣 = 一级总返佣金额
  164. return bcadd($oneBrokerage, $brokeragePrice, 2);
  165. } else {
  166. //获取二级返佣比例
  167. $storeBrokerageTwo = sys_config('store_brokerage_two');
  168. //二级返佣比例小于等于0 直接返回
  169. if ($storeBrokerageTwo <= 0) {
  170. return $twoBrokerage;
  171. }
  172. //计算获取二级返佣比例
  173. $brokerageRatio = bcdiv($storeBrokerageTwo, 100, 4);
  174. $brokeragePrice = bcmul($sumProductPrice, $brokerageRatio, 2);
  175. //固定返佣 + 比例返佣 = 二级总返佣金额
  176. return bcadd($twoBrokerage, $brokeragePrice, 2);
  177. }
  178. }
  179. /**
  180. * 保存购物车info
  181. * @param $oid
  182. * @param array $cartInfo
  183. * @return int
  184. */
  185. public function setCartInfo($oid, array $cartInfo)
  186. {
  187. $group = [];
  188. foreach ($cartInfo as $cart) {
  189. $group[] = [
  190. 'oid' => $oid,
  191. 'cart_id' => $cart['id'],
  192. 'product_id' => $cart['productInfo']['id'],
  193. 'cart_info' => json_encode($cart),
  194. 'cart_num' => $cart['cart_num'],
  195. 'surplus_num' => $cart['cart_num'],
  196. 'unique' => md5($cart['id'] . '' . $oid)
  197. ];
  198. }
  199. return $this->dao->saveAll($group);
  200. }
  201. /**
  202. * 订单创建成功之后计算订单(实际优惠、积分、佣金、上级、上上级)
  203. * @param $oid
  204. * @param array $cartInfo
  205. * @return bool
  206. */
  207. public function updateCartInfo($oid, array $cartInfo)
  208. {
  209. foreach ($cartInfo as $cart) {
  210. $group = [
  211. 'cart_info' => json_encode($cart)
  212. ];
  213. $this->dao->update(['oid' => $oid, 'cart_id' => $cart['id']], $group);
  214. }
  215. return true;
  216. }
  217. /**
  218. * 商品编号
  219. * @param $cartId
  220. * @return array
  221. */
  222. public function getCartIdsProduct($cartId)
  223. {
  224. return $this->dao->getColumn([['cart_id', 'in', $cartId]], 'product_id', 'oid');
  225. }
  226. /**
  227. * 获取某个订单还可以拆分商品 split_status 0:未拆分1:部分拆分2:拆分完成
  228. * @param int $oid
  229. * @param string $field
  230. * @param string $key
  231. * @return array
  232. */
  233. public function getSplitCartList(int $oid, string $field = '*', string $key = 'cart_id')
  234. {
  235. $cartInfo = $this->dao->getColumn([['oid', '=', $oid], ['split_status', 'IN', [0, 1]]], $field, $key);
  236. foreach ($cartInfo as &$item) {
  237. if ($field == 'cart_info') {
  238. $item = is_string($item) ? json_decode($item, true) : $item;
  239. } else {
  240. if (isset($item['cart_info'])) $item['cart_info'] = is_string($item['cart_info']) ? json_decode($item['cart_info'], true) : $item['cart_info'];
  241. if (isset($item['cart_num']) && !$item['cart_num']) {//兼容之前老数据
  242. $item['cart_num'] = $item['cart_info']['cart_num'] ?? 0;
  243. }
  244. }
  245. }
  246. return $cartInfo;
  247. }
  248. /**
  249. * TODO 弃用
  250. * 检测这些商品是否还可以拆分
  251. * @param int $oid
  252. * @param array $cart_data
  253. * @return bool
  254. * @throws \think\db\exception\DataNotFoundException
  255. * @throws \think\db\exception\DbException
  256. * @throws \think\db\exception\ModelNotFoundException
  257. */
  258. public function checkCartIdsIsSplit(int $oid, array $cart_data)
  259. {
  260. if (!$cart_data) return false;
  261. $ids = array_unique(array_column($cart_data, 'cart_id'));
  262. if ($this->dao->getCartInfoList(['oid' => $oid, 'cart_id' => $ids, 'split_status' => 2], ['cart_id'])) {
  263. throw new ValidateException('您选择的商品已经拆分完成,请刷新或稍后重新选择');
  264. }
  265. $cartInfo = $this->getSplitCartList($oid, 'surplus_num,cart_info,cart_num', 'cart_id');
  266. if (!$cartInfo) {
  267. throw new ValidateException('该订单已发货完成');
  268. }
  269. foreach ($cart_data as $cart) {
  270. $surplus_num = $cartInfo[$cart['cart_id']]['surplus_num'] ?? 0;
  271. if (!$surplus_num) {//兼容之前老数据
  272. $_info = $cartInfo[$cart['cart_id']]['cart_info'];
  273. $surplus_num = $_info['cart_num'] ?? 0;
  274. }
  275. if ($cart['cart_num'] > $surplus_num) {
  276. throw new ValidateException('您选择商品拆分数量大于购买数量');
  277. }
  278. }
  279. return true;
  280. }
  281. /**
  282. * 获取可退款商品
  283. * @param int $oid
  284. * @param string $field
  285. * @param string $key
  286. * @return array
  287. */
  288. public function getRefundCartList(int $oid, string $field = '*', string $key = '')
  289. {
  290. $cartInfo = array_merge($this->dao->getColumn(['oid' => $oid], $field, 'id'));
  291. foreach ($cartInfo as $key => &$item) {
  292. if ($field == 'cart_info') {
  293. $item = is_string($item) ? json_decode($item, true) : $item;
  294. } else {
  295. if (isset($item['cart_info'])) $item['cart_info'] = is_string($item['cart_info']) ? json_decode($item['cart_info'], true) : $item['cart_info'];
  296. if (isset($item['cart_num']) && !$item['cart_num']) {//兼容之前老数据
  297. $item['cart_num'] = $item['cart_info']['cart_num'] ?? 0;
  298. }
  299. }
  300. $surplus = (int)bcsub((string)$item['cart_num'], (string)$item['refund_num'], 0);
  301. if ($surplus > 0) {
  302. $item['surplus_num'] = $surplus;
  303. } else {
  304. unset($cartInfo['key']);
  305. }
  306. }
  307. return $cartInfo;
  308. }
  309. }