AgentOrderModel.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * AgentOrderModel.php UTF-8
  4. * 渠道订单数据操作
  5. *
  6. * @date : 2017/10/12 20:07
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 7.2
  11. * Added by wuyonghong BEGIN 2017/10/12 ISSUES:3772 渠道收益计算重写
  12. */
  13. namespace huo\model\agent;
  14. use huo\model\common\CommonModel;
  15. use huo\model\game\GameModel;
  16. use huo\model\member\MemberModel;
  17. use huo\model\user\UserModel;
  18. use huolib\constant\OrderConst;
  19. class AgentOrderModel extends CommonModel {
  20. protected $name = 'agent_order';
  21. // 开启自动写入时间戳字段
  22. protected $autoWriteTimestamp = true;
  23. public function mem() {
  24. return $this->belongsTo(MemberModel::className(), 'mem_id', 'id', '', 'left')->field('id,username')
  25. ->setEagerlyType(0);
  26. }
  27. public function agent() {
  28. return $this->belongsTo(UserModel::className(), 'agent_id', 'id')->field('id,user_login,user_nicename,role_id');
  29. }
  30. public function fromagent() {
  31. return $this->belongsTo(UserModel::className(), 'from_id', 'id')->field('id,user_login,user_nicename');
  32. }
  33. public function parentagent() {
  34. return $this->belongsTo(UserModel::className(), 'parent_id', 'id')->field('id,user_login,user_nicename');
  35. }
  36. public function game() {
  37. return $this->belongsTo(GameModel::className(), 'app_id', 'id')->field('id,name');
  38. }
  39. /**
  40. * @param string $order_id
  41. *
  42. * @return array|bool|
  43. */
  44. public function getInfoByOrderId($order_id = '') {
  45. $_map['order_id'] = $order_id;
  46. $_info = $this->where($_map)->find();
  47. if (false === $_info) {
  48. return false;
  49. }
  50. if (is_object($_info)) {
  51. return $_info->toArray();
  52. } else {
  53. return $_info;
  54. }
  55. }
  56. /**
  57. * @param array $data
  58. *
  59. * @return bool|int
  60. */
  61. public function createOrder($data) {
  62. $_data['order_id'] = get_val($data, 'order_id', '');
  63. $_data['mem_id'] = get_val($data, 'mem_id', 0);
  64. $_data['from_id'] = get_val($data, 'from_id', 0);
  65. $_data['agent_id'] = get_val($data, 'agent_id', 0);
  66. $_data['app_id'] = get_val($data, 'app_id', 0);
  67. $_data['amount'] = get_val($data, 'amount', 0.00);
  68. $_data['real_amount'] = get_val($data, 'real_amount', 0.00);
  69. $_data['rebate_cnt'] = get_val($data, 'rebate_cnt', 0.00);
  70. $_data['agent_rate'] = get_val($data, 'agent_rate', 0.00);
  71. $_data['agent_gain'] = get_val($data, 'agent_gain', 0.00);
  72. $_data['flag'] = get_val($data, 'flag', 0.00);
  73. $_data['status'] = get_val($data, 'status', 1);
  74. $_data['parent_id'] = get_val($data, 'parent_id', 0);
  75. $_data['parent_rate'] = get_val($data, 'parent_rate', 0.00);
  76. $_data['parent_gain'] = get_val($data, 'parent_gain', 0.00);
  77. $_data['payway'] = get_val($data, 'payway', '');
  78. $_data['ptb_cnt'] = get_val($data, 'ptb_cnt', 0);
  79. $_data['rebate_cnt'] = get_val($data, 'rebate_cnt', 0);
  80. $_data['discount'] = get_val($data, 'discount', 1);
  81. $_data['payway'] = get_val($data, 'payway', '');
  82. $_data['remark'] = get_val($data, 'remark', '');
  83. if ($_obj = self::create($_data, true)) {
  84. return $_obj->id;
  85. } else {
  86. return false;
  87. }
  88. }
  89. /**
  90. * 昨日金额
  91. *
  92. *
  93. * @param array $map
  94. *
  95. * @return float
  96. */
  97. public function yesterdayMoney($map = []) {
  98. return $this->getSumMoney(strtotime('yesterday'), strtotime('today') - 1, $map);
  99. }
  100. /**
  101. * 今日金额
  102. *
  103. * @param array $map
  104. *
  105. * @return float
  106. */
  107. public function todayMoney($map = []) {
  108. return $this->getSumMoney(strtotime('today'), time(), $map);
  109. }
  110. /**
  111. * 本周金额
  112. *
  113. * @param array $map
  114. *
  115. * @return float|int
  116. */
  117. public function thisWeekMoney($map = []) {
  118. return $this->getSumMoney(strtotime(date('Y-m-d 00:00:00', strtotime('this week'))), time(), $map);
  119. }
  120. /**
  121. * 本月金额
  122. *
  123. * @param array $map
  124. *
  125. * @return float|int
  126. */
  127. public function thisMonthMoney($map = []) {
  128. return $this->getSumMoney(strtotime(date('Y-m-01 00:00:00')), time(), $map);
  129. }
  130. /**
  131. * 获取金额
  132. *
  133. * @param int $start_time 开始时间
  134. * @param int $end_time 结束时间
  135. *
  136. * @param array $map
  137. *
  138. * @return float|int
  139. */
  140. public function getSumMoney($start_time, $end_time, $map = []) {
  141. $where['status'] = OrderConst::DTB_STATUS_SUC;
  142. $where['create_time'] = ['between', [$start_time, $end_time]];
  143. return $this->where($map)->where($where)->sum('agent_gain');
  144. }
  145. public function getInfoByAgentIdAndFlag($agent_id, $flag) {
  146. $_info = $this->where('agent_id', '=', $agent_id)
  147. ->where('flag', '=', $flag)
  148. ->find();
  149. if (is_object($_info)) {
  150. $_info = $_info->toArray();
  151. }
  152. return $_info;
  153. }
  154. }