<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\api\controller\Common;
use app\common\model\GiftUserParty;
use app\common\model\UserExchangeLog;
use app\common\model\UserExchsoundLog;
use applebuy\applebuy;
use fast\Random;
use app\common\model\RecharOrder;
use kjpay\kjpay;
use kjwithdraw\kjwithdraw;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Request;
use think\Db;
use app\common\library\Sms as Smslib;

/**
 * 钱包接口
 */
class Money extends Common
{
    protected $noNeedLogin = ['getRecharConfig', 'getRateMoney', 'getExchangeConfig', 'getExchsoundConfig', 'rebateDuty',
        'kjRecharge', 'changeWithDrawStatus','moneyConfig'];
    protected $noNeedRight = '*';

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





    /**
     * 消息-聊天-更多-转账-钻石
     */
    public function givejeweltouser(){
        $money = intval(input('money',0));
        $user_id = input('user_id',0);
        $pay_password = input('paypassword','');

        //验证用户权限是否能转账
        if($this->auth->power->transfer == 1){
            $this->error('您已经被限制消费-转账');
        }
        //不允许给自己转账
        if ($user_id == $this->auth->id) {
            $this->error('不允许给自己转账');
        }
        //验证被转账用户存在
        $check = Db::name('user')->where('id',$user_id)->find();
        if(empty($check)){
            $this->error('不存在的用户');
        }
        //验证操作密码
        if(!$this->auth->pay_password){
            $this->error('请先设置支付密码');
        }
        if ($this->auth->pay_password != $this->getEncryptPassword($pay_password, $this->auth->pay_salt)) {
            $this->error('支付密码有误');
        }

        Db::startTrans();

        //减少和增加余额
        $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,$money,'-',0,'私聊转账给:'.$check['nickname'],11,'jewel');
        if($rs_wallet['status'] === false){
            Db::rollback();
            $this->error($rs_wallet['msg']);
        }

        $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$money,'+',0,$this->auth->nickname.':私聊到账',12,'jewel');
        if($rs_wallet['status'] === false){
            Db::rollback();
            $this->error($rs_wallet['msg']);
        }

        Db::commit();
        $this->success('转账成功');
    }

    /**
     * 获取密码加密后的字符串
     * @param string $password 密码
     * @param string $salt 密码盐
     * @return string
     */
    private function getEncryptPassword($password, $salt = '')
    {
        return md5(md5($password) . $salt);
    }



    //提现转账(新版2020-01-01)
    public function withdrawTransfer() {
        $info = [
            'money' => 0.10,
            'alipay_account' => '',
            'name' => '',
        ];
        $data['out_biz_no'] = getMillisecond() . mt_rand(1, 1000); //商户订单号
        $data['trans_amount'] = $info['money']; //订单总金额,单位为元,精确到小数点后两位
        $data['product_code'] = 'TRANS_ACCOUNT_NO_PWD';//业务产品码,收发现金红包固定为:STD_RED_PACKET;单笔无密转账到支付宝账户固定为:TRANS_ACCOUNT_NO_PWD;单笔无密转账到银行卡固定为:TRANS_BANKCARD_NO_PWD
        $data['biz_scene'] = 'DIRECT_TRANSFER'; //描述特定的业务场景,可传的参数如下:PERSONAL_COLLECTION:C2C现金红包-领红包;DIRECT_TRANSFER:B2C现金红包、单笔无密转账到支付宝/银行卡
        $data['order_title'] = 'GG语音提现'; //转账业务的标题,用于在支付宝用户的账单里显示

        $data['payee_info']['identity'] = $info['alipay_account'];//收款方支付宝id或支付宝账户

        /*if ($info['alipay_type'] == 1) { //支付宝账户
            $data['payee_info']['identity_type'] = 'ALIPAY_LOGON_ID';
            //收款支付宝账号真实姓名, 不为空时支付宝会验证
            $data['payee_info']['name'] = $info['name'];
        } else { //支付宝id
            $data['payee_info']['identity_type'] = 'ALIPAY_USER_ID';
            $data['payee_info']['name'] = '';
        }*/
        //支付宝id
        $data['payee_info']['identity_type'] = 'ALIPAY_LOGON_ID';
        $data['payee_info']['name'] = $info['name'];

        //转账备注(支持200个英文/100个汉字)。当付款方为企业账户,且转账金额达到(大于等于)50000元,remark不能为空。收款方可见,会展示在收款用户的收支详情中。
        $data['remark'] = 'GG语音提现';
        require_once("../extend/AliPay/AliPay.php");
        $alipay =new \AliPay();
        $result =$alipay->AliPayWithdraw($data);
        return $result;
    }


}