DayAgentLogic.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * DayAgentLogic.php UTF-8
  4. *
  5. *
  6. * @date : 2018/8/17 11:15
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : luowei <lw@huosdk.com>
  10. * @version : HuoMP 1.0
  11. */
  12. namespace huomp\logic\data;
  13. use huo\controller\agent\AgentCache;
  14. use huo\controller\member\MemCache;
  15. use huo\model\common\CommonModel;
  16. use huo\model\data\DayAgentModel;
  17. use huolib\tool\StrUtils;
  18. class DayAgentLogic extends CommonModel {
  19. public function getWhere($param = []) {
  20. $_map = [];
  21. if (!empty($param['agent_id'])) {
  22. $_map['agent_id'] = $param['agent_id'];
  23. }
  24. if (!empty($param['start_time']) && !empty($param['end_time'])) {
  25. $_map['date'] = ['between', [$param['start_time'], $param['end_time']]];
  26. } elseif (!empty($param['start_time'])) {
  27. $_map['date'] = ['egt', $param['start_time']];
  28. } elseif (!empty($param['end_time'])) {
  29. $_map['date'] = ['elt', $param['end_time']];
  30. }
  31. if (!empty($param['role_id'])) {
  32. $_map['mpagent.role_id'] = $param['role_id'];
  33. }
  34. return $_map;
  35. }
  36. public function getRankList($agent_id, $where, $page = '1,10') {
  37. $_map = $this->getWhere($where);
  38. $_field = [
  39. 'sum(reg_cnt)' => 'sum_reg_cnt',
  40. 'sum(share_money)' => 'sum_share_money',
  41. ];
  42. $_data = $this->getList($_field, $_map, $page);
  43. $_ranking = $this->getRanking($agent_id, $_field, $_map);
  44. return array_merge($_data, ['ranking' => $_ranking]);
  45. }
  46. public function getList($field, $where, $page = '1,10') {
  47. $_model = new DayAgentModel();
  48. $_count = $_model->with('mpagent')->where($where)->group('day_agent_model.agent_id')->count();
  49. if (empty($_count)) {
  50. return [
  51. 'count' => 0,
  52. 'list' => [],
  53. ];
  54. }
  55. $_order = 'sum_share_money desc';
  56. $_data_list = $_model->with('mpagent')
  57. ->field($field)
  58. ->where($where)
  59. ->order($_order)
  60. ->page($page)
  61. ->group('day_agent_model.agent_id')
  62. ->select();
  63. if (is_object($_data_list)) {
  64. $_data_list = $_data_list->toArray();
  65. }
  66. if (empty($_data_list)) {
  67. return [
  68. 'count' => 0,
  69. 'list' => [],
  70. ];
  71. }
  72. $_data = [];
  73. $_mem_cache = new MemCache();
  74. $_agent_cache = new AgentCache();
  75. foreach ($_data_list as $_item) {
  76. $_agent_info = $_agent_cache->getInfoByAgentId($_item['agent_id']);
  77. $_mem_info = $_mem_cache->getInfoById($_agent_info['mem_id']);
  78. $_data[] = [
  79. 'avatar' => !empty($_mem_info) ? $_mem_info['avatar'] : '',
  80. 'nickname' => !empty($_mem_info) ? $_mem_info['nickname'] : '',
  81. 'mem_id' => $_agent_info['mem_id'],
  82. 'agent_id' => $_item['agent_id'],
  83. 'reg_cnt' => StrUtils::formatNumber($_item['sum_reg_cnt']),
  84. 'share_total' => StrUtils::formatNumber($_item['sum_share_money']),
  85. ];
  86. }
  87. return [
  88. 'count' => $_count,
  89. 'list' => $_data,
  90. ];
  91. }
  92. /**
  93. * 获取排名
  94. * @param $agent_id
  95. * @param $field
  96. * @param $where
  97. *
  98. * @return int|string
  99. */
  100. public function getRanking($agent_id, $field, $where) {
  101. $_model = new DayAgentModel();
  102. $_order = 'sum_share_money desc';
  103. $_day_agent_info = $_model->with('mpagent')
  104. ->field($field)
  105. ->where($where)
  106. ->where('day_agent_model.agent_id', '=', $agent_id)
  107. ->order($_order)
  108. ->group('day_agent_model.agent_id')
  109. ->find();
  110. if (is_object($_day_agent_info)) {
  111. $_day_agent_info = $_day_agent_info->toArray();
  112. }
  113. $_agent_share_total = !empty($_day_agent_info) ? $_day_agent_info['sum_share_money'] : 0;
  114. $_ranking = $_model->with('mpagent')
  115. ->field($field)
  116. ->where($where)
  117. ->group('day_agent_model.agent_id')
  118. ->having('sum_share_money>'.$_agent_share_total)
  119. ->order($_order)
  120. ->count();
  121. $_ranking += 1;
  122. return $_ranking;
  123. }
  124. }