OverviewCache.Class.php 14 KB

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