UserCollectUpdateService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2019/9/16
  6. * Time: 下午2:41
  7. */
  8. namespace app\main\service;
  9. use app\main\constants\OrderContents;
  10. use app\main\constants\UserConstants;
  11. use app\main\model\object\ReturnObject;
  12. use app\main\model\object\UserCollectObject;
  13. /**
  14. * Class UserCollectUpdateService
  15. * @package app\main\service
  16. */
  17. class UserCollectUpdateService extends BaseService
  18. {
  19. /**
  20. * @var UserCollectUpdateService
  21. */
  22. private static $self;
  23. /**
  24. * @var int 截止时间
  25. */
  26. public $end_time;
  27. public static function instance()
  28. {
  29. if (!self::$self) {
  30. self::$self = new self();
  31. }
  32. return self::$self;
  33. }
  34. /**
  35. * 设置时间
  36. * @param $time
  37. */
  38. public function setCurrentTime($time)
  39. {
  40. $this->end_time = $time;
  41. }
  42. /**
  43. * 获取昨日有数据的账号
  44. * @return ReturnObject
  45. */
  46. public function getAdminIdsToUpdate()
  47. {
  48. $admin_ids = UserCollectService::instance()->getUserCollectModel()
  49. ->where('createdate', date('Ymd', $this->end_time))
  50. ->where('type', OrderContents::ORDER_COLLECT_TYPE_DAY)
  51. ->group('admin_id')
  52. ->column('admin_id');
  53. return $this->setData($admin_ids)->getReturn();
  54. }
  55. /**
  56. * 获取月度汇总
  57. * @param $type
  58. * @param $admin_ids
  59. * @return ReturnObject
  60. */
  61. public function getUserCollectData($type, $admin_ids)
  62. {
  63. $fields = [
  64. 'admin_id',
  65. 'IFNULL(sum(increase),0)' => 'increase',
  66. 'IFNULL(sum(increase_m),0)' => 'increase_m',
  67. 'IFNULL(sum(increase_f),0)' => 'increase_f',
  68. 'IFNULL(sum(increase_fllow),0)' => 'increase_fllow',
  69. 'IFNULL(sum(unfollow_num),0)' => 'unfollow_num',
  70. 'IFNULL(sum(net_follow_num),0)' => 'net_follow_num',
  71. 'IFNULL(sum(guide_follow_num),0)' => 'guide_follow_num',
  72. 'IFNULL(sum(increase_recharge),0)' => 'increase_recharge',
  73. 'IFNULL(sum(day_recharge_user_money),0)' => 'day_recharge_user_money',
  74. 'IFNULL(sum(day_recharge_user_count),0)' => 'day_recharge_user_count',
  75. ];
  76. $query = UserService::instance()->getUserCollectModel();
  77. //查询月度统计数计
  78. if ($type == UserConstants::USER_COLLECT_TYPE_MONTH) {
  79. $from = date('Ym01', $this->end_time);
  80. $to = date('Ymd', $this->end_time);
  81. $query
  82. ->whereBetween('createdate', "$from,$to")
  83. ->whereIn('admin_id', $admin_ids)
  84. ->where('type', UserConstants::USER_COLLECT_TYPE_DAY);
  85. //查询汇总数据
  86. } else {
  87. $query->where('type', UserConstants::USER_COLLECT_TYPE_MONTH)
  88. ->whereIn('admin_id', $admin_ids);
  89. }
  90. $data = $query->field($fields)
  91. ->group('admin_id')
  92. ->select();
  93. $return = [];
  94. if ($data) {
  95. foreach ($data as $item) {
  96. $collect = (new UserCollectObject())->bind($item->getData());
  97. $collect->type = $type;
  98. $return[] = $collect;
  99. }
  100. }
  101. return $this->setData($return)->getReturn();
  102. }
  103. /**
  104. * 数据更新
  105. * @param $list
  106. */
  107. public function insertOrUpdateCollect($list)
  108. {
  109. foreach ($list as $item) {
  110. $check = UserCollectService::instance()->getUserCollectModel()->mkParamsForCheck($item);
  111. $data = UserCollectService::instance()->getUserCollectModel()->where($check)->field('id')->find();
  112. if (!$data) {
  113. try {
  114. $insert = UserCollectService::instance()->getUserCollectModel()->mkParamsForInsert($item);
  115. $result = UserCollectService::instance()->getUserCollectModel()->insert($insert);
  116. if ($result) {
  117. LogService::debug('用户统计新增完成' . json_encode($insert, JSON_UNESCAPED_UNICODE));
  118. } else {
  119. LogService::error('用户统计新增失败' . json_encode($insert, JSON_UNESCAPED_UNICODE));
  120. }
  121. continue;
  122. } catch (\Exception $e) {
  123. LogService::exception($e);
  124. }
  125. }
  126. $update = UserCollectService::instance()->getUserCollectModel()->mkParamsForUpdate($item);
  127. $result = UserCollectService::instance()->getUserCollectModel()->update($update, $check);
  128. if ($result) {
  129. LogService::debug('用户统计更新完成:' . json_encode($update, JSON_UNESCAPED_UNICODE) . json_encode($check, JSON_UNESCAPED_UNICODE));
  130. } else {
  131. LogService::error('用户统计更新失败:' . json_encode($update, JSON_UNESCAPED_UNICODE) . json_encode($check, JSON_UNESCAPED_UNICODE));
  132. }
  133. }
  134. }
  135. /**
  136. * 获取统计时期
  137. * @param $type
  138. * @return ReturnObject
  139. */
  140. public function getCreateDate($type)
  141. {
  142. $date = date('Ym01', $this->end_time);
  143. if ($type == UserConstants::USER_COLLECT_TYPE_TOTAL) {
  144. $date = '20180101';
  145. }
  146. return $this->setData($date)->getReturn();
  147. }
  148. }