<?php
namespace app\common\model;
use \think\Db;
/**
 * 订单支付模型
 */
class Recharge
{


    /**
     * 支付回调
     */
    //pc端充值金币
    public function rechargepaysucc($orderId,$args){
        $order = Db::name('pay_order')->where(array('pay_no'=>$orderId,'status'=>2))->find();

        if( $order )
        {
            $remark = $order['payment_class'] == 'alipay' ? '支付宝' : '微信';
            Db::startTrans();
            //更新订单状态
            $rs = Db::name('pay_order')->where(['id'=>$order['id']])->update(['status'=>1]);
            if(!$rs){
                Db::rollback();
                filePut('[wallet][paySucc] pay_order update fail'.$orderId);
                abort(500,'订单更新失败');
            }

            //先充值
            $result = model('Wallet')->lockChangeAccountRemain($args['user_id'],0,'gold',$args['gold'],10, $remark . '金币充值:'.$orderId,'pay_order',$order['id']);
            if($result['status']===false)
            {
                Db::rollback();
                filePut('[wallet][paySucc]网银充值入账更新余额失败 recharge money fail'.$orderId.$result['msg']);
                abort(500,'网银充值入账 model wallet recharge money fail');
            }

            Db::commit();


            return true;
        }else{
            filePut('[wallet][paySucc]在线支付订单有误 '.$orderId);
            abort(500,'model wallet fail');
            return false;
        }
    }

