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.

188 lines
6.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. <?php
  2. namespace App\Service;
  3. use Hyperf\Di\Annotation\Inject;
  4. use Hyperf\DbConnection\Db;
  5. use App\Model\Goods;
  6. use App\Model\Combination;
  7. use App\Model\ShopCar;
  8. class PurchaseLimitService implements PurchaseLimitServiceInterface
  9. {
  10. public function addShopCar($params)
  11. {
  12. //获取主表商品表信息
  13. $goods = Goods::where([
  14. ['id','=',$params['good_id']],
  15. ])
  16. ->select('is_max','restrict_num','box_money','inventory','money')
  17. ->first();
  18. //获取购物车该商品购买数量
  19. $num = ShopCar::where([
  20. ['user_id', $params['user_id']],
  21. ['good_id', $params['good_id']],
  22. ])
  23. ->sum('num');
  24. //限购检验
  25. if($goods->restrict_num > 0 && $goods->restrict_num <= $num)
  26. {
  27. $error = [
  28. 'error' => '超过商品限购数量'
  29. ];
  30. return $error;
  31. }
  32. //获取规格表商品信息
  33. if($params['combination_id'] > 0)
  34. {
  35. $combination = Combination::where([
  36. ['id', '=', $params['combination_id']],
  37. ])
  38. ->select('wm_money', 'number')
  39. ->first();
  40. $inventory = $combination->number;
  41. $money = $combination->wm_money;
  42. }else{
  43. $inventory = $goods->inventory;
  44. $money = $goods->money;
  45. }
  46. //库存校验 is_max 无限库存
  47. if($goods->is_max != Goods::INVENTORY_NOLIMIT && ($num + $params['num']) > $inventory)
  48. {
  49. $error = [
  50. 'error' => '库存不足'
  51. ];
  52. return $error;
  53. }
  54. //更新购物车
  55. $exists_sql = ShopCar::where([
  56. ['user_id', '=', $params['user_id']],
  57. ['good_id', '=', $params['good_id']],
  58. ['market_id','=',$params['market_id']],
  59. ['combination_id','=', $params['combination_id']],
  60. ]);
  61. if($params['combination_id'] > 0) {
  62. $exists_sql->where('combination_id',$params['combination_id']);
  63. }
  64. $exists = $exists_sql->exists();
  65. if($exists)
  66. {
  67. $update = ShopCar::where([
  68. ['user_id', '=', $params['user_id']],
  69. ['good_id', '=', $params['good_id']],
  70. ['market_id','=',$params['market_id']],
  71. ]);
  72. if($params['combination_id'] > 0) {
  73. $update->where('combination_id',$params['combination_id']);
  74. }
  75. $update->increment('num', $params['num']);
  76. }else{
  77. $son_id = empty($params['son_id']) ? 0 : $params['son_id'];
  78. $dr_id = empty($params['dr_id']) ? 0 : $params['dr_id'];
  79. $combination_id = empty($params['combination_id']) ? 0 : $params['combination_id'];
  80. $qg_name = empty($params['qg_name']) ? ' ' : $params['qg_name'];
  81. $qg_logo = empty($params['qg_logo']) ? ' ' :$params['qg_logo'];
  82. ShopCar::insert(
  83. [
  84. 'market_id' => $params['market_id'],
  85. 'good_id' => $params['good_id'],
  86. 'store_id' => $params['store_id'],
  87. 'user_id' => $params['user_id'],
  88. 'combination_id' => $combination_id,
  89. 'num' => $params['num'],
  90. 'spec' => $params['spec'],
  91. 'son_id' => $son_id,
  92. 'dr_id' => $dr_id,
  93. 'qg_name' => $qg_name,
  94. 'qg_logo' => $qg_logo,
  95. 'money' => $money,
  96. 'box_money' => $goods->box_money,
  97. ]
  98. );
  99. }
  100. return true;
  101. // if($params['goods_id'] == 1561){
  102. // return false;
  103. // }else{
  104. // return '加入购物车成功';
  105. // }
  106. }
  107. public function updateShopCar($params)
  108. {
  109. if($params['num'] <= 0)
  110. {
  111. ShopCar::where('id',$params['id'])->delete();
  112. }else {
  113. $shop_car = ShopCar::where('id',$params['id'])
  114. ->select('good_id','user_id')
  115. ->first();
  116. //获取主表商品表信息
  117. $goods = Goods::where([
  118. ['id','=',$shop_car['good_id']],
  119. ])
  120. ->select('is_max','restrict_num','box_money','inventory','money')
  121. ->first();
  122. //获取购物车该商品购买数量
  123. $num = ShopCar::where([
  124. ['user_id', $shop_car['user_id']],
  125. ['good_id', $shop_car['good_id']],
  126. ])
  127. ->sum('num');
  128. //限购检验
  129. if($goods->restrict_num > 0 && $goods->restrict_num <= $num)
  130. {
  131. $error = [
  132. 'error' => '超过商品限购数量'
  133. ];
  134. return $error;
  135. }
  136. if ($shop_car['combination_id'] > 0)
  137. {
  138. $combination = Combination::where([
  139. ['id', '=', $shop_car['combination_id']],
  140. ])
  141. ->select('wm_money', 'number')
  142. ->first();
  143. $inventory = $combination->number;
  144. }else{
  145. $inventory = $goods->inventory;
  146. }
  147. //库存校验 is_max 无限库存
  148. if($goods->is_max != Goods::INVENTORY_NOLIMIT && $params['num'] > $inventory)
  149. {
  150. $error = [
  151. 'error' => '库存不足'
  152. ];
  153. return $error;
  154. }
  155. ShopCar::where('id',$params['id'])
  156. ->update(['num' => $params['num']]);
  157. return true;
  158. }
  159. // if($params['good_id'] == 1561){
  160. // return false;
  161. // }else{
  162. // return '更新购物车成功';
  163. // }
  164. }
  165. public function getStoreIdByMarketId($params)
  166. {
  167. $res = [
  168. 'id' => 7,
  169. 'item' => 1,
  170. 'item_text' => 'page',
  171. 'logo' => 'http://lanzutest.lanzulive.com/attachment/images/2/2020/08/PY55Y3Mz17yJo17rv1Z7vImX1V5159.jpg',
  172. 'redirect_url' => '/zh_cjdianc/pages/takeout/takeoutindex?store_id=123',
  173. 'src' => '/zh_cjdianc/pages/takeout/takeoutindex?store_id=123',
  174. 'src2' => '/zh_cjdianc/pages/takeout/takeoutindex?store_id=123',
  175. ];
  176. return $res;
  177. }
  178. public function test($params)
  179. {
  180. return $params;
  181. }
  182. }