xhStatDealByDayService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\components\dict;
  5. use common\components\stringUtil;
  6. use linslin\yii2\curl;
  7. use common\components\wxUtil;
  8. use common\components\util;
  9. use common\models\xhStatDealByDay;
  10. class xhStatDealByDayService {
  11. /**
  12. * 收集当天的订单
  13. */
  14. public static function gatherDeal($sjId)
  15. {
  16. $todayTime = strtotime(date("Y-m-d"));
  17. $dealKey = $sjId.dict::getCacheKey('statDealByDay').$todayTime;
  18. $num = Yii::$app->redis->executeCommand('INCR', [$dealKey]);//记录当天订单的增加
  19. return $num;
  20. }
  21. /**
  22. * 将昨天的订单数保存入库
  23. */
  24. public static function saveYesterdayDeal($sjId)
  25. {
  26. $yesTime = strtotime(date("Y-m-d",strtotime("-1 day")));
  27. $arr = xhStatDealByDay::getByCondition(['sjId' => $sjId,'time' => $yesTime]);
  28. if(!empty($arr)){
  29. return [];
  30. }
  31. $dealKey = $sjId.dict::getCacheKey('statDealByDay').$yesTime;
  32. $num = Yii::$app->redis->executeCommand('GET', [$dealKey]);
  33. $num = !empty($num) ? $num : 0;
  34. $totalNum = self::getYesterdayTotalNum($sjId);
  35. $data = ['time' => $yesTime,'riseNum' =>$num,'totalNum' => $totalNum,'sjId' => $sjId,'createTime' => date("Y-m-d H:i:s")];
  36. $saveData = self::add($data);
  37. return $saveData;
  38. }
  39. /**
  40. * 取昨天及以前的订单总数和
  41. */
  42. public static function getYesterdayTotalNum($sjId)
  43. {
  44. $totalNum = self::getTodayNum($sjId);
  45. $todayNum = self::getTodayNum($sjId);
  46. $num = $totalNum - $todayNum;
  47. return $num;
  48. }
  49. /**
  50. * 取总的订单数
  51. */
  52. public static function getTotalNum($sjId)
  53. {
  54. $asset = xhMerchantAssetService::getBySjId($sjId);
  55. $totalDeal = $asset['totalDeal'];
  56. return $totalDeal;
  57. }
  58. /**
  59. * 取今天的订单数
  60. */
  61. public static function getTodayNum($sjId)
  62. {
  63. $time = strtotime(date("Y-m-d"));
  64. $numKey = $sjId.dict::getCacheKey('statDealByDay').$time;
  65. $nowNum = Yii::$app->redis->executeCommand('GET', [$numKey]);//记录当天访问网站人数
  66. $nowNum = !empty($nowNum) ? $nowNum : 0;
  67. return $nowNum;
  68. }
  69. public static function add($data)
  70. {
  71. xhStatDealByDay::add($data);
  72. }
  73. /**
  74. * 获取昨天订单增加数
  75. */
  76. public static function getYesNum($sjId)
  77. {
  78. $yesTime = strtotime(date("Y-m-d",strtotime("-1 day")));
  79. $numKey = $sjId.dict::getCacheKey('statDealByDay').$yesTime;
  80. $nowNum = Yii::$app->redis->executeCommand('GET', [$numKey]);//记录当天访问网站人数
  81. return !empty($nowNum) ? $nowNum : 0;
  82. }
  83. /**
  84. * 清空昨天的订单增加数据(包括缓存与数据库)
  85. */
  86. public static function clearYesNum($sjId)
  87. {
  88. $yesTime = strtotime(date("Y-m-d",strtotime("-1 day")));
  89. $dealKey = $sjId.dict::getCacheKey('statDealByDay').$yesTime;
  90. $yesterdayVisitNum = Yii::$app->redis->executeCommand('DEL', [$dealKey]);
  91. xhStatDealByDay::deleteByCondition(['sjId' => $sjId,'time' => $yesTime]);
  92. }
  93. /**
  94. * 模拟创建昨天的订单数
  95. */
  96. public static function imitationCreateYesNum($sjId)
  97. {
  98. echo "\n模拟创建昨天的订单数……\n";
  99. $yesTime = strtotime(date("Y-m-d",strtotime("-1 day")));
  100. $dealKey = $sjId.dict::getCacheKey('statDealByDay').$yesTime;
  101. $num = rand(11,80);
  102. Yii::$app->redis->executeCommand('SET', [$dealKey,$num]);
  103. echo "\n创建完成\n";
  104. }
  105. }