<?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('不存在的用户');
        }

        //检查防重复解锁
        $check_map = [
            'user_id' => $this->auth->id,
            'to_user_id' => $to_user_id,
            'endtime' => ['gt',time()],
        ];
        $check_repeat = Db::name('order_wechataccount')->where($check_map)->order('id desc')->find();
        if(!empty($check_repeat)){
            $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_info['username'],'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('不存在的用户');
        }

        //检查防重复解锁
        $check_map = [
            'user_id' => $this->auth->id,
            'to_user_id' => $to_user_id,
            'endtime' => ['gt',time()],
        ];
        $check_repeat = Db::name('order_secretvideo')->where($check_map)->order('id desc')->find();
        if(!empty($check_repeat)){
            $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,
            'type' => 1,   //1 金币付费
        ];

        //年费vip干预,一天只一次
        $today_start = strtotime(date('y-m-d'));
        $today_end   = $today_start + 86399;
        $check_free_map = [
            'user_id' => $this->auth->id,
            'to_user_id' => $to_user_id,
            'createtime' => ['between',[$today_start,$today_end]],
            'type' => 2,
        ];
        $check_free = Db::name('order_secretvideo')->where($check_free_map)->count('id');

        //年费vip,且今天没使用过权限,优先使用vip权限
        $wallet = model('wallet')->getwallet($this->auth->id);
        if($wallet['vip_endtime'] > time() && $wallet['vip_level'] == 40 && $check_free == 0 ){
            $data['price'] = 0;
            $data['type'] = 2;         //2 年vip免费
            $data['endtime'] = time(); //立即过期,就看一下
            $price = 0;
        }

        //log
        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,72,'解锁:'.$to_user_info['username'],'order_secretvideo',$order_id);
            if($wallet_rs['status'] === false){
                Db::rollback();
                $this->error($wallet_rs['msg']);
            }
        }

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