<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 联盟城市
 */
namespace app\ais\model;
use app\ais\model\AisBankCash;
use think\Model;

class AisBank extends Model{


  /**
     * 积分增加
     * @param integer $miniapp_id
     * @param integer $uid
     * @param integer $money（元）
     * @return void
     */
    public static function points(int $weapp_id,int $uid,float $points){
        $info = self::where(['uid' => $uid])->find();
        if(empty($info)){
            $data['member_miniapp_id'] = $weapp_id;
            $data['uid']               = $uid;
            $data['point']             = intval($points);
            $data['update_time']       = time();
            return self::create($data);
        }else{
            $info->point       = ['inc',intval($points)];
            $info->update_time = time();
            return $info->save();
        }
    }

    /**
     * 帐号充值
     * @param integer $miniapp_id
     * @param integer $uid
     * @param integer $money（元）
     * @return void
     */
    public static function recharge(int $weapp_id,int $uid,float $money){
        $info = self::where(['uid' => $uid])->find();
        if(empty($info)){
            $data['member_miniapp_id'] = $weapp_id;
            $data['uid']               = $uid;
            $data['money']             = $money;
            $data['point']             = intval($money);
            $data['update_time']       = time();
            return self::create($data);
        }else{
            $info->money       = ['inc',$money];
            $info->point       = ['inc',intval($money)];
            $info->update_time = time();
            return $info->save();
        }
    }

    /**
     * 提现申请(小程序API)
     * @param integer $miniapp_id
     * @param integer $uid
     * @param integer $money（元）
     * @return void
     */
    public static function cashOut(int $uid,float $money){
        $info = self::where(['uid' => $uid])->find();
        if(empty($info) || $info->money < $money ){
            return;
        }
        $info->money      = ['dec',$money];
        $info->tobo_paid  = ['inc',$money];
        $info->update_time = time();
        $info->save();
        //创建提现记录
        $data['member_miniapp_id'] = $info->member_miniapp_id;
        $data['uid']               = $info->uid; 
        $data['money']             = $money;
        $data['realmoney']         = 0;
        $data['state']             = 0;
        $data['update_time']       = time();
        return AisBankCash::create($data);
    }
}