123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- <?php
- /**
- * 首页概况缓存
- * Created by PhpStorm.
- * User: wxj
- * Date: 2019/11/5
- * Time: 10:14
- */
- namespace Jobs\Cache;
- use http\Exception;
- use Mall\Framework\Factory;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\ErrorCode;
- class OverviewCache
- {
- private $cache;
- private $expireTime = 86400;
- private $orderDay = 7;//近7天订单趋势
- protected $aggregateStatisticsKey = 'aggregateStatistics';//总数统计 区分企业
- protected $businessOverviewEnterpriseKey = 'businessOverviewEnterprise';//经营概况_日期_企业Id_(全店)
- protected $businessOverviewEnterpriseShopKey = 'businessOverviewEnterpriseShop';//经营概况_日期_企业Id_商铺id
- protected $rankingEnterpriseKey = 'rankingEnterprise';//销量排行榜_日期_企业Id_(全店)
- protected $rankingEnterpriseShopKey = 'rankingEnterpriseShop';//销量排行榜_日期_企业Id_商铺id
- protected $salesMoneyRankingEnterpriseKey = 'salesMoneyRankingEnterprise';//销额排行榜_日期_企业Id_(全店)
- protected $salesMoneyRankingEnterpriseShopKey = 'salesMoneyRankingEnterpriseShop';//销额排行榜_日期_企业Id_商铺id
- protected $orderTrendKey = 'orderTrend';//近7天订单趋势_企业Id
- public function __construct()
- {
- $this->cache = Factory::cache('finance');
- }
- /**
- * 总数统计
- * @param $enterpriseId
- * @param string $key
- * @param $change_num
- * @return ResultWrapper
- */
- public function saveAggregateStatistics($enterpriseId, $key = 'totalShouldReceive', $change_num)
- {
- if (empty($key)) {
- return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
- }
- //获取Key的过期时间,大于0的话就不再设置了
- $Key = $this->aggregateStatisticsKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
- $ttl = $this->cache->ttl($Key);
- $old = self::getAggregateStatistics($enterpriseId,$key);
- $this->cache->hset($Key , $key, $change_num + $old);
- //如果键没有设置,那么设置过期时间
- if ($ttl == -2) {
- $this->cache->expire($Key, $this->expireTime);
- }
- }
- /**
- * 获取总数统计
- * @param $enterpriseId
- * @param $key
- * @return array
- */
- public function getAggregateStatistics($enterpriseId, $key = 'totalShouldReceive')
- {
- $Key = $this->aggregateStatisticsKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
- $result = $this->cache->hget($Key, $key);
- return $result ? $result : 0;
- }
- /**
- * 删除总数统计
- * @param $enterpriseId
- * @param $key
- * @return array
- */
- public function delAggregateStatistics($enterpriseId, $key = 'totalShouldReceive')
- {
- $Key = $this->aggregateStatisticsKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
- return $this->cache->hdel($Key, $key);
- }
- /**************************经营概况 start**********************************/
- /**
- * 经营概况
- * @param $enterpriseId
- * @param string $key
- * @param $change_num
- * @param null $shopId
- * @return ResultWrapper
- */
- public function saveBusinessOverview($enterpriseId, $key = 'orderTotalMoney', $change_num, $shopId = null)
- {
- if (empty($key)) {
- return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
- }
- //获取Key的过期时间,大于0的话就不再设置了
- $Key = $this->businessOverviewEnterpriseKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
- if (!empty($shopId)) {
- $Key = $this->businessOverviewEnterpriseShopKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId . '::ShopId_' . $shopId;
- }
- $ttl = $this->cache->ttl($Key);
- $old = self::getBusinessOverview($enterpriseId, $key, $shopId);
- $this->cache->hset($Key, $key, $old + $change_num);
- //如果键没有设置,那么设置过期时间
- if ($ttl == -2) {
- $this->cache->expire($Key, $this->expireTime);
- }
- }
- /**
- * 获取经营概况数字
- * @param $enterpriseId
- * @param string $key
- * @param null $shopId
- * @return ResultWrapper
- */
- public function getBusinessOverview($enterpriseId, $key = 'totalShouldReceive', $shopId = null)
- {
- if (empty($key)) {
- return ResultWrapper::fail('要获取的key不存在', ErrorCode::$paramError);
- }
- //获取Key的过期时间,大于0的话就不再设置了
- $Key = $this->businessOverviewEnterpriseKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
- if (!empty($shopId)) {
- $Key = $this->businessOverviewEnterpriseShopKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId . '::ShopId_' . $shopId;
- }
- $result = $this->cache->hget($Key, $key);
- return $result ? $result : 0;
- }
- /**
- * 删除经营概况统计
- * @param $enterpriseId
- * @param string $key
- * @param null $shopId
- * @return mixed
- */
- public function delBusinessOverview($enterpriseId, $key = 'totalShouldReceive', $shopId = null)
- {
- //获取Key的过期时间,大于0的话就不再设置了
- $Key = $this->businessOverviewEnterpriseKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
- if (!empty($shopId)) {
- $Key = $this->businessOverviewEnterpriseShopKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId . '::ShopId_' . $shopId;
- }
- return $this->cache->hdel($Key, $key);
- }
- /**************************经营概况 end**********************************/
- /**************************销量排行榜 start**********************************/
- /**
- * 销量排行榜
- * @param $enterpriseId
- * @param string $suffix
- * @param string $id
- * @param $change_num
- * @param null $shopId
- * @return ResultWrapper
- */
- public function saveRanking($enterpriseId, $suffix = 'categoryRanking', $id, $change_num, $shopId = null)
- {
- if (empty($suffix) || empty($id)) {
- return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
- }
- //获取Key的过期时间,大于0的话就不再设置了
- $Key = $this->rankingEnterpriseKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
- if (!empty($shopId)) {
- $Key = $this->rankingEnterpriseShopKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId.'::ShopId_'. $shopId;
- }
- $ttl = $this->cache->ttl($Key);
- $old = $this->cache->zscore($Key, $id);
- $old = $old ? $old : 0;
- $change_num = bcmul($change_num,100);
- $this->cache->zAdd($Key, bcadd($old,$change_num), $id);
- //如果键没有设置,那么设置过期时间
- if ($ttl == -2) {
- $this->cache->expire($Key, $this->expireTime);
- }
- }
- /**
- * 获取排行榜,按score从大到小排行
- * @param $enterpriseId
- * @param string $suffix
- * @param $id
- * @param $change_num
- * @param null $shopId
- * @return ResultWrapper
- */
- public function getRanking($enterpriseId, $suffix = 'categoryRanking', $shopId = null)
- {
- if (empty($suffix)) {
- return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
- }
- //获取Key的过期时间,大于0的话就不再设置了
- $Key = $this->rankingEnterpriseKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
- if (!empty($shopId)) {
- $Key = $this->rankingEnterpriseShopKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId.'::ShopId_'. $shopId;
- }
- $result = $this->cache->zRevRange($Key, 0, -1, true);
- if(!empty($result)) {
- foreach ($result as $key=>$value) {
- if ($value) {
- $result[$key] = bcdiv($value,100,2);
- }
- }
- }
- return $result ? $result : [];
- }
- /**
- * 删除排行榜 key
- * @param $enterpriseId
- * @param string $suffix
- * @param null $shopId
- * @return mixed
- */
- public function delRanking($enterpriseId, $suffix = 'categoryRanking', $shopId = null)
- {
- $Key = $this->rankingEnterpriseKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
- if (!empty($shopId)) {
- $Key = $this->rankingEnterpriseShopKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId.'::ShopId_'. $shopId;
- }
- return $this->cache->del($Key);
- }
- /**************************销量排行榜 end**********************************/
- /**************************销额排行榜 start**********************************/
- /**
- * 销额排行榜
- * @param $enterpriseId
- * @param string $suffix
- * @param string $id
- * @param $change_num
- * @param null $shopId
- * @return ResultWrapper
- */
- public function saveSalesMoneyRanking($enterpriseId, $suffix = 'categoryRanking', $id, $change_num, $shopId = null)
- {
- if (empty($suffix) || empty($id)) {
- return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
- }
- //获取Key的过期时间,大于0的话就不再设置了
- $Key = $this->salesMoneyRankingEnterpriseKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
- if (!empty($shopId)) {
- $Key = $this->salesMoneyRankingEnterpriseShopKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId.'::ShopId_'. $shopId;
- }
- $ttl = $this->cache->ttl($Key);
- $old = $this->cache->zscore($Key, $id);
- $old = $old ? $old : 0;
- $change_num = bcmul($change_num,100);
- $this->cache->zAdd($Key, bcadd($old, $change_num), $id);
- //如果键没有设置,那么设置过期时间
- if ($ttl == -2) {
- $this->cache->expire($Key, $this->expireTime);
- }
- }
- /**
- * 获取排行榜,按score从大到小排行
- * @param $enterpriseId
- * @param string $suffix
- * @param $id
- * @param $change_num
- * @param null $shopId
- * @return ResultWrapper
- */
- public function getSalesMoneyRanking($enterpriseId, $suffix = 'categoryRanking', $shopId = null)
- {
- if (empty($suffix)) {
- return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
- }
- //获取Key的过期时间,大于0的话就不再设置了
- $Key = $this->salesMoneyRankingEnterpriseKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
- if (!empty($shopId)) {
- $Key = $this->salesMoneyRankingEnterpriseShopKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId.'::ShopId_'. $shopId;
- }
- $result = $this->cache->zRevRange($Key, 0, -1, true);
- if(!empty($result)) {
- foreach ($result as $key=>$value) {
- if ($value) {
- $result[$key] = bcdiv($value,100,2);
- }
- }
- }
- return $result ? $result : [];
- }
- /**
- * 删除排行榜 key
- * @param $enterpriseId
- * @param string $suffix
- * @param null $shopId
- * @return mixed
- */
- public function delSalesMoneyRanking($enterpriseId, $suffix = 'categoryRanking', $shopId = null)
- {
- $Key = $this->salesMoneyRankingEnterpriseKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
- if (!empty($shopId)) {
- $Key = $this->salesMoneyRankingEnterpriseShopKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId.'::ShopId_'. $shopId;
- }
- return $this->cache->del($Key);
- }
- /**************************销额排行榜 end**********************************/
- /**************************近7天订单趋势 start**********************************/
- /**
- * 近7天订单趋势 订单审核后调用此方法
- * @param $enterpriseId
- * @param $orderMoney
- * @param $goodsNum 商品数量
- * @return void
- */
- public function saveOrderTrend($enterpriseId, $orderMoney, $goodsNum)
- {
- $Key = $this->orderTrendKey. '::EnterpriseId_' . $enterpriseId;
- //删除前30天的值
- $keys = $this->cache->hkeys($Key);
- if(!empty($keys)) {
- foreach ($keys as $k) {
- if(strtotime($k) <= strtotime('-30 days')) {
- $this->cache->hdel($Key, $k);
- }
- }
- }
- //获取今天的值
- $oldValue = $this->cache->hget($Key, date('Y-m-d'));
- if(!empty($oldValue)) {
- $oldValue = json_decode($oldValue, true);
- }
- $newValue = [
- 'orderMoney'=> $orderMoney + (isset($oldValue['orderMoney']) ? $oldValue['orderMoney'] : 0),
- 'goodsNum'=> $goodsNum + (isset($oldValue['goodsNum']) ? $oldValue['goodsNum'] : 0),
- 'orderNum'=> 1 + (isset($oldValue['orderNum']) ? $oldValue['orderNum'] : 0),
- ];
- $this->cache->hset($Key, date('Y-m-d'), json_encode($newValue));
- }
- /**
- * 获取近7天订单趋势
- * @param $enterpriseId
- * @return ResultWrapper
- */
- public function getOrderTrend($enterpriseId)
- {
- $Key = $this->orderTrendKey. '::EnterpriseId_' . $enterpriseId;
- $result = [];
- for($i=6; $i>=0; $i--) {
- $date = date('Y-m-d',strtotime("-$i days"));
- $value = $this->cache->hget($Key, $date);
- $value = empty($value) ? [] : json_decode($value, true);
- $result[] = [
- 'date' => $date,
- 'orderMoney' => isset($value['orderMoney']) ? $value['orderMoney'] : 0,
- 'goodsNum' => isset($value['goodsNum']) ? $value['goodsNum'] : 0,
- 'orderNum' => isset($value['orderNum']) ? $value['orderNum'] : 0,
- ];
- }
- return $result;
- }
- /**************************近7天订单趋势 end**********************************/
- }
|