PtbAgentChargeLogic.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * PtbAgentChargeLogic.php UTF-8
  4. * 渠道平台币收入逻辑
  5. *
  6. * @date : 2018/5/18 17:22
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\logic\finance;
  13. use huo\logic\agent\AgentLogic;
  14. use huo\model\common\CommonModel;
  15. use huo\model\finance\PtbAgentChargeModel;
  16. use huolib\constant\CommonConst;
  17. use huolib\tool\StrUtils;
  18. class PtbAgentChargeLogic extends CommonModel {
  19. /**
  20. * @param array $param
  21. *
  22. * @return array
  23. */
  24. protected function getWhere($param = []) {
  25. $_map = [];
  26. if (!empty($param['start_time']) && !empty($param['start_time'])) {
  27. $_map['ptb_agent_charge_model.create_time'] = ['between', [strtotime($param['start_time']),
  28. CommonConst::CONST_DAY_SECONDS + strtotime(
  29. $param['end_time']
  30. )]];
  31. } else if (!empty($param['start_time'])) {
  32. $_map['ptb_agent_charge_model.create_time'] = ['gt', strtotime($param['start_time'])];
  33. } else if (!empty($param['end_time'])) {
  34. $_map['ptb_agent_charge_model.create_time'] = ['lt', CommonConst::CONST_DAY_SECONDS + strtotime(
  35. $param['end_time']
  36. )];
  37. }
  38. if (!empty($param['order_id'])) {
  39. $_map['order_id'] = $param['order_id'];
  40. }
  41. if (!empty($param['status'])) {
  42. $_map['status'] = $param['status'];
  43. }
  44. if (!empty($param['type'])) {
  45. $_map['type'] = $param['type'];
  46. }
  47. if (!empty($param['parent_id'])) {
  48. $_agent_ids = (new AgentLogic())->getAgentIds($param['parent_id']);
  49. $_map['ptb_agent_charge_model.agent_id'] = ['in', $_agent_ids];
  50. }
  51. if (!empty($param['agent_id'])) {
  52. $_map['ptb_agent_charge_model.agent_id'] = $param['agent_id'];
  53. }
  54. return $_map;
  55. }
  56. public function getField($agent_id) {
  57. return [];
  58. }
  59. /**
  60. * 获取获取记录
  61. *
  62. * @param $agent_id
  63. * @param $param
  64. * @param string $page
  65. * @param string $order
  66. *
  67. * @return array
  68. */
  69. public function getAgentList($agent_id, $param, $page = '1,10', $order = '-create_time') {
  70. $_map = $this->getWhere($param);
  71. $field = $this->getField($agent_id);
  72. if (!empty($agent_id)) {
  73. $_map['agent_id'] = $agent_id;
  74. }
  75. return $this->getList($field, $_map, $page, $order);
  76. }
  77. /**
  78. * 获取列表
  79. *
  80. * @param array $field
  81. * @param array $where
  82. * @param string $page
  83. * @param string $order
  84. *
  85. * @return array
  86. */
  87. public function getList($field = [], $where, $page = '1,10', $order = '-create_time') {
  88. $_map = $where;
  89. $_model = new PtbAgentChargeModel();
  90. $_count = $_model->alias('ptb_agent_charge_model')->where($_map)->count();
  91. if (empty($_count)) {
  92. return [
  93. 'count' => 0,
  94. 'sum' => [],
  95. 'list' => []
  96. ];
  97. }
  98. $_field = $field;
  99. if (empty($field)) {
  100. $_field = [];
  101. }
  102. $_order = $_model->orderFilter($order);
  103. $_datas = $_model->alias('ptb_agent_charge_model')
  104. ->with('agent,back')
  105. ->where($where)
  106. ->field($_field)
  107. ->order($_order)
  108. ->page($page)
  109. ->select();
  110. if (is_object($_datas)) {
  111. $_datas = $_datas->toArray();
  112. }
  113. if (empty($_datas)) {
  114. return [
  115. 'count' => $_count,
  116. 'sum' => [],
  117. 'list' => []
  118. ];
  119. }
  120. $_data = [];
  121. foreach ($_datas as $_k => $_v) {
  122. foreach ($_field as $_f) {
  123. $_data[$_k][$_f] = $_v[$_f];
  124. }
  125. }
  126. $_sum_field = [
  127. 'sum(amount)' => 'sum_amount',
  128. 'sum(real_amount)' => 'sum_real_amount',
  129. 'sum(rebate_cnt)' => 'sum_rebate_cnt',
  130. 'sum(ptb_cnt)' => 'sum_ptb_cnt'
  131. ];
  132. $_sum_data = $_model->alias('ptb_agent_charge_model')
  133. ->field($_sum_field)
  134. ->where($where)
  135. ->find();
  136. if (is_object($_sum_data)) {
  137. $_sum_data = $_sum_data->toArray();
  138. }
  139. $_sum = [];
  140. foreach ($_sum_data as $_k => $_v) {
  141. $_sum[$_k] = StrUtils::formatNumber($_v);
  142. }
  143. return [
  144. 'count' => $_count,
  145. 'sum' => $_sum,
  146. 'list' => $_datas
  147. ];
  148. }
  149. /**
  150. * 获取收入列表
  151. *
  152. * @param array $where
  153. * @param string $page
  154. * @param string $order
  155. *
  156. * @return array
  157. */
  158. public function getIncomeList($where = [], $page = '1,10', $order = '-create_time') {
  159. $_map = $this->getWhere($where);
  160. $field = [];
  161. return $this->getList($field, $_map, $page, $order);
  162. }
  163. /**
  164. * @param $order_id
  165. *
  166. * @return array|false|\PDOStatement|string|PtbAgentChargeModel
  167. */
  168. public function getDetailByOrderId($order_id) {
  169. $_ptb_agent_charge = (new PtbAgentChargeModel())->where('order_id', '=', $order_id)->find();
  170. return $_ptb_agent_charge;
  171. }
  172. }