AgentOrderLogic.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * AgentOrderLogic.php UTF-8
  4. * 收入订单逻辑处理
  5. *
  6. * @date : 2018/5/21 16:36
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huomp\logic\finance;
  13. use huo\controller\finance\AoRequest;
  14. use huo\model\agent\AgentOrderModel;
  15. use huo\model\common\CommonModel;
  16. use huo\model\user\UserModel;
  17. use huolib\constant\AgentConst;
  18. use huolib\constant\CommonConst;
  19. use huolib\tool\StrUtils;
  20. use huolib\tool\Time;
  21. class AgentOrderLogic extends CommonModel {
  22. /**
  23. * 统计总收入 今日收入 昨日收入
  24. *
  25. * @param int $agent_id 渠道ID
  26. * @param int $role_type 角色类型
  27. *
  28. * @return array
  29. * sum_share_total 统计总收入
  30. * today_share_total 今日收入
  31. * yesterday_share_total 昨日收入
  32. */
  33. public function getStaticShareMoney($agent_id, $role_type = AgentConst::ROLE_TYPE_AGENT) {
  34. $_sum = 'agent_gain';
  35. if (AgentConst::ROLE_TYPE_GROUP == $role_type) {
  36. $_sum = 'parent_gain';
  37. }
  38. $_map['agent_id'] = $agent_id;
  39. $_order_model = new AgentOrderModel();
  40. $_rdata['sum_share_total'] = $_order_model->where($_map)->sum($_sum);
  41. list($today_start, $today_end) = Time::today();
  42. $_map['create_time'] = ['gt', $today_start];
  43. $_rdata['today_share_total'] = $_order_model->where($_map)->sum($_sum);
  44. $_map['create_time'] = ['between', [$today_start - CommonConst::CONST_DAY_SECONDS, $today_start]];
  45. $_rdata['yesterday_share_total'] = $_order_model->where($_map)->sum($_sum);
  46. return $_rdata;
  47. }
  48. /**
  49. * @param array $param
  50. *
  51. * @param int $agent_id
  52. *
  53. * @return array
  54. */
  55. protected function getWhere($param = [], $agent_id = 0) {
  56. $_map = [];
  57. if (!empty($param['start_time']) && !empty($param['start_time'])) {
  58. $_map['create_time'] = ['between',
  59. [strtotime($param['start_time']),
  60. CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])]];
  61. } else if (!empty($param['start_time'])) {
  62. $_map['create_time'] = ['gt', strtotime($param['start_time'])];
  63. } else if (!empty($param['end_time'])) {
  64. $_map['create_time'] = ['lt',
  65. CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])];
  66. }
  67. if (!empty($param['status'])) {
  68. $_map['status'] = $param['status'];
  69. }
  70. if (!empty($param['agent_id'])) {
  71. $_map['agent_id'] = $param['agent_id'];
  72. }
  73. if (empty($_map['agent_id']) && !empty($param['agent_name'])) {
  74. $_ids = (new UserModel())->getIdsByNickName($param['agent_name']);
  75. $_map['agent_id'] = ['in', $_ids];
  76. }
  77. if (!empty($param['parent_id'])) {
  78. $_map['parent_id'] = $param['parent_id'];
  79. }
  80. if (empty($_map['parent_id']) && !empty($param['parent_agent_name'])) {
  81. $_ids = (new UserModel())->getIdsByNickName($param['parent_agent_name']);
  82. $_map['parent_id'] = ['in', $_ids];
  83. }
  84. if (!empty($param['order_id'])) {
  85. $_map['order_id'] = $param['order_id'];
  86. }
  87. if (!empty($param['app_id'])) {
  88. $_map['app_id'] = $param['app_id'];
  89. }
  90. if (!empty($param['flag'])) {
  91. $_map['flag'] = $param['flag'];
  92. }
  93. if (empty($_map['flag'])) {
  94. $_map['flag'] = ['in',
  95. [AoRequest::FLAG_REGISTER, AoRequest::FLAG_OPEN_GAME, AoRequest::FLAG_INVITE_REGISTER,
  96. AoRequest::FLAG_INVITE_OPEN_GAME]];
  97. }
  98. return $_map;
  99. }
  100. /**
  101. * 获取收入记录
  102. *
  103. * @param $param
  104. * @param string $page
  105. * @param string $order
  106. *
  107. * @return array
  108. */
  109. public function getIncomeList($param, $page = '1,10', $order = '-create_time') {
  110. $_map = $this->getWhere($param);
  111. $_field = [
  112. 'id' => 'id',
  113. 'order_id' => 'order_id',
  114. 'agent_id' => 'agent_id',
  115. 'app_id' => 'app_id',
  116. 'create_time' => 'create_time',
  117. 'parent_id' => 'parent_id',
  118. 'flag' => 'flag',
  119. 'status' => 'status',
  120. 'agent_gain' => 'agent_gain',
  121. 'parent_gain' => 'parent_gain',
  122. 'remark' => 'remark',
  123. ];
  124. return $this->getList($_field, $_map, $page, $order);
  125. }
  126. /**
  127. * @param array $field
  128. * @param array $where
  129. * @param string $page
  130. * @param string $order
  131. *
  132. * @return array
  133. * @throws \think\Exception
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\ModelNotFoundException
  136. * @throws \think\exception\DbException
  137. */
  138. public function getList($field, $where, $page = '1,10', $order = '-create_time') {
  139. $_sum_data['agent_gain'] = 0;
  140. $_sum_data['parent_gain'] = 0;
  141. $_map = $where;
  142. $_ao_model = new AgentOrderModel();
  143. $_count = $_ao_model->where($_map)->count();
  144. if (empty($_count)) {
  145. return [
  146. 'count' => 0,
  147. 'sum' => $_sum_data,
  148. 'list' => []
  149. ];
  150. }
  151. $_sum_field = [
  152. 'sum(agent_gain)' => 'sum_agent_gain',
  153. 'sum(parent_gain)' => 'sum_parent_gain',
  154. ];
  155. $_ao_sum_data = $_ao_model
  156. ->field($_sum_field)
  157. ->where($where)
  158. ->find();
  159. if (is_object($_ao_sum_data)) {
  160. $_ao_sum_data = $_ao_sum_data->toArray();
  161. }
  162. $_sum_data['agent_gain'] = StrUtils::formatNumber($_ao_sum_data['sum_agent_gain']);
  163. $_sum_data['parent_gain'] = StrUtils::formatNumber($_ao_sum_data['sum_parent_gain']);
  164. $_sum_data['sum_agent_gain'] = StrUtils::formatNumber($_ao_sum_data['sum_agent_gain']);
  165. $_sum_data['sum_parent_gain'] = StrUtils::formatNumber($_ao_sum_data['sum_parent_gain']);
  166. $_field = [];
  167. $_field = array_merge($_field, $field);
  168. $_order = $_ao_model->orderFilter($order);
  169. $_ao_datas = $_ao_model
  170. ->with('game,agent,parentagent')
  171. ->field($_field)
  172. ->where($where)
  173. ->order($_order)
  174. ->page($page)
  175. ->select();
  176. if (is_object($_ao_datas)) {
  177. $_ao_datas = $_ao_datas->toArray();
  178. }
  179. if (empty($_ao_datas)) {
  180. return [
  181. 'count' => $_count,
  182. 'sum' => $_sum_data,
  183. 'list' => []
  184. ];
  185. }
  186. $_data = [];
  187. foreach ($_ao_datas as $_ao_data) {
  188. foreach ($_field as $_v) {
  189. $_list[$_v] = $_ao_data[$_v];
  190. }
  191. $_list['agent_name'] = $_ao_data['agent']['user_nicename'];
  192. $_list['parent_name'] = !empty($_ao_data['parentagent']) ? $_ao_data['parentagent']['user_nicename'] : '';
  193. $_list['gamename'] = !empty($_ao_data['game']) ? $_ao_data['game']['name'] : '';
  194. $_data[] = $_list;
  195. }
  196. return [
  197. 'count' => $_count,
  198. 'sum' => $_sum_data,
  199. 'list' => $_data
  200. ];
  201. }
  202. }