Cart.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 数据层模型
  13. */
  14. class Cart extends BaseModel {
  15. /**
  16. * 购物车商品总金额
  17. */
  18. private $cart_all_price = 0;
  19. /**
  20. * 购物车商品总数
  21. */
  22. private $cart_goods_num = 0;
  23. /**
  24. * 取属性值魔术方法
  25. * @access public
  26. * @author csdeshang
  27. * @param type $name 名称
  28. * @return type
  29. */
  30. public function __get($name) {
  31. return $this->$name;
  32. }
  33. /**
  34. * 检查购物车内商品是否存在
  35. * @access public
  36. * @author csdeshang
  37. * @param array $condition 检索条件
  38. * @return bool
  39. */
  40. public function checkCart($condition = array()) {
  41. return Db::name('cart')->where($condition)->find();
  42. }
  43. /**
  44. * 会员购物车内商品数
  45. * @access public
  46. * @author csdeshang
  47. * @param int $memberId 会员ID
  48. * @return int
  49. */
  50. public function getCartCountByMemberId($memberId) {
  51. $memberId = intval($memberId);
  52. return Db::name('cart')->where(array('buyer_id' => $memberId,))->sum('goods_num');
  53. }
  54. /**
  55. * 取得 单条购物车信息
  56. * @access public
  57. * @author csdeshang
  58. * @param array $condition 检索条件
  59. * @param string $field 字段
  60. * @return array
  61. */
  62. public function getCartInfo($condition = array(), $field = '*') {
  63. return Db::name('cart')->field($field)->where($condition)->find();
  64. }
  65. /**
  66. * 将商品添加到购物车中
  67. * @access public
  68. * @author csdeshang
  69. * @param array $data 商品数据信息
  70. * @param string $save_type 保存类型,可选值 db,cookie
  71. * @param int $quantity 购物数量
  72. * @return type
  73. */
  74. public function addCart($data = array(), $save_type = '', $quantity = null) {
  75. $method = '_addCartdb';
  76. $result = $this->$method($data, $quantity);
  77. if(!$result){
  78. return false;
  79. }
  80. //更改购物车总商品数和总金额,传递数组参数只是给DB使用
  81. $this->getCartNum('db', array('buyer_id' => isset($data['buyer_id'])?$data['buyer_id']:0));
  82. return $result;
  83. }
  84. /**
  85. * 添加数据库购物车
  86. * @access public
  87. * @author csdeshang
  88. * @param array $goods_info 商品信息
  89. * @param int $quantity 购物数量
  90. * @return type
  91. */
  92. private function _addCartDb($goods_info = array(), $quantity) {
  93. //验证购物车商品是否已经存在
  94. $condition = array();
  95. $condition[] = array('goods_id','=',$goods_info['goods_id']);
  96. $condition[] = array('buyer_id','=',$goods_info['buyer_id']);
  97. if (isset($goods_info['bl_id'])) {
  98. $condition[] = array('bl_id','=',$goods_info['bl_id']);
  99. } else {
  100. $condition[] = array('bl_id','=',0);
  101. }
  102. //如果购物车
  103. $check_cart = $this->checkCart($condition);
  104. if (!empty($check_cart)) {
  105. $quantity = $check_cart['goods_num'] + $quantity;
  106. if($quantity>$goods_info['goods_storage']){
  107. $this->error_code = 10001;
  108. $this->error_message = '库存不足';
  109. return false;
  110. }
  111. //如果商品存在则更新数量
  112. return Db::name('cart')->where($condition)->update(array('goods_num'=>$quantity));
  113. } else {
  114. //如果商品存在则插入
  115. $array = array();
  116. $array['buyer_id'] = $goods_info['buyer_id'];
  117. $array['store_id'] = $goods_info['store_id'];
  118. $array['goods_id'] = $goods_info['goods_id'];
  119. $array['goods_name'] = $goods_info['goods_name'];
  120. $array['goods_price'] = $goods_info['goods_price'];
  121. $array['goods_num'] = $quantity;
  122. $array['goods_image'] = $goods_info['goods_image'];
  123. $array['store_name'] = $goods_info['store_name'];
  124. $array['bl_id'] = isset($goods_info['bl_id']) ? $goods_info['bl_id'] : 0;
  125. return Db::name('cart')->insertGetId($array);
  126. }
  127. }
  128. /**
  129. * 更新购物车
  130. * @access public
  131. * @author csdeshang
  132. * @param array $data 商品信息
  133. * @param array $condition 检索条件
  134. * @return bool
  135. */
  136. public function editCart($data, $condition,$buyer_id) {
  137. $result = Db::name('cart')->where($condition)->update($data);
  138. if ($result) {
  139. $this->getCartNum('db', array('buyer_id' => $buyer_id));
  140. }
  141. return $result;
  142. }
  143. /**
  144. * 购物车列表
  145. * @access public
  146. * @author csdeshang
  147. * @param string $type 存储类型 db,cookie
  148. * @param array $condition 检索条件
  149. * @param int $limit 限制
  150. * @return array
  151. */
  152. public function getCartList($type, $condition = array(), $limit = 0) {
  153. $cart_list = Db::name('cart')->where($condition)->limit($limit)->select()->toArray();
  154. $cart_list = is_array($cart_list) ? $cart_list : array();
  155. //顺便设置购物车商品数和总金额
  156. $this->cart_goods_num = count($cart_list);
  157. $cart_all_price = 0;
  158. if (is_array($cart_list)) {
  159. foreach ($cart_list as $val) {
  160. $cart_all_price += $val['goods_price'] * $val['goods_num'];
  161. }
  162. }
  163. $this->cart_all_price = ds_price_format($cart_all_price);
  164. return !is_array($cart_list) ? array() : $cart_list;
  165. }
  166. /**
  167. * 删除购物车商品
  168. * @access public
  169. * @author csdeshang
  170. * @param string $type 存储类型 db,cookie
  171. * @param array $condition 检索条件
  172. * @return bool
  173. */
  174. public function delCart($type, $condition = array(),$buyer_id) {
  175. $result = Db::name('cart')->where($condition)->delete();
  176. //重新计算购物车商品数和总金额
  177. if ($result) {
  178. $this->getCartNum('db', array('buyer_id' => $buyer_id));
  179. }
  180. return $result;
  181. }
  182. /**
  183. * 清空购物车
  184. * @access public
  185. * @author csdeshang
  186. * @param string $type 存储类型 db,cookie
  187. * @param array $condition 检索条件
  188. * @return bool
  189. */
  190. public function clearCart($type, $condition = array()) {
  191. //数据库暂无浅清空操作
  192. }
  193. /**
  194. * 计算购物车总商品数和总金额
  195. * @access public
  196. * @author csdeshang
  197. * @param string $type 购物车信息保存类型 db,cookie
  198. * @param array $condition 只有登录后操作购物车表时才会用到该参数
  199. * @return type
  200. */
  201. public function getCartNum($type, $condition = array()) {
  202. $cart_all_price = 0;
  203. $cart_goods = $this->getCartList('db', $condition);
  204. $this->cart_goods_num = count($cart_goods);
  205. if (!empty($cart_goods) && is_array($cart_goods)) {
  206. foreach ($cart_goods as $val) {
  207. $cart_all_price += $val['goods_price'] * $val['goods_num'];
  208. }
  209. }
  210. $this->cart_all_price = ds_price_format($cart_all_price);
  211. @cookie('cart_goods_num', $this->cart_goods_num, 2 * 3600);
  212. return $this->cart_goods_num;
  213. }
  214. }