CartRepository.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace app\common\cart;
  3. use app\common\BaseRepository;
  4. use think\exception\ValidateException;
  5. use think\facade\Db;
  6. use think\facade\Log;
  7. class CartRepository extends BaseRepository
  8. {
  9. /**
  10. * TODO 商品检测
  11. * @param $data
  12. * @return array
  13. */
  14. public function checkProduct($data): array
  15. {
  16. if( $data['cart_num'] < 0 ) return $this -> getMessage(101);
  17. $productSelect = Db::query("select * from `rrx_store_product` where `product_id`=? limit 1", [$data['product_id']]);
  18. if(!$productSelect) return $this -> getMessage(102);
  19. $product = $productSelect[0];
  20. $skuSelect = Db::query("select * from `rrx_store_product_attr_value` where `unique`=? and `product_id`=? limit 1", [$data['product_attr_unique'],$data['product_id']]);
  21. $sku = $skuSelect[0];
  22. if(!$sku) return $this -> getMessage(103);
  23. if($sku['product_id'] != $data['product_id']) return $this -> getMessage(104);
  24. //积分
  25. if(isset($product['mer_id']) && $product['mer_id'] == '148' && !$data['is_new']) return $this -> getMessage(105);
  26. //秒杀
  27. $nowTime = date("Y-m-d H:00:00");//当前小时
  28. $seckill = Db::query("select * from `rrx_product_seckill` where `seckill_date`=? and `product_id`=? and `status`=1 limit 1",[$nowTime,$data['product_id']]);
  29. if($seckill && !$data['is_new']) return $this -> getMessage(105);
  30. return ['product' => $product,'sku' => $sku];
  31. }
  32. /**
  33. * TODO 购物车检测
  34. * @param $uid
  35. * @param $product_attr_unique
  36. * @return array|boolean
  37. */
  38. public function checkCart($uid,$product_attr_unique){
  39. // ['is_del'=>0,'is_fail'=>0,'is_new'=>0,'is_pay'=>0,'uid' => $uid,'product_type' => 0,'product_attr_unique' => $sku];
  40. $cartSelect = Db::query("select * from `rrx_store_cart` where `is_del`=? and `is_fail`=? and `is_new`=?
  41. and `is_pay`=? and `uid`=? and `product_type`=? and `product_attr_unique`=? limit 1",
  42. [0,0,0,0,$uid,0,$product_attr_unique]);
  43. if($cartSelect) return $cartSelect[0];
  44. return false;
  45. }
  46. /**
  47. * TODO 购物车数量修改
  48. * @param $cart_id
  49. * @param $cart_num
  50. */
  51. public function updateCart($cart_id,$cart_num){
  52. try {
  53. Db::query("UPDATE `rrx_store_cart` SET `cart_num`=? WHERE `cart_id`=?",[$cart_num,$cart_id]);
  54. }catch (\Exception $e){
  55. log::error(parseException($e));
  56. return $this -> getMessage(108);
  57. }
  58. }
  59. /**
  60. * TODO 商品检测
  61. * @param $data
  62. * @return array
  63. */
  64. public function createCart($data){
  65. try {
  66. Db::query("INSERT INTO `rrx_store_cart` SET
  67. `product_id`=?,
  68. `product_attr_unique`=?,
  69. `cart_num`=?,
  70. `is_new`=?,
  71. `uid`=?,
  72. `mer_id`=?,
  73. `create_time`=?",
  74. [$data['product_id'],$data['product_attr_unique'],$data['cart_num'],$data['is_new'],$data['uid'],$data['mer_id'],date("Y-m-d H:i:s")]);
  75. //返回购物车ID
  76. $cartId = Db::query("SELECT LAST_INSERT_ID()");
  77. return ['cart_id'=>$cartId[0]['LAST_INSERT_ID()']];
  78. }catch (\Exception $e){
  79. log::error(parseException($e));
  80. return $this -> getMessage(107);
  81. }
  82. }
  83. /**
  84. * TODO 获取单个购物车详情
  85. * @param $cartId
  86. * @param $uid
  87. * @return array
  88. */
  89. public function getOne($cartId,$uid){
  90. //'is_del'=>0,'is_fail'=>0,'is_new'=>0,'is_pay'=>0,'uid' => $uid];
  91. $cartSelect = Db::query("select * from `rrx_store_cart` where `uid`=? and `cart_id`=? limit 1",[$uid,$cartId]);
  92. if(!$cartSelect) return $this -> getMessage(109);
  93. return $cartSelect[0];
  94. }
  95. }