| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace addons\Community\common\service;
- use addons\Community\common\enums\ConfigEnum;
- use addons\Community\common\models\CommunityHead;
- use addons\Community\common\models\CommunityHeadCommission;
- use common\enums\StatusEnum;
- use common\models\user\User;
- class CommissionService
- {
- /**
- * 列表数据
- * @param int $mall_id
- * @param array $params
- * @param bool $is_api
- * @return array|mixed
- */
- public function page(int $mall_id, array $params, bool $is_api = false)
- {
- // 组织条件
- $user_ids = [];
- if (!empty($params["user_id"])) {
- $user_ids[] = $params["user_id"];
- }
- if (!empty($params["member"])) {
- $user_or[] = 'or';
- $user_or[] = ['like', 'mobile', $params['member']];
- $user_or[] = ['like', 'nickname', $params['member']];
- $u_ids = User::find()->where(['mall_id' => $mall_id])->andWhere($user_or)->column();
- if (!empty($u_ids)) {
- $user_ids = array_merge($user_ids, $u_ids);
- }
- }
- if (!empty($params['name'])) {
- $head_ids = CommunityHead::find()->where(['mall_id' => $mall_id])
- ->andWhere(['like', 'name', $params['name']])->select('id')->column();
- if (!empty($head_ids)) {
- $where[] = ['head_id' => $head_ids];
- }
- }
- if (!empty($params['date'])) { // 月份
- $month_start = strtotime($params['date']);
- $month_end = strtotime("+1 months", $month_start) - 1; // 减去一天
- $where[] = ['between', 'created_at', $month_start, $month_end];
- }
- if (!empty($user_ids)) $where[] = ['in', 'user_id', $user_ids];
- ($params['start'] ?? false) && $where[] = ['>=', 'updated_at', strtotime($params['start'])];
- ($params['end'] ?? false) && $where[] = ['<=', 'updated_at', strtotime($params['end'])];
- if ($is_api) {
- $map['select'] = 'id, commission, order_id, is_inflow';
- $where[] = ['reward_type' => ConfigEnum::COMMUNITY_REWARD_TYPE_COMMISSION];
- $map['with'] = ['order' => function($query) {
- $query->select('order_no, id');
- }];
- } else {
- $map["with"] = ['user' => function($query) {
- $query->select('id,mobile,nickname,avatar_url');
- }, 'head' => function($query) {
- return $query->select('id, name, logo, mobile');
- }];
- }
- $where[] = ["mall_id" => $mall_id];
- $map["where"] = $where;
- $map["limit"] = $params["limit"];
- $map["page"] = $params["page"];
- $map["order"]= 'id desc';
- $ret = CommunityHeadCommission::listPage($map);
- if (CommunityHeadCommission::$static_error) return CommunityHeadCommission::$static_error;
- $this->statistics($mall_id, !empty($params['user_id']) ? intval($params['user_id']) : 0 , $ret);
- $ret['reward_map'] = ConfigEnum::COMMUNITY_REWARD_TYPE_UNITS;
- return $ret;
- }
- /**
- * 数据统计
- * @param int $mall_id
- * @param int $user_id
- * @param array $data
- * @return void
- */
- public function statistics(int $mall_id, int $user_id, array &$data)
- {
- // 总额统计
- $sum_where = [['mall_id' => $mall_id], ['status' => StatusEnum::ENABLED]];
- if ($user_id) {
- $sum_where[] = ['user_id' => $user_id];
- }
- $sum_where[] = ['reward_type' => ConfigEnum::COMMUNITY_REWARD_TYPE_COMMISSION];
- $sum = CommunityHeadCommission::getOne($sum_where, true, [], false, 'SUM(commission) as commission');
- $data["commission"] = $sum['commission'] ?? 0;
- $today_at = strtotime(date('Y-m-d'));
- // 今日收入统计
- $today_sum_where = [
- ['mall_id' => $mall_id],
- ['>=', 'created_at', $today_at],
- ['status' => StatusEnum::ENABLED],
- ['reward_type' => ConfigEnum::COMMUNITY_REWARD_TYPE_COMMISSION]
- ];
- if ($user_id) {
- $today_sum_where[] = ['user_id' => $user_id];
- }
- $today_sum = CommunityHeadCommission::getOne($today_sum_where, true, [], false, 'SUM(commission) as commission');
- $data["today_commission"] = $today_sum['commission'] ?? 0;
- // 今日笔数统计
- $cnt = CommunityHeadCommission::getOne($today_sum_where, true, [], false, 'COUNT(*) as count');
- $data['today_count'] = $cnt['count'] ?? 0;
- }
- }
|