    //app充值金币
    public function goldpaysucc($orderId,$args,$type=0){
        $order = Db::name('pay_order')->where(array('pay_no'=>$orderId,'status'=>2))->find();

        if( $order )
        {

            Db::startTrans();
            //更新订单状态
            $rs = Db::name('pay_order')->where(['id'=>$order['id']])->update(['status'=>1]);
            if(!$rs){
                Db::rollback();
                filePut('[wallet][paySucc] pay_order update fail'.$orderId);
                abort(500,'订单更新失败');
            }

            $remark = $order['payment_class'] == 'alipay' ? '支付宝' : '微信';
            //先充值
            $result = model('Wallet')->lockChangeAccountRemain($args['user_id'],0,'gold',$args['gold'],10, $remark . '金币充值:'.$orderId,'pay_order',$order['id'],1);
            if($result['status']===false)
            {
                Db::rollback();
                filePut('[wallet][paySucc]网银充值入账更新余额失败 recharge money fail'.$orderId.$result['msg']);
                abort(500,'网银充值入账 model wallet recharge money fail');
            }
            //若有上级,给上级返利
            if ($args['intro_uid']) {
                //获取返利比率
                $is_agent = Db::name('user')->where(['id' => $args['intro_uid']])->value('is_agent');
                if ($type == 1){
                    $intro_recharge_rebate_rate = 10;
                }else{
                    $intro_recharge_rebate_rate = $is_agent ? (int)config('site.h_intro_recharge_rebate_rate') : (int)config('site.intro_recharge_rebate_rate');
                }
         
                if ($intro_recharge_rebate_rate > 0 && $intro_recharge_rebate_rate <= 100) {
                    //上级获得金额数量
                    $intro_uid_money = number_format($args['money'] * $intro_recharge_rebate_rate / 100, 2, '.', '');
                    if ($intro_uid_money > 0) {
                        $intro_result = model('wallet')->lockChangeAccountRemain($args['intro_uid'],$args['user_id'],'money',$intro_uid_money,65,'邀请人充值奖励:'.$orderId,'pay_order',$order['id']);
                        if($intro_result['status']===false)
                        {
                            Db::rollback();
                            filePut('[wallet][paySucc]上级返利更新余额失败 recharge money fail'.$orderId.$intro_result['msg']);
                            abort(500,'上级返利 model wallet recharge money fail');
                        }
                    }
                }
            }

            //首充
            $first_map = [
                'uid' => $args['user_id'],
//                'pg_id' => $args['pg_id'],
            ];
            $first_check = Db::name('user_paygold_log')->where($first_map)->find();
            if(empty($first_check)){

                //首充日志
                $first_map['createtime'] = time();
                $first_log_id = Db::name('user_paygold_log')->insertGetId($first_map);
                if(!$first_log_id){
                    Db::rollback();
                    abort(500,'首充赠送失败');
                }
                //送金币
                if(isset($args['first_gold']) && $args['first_gold'] > 0){
                    $result = model('Wallet')->lockChangeAccountRemain($args['user_id'],0,'gold',$args['first_gold'],14, $remark . '金币充值:'.$orderId,'pay_order',$order['id']);
                    if($result['status']===false)
                    {
                        Db::rollback();
                        filePut('[wallet][paySucc]网银充值入账更新余额失败 recharge money fail'.$orderId.$result['msg']);
                        abort(500,'网银充值入账 model wallet recharge money fail');
                    }
                }
                //送vip
                if(isset($args['first_vipdays']) && $args['first_vipdays'] > 0){
                    $user_info = Db::name('user_wallet')->where('user_id',$args['user_id'])->lock(true)->find();
                    if($user_info['vip_endtime'] < time()){
                        //过期了
                        $vip_endtime = time() + (intval($args['first_vipdays']) * 86400);
                    }else{
                        //追加vip
                        $vip_endtime = $user_info['vip_endtime'] + (intval($args['first_vipdays']) * 86400);
                    }
                    $result = Db::name('user_wallet')->where('user_id',$args['user_id'])->update(['vip_endtime'=>$vip_endtime]);
                    if($result === false)
                    {
                        Db::rollback();
                        filePut('[wallet][paySucc]网银充值入账更新vip时间失败 recharge money fail'.$orderId);
                        abort(500,'网银充值入账 model wallet recharge money fail');
                    }
                }

            }
            //vip充值额外送金币
            if(isset($args['vip_gold']) && $args['vip_gold'] > 0){
                $result = model('Wallet')->lockChangeAccountRemain($args['user_id'],0,'gold',$args['vip_gold'],101, $remark . '金币充值:'.$orderId,'pay_order',$order['id']);
                if($result['status']===false)
                {
                    Db::rollback();
                    filePut('[wallet][paySucc]网银充值入账更新余额失败 recharge money fail'.$orderId.$result['msg']);
                    abort(500,'网银充值入账 model wallet recharge money fail');
                }
            }

            Db::commit();


            return true;
        }else{
            filePut('[wallet][paySucc]在线支付订单有误 '.$orderId);
            abort(500,'model wallet fail');
            return false;
        }
    }
    //app充值vip
    public function vippaysucc($orderId,$args){
        $order = Db::name('pay_order')->where(array('pay_no'=>$orderId,'status'=>2))->find();

        if( $order )
        {

            Db::startTrans();
            //更新订单状态
            $rs = Db::name('pay_order')->where(['id'=>$order['id']])->update(['status'=>1]);
            if(!$rs){
                Db::rollback();
                filePut('[wallet][paySucc] pay_order update fail'.$orderId);
                abort(500,'订单更新失败');
            }

            //先充值
            $user_info = Db::name('user_wallet')->where('user_id',$args['user_id'])->lock(true)->find();
            if($user_info['vip_endtime'] < time()){
                //过期了
                $vip_endtime = time() + (intval($args['days']) * 86400);
            }else{
                //追加vip
                $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
            }
            $update_data = [
                'vip_endtime'=>$vip_endtime,
                'vip_level'  =>$args['vip_level'], //修改等级,同级不影响,向上立刻改,向下不允许
                'pay_money' => bcadd($user_info['pay_money'], $args['money'], 2)
            ];
            $result = Db::name('user_wallet')->where('user_id',$args['user_id'])->update($update_data);
            if($result === false)
            {
                Db::rollback();
                filePut('[wallet][paySucc]网银充值入账更新vip时间失败 recharge money fail'.$orderId);
                abort(500,'网银充值入账 model wallet recharge money fail');
            }

            if ($args['gold_num'] > 0) {
                //赠送金币
                $wallet_rs = model('wallet')->lockChangeAccountRemain($args['user_id'], 0,'gold', $args['gold_num'], 17, '开通会员赠送金币');
                if ($wallet_rs['status'] === false) {
                    Db::rollback();
                    filePut('[wallet][paySucc] gold update fail' . $orderId);
                    abort(500, '赠送金币更新失败');
                }
            }

            //tag任务赠送金币
            //开通VIP 50金币
//            $task_rs = \app\common\model\TaskLog::tofinish($args['user_id'],9);
//            if($task_rs === false){
//                Db::rollback();
//                $this->error('完成任务赠送奖励失败');
//            }


            Db::commit();

            return true;
        }else{
            filePut('[wallet][paySucc]在线支付订单有误 '.$orderId);
            abort(500,'model wallet fail');
            return false;
        }
    }
}