Cart.php 7.8 KB

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