CommissionService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace addons\Community\common\service;
  3. use addons\Community\common\enums\ConfigEnum;
  4. use addons\Community\common\models\CommunityHead;
  5. use addons\Community\common\models\CommunityHeadCommission;
  6. use common\enums\StatusEnum;
  7. use common\models\user\User;
  8. class CommissionService
  9. {
  10. /**
  11. * 列表数据
  12. * @param int $mall_id
  13. * @param array $params
  14. * @param bool $is_api
  15. * @return array|mixed
  16. */
  17. public function page(int $mall_id, array $params, bool $is_api = false)
  18. {
  19. // 组织条件
  20. $user_ids = [];
  21. if (!empty($params["user_id"])) {
  22. $user_ids[] = $params["user_id"];
  23. }
  24. if (!empty($params["member"])) {
  25. $user_or[] = 'or';
  26. $user_or[] = ['like', 'mobile', $params['member']];
  27. $user_or[] = ['like', 'nickname', $params['member']];
  28. $u_ids = User::find()->where(['mall_id' => $mall_id])->andWhere($user_or)->column();
  29. if (!empty($u_ids)) {
  30. $user_ids = array_merge($user_ids, $u_ids);
  31. }
  32. }
  33. if (!empty($params['name'])) {
  34. $head_ids = CommunityHead::find()->where(['mall_id' => $mall_id])
  35. ->andWhere(['like', 'name', $params['name']])->select('id')->column();
  36. if (!empty($head_ids)) {
  37. $where[] = ['head_id' => $head_ids];
  38. }
  39. }
  40. if (!empty($params['date'])) { // 月份
  41. $month_start = strtotime($params['date']);
  42. $month_end = strtotime("+1 months", $month_start) - 1; // 减去一天
  43. $where[] = ['between', 'created_at', $month_start, $month_end];
  44. }
  45. if (!empty($user_ids)) $where[] = ['in', 'user_id', $user_ids];
  46. ($params['start'] ?? false) && $where[] = ['>=', 'updated_at', strtotime($params['start'])];
  47. ($params['end'] ?? false) && $where[] = ['<=', 'updated_at', strtotime($params['end'])];
  48. if ($is_api) {
  49. $map['select'] = 'id, commission, order_id, is_inflow';
  50. $where[] = ['reward_type' => ConfigEnum::COMMUNITY_REWARD_TYPE_COMMISSION];
  51. $map['with'] = ['order' => function($query) {
  52. $query->select('order_no, id');
  53. }];
  54. } else {
  55. $map["with"] = ['user' => function($query) {
  56. $query->select('id,mobile,nickname,avatar_url');
  57. }, 'head' => function($query) {
  58. return $query->select('id, name, logo, mobile');
  59. }];
  60. }
  61. $where[] = ["mall_id" => $mall_id];
  62. $map["where"] = $where;
  63. $map["limit"] = $params["limit"];
  64. $map["page"] = $params["page"];
  65. $map["order"]= 'id desc';
  66. $ret = CommunityHeadCommission::listPage($map);
  67. if (CommunityHeadCommission::$static_error) return CommunityHeadCommission::$static_error;
  68. $this->statistics($mall_id, !empty($params['user_id']) ? intval($params['user_id']) : 0 , $ret);
  69. $ret['reward_map'] = ConfigEnum::COMMUNITY_REWARD_TYPE_UNITS;
  70. return $ret;
  71. }
  72. /**
  73. * 数据统计
  74. * @param int $mall_id
  75. * @param int $user_id
  76. * @param array $data
  77. * @return void
  78. */
  79. public function statistics(int $mall_id, int $user_id, array &$data)
  80. {
  81. // 总额统计
  82. $sum_where = [['mall_id' => $mall_id], ['status' => StatusEnum::ENABLED]];
  83. if ($user_id) {
  84. $sum_where[] = ['user_id' => $user_id];
  85. }
  86. $sum_where[] = ['reward_type' => ConfigEnum::COMMUNITY_REWARD_TYPE_COMMISSION];
  87. $sum = CommunityHeadCommission::getOne($sum_where, true, [], false, 'SUM(commission) as commission');
  88. $data["commission"] = $sum['commission'] ?? 0;
  89. $today_at = strtotime(date('Y-m-d'));
  90. // 今日收入统计
  91. $today_sum_where = [
  92. ['mall_id' => $mall_id],
  93. ['>=', 'created_at', $today_at],
  94. ['status' => StatusEnum::ENABLED],
  95. ['reward_type' => ConfigEnum::COMMUNITY_REWARD_TYPE_COMMISSION]
  96. ];
  97. if ($user_id) {
  98. $today_sum_where[] = ['user_id' => $user_id];
  99. }
  100. $today_sum = CommunityHeadCommission::getOne($today_sum_where, true, [], false, 'SUM(commission) as commission');
  101. $data["today_commission"] = $today_sum['commission'] ?? 0;
  102. // 今日笔数统计
  103. $cnt = CommunityHeadCommission::getOne($today_sum_where, true, [], false, 'COUNT(*) as count');
  104. $data['today_count'] = $cnt['count'] ?? 0;
  105. }
  106. }