OverviewCache.Class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. /**
  3. * 首页概况缓存
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/11/5
  7. * Time: 10:14
  8. */
  9. namespace JinDouYun\Cache;
  10. use http\Exception;
  11. use JinDouYun\Dao\Customer\DCustomer;
  12. use JinDouYun\Dao\Purchase\DSupplier;
  13. use Mall\Framework\Cache\Redis;
  14. use Mall\Framework\Core\StatusCode;
  15. use Mall\Framework\Factory;
  16. use Mall\Framework\Core\ResultWrapper;
  17. use Mall\Framework\Core\ErrorCode;
  18. class OverviewCache
  19. {
  20. /**
  21. * @var Redis
  22. */
  23. private $cache;
  24. private $expireTime = 86400;
  25. private $orderDay = 7;//近7天订单趋势
  26. protected $aggregateStatisticsKey = 'aggregateStatistics';//总数统计 区分企业
  27. protected $businessOverviewEnterpriseKey = 'businessOverviewEnterprise';//经营概况_日期_企业Id_(全店)
  28. protected $businessOverviewEnterpriseShopKey = 'businessOverviewEnterpriseShop';//经营概况_日期_企业Id_商铺id
  29. protected $rankingEnterpriseKey = 'rankingEnterprise';//销量排行榜_日期_企业Id_(全店)
  30. protected $rankingEnterpriseShopKey = 'rankingEnterpriseShop';//销量排行榜_日期_企业Id_商铺id
  31. protected $salesMoneyRankingEnterpriseKey = 'salesMoneyRankingEnterprise';//销额排行榜_日期_企业Id_(全店)
  32. protected $salesMoneyRankingEnterpriseShopKey = 'salesMoneyRankingEnterpriseShop';//销额排行榜_日期_企业Id_商铺id
  33. protected $orderTrendKey = 'orderTrend';//近7天订单趋势_企业Id
  34. public function __construct()
  35. {
  36. $this->cache = Factory::cache('finance');
  37. }
  38. /**
  39. * 总数统计
  40. * @param $enterpriseId
  41. * @param string $key
  42. * @param $change_num
  43. * @return ResultWrapper
  44. */
  45. public function saveAggregateStatistics($enterpriseId, $key = 'totalShouldReceive', $change_num)
  46. {
  47. if (empty($key)) {
  48. return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
  49. }
  50. $new = 0;
  51. //获取Key的过期时间,大于0的话就不再设置了
  52. $Key = $this->aggregateStatisticsKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
  53. $ttl = $this->cache->ttl($Key);
  54. // $old = self::getAggregateStatistics($enterpriseId,$key);
  55. $objDCustomer = new DCustomer();
  56. $objDSupplier = new DSupplier();
  57. $objDCustomer->setTable('qianniao_customer_'.$enterpriseId);
  58. $objDSupplier->setTable('qianniao_supplier_'.$enterpriseId);
  59. if($key == 'totalShouldReceive'){
  60. $tmpNew = $objDCustomer->query('select sum(money) as sum from '.$objDCustomer->get_Table().' where deleteStatus = '.StatusCode::$standard.' and enableStatus = '.StatusCode::$standard);
  61. }
  62. if($key == 'totalShouldPay'){
  63. $tmpNew = $objDSupplier->query('select sum(money) as sum from '.$objDSupplier->get_Table().' where deleteStatus = '.StatusCode::$standard.' and enableStatus = '.StatusCode::$standard);
  64. }
  65. if(empty($tmpNew)){
  66. $tmpNew[0]['sum'] = 0;
  67. }
  68. $this->cache->hset($Key , $key, bcadd($new,$tmpNew[0]['sum'],2));
  69. //如果键没有设置,那么设置过期时间
  70. if ($ttl == -2) {
  71. $this->cache->expire($Key, $this->expireTime);
  72. }
  73. }
  74. /**
  75. * @param $enterpriseId
  76. * @param $key
  77. * @param $change_num
  78. */
  79. public function setAggregateStatistics($enterpriseId, $key, $change_num)
  80. {
  81. //获取Key的过期时间,大于0的话就不再设置了
  82. $Key = $this->aggregateStatisticsKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
  83. $ttl = $this->cache->ttl($Key);
  84. $this->cache->hset($Key , $key, $change_num);
  85. //如果键没有设置,那么设置过期时间
  86. if ($ttl == -2) {
  87. $this->cache->expire($Key, $this->expireTime);
  88. }
  89. }
  90. /**
  91. * 获取总数统计
  92. * @param $enterpriseId
  93. * @param $key
  94. * @return array
  95. */
  96. public function getAggregateStatistics($enterpriseId, $key = 'totalShouldReceive')
  97. {
  98. $Key = $this->aggregateStatisticsKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
  99. $result = $this->cache->hget($Key, $key);
  100. // if(empty($result)){
  101. // $objDCustomer = new DCustomer();
  102. // $objDSupplier = new DSupplier();
  103. // $objDCustomer->setTable('qianniao_customer_'.$enterpriseId);
  104. // $objDSupplier->setTable('qianniao_supplier_'.$enterpriseId);
  105. // if($key = 'totalShouldReceive'){
  106. // $new = $objDCustomer->query('select sum(money) as sum from '.$objDCustomer->get_Table().' where deleteStatus = '.StatusCode::$standard.' and enableStatus = '.StatusCode::$standard);
  107. // }
  108. // if($key = 'totalShouldPay'){
  109. // $new = $objDCustomer->query('select sum(money) as sum from '.$objDSupplier->get_Table().' where deleteStatus = '.StatusCode::$standard.' and enableStatus = '.StatusCode::$standard);
  110. // }
  111. // self::setAggregateStatistics($enterpriseId,$key, $new[0]['sum']);
  112. // }
  113. return $result ? $result : 0;
  114. }
  115. /**
  116. * 删除总数统计
  117. * @param $enterpriseId
  118. * @param $key
  119. * @return array
  120. */
  121. public function delAggregateStatistics($enterpriseId, $key = 'totalShouldReceive')
  122. {
  123. $Key = $this->aggregateStatisticsKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
  124. return $this->cache->hdel($Key, $key);
  125. }
  126. /**************************经营概况 start**********************************/
  127. /**
  128. * 经营概况
  129. * @param $enterpriseId
  130. * @param string $key
  131. * @param $change_num
  132. * @param null $shopId
  133. * @return ResultWrapper
  134. */
  135. public function saveBusinessOverview($enterpriseId, $key = 'orderTotalMoney', $change_num, $shopId = null)
  136. {
  137. if (empty($key)) {
  138. return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
  139. }
  140. //获取Key的过期时间,大于0的话就不再设置了
  141. $Key = $this->businessOverviewEnterpriseKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
  142. if (!empty($shopId)) {
  143. $Key = $this->businessOverviewEnterpriseShopKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId . '::ShopId_' . $shopId;
  144. }
  145. $ttl = $this->cache->ttl($Key);
  146. $old = self::getBusinessOverview($enterpriseId, $key, $shopId);
  147. $this->cache->hset($Key, $key, $old + $change_num);
  148. //如果键没有设置,那么设置过期时间
  149. if ($ttl == -2) {
  150. $this->cache->expire($Key, $this->expireTime);
  151. }
  152. }
  153. /**
  154. * @param int $enterpriseId
  155. * @param string $key
  156. * @param string $change_num
  157. * @param null $shopId
  158. * @return bool
  159. */
  160. public function setBusinessOverview(int $enterpriseId,string $key,string $change_num, $shopId = null)
  161. {
  162. //获取Key的过期时间,大于0的话就不再设置了
  163. $Key = $this->businessOverviewEnterpriseKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
  164. if (!empty($shopId)) {
  165. $Key = $this->businessOverviewEnterpriseShopKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId . '::ShopId_' . $shopId;
  166. }
  167. $ttl = $this->cache->ttl($Key);
  168. $this->cache->hset($Key, $key, $change_num);
  169. //如果键没有设置,那么设置过期时间
  170. if ($ttl == -2) {
  171. $this->cache->expire($Key, $this->expireTime);
  172. }
  173. return true;
  174. }
  175. /**
  176. * 获取经营概况数字
  177. * @param $enterpriseId
  178. * @param string $key
  179. * @param null $shopId
  180. * @return ResultWrapper
  181. */
  182. public function getBusinessOverview($enterpriseId, $key = 'totalShouldReceive', $shopId = null)
  183. {
  184. if (empty($key)) {
  185. return ResultWrapper::fail('要获取的key不存在', ErrorCode::$paramError);
  186. }
  187. //获取Key的过期时间,大于0的话就不再设置了
  188. $Key = $this->businessOverviewEnterpriseKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
  189. if (!empty($shopId)) {
  190. $Key = $this->businessOverviewEnterpriseShopKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId . '::ShopId_' . $shopId;
  191. }
  192. $result = $this->cache->hget($Key, $key);
  193. return $result ? $result : 0;
  194. }
  195. /**
  196. * 删除经营概况统计
  197. * @param $enterpriseId
  198. * @param string $key
  199. * @param null $shopId
  200. * @return mixed
  201. */
  202. public function delBusinessOverview($enterpriseId, $key = 'totalShouldReceive', $shopId = null)
  203. {
  204. //获取Key的过期时间,大于0的话就不再设置了
  205. $Key = $this->businessOverviewEnterpriseKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
  206. if (!empty($shopId)) {
  207. $Key = $this->businessOverviewEnterpriseShopKey . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId . '::ShopId_' . $shopId;
  208. }
  209. return $this->cache->hdel($Key, $key);
  210. }
  211. /**************************经营概况 end**********************************/
  212. /**************************销量排行榜 start**********************************/
  213. /**
  214. * 销量排行榜
  215. * @param $enterpriseId
  216. * @param string $suffix
  217. * @param string $id
  218. * @param $change_num
  219. * @param null $shopId
  220. * @return ResultWrapper
  221. */
  222. public function saveRanking($enterpriseId, $suffix = 'categoryRanking', $id, $change_num, $shopId = null)
  223. {
  224. if (empty($suffix) || empty($id)) {
  225. return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
  226. }
  227. //获取Key的过期时间,大于0的话就不再设置了
  228. $Key = $this->rankingEnterpriseKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
  229. if (!empty($shopId)) {
  230. $Key = $this->rankingEnterpriseShopKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId.'::ShopId_'. $shopId;
  231. }
  232. $ttl = $this->cache->ttl($Key);
  233. $old = $this->cache->zscore($Key, $id);
  234. $old = $old ? $old : 0;
  235. $change_num = bcmul($change_num,100);
  236. $this->cache->zAdd($Key, bcadd($old, $change_num), $id);
  237. //如果键没有设置,那么设置过期时间
  238. if ($ttl == -2) {
  239. $this->cache->expire($Key, $this->expireTime);
  240. }
  241. }
  242. /**
  243. * 获取排行榜,按score从大到小排行
  244. * @param $enterpriseId
  245. * @param string $suffix
  246. * @param $id
  247. * @param $change_num
  248. * @param null $shopId
  249. * @return ResultWrapper
  250. */
  251. public function getRanking($enterpriseId, $suffix = 'categoryRanking', $shopId = null)
  252. {
  253. if (empty($suffix)) {
  254. return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
  255. }
  256. //获取Key的过期时间,大于0的话就不再设置了
  257. $Key = $this->rankingEnterpriseKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
  258. if (!empty($shopId)) {
  259. $Key = $this->rankingEnterpriseShopKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId.'::ShopId_'. $shopId;
  260. }
  261. $result = $this->cache->zRevRange($Key, 0, -1, true);
  262. if(!empty($result)) {
  263. foreach ($result as $key=>$value) {
  264. if ($value) {
  265. $result[$key] = bcdiv($value,100,2);
  266. }
  267. }
  268. }
  269. return $result ? $result : [];
  270. }
  271. /**
  272. * 删除排行榜 key
  273. * @param $enterpriseId
  274. * @param string $suffix
  275. * @param null $shopId
  276. * @return mixed
  277. */
  278. public function delRanking($enterpriseId, $suffix = 'categoryRanking', $shopId = null)
  279. {
  280. $Key = $this->rankingEnterpriseKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
  281. if (!empty($shopId)) {
  282. $Key = $this->rankingEnterpriseShopKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId.'::ShopId_'. $shopId;
  283. }
  284. return $this->cache->del($Key);
  285. }
  286. /**************************销量排行榜 end**********************************/
  287. /**************************销额排行榜 start**********************************/
  288. /**
  289. * 销额排行榜
  290. * @param $enterpriseId
  291. * @param string $suffix
  292. * @param string $id
  293. * @param $change_num
  294. * @param null $shopId
  295. * @return ResultWrapper
  296. */
  297. public function saveSalesMoneyRanking($enterpriseId, $suffix = 'categoryRanking', $id, $change_num, $shopId = null)
  298. {
  299. if (empty($suffix) || empty($id)) {
  300. return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
  301. }
  302. //获取Key的过期时间,大于0的话就不再设置了
  303. $Key = $this->salesMoneyRankingEnterpriseKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
  304. if (!empty($shopId)) {
  305. $Key = $this->salesMoneyRankingEnterpriseShopKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId.'::ShopId_'. $shopId;
  306. }
  307. $ttl = $this->cache->ttl($Key);
  308. $old = $this->cache->zscore($Key, $id);
  309. $old = $old ? $old : 0;
  310. $change_num = bcmul($change_num,100);
  311. $this->cache->zAdd($Key, bcadd($old, $change_num), $id);
  312. //如果键没有设置,那么设置过期时间
  313. if ($ttl == -2) {
  314. $this->cache->expire($Key, $this->expireTime);
  315. }
  316. }
  317. /**
  318. * 获取排行榜,按score从大到小排行
  319. * @param $enterpriseId
  320. * @param string $suffix
  321. * @param $id
  322. * @param $change_num
  323. * @param null $shopId
  324. * @return ResultWrapper
  325. */
  326. public function getSalesMoneyRanking($enterpriseId, $suffix = 'categoryRanking', $shopId = null)
  327. {
  328. if (empty($suffix)) {
  329. return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
  330. }
  331. //获取Key的过期时间,大于0的话就不再设置了
  332. $Key = $this->salesMoneyRankingEnterpriseKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
  333. if (!empty($shopId)) {
  334. $Key = $this->salesMoneyRankingEnterpriseShopKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId.'::ShopId_'. $shopId;
  335. }
  336. $result = $this->cache->zRevRange($Key, 0, -1, true);
  337. if(!empty($result)) {
  338. foreach ($result as $key=>$value) {
  339. if ($value) {
  340. $result[$key] = bcdiv($value,100,2);
  341. }
  342. }
  343. }
  344. return $result ? $result : [];
  345. }
  346. /**
  347. * 删除排行榜 key
  348. * @param $enterpriseId
  349. * @param string $suffix
  350. * @param null $shopId
  351. * @return mixed
  352. */
  353. public function delSalesMoneyRanking($enterpriseId, $suffix = 'categoryRanking', $shopId = null)
  354. {
  355. $Key = $this->salesMoneyRankingEnterpriseKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId;
  356. if (!empty($shopId)) {
  357. $Key = $this->salesMoneyRankingEnterpriseShopKey . '::' . $suffix . '::' . date('Y-m-d') . '::EnterpriseId_' . $enterpriseId.'::ShopId_'. $shopId;
  358. }
  359. return $this->cache->del($Key);
  360. }
  361. /**************************销额排行榜 end**********************************/
  362. /**************************近7天订单趋势 start**********************************/
  363. /**
  364. * 近7天订单趋势 订单审核后调用此方法
  365. * @param $enterpriseId
  366. * @param $orderMoney
  367. * @param $goodsNum 商品数量
  368. * @return void
  369. */
  370. public function saveOrderTrend($enterpriseId, $orderMoney, $goodsNum)
  371. {
  372. $Key = $this->orderTrendKey. '::EnterpriseId_' . $enterpriseId;
  373. //删除前30天的值
  374. $keys = $this->cache->hkeys($Key);
  375. if(!empty($keys)) {
  376. foreach ($keys as $k) {
  377. if(strtotime($k) <= strtotime('-30 days')) {
  378. $this->cache->hdel($Key, $k);
  379. }
  380. }
  381. }
  382. //获取今天的值
  383. $oldValue = $this->cache->hget($Key, date('Y-m-d'));
  384. if(!empty($oldValue)) {
  385. $oldValue = json_decode($oldValue, true);
  386. }
  387. $newValue = [
  388. 'orderMoney'=> $orderMoney + (isset($oldValue['orderMoney']) ? $oldValue['orderMoney'] : 0),
  389. 'goodsNum'=> $goodsNum + (isset($oldValue['goodsNum']) ? $oldValue['goodsNum'] : 0),
  390. 'orderNum'=> 1 + (isset($oldValue['orderNum']) ? $oldValue['orderNum'] : 0),
  391. ];
  392. $this->cache->hset($Key, date('Y-m-d'), json_encode($newValue));
  393. }
  394. /**
  395. * 获取近7天订单趋势
  396. * @param $enterpriseId
  397. * @return ResultWrapper
  398. */
  399. public function getOrderTrend($enterpriseId)
  400. {
  401. $Key = $this->orderTrendKey. '::EnterpriseId_' . $enterpriseId;
  402. $result = [];
  403. for($i=6; $i>=0; $i--) {
  404. $date = date('Y-m-d',strtotime("-$i days"));
  405. $value = $this->cache->hget($Key, $date);
  406. $value = empty($value) ? [] : json_decode($value, true);
  407. $result[] = [
  408. 'date' => $date,
  409. 'orderMoney' => isset($value['orderMoney']) ? $value['orderMoney'] : 0,
  410. 'goodsNum' => isset($value['goodsNum']) ? $value['goodsNum'] : 0,
  411. 'orderNum' => isset($value['orderNum']) ? $value['orderNum'] : 0,
  412. ];
  413. }
  414. return $result;
  415. }
  416. /**************************近7天订单趋势 end**********************************/
  417. /**
  418. * 未出库订单总数
  419. * shopId为键 count为值
  420. * @param $enterpriseId
  421. * @param $shopId
  422. * @param $count
  423. * @return bool
  424. */
  425. public function setStatisticsCountInventoryOut($enterpriseId, $shopId, $count)
  426. {
  427. $key = $this->businessOverviewEnterpriseKey . '::OrderNumOfNotOutOfStock::EnterpriseId_' . $enterpriseId;
  428. return $this->cache->zadd($key, $count, $shopId);
  429. }
  430. public function getStatisticsCountInventoryOut($enterpriseId, $shopId)
  431. {
  432. $key = $this->businessOverviewEnterpriseKey . '::OrderNumOfNotOutOfStock::EnterpriseId_' . $enterpriseId;
  433. return $this->cache->zscore($key, $shopId);
  434. }
  435. public function delStatisticsCountInventoryOut($enterpriseId, $shopId)
  436. {
  437. $key = $this->businessOverviewEnterpriseKey . '::OrderNumOfNotOutOfStock::EnterpriseId_' . $enterpriseId;
  438. $this->cache->zrem($key, 0);
  439. return $this->cache->zrem($key, $shopId);
  440. }
  441. }