123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- namespace app\common\model;
- use think\facade\Db;
- class Cart extends BaseModel {
-
- private $cart_all_price = 0;
-
- private $cart_goods_num = 0;
-
- public function __get($name) {
- return $this->$name;
- }
-
- public function checkCart($condition = array()) {
- return Db::name('cart')->where($condition)->find();
- }
-
- public function getCartCountByMemberId($memberId) {
- $memberId = intval($memberId);
- return Db::name('cart')->where(array('buyer_id' => $memberId,))->sum('goods_num');
- }
-
- public function getCartInfo($condition = array(), $field = '*') {
- return Db::name('cart')->field($field)->where($condition)->find();
- }
-
- public function addCart($data = array(), $save_type = '', $quantity = null) {
- $method = '_addCartdb';
- $result = $this->$method($data, $quantity);
- if(!$result){
- return false;
- }
-
- $this->getCartNum('db', array('buyer_id' => isset($data['buyer_id'])?$data['buyer_id']:0));
- return $result;
- }
-
- private function _addCartDb($goods_info = array(), $quantity) {
-
- $condition = array();
- $condition[] = array('goods_id','=',$goods_info['goods_id']);
- $condition[] = array('buyer_id','=',$goods_info['buyer_id']);
- if (isset($goods_info['bl_id'])) {
- $condition[] = array('bl_id','=',$goods_info['bl_id']);
- } else {
- $condition[] = array('bl_id','=',0);
- }
-
- $check_cart = $this->checkCart($condition);
-
- if (!empty($check_cart)) {
- $quantity = $check_cart['goods_num'] + $quantity;
- if($quantity>$goods_info['goods_storage']){
- $this->error_code = 10001;
- $this->error_message = '库存不足';
- return false;
- }
-
- return Db::name('cart')->where($condition)->update(array('goods_num'=>$quantity));
-
- } else {
-
- $array = array();
- $array['buyer_id'] = $goods_info['buyer_id'];
- $array['store_id'] = $goods_info['store_id'];
- $array['goods_id'] = $goods_info['goods_id'];
- $array['goods_name'] = $goods_info['goods_name'];
- $array['goods_price'] = $goods_info['goods_price'];
- $array['goods_num'] = $quantity;
- $array['goods_image'] = $goods_info['goods_image'];
- $array['store_name'] = $goods_info['store_name'];
- $array['bl_id'] = isset($goods_info['bl_id']) ? $goods_info['bl_id'] : 0;
- return Db::name('cart')->insertGetId($array);
- }
-
-
- }
-
- public function editCart($data, $condition,$buyer_id) {
- $result = Db::name('cart')->where($condition)->update($data);
- if ($result) {
- $this->getCartNum('db', array('buyer_id' => $buyer_id));
- }
- return $result;
- }
-
- public function getCartList($type, $condition = array(), $limit = 0) {
- $cart_list = Db::name('cart')->where($condition)->limit($limit)->select()->toArray();
- $cart_list = is_array($cart_list) ? $cart_list : array();
-
- $this->cart_goods_num = count($cart_list);
- $cart_all_price = 0;
- if (is_array($cart_list)) {
- foreach ($cart_list as $val) {
- $cart_all_price += $val['goods_price'] * $val['goods_num'];
- }
- }
- $this->cart_all_price = ds_price_format($cart_all_price);
- return !is_array($cart_list) ? array() : $cart_list;
- }
-
- public function delCart($type, $condition = array(),$buyer_id) {
- $result = Db::name('cart')->where($condition)->delete();
-
- if ($result) {
- $this->getCartNum('db', array('buyer_id' => $buyer_id));
- }
- return $result;
- }
-
- public function clearCart($type, $condition = array()) {
-
-
- }
-
- public function getCartNum($type, $condition = array()) {
- $cart_all_price = 0;
- $cart_goods = $this->getCartList('db', $condition);
- $this->cart_goods_num = count($cart_goods);
- if (!empty($cart_goods) && is_array($cart_goods)) {
- foreach ($cart_goods as $val) {
- $cart_all_price += $val['goods_price'] * $val['goods_num'];
- }
- }
- $this->cart_all_price = ds_price_format($cart_all_price);
- @cookie('cart_goods_num', $this->cart_goods_num, 2 * 3600);
- return $this->cart_goods_num;
- }
- }
- ?>
|