123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\wap\model\store;
- use basic\ModelBasic;
- use traits\ModelTrait;
- /**商品购物车
- * Class StoreCart
- * @package app\wap\model\store
- */
- class StoreCart extends ModelBasic
- {
- use ModelTrait;
- protected $insert = ['add_time'];
- protected function setAddTimeAttr()
- {
- return time();
- }
- /**加入购物车
- * @param $uid
- * @param $product_id
- * @param int $cart_num
- * @param string $product_attr_unique
- * @param string $type
- * @param int $is_new
- * @param int $combination_id
- * @param int $seckill_id
- * @param int $integral_id
- * @return array|bool|false|object|\PDOStatement|string|\think\Model
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function setCart($uid, $product_id, $cart_num = 1, $product_attr_unique = '', $type = 'product', $is_new = 0, $combination_id = 0, $seckill_id = 0, $integral_id = 0)
- {
- if ($cart_num < 1) $cart_num = 1;
- if (!StoreProduct::isValidProduct($product_id))
- return self::setErrorInfo('该产品已下架或删除');
- if (StoreProduct::getProductStock($product_id, $product_attr_unique) < $cart_num)
- return self::setErrorInfo('该产品库存不足');
- $where = [
- 'type' => $type,
- 'uid' => $uid,
- 'product_id' => $product_id,
- 'product_attr_unique' => $product_attr_unique,
- 'is_new' => $is_new,
- 'is_pay' => 0,
- 'is_del' => 0,
- 'combination_id' => $combination_id,
- 'seckill_id' => $seckill_id,
- 'integral_id' => $integral_id
- ];
- if ($cart = self::where($where)->find()) {
- $cart->cart_num = $cart_num;
- $cart->add_time = time();
- $cart->save();
- return $cart;
- } else {
- return self::set(compact('uid', 'product_id', 'cart_num', 'product_attr_unique', 'is_new', 'type', 'combination_id', 'seckill_id', 'integral_id'));
- }
- }
- /**删除购物车
- * @param $uid
- * @param $ids
- * @return StoreCart
- */
- public static function removeUserCart($uid, $ids)
- {
- return self::where('uid', $uid)->where('id', 'IN', $ids)->update(['is_del' => 1]);
- }
- /**获取购物车数量
- * @param $uid
- * @param $type
- * @return int|string
- * @throws \think\Exception
- */
- public static function getUserCartNum($uid, $type)
- {
- return self::where('uid', $uid)->where('type', $type)->where('is_pay', 0)->where('is_del', 0)->where('is_new', 0)->count();
- }
- /**修改购物车商品数量
- * @param $cartId
- * @param $cartNum
- * @param $uid
- * @return StoreCart|bool
- */
- public static function changeUserCartNum($cartId, $cartNum, $uid)
- {
- if (!self::be(['uid' => $uid, 'id' => $cartId, 'cart_num' => $cartNum])) {
- return self::where('uid', $uid)->where('id', $cartId)->update(['cart_num' => $cartNum]);
- } else {
- return true;
- }
- }
- /**获取购物车数据
- * @param $uid
- * @param string $cartIds
- * @param int $status
- * @param $is_vip
- * @return array
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function getUserProductCartList($uid, $cartIds = '', $status = 0, $is_vip)
- {
- $productInfoField = 'id,image,slider_image,price,cost,ot_price,vip_price,postage,mer_id,give_gold_num,free_shipping,cate_id,sales,stock,store_name,unit_name,is_show,is_del,is_postage';
- $model = new self();
- $valid = $invalid = [];
- if ($cartIds)
- $model = $model->where('uid', $uid)->where('type', 'product')->where('is_pay', 0)
- ->where('is_del', 0);
- else
- $model = $model->where('uid', $uid)->where('type', 'product')->where('is_pay', 0)->where('is_new', 0)
- ->where('is_del', 0);
- if ($cartIds) $model->where('id', 'IN', $cartIds);
- $list = $model->select()->toArray();
- if (!count($list)) return compact('valid', 'invalid');
- foreach ($list as $k => $cart) {
- $product = StoreProduct::field($productInfoField)
- ->find($cart['product_id'])->toArray();
- $cart['productInfo'] = $product;
- //商品不存在
- if (!$product) {
- $model->where('id', $cart['id'])->update(['is_del' => 1]);
- //商品删除或无库存
- } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) {
- $invalid[] = $cart;
- } else {
- $cart['truePrice'] = $is_vip ? (isset($cart['productInfo']['vip_price']) ? (float)$cart['productInfo']['vip_price'] : (float)$cart['productInfo']['price']) : (float)$cart['productInfo']['price'];
- $cart['costPrice'] = (float)$cart['productInfo']['cost'];
- $cart['trueStock'] = $cart['productInfo']['stock'];
- $valid[] = $cart;
- }
- }
- foreach ($valid as $k => $cart) {
- if ($cart['trueStock'] < $cart['cart_num']) {
- $cart['cart_num'] = $cart['trueStock'];
- $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]);
- $valid[$k] = $cart;
- }
- }
- return compact('valid', 'invalid');
- }
- }
|