123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- /**
- * 商品收藏管理模块
- * Created by PhpStorm.
- * User: wxj
- * Date: 2019/10/30
- * Time: 14:02
- */
- namespace JinDouYun\Model\Goods;
- use JinDouYun\Cache\GoodsBasicRelevant;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Dao\Goods\DGoodsCollect;
- use JinDouYun\Cache\CollectCache;
- use function GuzzleHttp\Psr7\str;
- class MGoodsCollect
- {
- private $objDGoodsCollect;
- private $objCollectCache;
- private $cutTable = 25000;//商品按照企业id分表
- private $enterpriseId;
- private $userCenterId;
- public function __construct($enterpriseId, $userCenterId)
- {
- $this->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);
- }
- }
|