<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 解锁微信号和私密视频
 */
class Unlockorder extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    //解锁微信订单
    public function wechataccount(){
        $to_user_id = input('to_user_id','');
        if(empty($to_user_id)){
            $this->error();
        }
        $to_user_info = Db::name('user')->find($to_user_id);
        if(empty($to_user_info)){
            $this->error('不存在的用户');
        }

        $price = config('site.unlock_wechataccount');
        $price = bcadd(intval($price),0,0);

        $data = [
            'user_id' => $this->auth->id,
            'to_user_id' => $to_user_id,
            'createtime' => time(),
            'endtime' => time() + 604800,
            'price' => $price,
        ];

        Db::startTrans();
        $order_id = Db::name('order_wechataccount')->insertGetId($data);
        if(!$order_id){
            Db::rollback();
            $this->error('解锁失败');
        }

        if($price > 0){
            $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',$price,71,'解锁:'.$to_user_id,'order_wechataccount',$order_id);
            if($wallet_rs['status'] === false){
                Db::rollback();
                $this->error($wallet_rs['msg']);
            }
        }

        Db::commit();
        $this->success('解锁成功');
    }

    //解锁私密视频订单
    public function secretvideo(){
        $to_user_id = input('to_user_id','');
        if(empty($to_user_id)){
            $this->error();
        }
        $to_user_info = Db::name('user')->find($to_user_id);
        if(empty($to_user_info)){
            $this->error('不存在的用户');
        }

        $price = config('site.unlock_secretvideo');
        $price = bcadd(intval($price),0,0);

        $data = [
            'user_id' => $this->auth->id,
            'to_user_id' => $to_user_id,
            'createtime' => time(),
            'endtime' => time() + 604800,
            'price' => $price,
        ];

        Db::startTrans();
        $order_id = Db::name('order_secretvideo')->insertGetId($data);
        if(!$order_id){
            Db::rollback();
            $this->error('解锁失败');
        }

        if($price > 0){
            $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',$price,71,'解锁:'.$to_user_id,'order_secretvideo',$order_id);
            if($wallet_rs['status'] === false){
                Db::rollback();
                $this->error($wallet_rs['msg']);
            }
        }

        Db::commit();
        $this->success('解锁成功');
    }
}