Cart.php 7.4 KB

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