<?php namespace app\api\model\Invite; use app\api\model\User as UserModel; use app\api\model\UserCoupon; use app\api\service\User as UserService; use app\common\model\invite\InviteConfig; use cores\exception\BaseException; use think\db\exception\DbException; use think\Paginator; class InviteLog extends \app\common\model\invite\InviteLog { /** * 隐藏字段 * @var array */ protected $hidden = [ 'store_id', 'update_time' ]; /** * @notes:获取列表记录 * @param array $where The conditions to filter the items. * @param int $listRows The number of rows to display per page. The default value is 15. * @return Paginator The paginated items. * @throws BaseException If an error occurred. * @throws DbException If a database exception occurred. */ public function getList(array $where = [], int $listRows = 15): Paginator { // 当前用户ID $userId = UserService::getCurrentLoginUserId(); $where['user_id'] = $userId; return $this->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(); } /** * @notes:获取被邀请人ID * @return array * @throws BaseException * @author: wanghousheng */ public function inviteeUserIds(): array { $userId = UserService::getCurrentLoginUserId(); return $this->where(['user_id' => $userId])->column('invitee_user_id'); } }