| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace app\common\cart;
- use app\common\BaseRepository;
- use think\exception\ValidateException;
- use think\facade\Db;
- use think\facade\Log;
- class CartRepository extends BaseRepository
- {
- /**
- * TODO 商品检测
- * @param $data
- * @return array
- */
- public function checkProduct($data): array
- {
- if( $data['cart_num'] < 0 ) return $this -> getMessage(101);
- $productSelect = Db::query("select * from `rrx_store_product` where `product_id`=? limit 1", [$data['product_id']]);
- if(!$productSelect) return $this -> getMessage(102);
- $product = $productSelect[0];
- $skuSelect = Db::query("select * from `rrx_store_product_attr_value` where `unique`=? and `product_id`=? limit 1", [$data['product_attr_unique'],$data['product_id']]);
- $sku = $skuSelect[0];
- if(!$sku) return $this -> getMessage(103);
- if($sku['product_id'] != $data['product_id']) return $this -> getMessage(104);
- //积分
- if(isset($product['mer_id']) && $product['mer_id'] == '148' && !$data['is_new']) return $this -> getMessage(105);
- //秒杀
- $nowTime = date("Y-m-d H:00:00");//当前小时
- $seckill = Db::query("select * from `rrx_product_seckill` where `seckill_date`=? and `product_id`=? and `status`=1 limit 1",[$nowTime,$data['product_id']]);
- if($seckill && !$data['is_new']) return $this -> getMessage(105);
- return ['product' => $product,'sku' => $sku];
- }
- /**
- * TODO 购物车检测
- * @param $uid
- * @param $product_attr_unique
- * @return array|boolean
- */
- public function checkCart($uid,$product_attr_unique){
- // ['is_del'=>0,'is_fail'=>0,'is_new'=>0,'is_pay'=>0,'uid' => $uid,'product_type' => 0,'product_attr_unique' => $sku];
- $cartSelect = Db::query("select * from `rrx_store_cart` where `is_del`=? and `is_fail`=? and `is_new`=?
- and `is_pay`=? and `uid`=? and `product_type`=? and `product_attr_unique`=? limit 1",
- [0,0,0,0,$uid,0,$product_attr_unique]);
- if($cartSelect) return $cartSelect[0];
- return false;
- }
- /**
- * TODO 购物车数量修改
- * @param $cart_id
- * @param $cart_num
- */
- public function updateCart($cart_id,$cart_num){
- try {
- Db::query("UPDATE `rrx_store_cart` SET `cart_num`=? WHERE `cart_id`=?",[$cart_num,$cart_id]);
- }catch (\Exception $e){
- log::error(parseException($e));
- return $this -> getMessage(108);
- }
- }
- /**
- * TODO 商品检测
- * @param $data
- * @return array
- */
- public function createCart($data){
- try {
- Db::query("INSERT INTO `rrx_store_cart` SET
- `product_id`=?,
- `product_attr_unique`=?,
- `cart_num`=?,
- `is_new`=?,
- `uid`=?,
- `mer_id`=?,
- `create_time`=?",
- [$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")]);
- //返回购物车ID
- $cartId = Db::query("SELECT LAST_INSERT_ID()");
- return ['cart_id'=>$cartId[0]['LAST_INSERT_ID()']];
- }catch (\Exception $e){
- log::error(parseException($e));
- return $this -> getMessage(107);
- }
- }
- /**
- * TODO 获取单个购物车详情
- * @param $cartId
- * @param $uid
- * @return array
- */
- public function getOne($cartId,$uid){
- //'is_del'=>0,'is_fail'=>0,'is_new'=>0,'is_pay'=>0,'uid' => $uid];
- $cartSelect = Db::query("select * from `rrx_store_cart` where `uid`=? and `cart_id`=? limit 1",[$uid,$cartId]);
- if(!$cartSelect) return $this -> getMessage(109);
- return $cartSelect[0];
- }
- /**
- * TODO 获取购物车列表
- * @param $uid
- * @return array
- */
- public function getListFromUid($uid){
- $list = Db::query("select
- `rsc`.`cart_num`,`rsc`.`cart_id`,
- `rm`.`mer_id`,`rm`.`mer_name`,`rm`.`mer_avatar`,
- `rspav`.`product_id`,`rspav`.`price`,`rspav`.`unique`,`rspav`.`sku`,`rspav`.`image`
- from `rrx_store_cart` `rsc`
- left JOIN `rrx_store_product_attr_value` `rspav` on `rsc`.`product_attr_unique` = `rspav`.`unique`
- left JOIN `rrx_merchant` `rm` on `rsc`.`mer_id` = `rm`.`mer_id`
- where
- `rsc`.`uid`=? and
- `rsc`.`is_del`=? and
- `rsc`.`is_new`=? and
- `rsc`.`is_pay`=?"
- ,[$uid,0,0,0]);
- // $list = Db::query("select * from `rrx_store_cart` where `uid`=? and `is_del`=? and `is_new`=? and `is_pay`=?",[$uid,0,0,0]);
- return $this -> formatCartList($list);
- }
- //购物车列表格式化
- public function formatCartList($list){
- $arr = [];
- foreach($list as $key => $item){
- if (empty($item['image']))$item['image'] = $this -> getProductImage($item['product_id']);
- // $arr[$item['mer_id']]['list'][] = $item;
- $mer_id = $item['mer_id'];
- $arr[$mer_id]['mer_id'] = $item['mer_id'];
- $arr[$mer_id]['mer_name'] = $item['mer_name'];
- $arr[$mer_id]['mer_avatar'] = $item['mer_avatar'];
- unset($item['mer_id'],$item['mer_name'],$item['mer_avatar']);
- $arr[$mer_id]['list'][] = $item;
- }
- return array_values($arr);
- }
- public function getProductImage($productId){
- $product = Db::query("select image from `rrx_store_product` where `product_id`=?",[$productId]);
- return $product[0]['image']??'';
- }
- }
|