Cart.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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,))->count();
  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. if($quantity != $check_cart['goods_num']){
  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. return 1;
  117. }
  118. } else {
  119. //如果商品存在则插入
  120. $array = array();
  121. $array['buyer_id'] = $goods_info['buyer_id'];
  122. $array['store_id'] = $goods_info['store_id'];
  123. $array['goods_id'] = $goods_info['goods_id'];
  124. $array['goods_name'] = $goods_info['goods_name'];
  125. $array['goods_price'] = $goods_info['goods_price'];
  126. $array['goods_num'] = $quantity;
  127. $array['goods_image'] = $goods_info['goods_image'];
  128. $array['store_name'] = $goods_info['store_name'];
  129. $array['bl_id'] = isset($goods_info['bl_id']) ? $goods_info['bl_id'] : 0;
  130. return Db::name('cart')->insertGetId($array);
  131. }
  132. }
  133. /**
  134. * 更新购物车
  135. * @access public
  136. * @author csdeshang
  137. * @param array $data 商品信息
  138. * @param array $condition 检索条件
  139. * @return bool
  140. */
  141. public function editCart($data, $condition,$buyer_id) {
  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. $cart_list = Db::name('cart')->where($condition)->limit($limit)->select()->toArray();
  159. $cart_list = is_array($cart_list) ? $cart_list : array();
  160. //顺便设置购物车商品数和总金额
  161. $this->cart_goods_num = count($cart_list);
  162. $cart_all_price = 0;
  163. if (is_array($cart_list)) {
  164. foreach ($cart_list as $val) {
  165. $cart_all_price += $val['goods_price'] * $val['goods_num'];
  166. }
  167. }
  168. $this->cart_all_price = ds_price_format($cart_all_price);
  169. return !is_array($cart_list) ? array() : $cart_list;
  170. }
  171. /**
  172. * 删除购物车商品
  173. * @access public
  174. * @author csdeshang
  175. * @param string $type 存储类型 db,cookie
  176. * @param array $condition 检索条件
  177. * @return bool
  178. */
  179. public function delCart($type, $condition = array(),$buyer_id) {
  180. $result = Db::name('cart')->where($condition)->delete();
  181. //重新计算购物车商品数和总金额
  182. if ($result) {
  183. $this->getCartNum('db', array('buyer_id' => $buyer_id));
  184. }
  185. return $result;
  186. }
  187. /**
  188. * 清空购物车
  189. * @access public
  190. * @author csdeshang
  191. * @param string $type 存储类型 db,cookie
  192. * @param array $condition 检索条件
  193. * @return bool
  194. */
  195. public function clearCart($type, $condition = array()) {
  196. //数据库暂无浅清空操作
  197. }
  198. /**
  199. * 计算购物车总商品数和总金额
  200. * @access public
  201. * @author csdeshang
  202. * @param string $type 购物车信息保存类型 db,cookie
  203. * @param array $condition 只有登录后操作购物车表时才会用到该参数
  204. * @return type
  205. */
  206. public function getCartNum($type, $condition = array()) {
  207. $cart_all_price = 0;
  208. $cart_goods = $this->getCartList('db', $condition);
  209. $this->cart_goods_num = count($cart_goods);
  210. if (!empty($cart_goods) && is_array($cart_goods)) {
  211. foreach ($cart_goods as $val) {
  212. $cart_all_price += $val['goods_price'] * $val['goods_num'];
  213. }
  214. }
  215. $this->cart_all_price = ds_price_format($cart_all_price);
  216. @cookie('cart_goods_num', $this->cart_goods_num, 2 * 3600);
  217. return $this->cart_goods_num;
  218. }
  219. }
  220. ?>