xhCartService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\models\xhCart;
  5. use common\cacheModels\shoppingCartCache;
  6. use common\components\dict;
  7. class xhCartService
  8. {
  9. /**
  10. * 增加一个商品
  11. */
  12. public static function add($userId, $goodsId, $data)
  13. {
  14. //增加了多价格选项时的变动
  15. if (isset($data['multiPriceId']) && is_numeric($data['multiPriceId']) && !empty($data['multiPriceId'])) {
  16. $multiPriceId = $data['multiPriceId'];
  17. $goodsPrice = xhGoodsPriceService::getById($multiPriceId);
  18. if (empty($goodsPrice)) {
  19. throw new \Exception('多规格ID:' . $multiPriceId . '不存在');
  20. }
  21. $data['goodsPriceId'] = $multiPriceId;
  22. }
  23. unset($data['multiPriceId']);
  24. unset($data['isMultiPrice']);
  25. $return = xhCart::add($data);
  26. return $return;
  27. }
  28. /**
  29. * 增加或修改商品记录
  30. */
  31. public static function replace($userId, $goodsId, $data)
  32. {
  33. $data['goodsPriceId'] = 0;
  34. if (isset($data['multiPriceId'])) {
  35. $multiPriceId = $data['multiPriceId'];
  36. } else {
  37. $multiPriceId = 0;
  38. }
  39. $cart = self::getByIds($userId, $goodsId, $multiPriceId);
  40. if (empty($cart)) {
  41. return self::add($userId, $goodsId, $data);
  42. }
  43. $id = $cart['id'];
  44. if (isset($data['multiPriceId']) && is_numeric($data['multiPriceId']) && !empty($data['multiPriceId'])) {
  45. $goodsPrice = xhGoodsPriceService::getById($data['multiPriceId']);
  46. if (empty($goodsPrice)) {//校验 multiPriceId 是否存在
  47. throw new \Exception('xhCartService:商品多种价格ID不存在');
  48. }
  49. $data['goodsPriceId'] = $data['multiPriceId'];
  50. }
  51. unset($data['multiPriceId']);
  52. unset($data['isMultiPrice']);
  53. self::updateById($id, $goodsId, $userId, $data);
  54. }
  55. /**
  56. * 删除购物车里每个商品的详细,同时删除购物车list的相应记录
  57. */
  58. public static function delByIds($userId, $goodsId, $goodsPriceId, $cartId = 0)
  59. {
  60. if ($cartId == 0) {
  61. $cart = xhCartService::getByIds($userId, $goodsId, $goodsPriceId);
  62. $cartId = $cart['id'];
  63. }
  64. $delResult = xhCart::deleteByCondition(['userId' => $userId, 'goodsId' => $goodsId, 'goodsPriceId' => $goodsPriceId]);
  65. if (!$delResult) {
  66. return false;
  67. }
  68. $cacheKey = $userId . dict::getCacheKey('cart') . $goodsId . '_' . $goodsPriceId;//xhCart表的缓存键
  69. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);//删除一个商品的缓存
  70. shoppingCartCache::delUserCart($userId, $goodsId, $goodsPriceId);//删除用户购物车里的商品
  71. }
  72. /**
  73. * 获取用户的购物车商品数据,含有商品信息
  74. */
  75. public static function getUserCartList($userId)
  76. {
  77. $arr = xhCart::getAllByCondition(['userId' => $userId]);
  78. $list = [];
  79. if (empty($arr)) {
  80. return $list;
  81. }
  82. foreach ($arr as $val) {
  83. $goodId = $val['goodsId'];
  84. $num = $val['num'];
  85. $multiPriceId = $val['goodsPriceId'];
  86. $goods = xhGoodsService::getById($goodId);
  87. $price = 0;
  88. if (!empty($goods)) {
  89. $price = $goods['price'];
  90. //商品封面小图标 ssh 2019.8.3
  91. $url = $goods['cover'];
  92. $pos = strrpos($url, '.');
  93. $pre = substr($url, 0, $pos);
  94. $ext = substr($url, ($pos + 1));
  95. $goods['middleCover'] = $pre . '_200.' . $ext;
  96. }
  97. $standardName = '';//规格名称
  98. if (!empty($multiPriceId)) {
  99. $goodsPrice = xhGoodsPriceService::getById($multiPriceId);
  100. if (!empty($goodsPrice)) {
  101. $price = $goodsPrice['price'];
  102. $standardName = $goodsPrice['title'];
  103. }
  104. }
  105. $list[] = ['goodsInfo' => $goods, 'num' => $num, 'multiPriceId' => $multiPriceId, 'price' => $price,
  106. 'selected' => 0,//用于小程序上标识是否选中 0未选中 1选中
  107. 'standardName' => $standardName];
  108. }
  109. return $list;
  110. }
  111. //修改商品记录(表数据 与 缓存)
  112. public static function updateById($cartId, $goodsId, $userId, $data)
  113. {
  114. $num = $data['num'];
  115. if (!is_numeric($num) || $num <= 0) {
  116. throw new \Exception('xhCartService addOneUserCart Error: param $data["num"] error');
  117. }
  118. $cart = self::getByIds($userId, $goodsId, $data['goodsPriceId']);
  119. $data['num'] += $cart['num'];
  120. xhCart::updateById($cartId, $data);//更新数据库
  121. $cacheKey = $userId . dict::getCacheKey('cart') . $goodsId . '_' . $cart['goodsPriceId'];//购物车表(xhCart)的缓存键
  122. $params = [$cacheKey];
  123. foreach ($data as $key => $val) {
  124. $params[] = $key;
  125. $params[] = $val;
  126. }
  127. Yii::$app->redis->executeCommand('HMSET', $params);//更新一个商品的缓存
  128. $goodsPriceId = $data['goodsPriceId'];
  129. shoppingCartCache::updateUserCart($userId, $goodsId, $goodsPriceId, $num);//更新用户购物车里的商品
  130. }
  131. /**
  132. * 获取购物车表(xhCart)商品缓存
  133. * @param int $userId
  134. * @param int $goodsId
  135. */
  136. public static function getByIds($userId, $goodsId, $goodsPriceId = 0)
  137. {
  138. $cacheKey = $userId . dict::getCacheKey('cart') . $goodsId . '_' . $goodsPriceId;//购物车表(xhCart)的缓存键
  139. $cart = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  140. if (!empty($cart)) {
  141. $arr = [];
  142. $i = 0;
  143. $x = 0;
  144. foreach ($cart as $key => $val) {
  145. $i++;
  146. if ($i % 2 == 0) {
  147. $arr[$x]['value'] = $val;
  148. $x++;
  149. } else {
  150. $arr[$x]['key'] = $val;
  151. }
  152. }
  153. $cartArr = [];
  154. foreach ($arr as $key => $val) {
  155. $cartArr[$val['key']] = $val['value'];
  156. }
  157. unset($cartArr['info_already_get_by_sql']);
  158. return $cartArr;
  159. }
  160. $cart = xhCart::getByCondition(['goodsId' => $goodsId, 'userId' => $userId, 'goodsPriceId' => $goodsPriceId]);
  161. $params = [$cacheKey];
  162. $params[] = 'info_already_get_by_sql';
  163. $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
  164. if (!empty($cart)) {
  165. foreach ($cart as $key => $val) {
  166. $params[] = $key;
  167. $params[] = $val;
  168. }
  169. }
  170. Yii::$app->redis->executeCommand('HMSET', $params);
  171. return $cart;
  172. }
  173. public static function refresh($userId, $goodsId, $goodsPriceId = 0)
  174. {
  175. $cacheKey = $userId . dict::getCacheKey('cart') . $goodsId . '_' . $goodsPriceId;//购物车表(xhCart)的缓存键
  176. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  177. self::getByIds($goodsId, $userId, $goodsPriceId);//添加一个商品的缓存
  178. }
  179. }