123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- <?php
- namespace JinDouYun\Cache;
- use http\Exception;
- use JinDouYun\Dao\Customer\DCustomer;
- use JinDouYun\Dao\Purchase\DSupplier;
- use Mall\Framework\Cache\Redis;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Factory;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\ErrorCode;
- class OverviewCache
- {
-
- private $cache;
- private $expireTime = 86400;
- private $orderDay = 7;
- protected $aggregateStatisticsKey = 'aggregateStatistics';
- protected $businessOverviewEnterpriseKey = 'businessOverviewEnterprise';
- protected $businessOverviewEnterpriseShopKey = 'businessOverviewEnterpriseShop';
- protected $rankingEnterpriseKey = 'rankingEnterprise';
- protected $rankingEnterpriseShopKey = 'rankingEnterpriseShop';
- protected $salesMoneyRankingEnterpriseKey = 'salesMoneyRankingEnterprise';
- protected $salesMoneyRankingEnterpriseShopKey = 'salesMoneyRankingEnterpriseShop';
- protected $orderTrendKey = 'orderTrend';
- public function __construct()
- {
- $this->cache = Factory::cache('finance');
- }
-
- public function saveAggregateStatistics($enterpriseId, $key = 'totalShouldReceive', $change_num)
- {
- if (empty($key)) {
- return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
- }
- $new = 0;
-
- $Key = $this->aggregateStatisticsKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
- $ttl = $this->cache->ttl($Key);
- $objDCustomer = new DCustomer();
- $objDSupplier = new DSupplier();
- $objDCustomer->setTable('qianniao_customer_'.$enterpriseId);
- $objDSupplier->setTable('qianniao_supplier_'.$enterpriseId);
- if($key == 'totalShouldReceive'){
- $tmpNew = $objDCustomer->query('select sum(money) as sum from '.$objDCustomer->get_Table().' where deleteStatus = '.StatusCode::$standard.' and enableStatus = '.StatusCode::$standard);
- }
- if($key == 'totalShouldPay'){
- $tmpNew = $objDSupplier->query('select sum(money) as sum from '.$objDSupplier->get_Table().' where deleteStatus = '.StatusCode::$standard.' and enableStatus = '.StatusCode::$standard);
- }
- if(empty($tmpNew)){
- $tmpNew[0]['sum'] = 0;
- }
- $this->cache->hset($Key , $key, bcadd($new,$tmpNew[0]['sum'],2));
-
- if ($ttl == -2) {
- $this->cache->expire($Key, $this->expireTime);
- }
- }
-
- public function setAggregateStatistics($enterpriseId, $key, $change_num)
- {
-
- $Key = $this->aggregateStatisticsKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
- $ttl = $this->cache->ttl($Key);
- $this->cache->hset($Key , $key, $change_num);
-
- if ($ttl == -2) {
- $this->cache->expire($Key, $this->expireTime);
- }
- }
-
- 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;
- }
-
- public function delAggregateStatistics($enterpriseId, $key = 'totalShouldReceive')
- {
- $Key = $this->aggregateStatisticsKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
- return $this->cache->hdel($Key, $key);
- }
-
-
- public function saveBusinessOverview($enterpriseId, $key = 'orderTotalMoney', $change_num, $shopId = null)
- {
- if (empty($key)) {
- return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
- }
-
- $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);
- }
- }
-
- public function setBusinessOverview(int $enterpriseId,string $key,string $change_num, $shopId = null)
- {
-
- $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);
- $this->cache->hset($Key, $key, $change_num);
-
- if ($ttl == -2) {
- $this->cache->expire($Key, $this->expireTime);
- }
- return true;
- }
-
- public function getBusinessOverview($enterpriseId, $key = 'totalShouldReceive', $shopId = null)
- {
- if (empty($key)) {
- return ResultWrapper::fail('要获取的key不存在', ErrorCode::$paramError);
- }
-
- $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;
- }
-
- public function delBusinessOverview($enterpriseId, $key = 'totalShouldReceive', $shopId = null)
- {
-
- $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);
- }
-
-
-
- public function saveRanking($enterpriseId, $suffix = 'categoryRanking', $id, $change_num, $shopId = null)
- {
- if (empty($suffix) || empty($id)) {
- return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
- }
-
- $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);
- }
- }
-
- public function getRanking($enterpriseId, $suffix = 'categoryRanking', $shopId = null)
- {
- if (empty($suffix)) {
- return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
- }
-
- $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 : [];
- }
-
- 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);
- }
-
-
-
- public function saveSalesMoneyRanking($enterpriseId, $suffix = 'categoryRanking', $id, $change_num, $shopId = null)
- {
- if (empty($suffix) || empty($id)) {
- return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
- }
-
- $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);
- }
- }
-
- public function getSalesMoneyRanking($enterpriseId, $suffix = 'categoryRanking', $shopId = null)
- {
- if (empty($suffix)) {
- return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
- }
-
- $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 : [];
- }
-
- 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);
- }
-
-
-
- public function saveOrderTrend($enterpriseId, $orderMoney, $goodsNum)
- {
- $Key = $this->orderTrendKey. '::EnterpriseId_' . $enterpriseId;
-
- $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));
- }
-
- 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;
- }
-
-
- public function setStatisticsCountInventoryOut($enterpriseId, $shopId, $count)
- {
- $key = $this->businessOverviewEnterpriseKey . '::OrderNumOfNotOutOfStock::EnterpriseId_' . $enterpriseId;
- return $this->cache->zadd($key, $count, $shopId);
- }
- public function getStatisticsCountInventoryOut($enterpriseId, $shopId)
- {
- $key = $this->businessOverviewEnterpriseKey . '::OrderNumOfNotOutOfStock::EnterpriseId_' . $enterpriseId;
- return $this->cache->zscore($key, $shopId);
- }
- public function delStatisticsCountInventoryOut($enterpriseId, $shopId)
- {
- $key = $this->businessOverviewEnterpriseKey . '::OrderNumOfNotOutOfStock::EnterpriseId_' . $enterpriseId;
- $this->cache->zrem($key, 0);
- return $this->cache->zrem($key, $shopId);
- }
- }
|