CartRepository.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /**
  96. * TODO 获取购物车列表
  97. * @param $uid
  98. * @return array
  99. */
  100. public function getListFromUid($uid){
  101. $list = Db::query("select
  102. `rsc`.`cart_num`,`rsc`.`cart_id`,
  103. `rm`.`mer_id`,`rm`.`mer_name`,`rm`.`mer_avatar`,
  104. `rspav`.`product_id`,`rspav`.`price`,`rspav`.`unique`,`rspav`.`sku`,`rspav`.`image`
  105. from `rrx_store_cart` `rsc`
  106. left JOIN `rrx_store_product_attr_value` `rspav` on `rsc`.`product_attr_unique` = `rspav`.`unique`
  107. left JOIN `rrx_merchant` `rm` on `rsc`.`mer_id` = `rm`.`mer_id`
  108. where
  109. `rsc`.`uid`=? and
  110. `rsc`.`is_del`=? and
  111. `rsc`.`is_new`=? and
  112. `rsc`.`is_pay`=?"
  113. ,[$uid,0,0,0]);
  114. // $list = Db::query("select * from `rrx_store_cart` where `uid`=? and `is_del`=? and `is_new`=? and `is_pay`=?",[$uid,0,0,0]);
  115. return $this -> formatCartList($list);
  116. }
  117. //购物车列表格式化
  118. public function formatCartList($list){
  119. $arr = [];
  120. foreach($list as $key => $item){
  121. if (empty($item['image']))$item['image'] = $this -> getProductImage($item['product_id']);
  122. // $arr[$item['mer_id']]['list'][] = $item;
  123. $mer_id = $item['mer_id'];
  124. $arr[$mer_id]['mer_id'] = $item['mer_id'];
  125. $arr[$mer_id]['mer_name'] = $item['mer_name'];
  126. $arr[$mer_id]['mer_avatar'] = $item['mer_avatar'];
  127. unset($item['mer_id'],$item['mer_name'],$item['mer_avatar']);
  128. $arr[$mer_id]['list'][] = $item;
  129. }
  130. return array_values($arr);
  131. }
  132. public function getProductImage($productId){
  133. $product = Db::query("select image from `rrx_store_product` where `product_id`=?",[$productId]);
  134. return $product[0]['image']??'';
  135. }
  136. }