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.

199 lines
6.8 KiB

  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. use App\Constants\LogLabel;
  9. use App\Commons\Log;
  10. use Hyperf\Utils\ApplicationContext;
  11. use App\TaskWorker\SSDBTask;
  12. use App\Constants\SsdbKeysPrefix;
  13. class ShopCarService implements ShopCarServiceInterface
  14. {
  15. public function addShopCar($params)
  16. {
  17. //获取ssdb购买记录
  18. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  19. $record_exists = $ssdb->exec('get',SsdbKeysPrefix::PURCHASE_RECORD. date('Ymd') .'_'.$params['user_id'].'_'.$params['good_id']);
  20. if($record_exists)
  21. {
  22. $error = [
  23. 'error' => '特价商品每日只能购买一次'
  24. ];
  25. return $error;
  26. }
  27. //一个订单只能添加一个特价商品
  28. if($params['money'] == 0.01){
  29. $goods_exists = ShopCar::where([
  30. ['money','=',0.01],
  31. ['user_id','=',$params['user_id']],
  32. ['good_id','!=',$params['good_id']]
  33. ])
  34. ->exists();
  35. if($goods_exists){
  36. $error = [
  37. 'error' => '一个订单只能添加一个特价商品'
  38. ];
  39. return $error;
  40. }
  41. }
  42. //获取主表商品表信息
  43. $goods = Goods::where([
  44. ['id','=',$params['good_id']],
  45. ])
  46. ->select('is_max','restrict_num','box_money','inventory','money')
  47. ->first();
  48. //获取购物车该商品购买数量
  49. $num = ShopCar::where([
  50. ['user_id', $params['user_id']],
  51. ['good_id', $params['good_id']],
  52. ])
  53. ->sum('num');
  54. //限购检验
  55. if($goods->restrict_num > 0 && $goods->restrict_num <= $num)
  56. {
  57. $error = [
  58. 'error' => '超过商品限购数量'
  59. ];
  60. return $error;
  61. }
  62. //获取规格表商品信息
  63. if($params['combination_id'] > 0)
  64. {
  65. $combination = Combination::where([
  66. ['id', '=', $params['combination_id']],
  67. ])
  68. ->select('wm_money', 'number')
  69. ->first();
  70. $inventory = $combination->number;
  71. $money = $combination->wm_money;
  72. }else{
  73. $inventory = $goods->inventory;
  74. $money = $goods->money;
  75. }
  76. //库存校验 is_max 无限库存
  77. if($goods->is_max != Goods::INVENTORY_NOLIMIT && ($num + $params['num']) > $inventory)
  78. {
  79. $error = [
  80. 'error' => '库存不足'
  81. ];
  82. return $error;
  83. }
  84. //更新购物车
  85. $exists_sql = ShopCar::where([
  86. ['user_id', '=', $params['user_id']],
  87. ['good_id', '=', $params['good_id']],
  88. ['market_id','=',$params['market_id']],
  89. ['combination_id','=', $params['combination_id']],
  90. ]);
  91. if($params['combination_id'] > 0) {
  92. $exists_sql->where('combination_id',$params['combination_id']);
  93. }
  94. $exists = $exists_sql->exists();
  95. if($exists)
  96. {
  97. $update = ShopCar::where([
  98. ['user_id', '=', $params['user_id']],
  99. ['good_id', '=', $params['good_id']],
  100. ['market_id','=',$params['market_id']],
  101. ]);
  102. if($params['combination_id'] > 0) {
  103. $update->where('combination_id',$params['combination_id']);
  104. }
  105. $update->increment('num', $params['num']);
  106. }else{
  107. $son_id = empty($params['son_id']) ? 0 : $params['son_id'];
  108. $dr_id = empty($params['dr_id']) ? 0 : $params['dr_id'];
  109. $combination_id = empty($params['combination_id']) ? 0 : $params['combination_id'];
  110. $qg_name = empty($params['qg_name']) ? ' ' : $params['qg_name'];
  111. $qg_logo = empty($params['qg_logo']) ? ' ' :$params['qg_logo'];
  112. ShopCar::insert(
  113. [
  114. 'market_id' => $params['market_id'],
  115. 'good_id' => $params['good_id'],
  116. 'store_id' => $params['store_id'],
  117. 'user_id' => $params['user_id'],
  118. 'combination_id' => $combination_id,
  119. 'num' => $params['num'],
  120. 'spec' => $params['spec'],
  121. 'son_id' => $son_id,
  122. 'dr_id' => $dr_id,
  123. 'qg_name' => $qg_name,
  124. 'qg_logo' => $qg_logo,
  125. 'money' => $money,
  126. 'box_money' => $goods->box_money,
  127. ]
  128. );
  129. }
  130. return true;
  131. // if($params['goods_id'] == 1561){
  132. // return false;
  133. // }else{
  134. // return '加入购物车成功';
  135. // }
  136. }
  137. public function updateShopCar($params)
  138. {
  139. if($params['num'] <= 0)
  140. {
  141. ShopCar::where('id',$params['id'])->delete();
  142. }else {
  143. $shop_car = ShopCar::where('id',$params['id'])
  144. ->select('good_id','user_id')
  145. ->first();
  146. //获取主表商品表信息
  147. $goods = Goods::where([
  148. ['id','=',$shop_car['good_id']],
  149. ])
  150. ->select('is_max','restrict_num','box_money','inventory','money')
  151. ->first();
  152. //获取购物车该商品购买数量
  153. $num = ShopCar::where([
  154. ['user_id', $shop_car['user_id']],
  155. ['good_id', $shop_car['good_id']],
  156. ])
  157. ->sum('num');
  158. //限购检验
  159. if($goods->restrict_num > 0 && $goods->restrict_num <= $num)
  160. {
  161. $error = [
  162. 'error' => '超过商品限购数量'
  163. ];
  164. return $error;
  165. }
  166. if ($shop_car['combination_id'] > 0)
  167. {
  168. $combination = Combination::where([
  169. ['id', '=', $shop_car['combination_id']],
  170. ])
  171. ->select('wm_money', 'number')
  172. ->first();
  173. $inventory = $combination->number;
  174. }else{
  175. $inventory = $goods->inventory;
  176. }
  177. //库存校验 is_max 无限库存
  178. if($goods->is_max != Goods::INVENTORY_NOLIMIT && $params['num'] > $inventory)
  179. {
  180. $error = [
  181. 'error' => '库存不足'
  182. ];
  183. return $error;
  184. }
  185. ShopCar::where('id',$params['id'])
  186. ->update(['num' => $params['num']]);
  187. return true;
  188. }
  189. // if($params['good_id'] == 1561){
  190. // return false;
  191. // }else{
  192. // return '更新购物车成功';
  193. // }
  194. }
  195. }