where($where) ->with(['invitee']) ->order(['create_time' => 'desc']) ->paginate($listRows); } private static function isExistReferee(int $userId): bool { return (bool)self::get(['invitee_user_id' => $userId]); } /** * @notes:新增 * @param $data * @return bool * @author: wanghousheng */ public function add($data): bool { $data['store_id'] = self::$storeId; return $this->save($data); } public static function createRelation(int $userId, int $refereeId): bool { // 自邀请 if ($userId == $refereeId) { return false; } // 判断当前用户是否已存在推荐关系 if (self::isExistReferee($userId)) { return false; } $data['invitee_user_id'] = $userId; $data['user_id'] = $refereeId; $model = new self; $model->add($data); self::gifts($refereeId); return true; } /** * @notes:发送奖励 * @param int $userId * @author: wanghousheng */ private static function gifts(int $userId) { $info = InviteConfig::detail(['store_id' => self::$storeId]); if ($info) { //是否有积分 if ($info['integral'] > 0) { UserModel::setIncPoints($userId, (int)$info['integral'], '邀请好友', self::$storeId); } //优惠券 if ($info['coupon_id'] > 0) { (new UserCoupon)->giveReceive(intval($info['coupon_id']), $userId); } } } /**获取金额 * @notes: * @return float * @throws BaseException * @author: wanghousheng */ public function sumMoney(): float { // 当前用户ID $userId = UserService::getCurrentLoginUserId(); return $this->where(['user_id' => $userId])->sum('money'); } /**累计人数 * @notes: * @return int * @throws BaseException * @author: wanghousheng */ public function countPeople(): int { $userId = UserService::getCurrentLoginUserId(); return $this->where(['user_id' => $userId])->count(); } }