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


    /**
     * 支付回调
     */
    public function rechargepaysucc($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,'订单更新失败');
            }

            //先充值
            $result = model('Wallet')->lockChangeAccountRemain($args['user_id'],'gold',$args['gold'],10, '金币充值:'.$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;
        }
    }

    public function goldpaysucc($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,'订单更新失败');
            }

            //先充值
            $result = model('Wallet')->lockChangeAccountRemain($args['user_id'],'gold',$args['gold'],10, '金币充值:'.$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');
            }

            //首充
            $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'],'gold',$args['first_gold'],14, '金币充值:'.$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');
                    }
                }

            }

            Db::commit();


            return true;
        }else{
            filePut('[wallet][paySucc]在线支付订单有误 '.$orderId);
            abort(500,'model wallet fail');
            return false;
        }
    }
    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);
            }
            $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');
            }

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


            Db::commit();

            //再扣款
            /* Db::startTrans();
             //需要更新某个支付表
             if(isset($args['payids']) && $args['payids'])
             {
                 logic('Cashier')->paySuccess($args,true);
                 filePut('[wallet][paySucc] end success '.$orderId);
            }
            Db::commit();*/


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