$accountId, 'name' => $name, 'num' => $num, 'type' => $type, 'action' => $action, 'created_at' => date('Y-m-d H:i:s'), 'surplus' => $surplus, 'operator' => $operator, 'operator_id' => $operatorId, 'remarks' => $remarks, ]); } /** * 用户 * */ public function account(): HasOne { return $this->hasOne(Account::class, 'id', 'account_id'); } /** * 来源用户 * */ public function sourceAccount(): HasOne { return $this->hasOne(Account::class, 'id', 'source_account_id'); } /** * 积分操作类型 * * @return string[] */ public static function scoreAction(): array { return [ self::ACTION_ORDER => '订单', self::ACTION_ADMIN_RECHARGE => '后台充值', self::ACTION_ADMIN_OPERATION => '后台操作', self::ACTION_SHARE_REG => '分享注册', self::ACTION_SHARE_REG_CHILD => '分享注册-下级分享', self::ACTION_SHARE_REG_SERVICE => '分享注册-客服获得积分', ]; } /** * 佣金操作类型 * * @return string[] */ public static function commissionAction(): array { return [ self::ACTION_ORDER => '订单返佣', self::ACTION_ADMIN_RECHARGE => '后台充值', self::ACTION_ADMIN_OPERATION => '后台操作', self::ACTION_WITHDRAWAL_RETURN => '提现退回', self::ACTION_WITHDRAWAL => '提现扣除', ]; } // /** * 分销明显 根据身份返回不同 * * @param int $accountId * @param int $position 身份 0=普通用户 1=一级代理 2=二级代理 * @param array $options * @return array * @throws \Exception */ public static function salesList(int $accountId, int $position, array $options = []): array { $page = $options['page'] ?? 1; $size = $options['size'] ?? 20; $action = $options['action'] ?? '';//查询具体某类操作 $where = []; $where[] = ['account_id', '=', $accountId]; $where[] = ['num', '>', 0]; if (!empty($action)) { $where[] = ['action', '=', $action]; } if ($position > 0) { $where[] = ['type', '=', self::TYPE_COMMISSION]; } else { $where[] = ['type', '=', self::TYPE_SCORE]; $where[] = ['action', '=', self::ACTION_ORDER]; } $field = ['id', 'account_id', 'source_account_id', 'name', 'num', 'created_at', 'type']; $list = self::findList($where, $field, $page, $size, function ($q) { return $q->order('created_at', 'desc')->order('id', 'desc')->with([ 'sourceAccount' => function ($query) { $query->field('id,nickname,headimgurl'); } ]); }); $list['list']->each(function ($item) { $item->account = $item->sourceAccount ? json_decode($item->sourceAccount, true) : []; if ($item['type'] == self::TYPE_SCORE) { $item->num = (int) $item->num; } unset($item->sourceAccount); }); return $list; } }