MGoodsCollect.Class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * 商品收藏管理模块
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/10/30
  7. * Time: 14:02
  8. */
  9. namespace JinDouYun\Model\Goods;
  10. use JinDouYun\Cache\GoodsBasicRelevant;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\StatusCode;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use JinDouYun\Dao\Goods\DGoodsCollect;
  15. use JinDouYun\Cache\CollectCache;
  16. use function GuzzleHttp\Psr7\str;
  17. class MGoodsCollect
  18. {
  19. private $objDGoodsCollect;
  20. private $objCollectCache;
  21. private $cutTable = 25000;//商品按照企业id分表
  22. private $enterpriseId;
  23. private $userCenterId;
  24. public function __construct($enterpriseId, $userCenterId)
  25. {
  26. $this->userCenterId = $userCenterId;
  27. $this->enterpriseId = $enterpriseId;
  28. $this->objDGoodsCollect = new DGoodsCollect('default');
  29. $this->objCollectCache = new CollectCache();
  30. if (isset($this->userCenterId) && empty($this->userCenterId)){
  31. $this->userCenterId = 1;//若是后台,初始默认值
  32. }
  33. $tableName = $this->objDGoodsCollect->getTableName('qianniao_goods_collect_'.$this->enterpriseId, $userCenterId, $this->cutTable);
  34. $this->objDGoodsCollect->setTable($tableName);
  35. }
  36. /**
  37. * 添加收藏
  38. * @param $goodsId
  39. * @return ResultWrapper
  40. * @throws \Exception
  41. */
  42. public function addGoodsCollect($goodsId)
  43. {
  44. $existResult =$this->hasCollect($goodsId);
  45. if($existResult) {
  46. //删除收藏记录
  47. $dbResult = $this->objDGoodsCollect->delete(['goodsId'=>$goodsId, 'userCenterId'=>$this->userCenterId]);
  48. if ($dbResult === false) {
  49. return ResultWrapper::fail($this->objDGoodsCollect->error(), ErrorCode::$dberror);
  50. }
  51. $this->objCollectCache->delCollect($this->enterpriseId,$this->userCenterId, $goodsId);
  52. return ResultWrapper::success($dbResult);
  53. }
  54. $params = [
  55. 'goodsId'=>$goodsId,
  56. 'userCenterId'=>$this->userCenterId,
  57. 'createTime'=>time(),
  58. 'updateTime'=>time(),
  59. ];
  60. $dbResult = $this->objDGoodsCollect->insert($params);
  61. if ($dbResult === false) {
  62. return ResultWrapper::fail($this->objDGoodsCollect->error(), ErrorCode::$dberror);
  63. }
  64. //存入redis
  65. $this->objCollectCache->cacheCollect($this->enterpriseId,$this->userCenterId, $goodsId);
  66. return ResultWrapper::success($dbResult);
  67. }
  68. public function hasCollect($goodsId) {
  69. /*$where = [
  70. 'goodsId'=>$goodsId,
  71. 'userCenterId'=>$this->userCenterId
  72. ];
  73. $result = $this->objDGoodsCollect->select($where);*/
  74. return $this->objCollectCache->getCollect($this->enterpriseId,$this->userCenterId, $goodsId);
  75. }
  76. /**
  77. * 获取收藏商品
  78. * @return ResultWrapper
  79. */
  80. public function getCollect()
  81. {
  82. $collect = $this->objCollectCache->getCollectList($this->enterpriseId,$this->userCenterId);
  83. return ResultWrapper::success($collect);
  84. }
  85. /**
  86. * 常购清单
  87. * @param string $areaCode
  88. * @return ResultWrapper
  89. * @throws \Exception
  90. */
  91. public function normalList(string $areaCode)
  92. {
  93. $normalListResult = self::getCollect();
  94. if (!$normalListResult->isSuccess()){
  95. return ResultWrapper::fail($normalListResult->getData(),$normalListResult->getErrorCode());
  96. }
  97. $normalList = $normalListResult->getData();
  98. if (empty($normalList)){
  99. return ResultWrapper::success([]);
  100. }
  101. //格式化数据
  102. $formatGoods = self::formatCollectGoodsMap($normalList,$areaCode);
  103. if (!$formatGoods->isSuccess()){
  104. return ResultWrapper::fail($formatGoods->getData(),$formatGoods->getErrorCode());
  105. }
  106. $mapping = self::formatCategoryGroup($formatGoods->getData());
  107. return ResultWrapper::success($mapping);
  108. }
  109. /**
  110. * @param array $goodsIds
  111. * @param string $areaCode
  112. * @return ResultWrapper
  113. * @throws \Exception
  114. */
  115. public function formatCollectGoodsMap(array $goodsIds,string $areaCode)
  116. {
  117. $objMGoods = new MGoods($this->enterpriseId,true,$this->userCenterId);
  118. $pageParams = pageToOffset(1, count($goodsIds));
  119. $selectParams = [
  120. 'limit' => $pageParams['limit'],
  121. 'offset' => $pageParams['offset'],
  122. 'userCenterId' => $this->userCenterId,
  123. 'goodsIds' => $goodsIds,
  124. 'areaCode' => $areaCode
  125. ];
  126. $result = $objMGoods->search($selectParams);
  127. if (!$result->isSuccess()){
  128. return ResultWrapper::fail($result->getData(),$result->getErrorCode());
  129. }
  130. $goodsData = $result->getData()['data'];
  131. return ResultWrapper::success($goodsData);
  132. }
  133. /**
  134. * @param array $data
  135. * @return array
  136. */
  137. public function formatCategoryGroup(array $data)
  138. {
  139. $objGoodsBasicRelevantCache = new GoodsBasicRelevant($this->enterpriseId);
  140. $map = [];
  141. if (empty($data)){
  142. return $map;
  143. }
  144. foreach ($data as &$item){
  145. unset($item['skuData']);
  146. unset($item['specGroup']);
  147. unset($item['unitData']);
  148. $cateGoryPath = explode(',',$item['categoryPath']);
  149. if (empty($cateGoryPath)){
  150. continue;
  151. }
  152. $topCategoryId =(int) array_shift($cateGoryPath);
  153. $map[$topCategoryId]['data'][] = $item;
  154. $map[$topCategoryId]['categoryName'] = $objGoodsBasicRelevantCache->getNameByCategoryId($topCategoryId);
  155. }
  156. return array_values($map);
  157. }
  158. /**
  159. * Doc: (des="查询用户收藏的商品数量")
  160. * User: XMing
  161. * Date: 2021/3/17
  162. * Time: 5:15 下午
  163. * @param int $userCenterId
  164. * @return ResultWrapper
  165. */
  166. public function getCollNum(int $userCenterId): ResultWrapper
  167. {
  168. $count = $this->objDGoodsCollect->count(['userCenterId'=>$userCenterId]);
  169. if ($count === false){
  170. return ResultWrapper::fail($this->objDGoodsCollect->error(),ErrorCode::$dberror);
  171. }
  172. return ResultWrapper::success($count);
  173. }
  174. }