<?php

namespace App\Models;

/**
 * App\Models\BuyInviteListModel
 *
 * @property int $id
 * @property int $uid 创建分享的用户
 * @property int $partner_id 卡片id
 * @property \Illuminate\Support\Carbon $created_at 创建时间
 * @property \Illuminate\Support\Carbon $updated_at 更新时间
 * @property int $expired_at 过期时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\BuyInviteListModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\BuyInviteListModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\BuyInviteListModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\BuyInviteListModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\BuyInviteListModel whereExpiredAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\BuyInviteListModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\BuyInviteListModel wherePartnerId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\BuyInviteListModel whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\BuyInviteListModel whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class BuyInviteListModel extends Model
{
    protected $table = "kdgx_partner_charge_buy_invite_list";

    protected $dateFormat = 'U';

    public $fillable = ['id', 'created_at', 'updated_at', 'uid', 'partner_id', 'expired_at'];

    /**
     * 获取折扣百分比
     * @param  int  $invite_id
     * @return mixed
     * @throws \ApiException
     */
    public function effectiveInvite(int $invite_id)
    {
        $invite = $this->find($invite_id);
        if (collect($invite)->isEmpty()) {
            throw new \ApiException("错误的资源", 401);
        }
        $recod = new RecodeModel();
        if (
            $recod->where([
            ['partner_id', $invite->partner_id],
            ['uid', $invite->uid]
            ])->exists()
        ) {
            return 100;
        }
        $model = new BuyInviteModel();
        $cnt = $model->where([
            ['invite_id', $invite->id],
            ['state', 0]
        ])->sum('dis_score');
        return $cnt;
    }

    /**
     * 自动解锁【卡片解锁度到达100%时】
     * @param  int $invite_id 发起用户id
     * @return boolean
     * @throws \ApiException
     */
    public function autoUnlock(int $invite_id)
    {
        $invite = $this->find($invite_id);
        if (collect($invite)->isEmpty()) {
            throw new \ApiException("错误的资源", 401);
        }
        if ($this->effectiveInvite($invite_id) >= 100) {
            $model = new RecodeModel();
            if ($model->isLock($invite->uid, $invite->partner_id)) {
                return true;
            } else {
                $model = new RecodeModel();
                $recode = $model->fill([
                    'uid' => $invite->uid,
                    'partner_id' => $invite->partner_id,
                    'create_at' => time()
                ]);
                if ($recode->save()) {
                    return true;
                } else {
                    throw new \ApiException('数据库异常', 501);
                }
            }
        } else {
            return false;
        }
    }
}
