Cart.php 8.0 KB

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