userCenterId = $userCenterId; $this->enterpriseId = $enterpriseId; $this->objDGoodsCollect = new DGoodsCollect('default'); $this->objCollectCache = new CollectCache(); if (isset($this->userCenterId) && empty($this->userCenterId)){ $this->userCenterId = 1;//若是后台,初始默认值 } $tableName = $this->objDGoodsCollect->getTableName('qianniao_goods_collect_'.$this->enterpriseId, $userCenterId, $this->cutTable); $this->objDGoodsCollect->setTable($tableName); } /** * 添加收藏 * @param $goodsId * @return ResultWrapper * @throws \Exception */ public function addGoodsCollect($goodsId) { $existResult =$this->hasCollect($goodsId); if($existResult) { //删除收藏记录 $dbResult = $this->objDGoodsCollect->delete(['goodsId'=>$goodsId, 'userCenterId'=>$this->userCenterId]); if ($dbResult === false) { return ResultWrapper::fail($this->objDGoodsCollect->error(), ErrorCode::$dberror); } $this->objCollectCache->delCollect($this->enterpriseId,$this->userCenterId, $goodsId); return ResultWrapper::success($dbResult); } $params = [ 'goodsId'=>$goodsId, 'userCenterId'=>$this->userCenterId, 'createTime'=>time(), 'updateTime'=>time(), ]; $dbResult = $this->objDGoodsCollect->insert($params); if ($dbResult === false) { return ResultWrapper::fail($this->objDGoodsCollect->error(), ErrorCode::$dberror); } //存入redis $this->objCollectCache->cacheCollect($this->enterpriseId,$this->userCenterId, $goodsId); return ResultWrapper::success($dbResult); } public function hasCollect($goodsId) { /*$where = [ 'goodsId'=>$goodsId, 'userCenterId'=>$this->userCenterId ]; $result = $this->objDGoodsCollect->select($where);*/ return $this->objCollectCache->getCollect($this->enterpriseId,$this->userCenterId, $goodsId); } /** * 获取收藏商品 * @return ResultWrapper */ public function getCollect() { $collect = $this->objCollectCache->getCollectList($this->enterpriseId,$this->userCenterId); return ResultWrapper::success($collect); } /** * 常购清单 * @param string $areaCode * @return ResultWrapper * @throws \Exception */ public function normalList(string $areaCode) { $normalListResult = self::getCollect(); if (!$normalListResult->isSuccess()){ return ResultWrapper::fail($normalListResult->getData(),$normalListResult->getErrorCode()); } $normalList = $normalListResult->getData(); if (empty($normalList)){ return ResultWrapper::success([]); } //格式化数据 $formatGoods = self::formatCollectGoodsMap($normalList,$areaCode); if (!$formatGoods->isSuccess()){ return ResultWrapper::fail($formatGoods->getData(),$formatGoods->getErrorCode()); } $mapping = self::formatCategoryGroup($formatGoods->getData()); return ResultWrapper::success($mapping); } /** * @param array $goodsIds * @param string $areaCode * @return ResultWrapper * @throws \Exception */ public function formatCollectGoodsMap(array $goodsIds,string $areaCode) { $objMGoods = new MGoods($this->enterpriseId,true,$this->userCenterId); $pageParams = pageToOffset(1, count($goodsIds)); $selectParams = [ 'limit' => $pageParams['limit'], 'offset' => $pageParams['offset'], 'userCenterId' => $this->userCenterId, 'goodsIds' => $goodsIds, 'areaCode' => $areaCode ]; $result = $objMGoods->search($selectParams); if (!$result->isSuccess()){ return ResultWrapper::fail($result->getData(),$result->getErrorCode()); } $goodsData = $result->getData()['data']; return ResultWrapper::success($goodsData); } /** * @param array $data * @return array */ public function formatCategoryGroup(array $data) { $objGoodsBasicRelevantCache = new GoodsBasicRelevant($this->enterpriseId); $map = []; if (empty($data)){ return $map; } foreach ($data as &$item){ unset($item['skuData']); unset($item['specGroup']); unset($item['unitData']); $cateGoryPath = explode(',',$item['categoryPath']); if (empty($cateGoryPath)){ continue; } $topCategoryId =(int) array_shift($cateGoryPath); $map[$topCategoryId]['data'][] = $item; $map[$topCategoryId]['categoryName'] = $objGoodsBasicRelevantCache->getNameByCategoryId($topCategoryId); } return array_values($map); } /** * Doc: (des="查询用户收藏的商品数量") * User: XMing * Date: 2021/3/17 * Time: 5:15 下午 * @param int $userCenterId * @return ResultWrapper */ public function getCollNum(int $userCenterId): ResultWrapper { $count = $this->objDGoodsCollect->count(['userCenterId'=>$userCenterId]); if ($count === false){ return ResultWrapper::fail($this->objDGoodsCollect->error(),ErrorCode::$dberror); } return ResultWrapper::success($count); } }