| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- namespace common\services;
- use Yii;
- use common\models\xhCart;
- use common\cacheModels\shoppingCartCache;
- use common\components\dict;
- class xhCartService
- {
- /**
- * 增加一个商品
- */
- public static function add($userId, $goodsId, $data)
- {
- //增加了多价格选项时的变动
- if (isset($data['multiPriceId']) && is_numeric($data['multiPriceId']) && !empty($data['multiPriceId'])) {
- $multiPriceId = $data['multiPriceId'];
- $goodsPrice = xhGoodsPriceService::getById($multiPriceId);
- if (empty($goodsPrice)) {
- throw new \Exception('多规格ID:' . $multiPriceId . '不存在');
- }
- $data['goodsPriceId'] = $multiPriceId;
- }
- unset($data['multiPriceId']);
- unset($data['isMultiPrice']);
- $return = xhCart::add($data);
- return $return;
- }
-
- /**
- * 增加或修改商品记录
- */
- public static function replace($userId, $goodsId, $data)
- {
- $data['goodsPriceId'] = 0;
- if (isset($data['multiPriceId'])) {
- $multiPriceId = $data['multiPriceId'];
- } else {
- $multiPriceId = 0;
- }
- $cart = self::getByIds($userId, $goodsId, $multiPriceId);
- if (empty($cart)) {
- return self::add($userId, $goodsId, $data);
- }
-
- $id = $cart['id'];
- if (isset($data['multiPriceId']) && is_numeric($data['multiPriceId']) && !empty($data['multiPriceId'])) {
- $goodsPrice = xhGoodsPriceService::getById($data['multiPriceId']);
- if (empty($goodsPrice)) {//校验 multiPriceId 是否存在
- throw new \Exception('xhCartService:商品多种价格ID不存在');
- }
- $data['goodsPriceId'] = $data['multiPriceId'];
- }
- unset($data['multiPriceId']);
- unset($data['isMultiPrice']);
- self::updateById($id, $goodsId, $userId, $data);
- }
-
-
- /**
- * 删除购物车里每个商品的详细,同时删除购物车list的相应记录
- */
- public static function delByIds($userId, $goodsId, $goodsPriceId, $cartId = 0)
- {
- if ($cartId == 0) {
- $cart = xhCartService::getByIds($userId, $goodsId, $goodsPriceId);
- $cartId = $cart['id'];
- }
- $delResult = xhCart::deleteByCondition(['userId' => $userId, 'goodsId' => $goodsId, 'goodsPriceId' => $goodsPriceId]);
- if (!$delResult) {
- return false;
- }
-
- $cacheKey = $userId . dict::getCacheKey('cart') . $goodsId . '_' . $goodsPriceId;//xhCart表的缓存键
- Yii::$app->redis->executeCommand('DEL', [$cacheKey]);//删除一个商品的缓存
- shoppingCartCache::delUserCart($userId, $goodsId, $goodsPriceId);//删除用户购物车里的商品
- }
-
- /**
- * 获取用户的购物车商品数据,含有商品信息
- */
- public static function getUserCartList($userId)
- {
- $arr = xhCart::getAllByCondition(['userId' => $userId]);
- $list = [];
- if (empty($arr)) {
- return $list;
- }
- foreach ($arr as $val) {
- $goodId = $val['goodsId'];
- $num = $val['num'];
- $multiPriceId = $val['goodsPriceId'];
- $goods = xhGoodsService::getById($goodId);
- $price = 0;
- if (!empty($goods)) {
- $price = $goods['price'];
- //商品封面小图标 ssh 2019.8.3
- $url = $goods['cover'];
- $pos = strrpos($url, '.');
- $pre = substr($url, 0, $pos);
- $ext = substr($url, ($pos + 1));
- $goods['middleCover'] = $pre . '_200.' . $ext;
- }
- $standardName = '';//规格名称
- if (!empty($multiPriceId)) {
- $goodsPrice = xhGoodsPriceService::getById($multiPriceId);
- if (!empty($goodsPrice)) {
- $price = $goodsPrice['price'];
- $standardName = $goodsPrice['title'];
- }
- }
- $list[] = ['goodsInfo' => $goods, 'num' => $num, 'multiPriceId' => $multiPriceId, 'price' => $price,
- 'selected' => 0,//用于小程序上标识是否选中 0未选中 1选中
- 'standardName' => $standardName];
- }
- return $list;
- }
-
- //修改商品记录(表数据 与 缓存)
- public static function updateById($cartId, $goodsId, $userId, $data)
- {
- $num = $data['num'];
- if (!is_numeric($num) || $num <= 0) {
- throw new \Exception('xhCartService addOneUserCart Error: param $data["num"] error');
- }
-
- $cart = self::getByIds($userId, $goodsId, $data['goodsPriceId']);
- $data['num'] += $cart['num'];
- xhCart::updateById($cartId, $data);//更新数据库
- $cacheKey = $userId . dict::getCacheKey('cart') . $goodsId . '_' . $cart['goodsPriceId'];//购物车表(xhCart)的缓存键
- $params = [$cacheKey];
- foreach ($data as $key => $val) {
- $params[] = $key;
- $params[] = $val;
- }
- Yii::$app->redis->executeCommand('HMSET', $params);//更新一个商品的缓存
- $goodsPriceId = $data['goodsPriceId'];
- shoppingCartCache::updateUserCart($userId, $goodsId, $goodsPriceId, $num);//更新用户购物车里的商品
- }
-
- /**
- * 获取购物车表(xhCart)商品缓存
- * @param int $userId
- * @param int $goodsId
- */
- public static function getByIds($userId, $goodsId, $goodsPriceId = 0)
- {
- $cacheKey = $userId . dict::getCacheKey('cart') . $goodsId . '_' . $goodsPriceId;//购物车表(xhCart)的缓存键
- $cart = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
- if (!empty($cart)) {
- $arr = [];
- $i = 0;
- $x = 0;
- foreach ($cart as $key => $val) {
- $i++;
- if ($i % 2 == 0) {
- $arr[$x]['value'] = $val;
- $x++;
- } else {
- $arr[$x]['key'] = $val;
- }
- }
- $cartArr = [];
- foreach ($arr as $key => $val) {
- $cartArr[$val['key']] = $val['value'];
- }
- unset($cartArr['info_already_get_by_sql']);
- return $cartArr;
- }
- $cart = xhCart::getByCondition(['goodsId' => $goodsId, 'userId' => $userId, 'goodsPriceId' => $goodsPriceId]);
- $params = [$cacheKey];
- $params[] = 'info_already_get_by_sql';
- $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
- if (!empty($cart)) {
- foreach ($cart as $key => $val) {
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET', $params);
- return $cart;
- }
-
- public static function refresh($userId, $goodsId, $goodsPriceId = 0)
- {
- $cacheKey = $userId . dict::getCacheKey('cart') . $goodsId . '_' . $goodsPriceId;//购物车表(xhCart)的缓存键
- Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
- self::getByIds($goodsId, $userId, $goodsPriceId);//添加一个商品的缓存
- }
-
- }
|