<?php
declare (strict_types = 1);

namespace app\model\api;

use library\basic\BaseModel;
use think\facade\Db;
use think\Model;
use \app\model\api\MemberDetail;
/**
 * @mixin \think\Model
 */
class Recharge extends BaseModel
{

    /**
     * 充值费用
     * @param $order_id
     */
    public function rechargeSuccess($order_id) {
        $data = $this->where('order_id',$order_id)->find();
        if(empty($data)) {
            return false;
        }
        //bool
        $bool = (new MemberDetail)->recharge($data['v'],$data['uid'],[],$data['sassid']);
        if(!$bool) {
            return false;
        }
        //是否升级
        $mData = (new Member)->where('uid',$data['uid'])->find();
        $siteLevel = Db::name("site_level")
                    ->where('sass_id',$data['sassid'])
                    ->where('level_id','>',$mData['levelid'])
                    ->where('update_money','<=',$data['v'])
                    ->where('update_money','>',0)
                    ->order("update_money desc")
                    ->find();
        //升级会员
        if(!empty($siteLevel)) {
            (new Member)->where('uid',$data['uid'])->save(['levelid'=>$siteLevel['level_id']]);
            (new MemberUpdate)->insert(['uid'=>$data['uid'],'update_level'=>$siteLevel['level_id'],'time'=>time()]);
        }
        $this->where('id',$data['id'])->save(['status'=>1]);
        return true;
    }


}
