UserCollagePartakeServices.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. declare (strict_types=1);
  12. namespace app\services\activity\collage;
  13. use app\services\BaseServices;
  14. use app\dao\activity\collage\UserCollagePartakeDao;
  15. use app\services\product\product\StoreProductServices;
  16. use app\services\product\sku\StoreProductAttrValueServices;
  17. use app\services\product\branch\StoreBranchProductServices;
  18. use app\services\store\SystemStoreServices;
  19. use app\services\order\StoreCartServices;
  20. use app\services\user\level\SystemUserLevelServices;
  21. use app\services\user\member\MemberCardServices;
  22. use app\services\user\UserServices;
  23. use think\exception\ValidateException;
  24. /**
  25. *
  26. * Class UserCollagePartakeServices
  27. * @package app\services\activity\collage
  28. * @mixin UserCollagePartakeDao
  29. */
  30. class UserCollagePartakeServices extends BaseServices
  31. {
  32. /**
  33. * UserCollagePartakeServices constructor.
  34. * @param UserCollagePartakeDao $dao
  35. */
  36. public function __construct(UserCollagePartakeDao $dao)
  37. {
  38. $this->dao = $dao;
  39. }
  40. /**用户拼单/桌码商品统计
  41. * @param array $where
  42. * @param string $numType
  43. * @param int $collage_id
  44. * @param int $store_id
  45. * @return array
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function getUserPartakeCount(array $where, string $numType = '0', int $collage_id = 0, int $store_id = 0)
  51. {
  52. $count = 0;
  53. $ids = [];
  54. $cartNums = [];
  55. $cartList = $this->dao->getUserPartakeList($where, 'id,cart_num,product_id');
  56. if ($cartList) {
  57. /** @var StoreProductServices $storeProductServices */
  58. $storeProductServices = app()->make(StoreProductServices::class);
  59. $productInfos = $storeProductServices->getColumn([['id', 'in', array_column($cartList, 'product_id')]], 'id,pid,type,relation_id', 'id');
  60. foreach ($cartList as $cart) {
  61. $productInfo = $productInfos[$cart['product_id']] ?? [];
  62. if (!$productInfo) continue;
  63. if (in_array($productInfo['type'], [0, 2]) || ($productInfo['type'] == 1 && $productInfo['relation_id'] == $store_id) || ($productInfo['type'] == 1 && $productInfo['pid'] > 0)) {
  64. $ids[] = $cart['id'];
  65. $cartNums[] = $cart['cart_num'];
  66. }
  67. }
  68. if ($numType) {
  69. $count = count($ids);
  70. } else {
  71. $count = array_sum($cartNums);
  72. }
  73. }
  74. return compact('count', 'ids');
  75. }
  76. /**获取购物车列表
  77. * @param array $where
  78. * @param int $page
  79. * @param int $limit
  80. * @param array $with
  81. * @return array
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\DbException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. */
  86. public function getPartakeList(array $where, int $page = 0, int $limit = 0, array $with = [])
  87. {
  88. return $this->search($where)->when($page && $limit, function ($query) use ($page, $limit) {
  89. $query->page($page, $limit);
  90. })->when(count($with), function ($query) use ($with, $where) {
  91. $query->with($with);
  92. })->order('add_time DESC')->select()->toArray();
  93. }
  94. /**
  95. * 处理购物车数据
  96. * @param int $uid
  97. * @param array $cartList
  98. * @param array $addr
  99. * @param int $shipping_type
  100. * @param int $store_id
  101. * @return array
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function handleCartList(int $uid, array $cartList, int $shipping_type = 1, int $store_id = 0)
  107. {
  108. if (!$cartList) {
  109. return [$cartList, [], [], [], 0, [], []];
  110. }
  111. /** @var StoreProductServices $productServices */
  112. $productServices = app()->make(StoreProductServices::class);
  113. /** @var MemberCardServices $memberCardService */
  114. $memberCardService = app()->make(MemberCardServices::class);
  115. $vipStatus = $memberCardService->isOpenMemberCardCache('vip_price', false);
  116. $tempIds = [];
  117. $userInfo = [];
  118. $discount = 100;
  119. $productIds = $allStock = $attrUniquesArr = [];
  120. if ($uid) {
  121. /** @var UserServices $user */
  122. $user = app()->make(UserServices::class);
  123. $userInfo = $user->getUserCacheInfo($uid);
  124. //用户等级是否开启
  125. if (sys_config('member_func_status', 1) && $userInfo) {
  126. $userInfo = $userInfo->toArray();
  127. /** @var SystemUserLevelServices $systemLevel */
  128. $systemLevel = app()->make(SystemUserLevelServices::class);
  129. $discount = $systemLevel->getDiscount($uid, (int)$userInfo['level'] ?? 0);
  130. }
  131. }
  132. if ($store_id) {//平台商品,在门店购买 验证门店库存
  133. /** @var StoreProductAttrValueServices $skuValueServices */
  134. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  135. /** @var StoreBranchProductServices $branchProductServics */
  136. $branchProductServics = app()->make(StoreBranchProductServices::class);
  137. foreach ($cartList as $cart) {
  138. $productInfo = $cart['productInfo'] ?? [];
  139. if (!$productInfo) continue;
  140. if ($productInfo['type'] == 1) {//平台、供应商商品验证门店库存
  141. continue;
  142. }
  143. $productIds[] = $cart['product_id'];
  144. $suk = $skuValueServices->value(['unique' => $cart['product_attr_unique'], 'product_id' => $cart['product_id'], 'type' => 0], 'suk');
  145. $branchProductInfo = $branchProductServics->isValidStoreProduct((int)$cart['product_id'], $store_id);
  146. if (!$branchProductInfo) {
  147. continue;
  148. }
  149. $attrValue = $skuValueServices->get(['suk' => $suk, 'product_id' => $branchProductInfo['id'], 'type' => 0]);
  150. if (!$attrValue) {
  151. continue;
  152. }
  153. $allStock[$attrValue['unique']] = $attrValue['stock'];
  154. $attrUniquesArr[$cart['product_attr_unique']] = $attrValue['unique'];
  155. }
  156. } else {
  157. $productIds = array_unique(array_column($cartList, 'product_id'));
  158. }
  159. /** @var SystemStoreServices $storeServices */
  160. $storeServices = app()->make(SystemStoreServices::class);
  161. $storeInfo = $storeServices->getNearbyStore(['id' => $store_id], '', '', '', 1);
  162. $valid = $invalid = [];
  163. foreach ($cartList as &$item) {
  164. if (isset($item['productInfo']['delivery_type'])) {
  165. $item['productInfo']['delivery_type'] = is_string($item['productInfo']['delivery_type']) ? explode(',', $item['productInfo']['delivery_type']) : $item['productInfo']['delivery_type'];
  166. } else {
  167. $item['productInfo']['delivery_type'] = [];
  168. }
  169. $item['productInfo']['express_delivery'] = in_array(1, $item['productInfo']['delivery_type']);
  170. $item['productInfo']['store_mention'] = in_array(2, $item['productInfo']['delivery_type']);
  171. $item['productInfo']['store_delivery'] = in_array(3, $item['productInfo']['delivery_type']);
  172. if (isset($item['attrInfo']) && $item['attrInfo'] && (!isset($item['productInfo']['attrInfo']) || !$item['productInfo']['attrInfo'])) {
  173. $item['productInfo']['attrInfo'] = $item['attrInfo'] ?? [];
  174. }
  175. $item['attrStatus'] = isset($item['productInfo']['attrInfo']['stock']) && $item['productInfo']['attrInfo']['stock'];
  176. $item['productInfo']['attrInfo']['image'] = $item['productInfo']['attrInfo']['image'] ?? $item['productInfo']['image'] ?? '';
  177. $item['productInfo']['attrInfo']['suk'] = $item['productInfo']['attrInfo']['suk'] ?? '已失效';
  178. if (isset($item['productInfo']['attrInfo'])) {
  179. $item['productInfo']['attrInfo'] = get_thumb_water($item['productInfo']['attrInfo']);
  180. }
  181. $item['productInfo'] = get_thumb_water($item['productInfo']);
  182. $productInfo = $item['productInfo'];
  183. //门店独立商品
  184. $isBranchProduct = isset($productInfo['type']) && isset($productInfo['pid']) && $productInfo['type'] == 1 && !$productInfo['pid'];
  185. $product_store_id = $isBranchProduct ? $productInfo['relation_id'] : 0;
  186. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  187. $item['costPrice'] = $productInfo['attrInfo']['cost'] ?? 0;
  188. $item['trueStock'] = $item['branch_stock'] = $productInfo['attrInfo']['stock'] ?? 0;
  189. $item['branch_sales'] = $productInfo['attrInfo']['sales'] ?? 0;
  190. $item['truePrice'] = $productInfo['attrInfo']['price'] ?? 0;
  191. $item['sum_price'] = $productInfo['attrInfo']['price'] ?? 0;
  192. if (!$isBranchProduct) {
  193. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  194. $item['truePrice'] = $truePrice;
  195. $item['vip_truePrice'] = $vip_truePrice;
  196. $item['price_type'] = $type;
  197. }
  198. } else {
  199. $item['costPrice'] = $item['productInfo']['cost'] ?? 0;
  200. $item['trueStock'] = $item['branch_sales'] = $item['productInfo']['stock'] ?? 0;
  201. $item['branch_sales'] = $item['productInfo']['sales'] ?? 0;
  202. $item['truePrice'] = $item['productInfo']['price'] ?? 0;
  203. $item['sum_price'] = $item['productInfo']['price'] ?? 0;
  204. if (!$isBranchProduct) {
  205. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($item['productInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $item['productInfo']['vip_price'] ?? 0, $item['productInfo']['is_vip'] ?? 0, true);
  206. $item['truePrice'] = $truePrice;
  207. $item['vip_truePrice'] = $vip_truePrice;
  208. $item['price_type'] = $type;
  209. }
  210. }
  211. $item['is_true_stock'] = $item['trueStock'] >= $item['cart_num'] ? true : false;
  212. $item['total_price'] = bcmul((string)$item['truePrice'], (string)$item['cart_num'], 2);
  213. $item['sum_price'] = bcmul((string)$item['sum_price'], (string)$item['cart_num'], 2);
  214. if (isset($item['status']) && $item['status'] == 0) {
  215. $item['is_valid'] = 0;
  216. $item['invalid_desc'] = '此商品已失效';
  217. $invalid[] = $item;
  218. } elseif (($item['productInfo']['type'] ?? 0) == 1 && ($item['productInfo']['pid'] ?? 0) == 0 && $storeInfo && ($item['productInfo']['relation_id'] ?? 0) != $storeInfo['id']) {
  219. $item['is_valid'] = 0;
  220. $item['invalid_desc'] = '此商品超出配送/自提范围';
  221. $invalid[] = $item;
  222. } elseif ((isset($item['productInfo']['delivery_type']) && !$item['productInfo']['delivery_type']) || in_array($item['productInfo']['product_type'], [1, 2, 3])) {
  223. $item['is_valid'] = 1;
  224. $valid[] = $item;
  225. } else {
  226. $condition = !in_array(isset($item['productInfo']['product_id']) ? $item['productInfo']['product_id'] : $item['productInfo']['id'], $productIds) || $item['cart_num'] > ($allStock[$attrUniquesArr[$item['product_attr_unique']] ?? ''] ?? 0);
  227. switch ($shipping_type) {
  228. case -1://购物车列表展示
  229. if ($isBranchProduct && $store_id && ($store_id != $product_store_id || !in_array(3, $item['productInfo']['delivery_type']))) {
  230. $item['is_valid'] = 0;
  231. $item['invalid_desc'] = '此商品超出配送/自提范围';
  232. $invalid[] = $item;
  233. } else {
  234. $item['is_valid'] = 1;
  235. $valid[] = $item;
  236. }
  237. break;
  238. case 1:
  239. //不送达
  240. if (in_array($item['productInfo']['temp_id'], $tempIds) || (isset($item['productInfo']['delivery_type']) && !in_array(1, $item['productInfo']['delivery_type']) && !in_array(3, $item['productInfo']['delivery_type']))) {
  241. $item['is_valid'] = 0;
  242. $item['invalid_desc'] = '此商品超出配送/自提范围';
  243. $invalid[] = $item;
  244. } elseif ($isBranchProduct && $store_id && ($store_id != $product_store_id || !in_array(3, $item['productInfo']['delivery_type']))) {
  245. $item['is_valid'] = 0;
  246. $item['invalid_desc'] = '此商品超出配送/自提范围';
  247. $invalid[] = $item;
  248. } elseif (in_array($productInfo['type'], [0, 2]) && $store_id && ($condition || (!in_array(2, $item['productInfo']['delivery_type']) && !in_array(3, $item['productInfo']['delivery_type'])))) {//平台商品 在门店购买 验证门店库存
  249. $item['is_valid'] = 0;
  250. $item['invalid_desc'] = '此商品超出配送/自提范围';
  251. $invalid[] = $item;
  252. } else {
  253. $item['is_valid'] = 1;
  254. $valid[] = $item;
  255. }
  256. break;
  257. case 2:
  258. //不支持到店自提
  259. if (isset($item['productInfo']['delivery_type']) && $item['productInfo']['delivery_type'] && !in_array(2, $item['productInfo']['delivery_type'])) {
  260. $item['is_valid'] = 0;
  261. $item['invalid_desc'] = '此商品超出配送/自提范围';
  262. $invalid[] = $item;
  263. } elseif ($isBranchProduct && $store_id && $store_id != $product_store_id) {
  264. $item['is_valid'] = 0;
  265. $item['invalid_desc'] = '此商品超出配送/自提范围';
  266. $invalid[] = $item;
  267. } elseif ($item['productInfo']['product_type'] == 1) {
  268. $item['is_valid'] = 0;
  269. $item['invalid_desc'] = '此商品超出配送/自提范围';
  270. $invalid[] = $item;
  271. } elseif (in_array($productInfo['type'], [0, 2]) && $store_id && $condition) {//平台、供应商商品 在门店购买 验证门店库存
  272. $item['is_valid'] = 0;
  273. $item['invalid_desc'] = '此商品超出配送/自提范围';
  274. $invalid[] = $item;
  275. } else {
  276. $item['is_valid'] = 1;
  277. $valid[] = $item;
  278. }
  279. break;
  280. case 4:
  281. //无库存||下架
  282. if ($isBranchProduct && $store_id && $store_id != $product_store_id) {
  283. $item['is_valid'] = 0;
  284. $item['invalid_desc'] = '此商品超出配送/自提范围';
  285. $invalid[] = $item;
  286. } elseif (in_array($productInfo['type'], [0, 2]) && $store_id && $condition) {
  287. $item['is_valid'] = 0;
  288. $invalid[] = $item;
  289. } else {
  290. $item['is_valid'] = 1;
  291. $valid[] = $item;
  292. }
  293. break;
  294. default:
  295. $item['is_valid'] = 1;
  296. $valid[] = $item;
  297. break;
  298. }
  299. }
  300. unset($item['attrInfo']);
  301. }
  302. return [$cartList, $valid, $invalid];
  303. }
  304. /**用户添加拼单商品
  305. * @param int $uid
  306. * @param int $productId
  307. * @param int $cart_num
  308. * @param string $product_attr_unique
  309. * @param int $collageId
  310. * @param int $storeId
  311. * @param int $type
  312. * @param int $isAdd
  313. * @return bool|\crmeb\basic\BaseModel|mixed|\think\Model
  314. * @throws \think\db\exception\DataNotFoundException
  315. * @throws \think\db\exception\DbException
  316. * @throws \think\db\exception\ModelNotFoundException
  317. */
  318. public function addUserPartakeProduct(int $uid, int $productId, int $cart_num = 1, string $product_attr_unique = '', int $collageId = 0, int $storeId = 0, int $type = 0, int $isAdd = 1)
  319. {
  320. if ($cart_num < 1) $cart_num = 1;
  321. /** @var StoreProductAttrValueServices $attrValueServices */
  322. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  323. if ($product_attr_unique == '') {
  324. $product_attr_unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  325. }
  326. //该商品已选择库存
  327. $sum_cart_num = $this->dao->sum(['collate_code_id' => $collageId, 'product_id' => $productId, 'product_attr_unique' => $product_attr_unique, 'store_id' => $storeId, 'status'=>1],'cart_num');
  328. //检测库存限量
  329. /** @var StoreCartServices $cartServices */
  330. $cartServices = app()->make(StoreCartServices::class);
  331. [$attrInfo, $product_attr_unique, $bargainPriceMin, $cart_num, $productInfo] = $cartServices->checkProductStock(
  332. $uid,
  333. $productId,
  334. $cart_num,
  335. $storeId,
  336. $product_attr_unique,
  337. false,
  338. $type,
  339. 0,
  340. 0,
  341. $sum_cart_num
  342. );
  343. $product_type = $productInfo['product_type'];
  344. $cart = $this->dao->getOne(['uid' => $uid, 'collate_code_id' => $collageId, 'product_id' => $productId, 'product_attr_unique' => $product_attr_unique, 'store_id' => $storeId, 'status'=>1]);
  345. if ($cart) {
  346. if ($isAdd) {
  347. $cart->cart_num = $cart->cart_num + $cart_num;
  348. } else {
  349. $cart->cart_num = $cart->cart_num - $cart_num;
  350. }
  351. if ($cart->cart_num == 0) {
  352. $res = $this->dao->delete($cart->id);
  353. } else {
  354. $cart->add_time = time();
  355. $res = $cart->save();
  356. }
  357. } else {
  358. $data = [
  359. 'uid' => $uid,
  360. 'collate_code_id' => $collageId,
  361. 'product_id' => $productId,
  362. 'product_attr_unique' => $product_attr_unique,
  363. 'product_type' => $product_type,
  364. 'store_id' => $storeId,
  365. 'cart_num' => $cart_num,
  366. 'add_time' => time()
  367. ];
  368. $res = $this->dao->save($data);
  369. }
  370. return $res;
  371. }
  372. /**重组数组
  373. * @param array $array
  374. * @param int $uid
  375. * @param int $sponsor_uid
  376. * @return array
  377. */
  378. public function array_val_chunk(array $array, int $uid, int $sponsor_uid, int $status)
  379. {
  380. $result = [];
  381. foreach ($array as $key => &$value) {
  382. if (!$value['userInfo']) {
  383. $value['userInfo'] = [
  384. 'uid' => 0,
  385. 'nickname' => '该用户已注销'
  386. ];
  387. }
  388. $result [$value ['uid']]['userInfo'] = $value['userInfo'];
  389. unset($value['userInfo']);
  390. $result [$value ['uid']]['goods'][] = $value;
  391. }
  392. /** @var UserServices $userServices */
  393. $userServices = app()->make(UserServices::class);
  394. if (array_key_exists($uid, $result)) {
  395. $undata = $result[$uid];
  396. } else {
  397. $userInfo = $userServices->getUserInfo($uid);
  398. if (!$userInfo) {
  399. $userInfo = [
  400. 'uid' => 0,
  401. 'nickname' => '该用户已注销'
  402. ];
  403. }
  404. if ($status >= 2) {
  405. $undata = [];
  406. } else {
  407. $undata = [
  408. 'userInfo' => $userInfo,
  409. 'goods' => []
  410. ];
  411. }
  412. }
  413. if (array_key_exists($sponsor_uid, $result)) {
  414. $sponsordata = $result[$sponsor_uid];
  415. } else {
  416. $userInfo = $userServices->getUserInfo($sponsor_uid);
  417. if (!$userInfo) {
  418. $userInfo = [
  419. 'uid' => 0,
  420. 'nickname' => '该用户已注销'
  421. ];
  422. }
  423. $sponsordata = [
  424. 'userInfo' => $userInfo,
  425. 'goods' => []
  426. ];
  427. }
  428. foreach ($result as $key => $item) {
  429. if ($key == $uid || $key == $sponsor_uid) {
  430. unset($result[$key]);
  431. }
  432. }
  433. if ($uid == $sponsor_uid) {
  434. array_unshift($result, $undata);
  435. } else {
  436. if ($undata) {
  437. array_unshift($result, $undata, $sponsordata);
  438. } else {
  439. array_unshift($result, $sponsordata);
  440. }
  441. }
  442. foreach ($result as $key => $item) {
  443. $truePrices = array_column($item['goods'], 'sum_price');
  444. $sum = array_sum($truePrices);
  445. $result[$key]['sum_price'] = $sum;
  446. $result[$key]['sumPrice'] = number_format($sum, 2);
  447. }
  448. return $result;
  449. }
  450. /**
  451. * 获取所有人拼单商品
  452. * @param int $collage_id
  453. * @param int $uid
  454. * @return array
  455. * @throws \think\db\exception\DataNotFoundException
  456. * @throws \think\db\exception\DbException
  457. * @throws \think\db\exception\ModelNotFoundException
  458. */
  459. public function getUserPartakeProduct(int $collage_id, int $uid)
  460. {
  461. /** @var UserCollageServices $collageServices */
  462. $collageServices = app()->make(UserCollageServices::class);
  463. $collage = $collageServices->get($collage_id);
  464. $where = ['collate_code_id' => $collage_id, 'status' => 1];
  465. [$cartList, $valid, $invalid] = $this->getUserCashierTablePartakeProduct($collage['uid'], $collage['store_id'], $where, '', ['userInfo', 'productInfo', 'attrInfo']);
  466. $cartList = $this->array_val_chunk($cartList, $uid, $collage['uid'], $collage['status']);
  467. return $cartList;
  468. }
  469. /**
  470. * 用户清空拼单
  471. * @param int $collage_id
  472. * @param int $uid
  473. * @return bool
  474. */
  475. public function emptyUserCollagePartake(int $collage_id, int $uid)
  476. {
  477. return $this->dao->del(['collate_code_id' => $collage_id, 'uid' => $uid]);
  478. }
  479. /**
  480. * 复制他人拼单商品
  481. * @param int $collage_id
  482. * @param int $c_uid
  483. * @param int $uid
  484. * @return bool
  485. * @throws \think\db\exception\DataNotFoundException
  486. * @throws \think\db\exception\DbException
  487. * @throws \think\db\exception\ModelNotFoundException
  488. */
  489. public function duplicateUserCollagePartake(int $collage_id, int $c_uid, int $uid)
  490. {
  491. $data = $this->dao->getUserPartakeList(['uid' => $c_uid, 'collate_code_id' => $collage_id, 'status' => 1], 'product_id,product_type,store_id,product_attr_unique,cart_num');
  492. foreach ($data as $key => $item) {
  493. $res = $this->addUserPartakeProduct($uid, (int)$item['product_id'], $item['cart_num'], $item['product_attr_unique'], $collage_id, $item['store_id'], 9, 1);
  494. if(!$res) continue;
  495. }
  496. return true;
  497. }
  498. /**
  499. * 拼单商品写入购物车
  500. * @param int $collage_id
  501. * @param int $uid
  502. * @param int $type
  503. * @return false|string
  504. * @throws \think\db\exception\DataNotFoundException
  505. * @throws \think\db\exception\DbException
  506. * @throws \think\db\exception\ModelNotFoundException
  507. */
  508. public function allUserSettleAccountsCollage(int $collage_id, int $uid, int $type)
  509. {
  510. $data = $this->dao->getUserPartakeList(['collate_code_id' => $collage_id, 'status' => 1], 'id,product_id,product_type,store_id,product_attr_unique,cart_num,status,is_settle');
  511. if (count($data) <= 0) return false;
  512. /** @var StoreCartServices $cartServices */
  513. $cartServices = app()->make(StoreCartServices::class);
  514. $cartIds = [];
  515. foreach ($data as $key => $item) {
  516. [$key, $cart_num] = $cartServices->setCart($uid, (int)$item['product_id'], (int)$item['cart_num'], $item['product_attr_unique'], $type, true, $collage_id, 0);
  517. if ($key && !$item['is_settle']) {
  518. $this->dao->update($item['id'], ['is_settle' => 1]);
  519. }
  520. $cartIds[] = $key;
  521. }
  522. if (count($cartIds) < 0) return false;
  523. /** @var UserCollageServices $collageServices */
  524. $collageServices = app()->make(UserCollageServices::class);
  525. $res = $collageServices->userUpdate($collage_id, ['status' => 1]);
  526. if (!$res) return false;
  527. $cartIds = array_unique($cartIds);
  528. $cartIds = implode(',', $cartIds);
  529. return $cartIds;
  530. }
  531. /**
  532. * 根据商品id获取购物车数量
  533. * @param array $ids
  534. * @param int $uid
  535. * @param int $storeId
  536. * @param int $collate_code_id
  537. * @return mixed
  538. */
  539. public function productIdByCartNum(array $ids, int $uid, int $storeId = 0, int $collate_code_id = 0)
  540. {
  541. return $this->search(['product_id' => $ids, 'uid' => $uid, 'store_id' => $storeId, 'collate_code_id' => $collate_code_id])->group('product_attr_unique')->column('cart_num,product_id', 'product_attr_unique');
  542. }
  543. /**
  544. * 用户注销信息删除拼单商品
  545. * @param int $uid
  546. * @return bool
  547. */
  548. public function logOffUserCollagePartake(int $uid)
  549. {
  550. return $this->dao->del(['uid' => $uid, 'is_settle' => 0]);
  551. }
  552. /**
  553. * 获取桌码商品
  554. * @param int $tableId
  555. * @return array
  556. * @throws \think\db\exception\DataNotFoundException
  557. * @throws \think\db\exception\DbException
  558. * @throws \think\db\exception\ModelNotFoundException
  559. */
  560. public function getUserTablePartakeProduct(int $tableId)
  561. {
  562. /** @var UserCollageServices $collageServices */
  563. $collageServices = app()->make(UserCollageServices::class);
  564. $table = $collageServices->get($tableId);
  565. if ($table['status'] == -1) {
  566. throw new ValidateException('桌码已取消');
  567. }
  568. $where = ['collate_code_id' => $tableId, 'status' => 1];
  569. [$cartList, $valid, $invalid] = $this->getUserCashierTablePartakeProduct($table['uid'], $table['store_id'], $where, '', ['userInfo', 'productInfo', 'attrInfo']);
  570. $result = [];
  571. foreach ($cartList as $key => &$value) {
  572. if (!$value['userInfo']) {
  573. $value['userInfo'] = [
  574. 'uid' => 0,
  575. 'nickname' => '该用户已注销'
  576. ];
  577. }
  578. $result [$value ['uid']]['userInfo'] = $value['userInfo'];
  579. $result [$value ['uid']]['order_time'] = date('H:i', $value['add_time']);
  580. unset($value['userInfo']);
  581. $result [$value ['uid']]['goods'][] = $value;
  582. }
  583. return $result;
  584. }
  585. /**
  586. * 订单商品信息
  587. * @param int $uid
  588. * @param int $store_id
  589. * @param array $where
  590. * @param string $field
  591. * @param array $with
  592. * @return array
  593. * @throws \think\db\exception\DataNotFoundException
  594. * @throws \think\db\exception\DbException
  595. * @throws \think\db\exception\ModelNotFoundException
  596. */
  597. public function getUserCashierTablePartakeProduct(int $uid, int $store_id, array $where, string $field = '*', array $with = [])
  598. {
  599. $cartList = $this->dao->getUserPartakeProductList($where, $field, $with);
  600. return $this->handleCartList($uid, $cartList, -1, $store_id);
  601. }
  602. /**
  603. * 获取订单商品信息
  604. * @param int $uid
  605. * @param int $store_id
  606. * @param array $where
  607. * @param string $field
  608. * @param array $with
  609. * @return array
  610. * @throws \think\db\exception\DataNotFoundException
  611. * @throws \think\db\exception\DbException
  612. * @throws \think\db\exception\ModelNotFoundException
  613. */
  614. public function getCashierTablePartakeProduct(array $where, string $field = '*', array $with = [])
  615. {
  616. $cartList = $this->dao->getUserPartakeProductList($where, $field, $with);
  617. if (!$cartList) return [];
  618. $data = $this->dataPartake($cartList, true);
  619. return $data;
  620. }
  621. /**
  622. * 桌码商品处理
  623. * @param int $uid
  624. * @param array $where
  625. * @param int $store_id
  626. * @return mixed
  627. */
  628. public function getTableCatePartakeList(int $uid, array $where, int $store_id)
  629. {
  630. $data = $this->getPartakeList($where, 0, 0, ['productInfo', 'attrInfo']);
  631. $data = $this->dataPartake($data, false);
  632. [$data, $valid, $invalid] = $this->handleCartList($uid, $data['cart'], -1, $store_id);
  633. return $valid;
  634. }
  635. /**
  636. * 数据处理
  637. * @param array $data
  638. * @param bool $is_price
  639. * @return array
  640. */
  641. public function dataPartake(array $data, bool $is_price)
  642. {
  643. $cart = [];
  644. $sum_price = 0;
  645. $cart_num = 0;
  646. foreach ($data as $key => $item) {
  647. if (isset($item['id'])) {
  648. $cart[$item['product_id'] . $item['product_attr_unique']]['id'] = $item['id'];
  649. }
  650. if (isset($item['uid'])) {
  651. $cart[$item['product_id'] . $item['product_attr_unique']]['uid'] = $item['uid'];
  652. }
  653. if (isset($item['collate_code_id'])) {
  654. $cart[$item['product_id'] . $item['product_attr_unique']]['collate_code_id'] = $item['collate_code_id'];
  655. }
  656. if (isset($item['product_id'])) {
  657. $cart[$item['product_id'] . $item['product_attr_unique']]['product_id'] = $item['product_id'];
  658. }
  659. if (isset($item['product_type'])) {
  660. $cart[$item['product_id'] . $item['product_attr_unique']]['product_type'] = $item['product_type'];
  661. }
  662. if (isset($item['product_attr_unique'])) {
  663. $cart[$item['product_id'] . $item['product_attr_unique']]['product_attr_unique'] = $item['product_attr_unique'];
  664. }
  665. if (isset($item['status'])) {
  666. $cart[$item['product_id'] . $item['product_attr_unique']]['status'] = $item['status'];
  667. }
  668. if (isset($item['is_print'])) {
  669. $cart[$item['product_id'] . $item['product_attr_unique']]['is_print'] = $item['is_print'];
  670. }
  671. if (isset($item['is_settle'])) {
  672. $cart[$item['product_id'] . $item['product_attr_unique']]['is_settle'] = $item['is_settle'];
  673. }
  674. if (isset($item['add_time'])) {
  675. $cart[$item['product_id'] . $item['product_attr_unique']]['add_time'] = $item['add_time'];
  676. }
  677. if (isset($item['productInfo'])) {
  678. $cart[$item['product_id'] . $item['product_attr_unique']]['productInfo'] = $item['productInfo'];
  679. }
  680. if (isset($item['attrInfo']) && isset($item['productInfo'])) {
  681. $cart[$item['product_id'] . $item['product_attr_unique']]['productInfo']['attrInfo'] = $item['attrInfo'];
  682. unset($item['attrInfo']);
  683. }
  684. if (isset($cart[$item['product_id'] . $item['product_attr_unique']]['cart_num'])) {
  685. $cart[$item['product_id'] . $item['product_attr_unique']]['cart_num'] += $item['cart_num'];
  686. } else {
  687. $cart[$item['product_id'] . $item['product_attr_unique']]['cart_num'] = $item['cart_num'];
  688. }
  689. $cart_num += $item['cart_num'];
  690. if ($is_price && isset($item['productInfo'])) {
  691. $sum_price = bcadd((string)$sum_price, bcmul((string)$item['cart_num'], (string)$item['productInfo']['price'], 2), 2);
  692. }
  693. }
  694. return compact('cart', 'sum_price', 'cart_num');
  695. }
  696. /**
  697. * 桌码商品写入购物车
  698. * @param int $tableId
  699. * @param int $uid
  700. * @param int $type
  701. * @return false|string
  702. * @throws \think\db\exception\DataNotFoundException
  703. * @throws \think\db\exception\DbException
  704. * @throws \think\db\exception\ModelNotFoundException
  705. */
  706. public function allUserSettleAccountsTableCode(int $tableId, int $uid, int $type)
  707. {
  708. $data = $this->dao->getUserPartakeList(['collate_code_id' => $tableId, 'status' => 1], 'id,product_id,product_type,store_id,product_attr_unique,cart_num,status,is_settle');
  709. if (count($data) <= 0) return false;
  710. /** @var StoreCartServices $cartServices */
  711. $cartServices = app()->make(StoreCartServices::class);
  712. $cartIds = [];
  713. $data = $this->dataPartake($data, false);
  714. foreach ($data['cart'] as $key => $item) {
  715. try {
  716. [$key, $cart_num] = $cartServices->setCart($uid, (int)$item['product_id'], $item['cart_num'], $item['product_attr_unique'], $type, true, $tableId, 0);
  717. if ($key && !$item['is_settle']) {
  718. $this->dao->update($item['id'], ['is_settle' => 1]);
  719. }
  720. $cartIds[] = $key;
  721. } catch (\Exception $e) {
  722. continue;
  723. }
  724. }
  725. if (count($cartIds) < 0) return false;
  726. $cartIds = array_unique($cartIds);
  727. $cartIds = implode(',', $cartIds);
  728. return $cartIds;
  729. }
  730. /**
  731. * 获取最后一条记录
  732. * @param array $where
  733. * @return array|\crmeb\basic\BaseModel|mixed|\think\Model|null
  734. * @throws \think\db\exception\DataNotFoundException
  735. * @throws \think\db\exception\DbException
  736. * @throws \think\db\exception\ModelNotFoundException
  737. */
  738. public function getMaxTime(array $where)
  739. {
  740. return $this->dao->getUserPartake($where);
  741. }
  742. /**
  743. * 获取用户信息
  744. * @param $where
  745. * @param $store_id
  746. * @return array
  747. * @throws \think\db\exception\DataNotFoundException
  748. * @throws \think\db\exception\DbException
  749. * @throws \think\db\exception\ModelNotFoundException
  750. */
  751. public function tableCodeUserAll($where, $store_id)
  752. {
  753. $uids = $this->dao->getUserPartakeList(['collate_code_id' => $where['table_id'], 'store_id' => $store_id], 'uid', ['userInfo']);
  754. foreach ($uids as $key => $item) {
  755. if (!$item['userInfo']) {
  756. unset($uids[$key]);
  757. }
  758. }
  759. $uids = array_unique($uids, SORT_REGULAR);
  760. return array_merge($uids);
  761. }
  762. /**
  763. * 获取打印订单的商品信息
  764. * @param array $table
  765. * @return array
  766. */
  767. public function getCartInfoPrintProduct(array $table)
  768. {
  769. $where = ['collate_code_id' => $table['id'], 'is_print' => 0, 'status' => 1];
  770. $data = $this->getCashierTablePartakeProduct($where, '', ['productInfo', 'attrInfo']);
  771. $product = [];
  772. if (!$data || !isset($data['cart'])) return $product;
  773. foreach ($data['cart'] as $item) {
  774. $value = $item;
  775. $value['productInfo']['store_name'] = $value['productInfo']['store_name'] ?? "";
  776. $value['productInfo']['store_name'] = substrUTf8($value['productInfo']['store_name'], 10, 'UTF-8', '');
  777. $product[] = $value;
  778. }
  779. return $product;
  780. }
  781. /**
  782. * 用户清空购物车
  783. * @param int $table_id
  784. * @return bool
  785. */
  786. public function emptyUserTablePartake(int $table_id)
  787. {
  788. return $this->dao->del(['collate_code_id' => $table_id]);
  789. }
  790. /**
  791. * 收银台购物车数量操作
  792. * @param array $where
  793. * @param int $store_id
  794. * @return bool|mixed
  795. * @throws \think\db\exception\DataNotFoundException
  796. * @throws \think\db\exception\DbException
  797. * @throws \think\db\exception\ModelNotFoundException
  798. */
  799. public function editTableCartProduct(array $where, int $store_id)
  800. {
  801. if ($where['cartNum'] < 1) {
  802. $cart_num = 1;
  803. } else {
  804. $cart_num = $where['cartNum'];
  805. }
  806. $cart = $this->dao->getOne(['collate_code_id' => $where['tableId'], 'product_id' => $where['productId'], 'product_attr_unique' => $where['uniqueId'], 'store_id' => $store_id]);
  807. if (!$cart) return false;
  808. if ($where['isAdd']) {
  809. $cart->cart_num = $cart->cart_num + $cart_num;
  810. } else {
  811. $cart->cart_num = $cart->cart_num - $cart_num;
  812. }
  813. if ($cart->cart_num == 0) {
  814. $res = $this->dao->delete($cart->id);
  815. } else {
  816. $res = $cart->save();
  817. }
  818. return $res;
  819. }
  820. /**
  821. * 用户删除拼单、桌码商品
  822. * @param int $collate_code_id
  823. * @param int $storeId
  824. * @param int $productId
  825. * @param string $uniqueId
  826. * @return bool
  827. */
  828. public function delUserCatePartake(int $collate_code_id, int $storeId, int $productId, string $uniqueId)
  829. {
  830. return $this->dao->del(['collate_code_id' => $collate_code_id, 'store_id' => $storeId, 'product_id' => $productId, 'product_attr_unique' => $uniqueId]);
  831. }
  832. }