You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

334 lines
12 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Constants\v3\ErrorCode;
  4. use App\Exception\ErrorCodeException;
  5. use App\Model\v3\Goods;
  6. use App\Model\v3\GoodsActivity;
  7. use App\Model\v3\ShoppingCart;
  8. use App\Model\v3\Store;
  9. use App\Service\v3\Interfaces\GoodsActivityServiceInterface;
  10. use App\Service\v3\Interfaces\GoodsServiceInterface;
  11. use App\Service\v3\Interfaces\ShopCartServiceInterface;
  12. use App\Service\v3\Interfaces\StoreServiceInterface;
  13. use Hyperf\DbConnection\Db;
  14. use Hyperf\Di\Annotation\Inject;
  15. class ShopCartService implements ShopCartServiceInterface
  16. {
  17. /**
  18. * @Inject
  19. * @var StoreServiceInterface
  20. */
  21. protected $storeService;
  22. /**
  23. * @Inject
  24. * @var GoodsServiceInterface
  25. */
  26. protected $goodsService;
  27. /**
  28. * @Inject
  29. * @var GoodsActivityServiceInterface
  30. */
  31. protected $goodsActivityService;
  32. public function do($userId,$marketId)
  33. {
  34. $storeIds = Db::table('lanzu_shopping_cart')->where([
  35. ['user_id','=',$userId],
  36. ['market_id','=',$marketId],
  37. ])->pluck('store_id')->toArray();
  38. $stores = Store::query()->with(['ShoppingCart' => function($query) use ($userId,$marketId){
  39. $query->where([
  40. ['user_id','=',$userId],
  41. ['market_id','=',$marketId],
  42. ]);
  43. }])->whereIn('id',$storeIds)
  44. ->get()
  45. ->toArray();
  46. $storeArr = [];
  47. foreach ($stores as $key => &$store){
  48. $sotreType = $this->storeService->check($store['id']);
  49. if(!$sotreType){
  50. continue;
  51. }
  52. $subtotal = 0;
  53. foreach ($store['shopping_cart'] as $k => &$shopcart){
  54. if($shopcart['activity_type'] == 1){
  55. $goodsType = $this->goodsService->check($shopcart['goods_id']);
  56. }else{
  57. $goodsType = $this->goodsActivityService->check($shopcart['goods_id'],$shopcart['num'],$shopcart['user_id']);
  58. }
  59. if($goodsType !== true){
  60. unset($store['shopping_cart'][$k]);
  61. continue;
  62. }
  63. if($shopcart['activity_type'] == 1){
  64. $builder = Goods::query();
  65. }else{
  66. $builder = GoodsActivity::query();
  67. }
  68. $shopcart['goods'] = $builder->where('id',$shopcart['goods_id'])->first()->toArray();
  69. $subtotal+= $shopcart['num'] * $shopcart['goods']['price'];
  70. }
  71. $store['subtotal'] = $subtotal;
  72. $storeArr[] = $store;
  73. }
  74. return $storeArr;
  75. }
  76. public function check($userId,$goodsId,$activityType = 1)
  77. {
  78. $ShoppingCart = ShoppingCart::query()->where([
  79. ['user_id','=',$userId],
  80. ['goods_id','=',$goodsId],
  81. ['activity_type','=',$activityType],
  82. ])
  83. ->select('num')
  84. ->first();
  85. return $ShoppingCart->num ?? 0;
  86. }
  87. public function undo($userId,$marketId)
  88. {
  89. $storeIds = Db::table('lanzu_shopping_cart')->where([
  90. ['user_id','=',$userId],
  91. ['market_id','=',$marketId],
  92. ])->pluck('store_id')->toArray();
  93. $stores = Store::query()->with(['ShoppingCart' => function($query) use ($userId,$marketId){
  94. $query->where([
  95. ['user_id','=',$userId],
  96. ['market_id','=',$marketId],
  97. ]);
  98. }])->whereIn('id',$storeIds)
  99. ->get()
  100. ->toArray();
  101. $storeArr = [];
  102. foreach ($stores as $key => &$store){
  103. $addStore = false;
  104. $sotreType = $this->storeService->check($store['id']);
  105. if(!$sotreType){
  106. $addStore = true;
  107. }
  108. foreach ($store['shopping_cart'] as $k => &$shopcart){
  109. if($shopcart['activity_type'] == 1){
  110. $goodsType = $this->goodsService->check($shopcart['goods_id']);
  111. }else{
  112. $goodsType = $this->goodsActivityService->check($shopcart['goods_id'],$shopcart['num'],$shopcart['user_id']);
  113. }
  114. if($goodsType === true){
  115. unset($store['shopping_cart'][$k]);
  116. continue;
  117. }
  118. $addStore = true;
  119. if($shopcart['activity_type'] == 1){
  120. $builder = Goods::query();
  121. }else{
  122. $builder = GoodsActivity::query();
  123. }
  124. $shopcart['goods'] = $builder->where('id',$shopcart['goods_id'])->first()->toArray();
  125. $shopcart['goods']['invalid_cause'] = $goodsType;
  126. }
  127. if($addStore){
  128. $storeArr[] = $store;
  129. }
  130. }
  131. return $storeArr;
  132. }
  133. public function countGoods($userId,$marketId)
  134. {
  135. $shoppingCart = $this->allForUser($userId,$marketId);
  136. $count = 0;
  137. foreach ($shoppingCart['store_lists'] as $store){
  138. foreach ($store['shopping_cart'] as $cart) {
  139. $num = $cart['num'];
  140. $count += $num;
  141. }
  142. }
  143. return $count;
  144. }
  145. public function getTotal($userId,$marketId)
  146. {
  147. $shoppingCart = $this->allForUser($userId,$marketId);
  148. return $shoppingCart['total'];
  149. }
  150. public function getGoodsByShopcartId($shopcartIds)
  151. {
  152. bcscale(6);
  153. // 查询当前用户的市场下的购物车数据
  154. $shopcartIdsArr = explode(',',$shopcartIds);
  155. $carts = ShoppingCart::query()
  156. ->with(['store', 'goods'])
  157. ->whereIn('id',$shopcartIdsArr)
  158. ->get();
  159. $cartList = [];
  160. $totalAmount = 0;
  161. foreach ($carts as $key => &$cart) {
  162. if (empty($cart->store)) {
  163. throw new ErrorCodeException(ErrorCode::STORE_NOT_AVAILABLE);
  164. continue;
  165. }
  166. if (empty($cart->goods)) {
  167. throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
  168. continue;
  169. }
  170. $shoppingCart = $cart->toArray();
  171. $store = $shoppingCart['store'];
  172. unset($shoppingCart['store']);
  173. // 商户是否歇业
  174. $checkStore = $this->storeService->check($store['id']);
  175. if (!$checkStore) {
  176. throw new ErrorCodeException(ErrorCode::STORE_NOT_AVAILABLE);
  177. continue;
  178. }
  179. // 商品是否失效
  180. if ($cart->activity_type == 2) {
  181. $goods = GoodsActivity::query()->find($cart->goods_id);
  182. $res = $this->goodsActivityService->check($goods, $cart->num, $cart->user_id);
  183. } else {
  184. $goods = Goods::query()->find($cart->goods_id);
  185. $res = $this->goodsService->check($goods, $cart->num);
  186. }
  187. if ($res === true) {
  188. $cartList['store_lists'][$store['id']]['store'] = $store;
  189. $cartList['store_lists'][$store['id']]['shopping_cart'][] = $shoppingCart;
  190. if (isset($cartList['store_lists'][$store['id']]['subtotal'])) {
  191. $cartList['store_lists'][$store['id']]['subtotal'] = bcadd(
  192. $cartList['store_lists'][$store['id']]['subtotal'],
  193. bcmul($cart->goods->price, $cart->num)
  194. );
  195. } else {
  196. $cartList['store_lists'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  197. }
  198. $totalAmount = bcadd($totalAmount, bcmul($cart->goods->price, $cart->num));
  199. }else{
  200. throw new ErrorCodeException($res);
  201. }
  202. }
  203. $cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : [];
  204. foreach ($cartList['store_lists'] as $key => &$value) {
  205. $value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
  206. }
  207. $cartList['total'] = bcadd($totalAmount, '0', 2);
  208. return $cartList['store_lists'];
  209. }
  210. /**
  211. * 获取用户购物车数据
  212. * @param $userId
  213. * @param $marketId
  214. * @return array
  215. */
  216. public function allForUser($userId, $marketId)
  217. {
  218. bcscale(6);
  219. // 查询当前用户的市场下的购物车数据
  220. $carts = ShoppingCart::query()
  221. ->with(['store',
  222. 'goods' => function($query) {
  223. $query->withoutGlobalScope('normal');
  224. }
  225. ])
  226. ->where(['user_id' => $userId])
  227. ->where(['market_id' => $marketId])
  228. ->get();
  229. $cartList = [];
  230. $totalAmount = 0;
  231. foreach ($carts as $key => &$cart) {
  232. if (empty($cart->store)||empty($cart->goods)) {
  233. continue;
  234. }
  235. $shoppingCart = $cart->toArray();
  236. $store = $shoppingCart['store'];
  237. unset($shoppingCart['store']);
  238. // 商户是否歇业
  239. $checkStore = $this->storeService->check($store['id']);
  240. if (!$checkStore) {
  241. $cartList['store_lists_invalid'][$store['id']]['store'] = $store;
  242. $cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart;
  243. if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) {
  244. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd(
  245. $cartList['store_lists_invalid'][$store['id']]['subtotal'],
  246. bcmul($cart->goods->price, $cart->num)
  247. );
  248. } else {
  249. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  250. }
  251. continue;
  252. }
  253. // 商品是否失效
  254. if ($cart->activity_type == 2) {
  255. $res = $this->goodsActivityService->check($cart->goods, $cart->num, $cart->user_id);
  256. } else {
  257. $res = $this->goodsService->check($cart->goods, $cart->num);
  258. }
  259. if ($res === true) {
  260. $cartList['store_lists'][$store['id']]['store'] = $store;
  261. $cartList['store_lists'][$store['id']]['shopping_cart'][] = $shoppingCart;
  262. if (isset($cartList['store_lists'][$store['id']]['subtotal'])) {
  263. $cartList['store_lists'][$store['id']]['subtotal'] = bcadd(
  264. $cartList['store_lists'][$store['id']]['subtotal'],
  265. bcmul($cart->goods->price, $cart->num)
  266. );
  267. } else {
  268. $cartList['store_lists'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  269. }
  270. $totalAmount = bcadd($totalAmount, bcmul($cart->goods->price, $cart->num));
  271. } else {
  272. $cartList['store_lists_invalid'][$store['id']]['store'] = $store;
  273. $cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart;
  274. if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) {
  275. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd(
  276. $cartList['store_lists_invalid'][$store['id']]['subtotal'],
  277. bcmul($cart->goods->price, $cart->num)
  278. );
  279. } else {
  280. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  281. }
  282. }
  283. }
  284. $cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : [];
  285. $cartList['store_lists_invalid'] = isset($cartList['store_lists_invalid']) ? array_values($cartList['store_lists_invalid']) : [];
  286. foreach ($cartList['store_lists'] as $key => &$value) {
  287. $value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
  288. }
  289. foreach ($cartList['store_lists_invalid'] as $key => &$value) {
  290. $value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
  291. }
  292. $cartList['total'] = bcadd($totalAmount, '0', 2);
  293. return $cartList;
  294. }
  295. }