AgentExtModel.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * AgentExtModel.php UTF-8
  4. * 渠道扩展表
  5. *
  6. * @date : 2018/5/9 14:17
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\user;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\AgentConst;
  15. class AgentExtModel extends CommonModel {
  16. protected $name = 'agent_ext';
  17. public function agent() {
  18. return $this->belongsTo('huo\model\user\UserModel', 'agent_id');
  19. }
  20. public function mem() {
  21. return $this->belongsTo('huo\model\member\MemberModel', 'mem_id')->field('id,username,nickname');
  22. }
  23. public function mpagent() {
  24. return $this->belongsTo('huo\model\user\UserModel', 'agent_id', 'id')->setEagerlyType(0);
  25. }
  26. /**
  27. * 获取渠道扩展信息
  28. *
  29. * @param $agent_id
  30. *
  31. * @return array|false
  32. */
  33. public function getAgentExtByAgentId($agent_id) {
  34. $_map['agent_id'] = $agent_id;
  35. $_info = $this->where($_map)->find();
  36. if (false === $_info) {
  37. return false;
  38. }
  39. if (is_object($_info)) {
  40. return $_info->toArray();
  41. } else {
  42. return $_info;
  43. }
  44. }
  45. /**
  46. * 更新数据
  47. *
  48. * @param array $data
  49. *
  50. * @param int $id
  51. *
  52. * @return bool|mixed
  53. */
  54. public function updateData($data = [], $id) {
  55. $_map['agent_id'] = $id;
  56. $_data = $data;
  57. $_rs = self::update($_data, $_map, true);
  58. if (false == $_rs) {
  59. return false;
  60. } else {
  61. return true;
  62. }
  63. }
  64. /**
  65. * 渠道流水排行
  66. *
  67. * @param int $size
  68. *
  69. * @return false|\PDOStatement|string|\think\Collection
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. * @throws \think\exception\DbException
  73. */
  74. public function getMoneyRank($size = 6) {
  75. return $this->alias('ag')
  76. ->field('ag.agent_id,u.user_nicename,ag.sum_money')
  77. ->join('user u', 'ag.agent_id=u.id', 'left')
  78. ->order('sum_money desc')
  79. ->limit($size)
  80. ->select();
  81. }
  82. /**
  83. * 获取渠道推广金额
  84. *
  85. * @param int $size
  86. *
  87. * @return false|\PDOStatement|string|\think\Collection
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. * @throws \think\exception\DbException
  91. */
  92. public function getAgShareTotalRank($size = 6) {
  93. $_map = [];
  94. $_map['role_id'] = AgentConst::AGENT_ROLE_MP_AGENT; //小游戏渠道
  95. return $this->with('mpagent')
  96. ->field('agent_id,user_nicename,share_total')
  97. ->where($_map)
  98. ->order('share_total desc')
  99. ->limit($size)
  100. ->select();
  101. }
  102. /**
  103. * 获取玩家渠道推广金额
  104. *
  105. * @param int $size
  106. *
  107. * @return false|\PDOStatement|string|\think\Collection
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. * @throws \think\exception\DbException
  111. */
  112. public function getMemShareTotalRank($size = 6) {
  113. $_map = [];
  114. $_map['role_id'] = AgentConst::AGENT_ROLE_MP_MEMBER; //小游戏玩家渠道
  115. return $this->with('mpagent,mem')
  116. ->field('mem_id,share_total')
  117. ->where($_map)
  118. ->order('share_total desc')
  119. ->limit($size)
  120. ->select();
  121. }
  122. /**
  123. * 渠道总流水
  124. *
  125. * @param $where
  126. *
  127. * @return float|int
  128. */
  129. public function sumMoney($where = []) {
  130. return $this->where($where)->sum('sum_money');
  131. }
  132. /**
  133. * 小游戏渠道总流水
  134. *
  135. * @param array $where
  136. *
  137. * @return float|int
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\ModelNotFoundException
  140. * @throws \think\exception\DbException
  141. */
  142. public function sumMpMoney($where = []) {
  143. $_field = [
  144. 'agent_id' => 'agent_id',
  145. 'sum(share_total)' => 'sum_money'
  146. ];
  147. $_data = self::with('mpagent')->where($where)->field($_field)->find();
  148. return $_data['sum_money'];
  149. }
  150. public function getCnt($where) {
  151. return $this->where($where)->count();
  152. }
  153. /**
  154. * 获取下级的所有总收益
  155. *
  156. * @param $agent_id
  157. *
  158. * @return float
  159. */
  160. public function getSubShareTotal($agent_id) {
  161. $_sub_ids = (new UserModel())->getIdsByParentId($agent_id);
  162. if (empty($_sub_ids)) {
  163. return 0.00;
  164. }
  165. $_map = [
  166. 'agent_id' => ['in', $_sub_ids]
  167. ];
  168. $_data = $this->where($_map)->sum('share_total');
  169. if (empty($_data)) {
  170. return 0.00;
  171. }
  172. return $_data;
  173. }
  174. }