AgentOrderLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 huo\logic\finance;
  13. use huo\controller\agent\Agent;
  14. use huo\model\agent\AgentOrderModel;
  15. use huo\model\common\CommonModel;
  16. use huolib\constant\AgentConst;
  17. use huolib\constant\CommonConst;
  18. use huolib\constant\PaywayConst;
  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['agent_order_model.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['agent_order_model.create_time'] = ['gt', strtotime($param['start_time'])];
  63. } else if (!empty($param['end_time'])) {
  64. $_map['agent_order_model.create_time'] = ['lt', CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])];
  65. }
  66. if (!empty($param['mem_id'])) {
  67. $_map['mem_id'] = $param['mem_id'];
  68. }
  69. if (!empty($param['status'])) {
  70. $_map['status'] = $param['status'];
  71. }
  72. if (!empty($param['from_id'])) {
  73. $_map['from_id'] = $param['from_id'];
  74. }
  75. if (!empty($param['agent_id'])) {
  76. $_map['agent_order_model.agent_id'] = $param['agent_id'];
  77. }
  78. if (!empty($param['parent_id'])) {
  79. $_map['agent_order_model.parent_id'] = $param['parent_id'];
  80. }
  81. if (!empty($param['order_id'])) {
  82. $_map['agent_order_model.order_id'] = $param['order_id'];
  83. }
  84. if (!empty($param['app_id'])) {
  85. $_map['agent_order_model.app_id'] = $param['app_id'];
  86. }
  87. if (!empty($param['username'])) {
  88. $_map['username'] = $param['username'];
  89. }
  90. if (!empty($param['agent_id'])) {
  91. $_map['agent_order_model.agent_id'] = $param['agent_id'];
  92. }
  93. if (!empty($agent_id)) {
  94. $_role_type = (new Agent())->getRoleType($agent_id);
  95. if (AgentConst::ROLE_TYPE_GROUP == $_role_type) {
  96. $_map['agent_order_model.parent_id'] = $agent_id;
  97. } elseif (AgentConst::ROLE_TYPE_AGENT == $_role_type) {
  98. $_map['agent_order_model.agent_id'] = $agent_id;
  99. }
  100. }
  101. /*平台币、游戏币支付的不显示*/
  102. if (!empty($param['pay_ignore'])) {
  103. $_map['agent_order_model.payway'] = $param['pay_ignore'];
  104. }
  105. return $_map;
  106. }
  107. public function getField($agent_id = '') {
  108. return [
  109. 'order_id' => 'order_id',
  110. 'payway' => 'payway',
  111. 'mem_id' => 'mem_id',
  112. 'username' => 'username',
  113. 'from_id' => 'from_id',
  114. 'agent_order_model.agent_id' => 'agent_id',
  115. 'agent_order_model.app_id' => 'app_id',
  116. 'amount' => 'amount',
  117. 'real_amount' => 'real_amount',
  118. 'rebate_cnt' => 'rebate_cnt',
  119. 'agent_order_model.create_time' => 'create_time',
  120. 'agent_gain' => 'agent_gain',
  121. 'parent_gain' => 'parent_gain',
  122. ];
  123. }
  124. /**
  125. * 获取玩家带来的收益
  126. *
  127. * @param int $agent_id
  128. * @param array $param
  129. * @param string $page
  130. * @param string $order
  131. *
  132. * @return array
  133. */
  134. public function getMemOrderList($agent_id, $param, $page = '1,10', $order = '-create_time') {
  135. $_map = $this->getWhere($param, $agent_id);
  136. $_map['agent_order_model.from_id'] = 0;
  137. $field = $this->getField($agent_id);
  138. return $this->getList($field, $_map, $page, $order);
  139. }
  140. /**
  141. * 获取下级带来的收益
  142. *
  143. * @param $agent_id
  144. * @param $param
  145. * @param string $page
  146. * @param string $order
  147. *
  148. * @return array
  149. */
  150. public function getSubAgentOrderList($agent_id, $param, $page = '1,10', $order = '-create_time') {
  151. $_map = $this->getWhere($param, $agent_id);
  152. $_map['agent_order_model.from_id'] = ['gt', 0];
  153. $field = $this->getField($agent_id);
  154. return $this->getList($field, $_map, $page, $order);
  155. }
  156. /**
  157. * 获取收入记录
  158. *
  159. * @param $param
  160. * @param string $page
  161. * @param string $order
  162. *
  163. * @return array
  164. */
  165. public function getIncomeList($param, $page = '1,10', $order = '-create_time') {
  166. $_map = $this->getWhere($param);
  167. $_field = [
  168. 'order_id' => 'order_id',
  169. 'mem_id' => 'mem_id',
  170. 'username' => 'username',
  171. 'from_id' => 'from_id',
  172. 'agent_order_model.agent_id' => 'agent_id',
  173. 'agent_order_model.app_id' => 'app_id',
  174. 'amount' => 'amount',
  175. 'real_amount' => 'real_amount',
  176. 'rebate_cnt' => 'rebate_cnt',
  177. 'payway' => 'payway',
  178. 'agent_order_model.create_time' => 'create_time',
  179. 'agent_order_model.agent_gain' => 'agent_gain',
  180. 'agent_order_model.parent_gain' => 'parent_gain',
  181. ];
  182. return $this->getList($_field, $_map, $page, $order);
  183. }
  184. /**
  185. * @param array $field
  186. * @param array $where
  187. * @param string $page
  188. * @param string $order
  189. *
  190. * @return array
  191. * @throws \think\Exception
  192. * @throws \think\db\exception\DataNotFoundException
  193. * @throws \think\db\exception\ModelNotFoundException
  194. * @throws \think\exception\DbException
  195. */
  196. public function getList($field, $where, $page = '1,10', $order = '-create_time') {
  197. $_sum_data['amount'] = 0;
  198. $_sum_data['real_amount'] = 0;
  199. $_sum_data['rebate_cnt'] = 0;
  200. $_sum_data['agent_gain'] = 0;
  201. $_sum_data['parent_gain'] = 0;
  202. $_map = $where;
  203. $_ao_model = new AgentOrderModel();
  204. $_count = $_ao_model->with('mem')->where($_map)->count();
  205. if (empty($_count)) {
  206. return [
  207. 'count' => 0,
  208. 'sum' => $_sum_data,
  209. 'list' => []
  210. ];
  211. }
  212. $_sum_field = [
  213. 'sum(amount)' => 'sum_amount',
  214. 'sum(real_amount)' => 'sum_real_amount',
  215. 'sum(rebate_cnt)' => 'sum_rebate_cnt',
  216. 'sum(agent_gain)' => 'sum_agent_gain',
  217. 'sum(parent_gain)' => 'sum_parent_gain',
  218. ];
  219. $_ao_sum_data = $_ao_model
  220. ->with('mem')
  221. ->field($_sum_field)
  222. ->where($where)
  223. ->find();
  224. if (is_object($_ao_sum_data)) {
  225. $_ao_sum_data = $_ao_sum_data->toArray();
  226. }
  227. $_sum_data['amount'] = StrUtils::formatNumber($_ao_sum_data['sum_amount']);
  228. $_sum_data['real_amount'] = StrUtils::formatNumber($_ao_sum_data['sum_real_amount']);
  229. $_sum_data['rebate_cnt'] = StrUtils::formatNumber($_ao_sum_data['sum_rebate_cnt']);
  230. $_sum_data['agent_gain'] = StrUtils::formatNumber($_ao_sum_data['sum_agent_gain']);
  231. $_sum_data['parent_gain'] = StrUtils::formatNumber($_ao_sum_data['sum_parent_gain']);
  232. $_sum_data['sum_amount'] = StrUtils::formatNumber($_ao_sum_data['sum_amount']);
  233. $_sum_data['sum_real_amount'] = StrUtils::formatNumber($_ao_sum_data['sum_real_amount']);
  234. $_sum_data['sum_rebate_cnt'] = StrUtils::formatNumber($_ao_sum_data['sum_rebate_cnt']);
  235. $_sum_data['sum_agent_gain'] = StrUtils::formatNumber($_ao_sum_data['sum_agent_gain']);
  236. $_sum_data['sum_parent_gain'] = StrUtils::formatNumber($_ao_sum_data['sum_parent_gain']);
  237. $_field = [];
  238. $_field = array_merge($_field, $field);
  239. $_order = $_ao_model->orderFilter($order);
  240. $_ao_datas = $_ao_model
  241. ->with('mem')
  242. ->with('game')
  243. ->with('agent')
  244. ->with('parentagent')
  245. ->with('fromagent')
  246. ->field($_field)
  247. ->where($where)
  248. ->order($_order)
  249. ->page($page)
  250. ->select();
  251. if (is_object($_ao_datas)) {
  252. $_ao_datas = $_ao_datas->toArray();
  253. }
  254. if (empty($_ao_datas)) {
  255. return [
  256. 'count' => $_count,
  257. 'sum' => $_sum_data,
  258. 'list' => []
  259. ];
  260. }
  261. $_data = [];
  262. foreach ($_ao_datas as $_ao_data) {
  263. foreach ($_field as $_v) {
  264. $_list[$_v] = $_ao_data[$_v];
  265. }
  266. $_list['payway'] = PaywayConst::getMsg($_ao_data['payway']);
  267. $_list['agent_name'] = $_ao_data['agent']['user_login'];
  268. $_list['from_name'] = !empty($_ao_data['fromagent']) ? $_ao_data['fromagent']['user_login'] : '';
  269. $_list['from_nicename'] = !empty($_ao_data['fromagent']) ? $_ao_data['fromagent']['user_nicename'] : '';
  270. $_list['parent_name'] = !empty($_ao_data['parentagent']) ? $_ao_data['parentagent']['user_login'] : '';
  271. $_list['gamename'] = !empty($_ao_data['game']) ? $_ao_data['game']['name'] : '';
  272. $_data[] = $_list;
  273. }
  274. return [
  275. 'count' => $_count,
  276. 'sum' => $_sum_data,
  277. 'list' => $_data
  278. ];
  279. }
  280. }