<?php

namespace app\api\controller;

use app\common\controller\Api;
use \think\Db;
use addons\epay\library\Service;

/*
 * 公众号搜索用户进行充值
 * */
class Recharge extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    //公众号支付完全放弃
    /*public function __construct(){
        echo __LINE__;
        exit;
    }*/

    //金币兑换配置列表
    public function config(){
        $rs = Db::name('paygold_webcon')->field('id,money,gold')->order('id asc')->select();
        if ($rs) {
            foreach ($rs as &$v) {
                $v['money_to_gold'] = config('site.money_to_gold');
            }
            $arr = [
                'id' => -1,
                'money' => 0,
                'gold' => 0,
                'money_to_gold' => config('site.money_to_gold')
            ];

            array_push($rs, $arr);
        }

        $this->success('success',$rs);
    }

    //根据uid检索人
    public function check_user()
    {
        $mobile = input('mobile');
        if(!$mobile){
            $this->error('请输入用户手机号');
        }

        //填充0
        $map = [
            'mobile'=>$mobile,
        ];

        $user_info = Db::name('user')->field('username,nickname')->where($map)->find();
        if (empty($user_info)) {
            $this->error('用户信息不存在');
        }
        $this->success('success',$user_info);
    }

    //创建订单,公众号
    public function recharge(){

        $rc_id = input('rc_id',0);
        $pay_type  = input('pay_type','wechat');
        $username = input('username',0);


        if(!$rc_id){
            $this->error('请选择充值金额');
        }

        if(!$username){
            $this->error('请输入用户手机号并选择');
        }

        //查找用户
        $user_info = Db::name('user')->field('id')->where('username',$username)->find();
        if (empty($user_info)) {
            $this->error('用户信息不存在');
        }

        //赋值money
        $recharge_config = Db::name('paygold_webcon')->where('id',$rc_id)->find();
        $money = $recharge_config['money'];
        $gold = $recharge_config['gold'];

        //创建订单
        $data = [];
        $data['status'] = 0;
        $pay_no = createUniqueNo('P',$user_info['id']);
        $data['pay_no'] = $pay_no;
        $data['money'] = $money;
        $data['payment_class'] = $pay_type;
        $data['user_id'] = $user_info['id'];
        $data['ext_info'] =  json_encode(['subject'=>'充值金币支付']);
        $data['memo'] =  '充值金币支付';
        $data['createtime'] = time();
        $data['payment'] = 'mp';
        $orderid = Db::name('pay_order')->insertGetId($data);

        //创建回调
        $data = [];
        $data['event'] = 'success';
        $data['class'] = 'app\common\model\Recharge';
        $data['method'] = 'rechargepaysucc';
        $data['args'] = json_encode(['user_id'=>$user_info['id'],'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id]);
        $data['pay_no'] = $pay_no;
        Db::name('pay_event')->insertGetId($data);

        $return = [
            'pay_no'=>$pay_no,
            'title' => '充值金币支付',
        ];

        $this->success('success',$return);
    }

    //公众号
    public function topay(){

        $openid = input('openid');
        $pay_no = input('pay_no');
        $pay_type  = input_post('pay_type','wechat');
        $orderInfo = Db::name('pay_order')->where('pay_no',$pay_no)->find();
        //下单
        $params = [
            'type'         => $pay_type,
            'orderid'      => $pay_no,
            'title'        => $orderInfo['memo'],
            'amount'       => $orderInfo['money'],
            // 'amount'       => 0.01,
            'method'       => 'mp',
            'openid'       => $openid,
            'notifyurl' => $this->request->root(true) . '/notify.php',
            //'returnurl' => $this->request->root(true) . '/location.php',
        ];
        $res = Service::submitOrder($params);
        $this->success('请求成功',json_decode($res,true));
    }

    //兑换金币
    public function exchangegold() {
        $id = input('id', 0, 'intval');
        $freemoney = input_post('freemoney', 0, 'intval'); //自定义
        $uid = $this->auth->id;
        if(!$id && !$freemoney){
            $this->error('请选择或填写兑换金额');
        }

        if ($id) {
            //赋值money
            $paygold_webcon = Db::name('paygold_webcon')->where('id', $id)->find();
            $money = $paygold_webcon ? $paygold_webcon['money'] : 0;
            $gold = $paygold_webcon ? $paygold_webcon['gold'] : 0;
        }
        if ($freemoney) {
            $money = $freemoney;
            $bili = config('site.money_to_gold') ?: 10;
            $gold  = bcmul($money,$bili,0);
        }

        if($money<=0) {
            $this->error('支付金额必须大于0');
        }
        if($money > 10000){
            $this->error('支付金额太大');
        }
        //验证余额
        $user_info = model('wallet')->getWallet($this->auth->id);
        if ($user_info['money'] < $money) {
            $this->success('您的余额不足', ['code' => 2]);
        }

        //开启事务
        Db::startTrans();
        //扣费
        $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'money',-$money,18,'兑换金币');
        if($wallet_rs['status'] === false){
            Db::rollback();
            $this->error($wallet_rs['msg']);
        }
        //赠送金币
        $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'gold',$gold,19,'获得兑换金币');
        if($wallet_rs['status'] === false){
            Db::rollback();
            $this->error($wallet_rs['msg']);
        }

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

        $this->success('兑换成功');
    }

    //兑换rmb
    public function exchangermb() {
        exit;//知音国际版才有钻石,这里用不到了
        $freemoney = input_post('freemoney', 0, 'intval'); //自定义
        $uid = $this->auth->id;
        if(!$freemoney){
            $this->error('请选择或填写兑换金额');
        }
        if ($freemoney) {
            $money = $freemoney;
            $bili = config('site.jewel_money') ?: 1;
            $gold = bcdiv($money,$bili,2);
        }
        if($money<=0) {
            $this->error('兑换金额必须大于0');
        }
        //验证余额
        $user_info = model('wallet')->getWallet($this->auth->id);
        if ($user_info['money'] < $money) {
            $this->success('您的钻石不足', ['code' => 2]);
        }

        //开启事务
        Db::startTrans();
        //扣费
        $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'money',-$money,25,'兑换人民币');
        if($wallet_rs['status'] === false){
            Db::rollback();
            $this->error($wallet_rs['msg']);
        }
        //兑换人民币
        $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'rmb_money',$gold,26,'钻石兑换人民币');
        if($wallet_rs['status'] === false){
            Db::rollback();
            $this->error($wallet_rs['msg']);
        }
        Db::commit();

        $this->success('兑换成功');
    }

}