<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 用户装扮接口
 */
class Userdecorate extends Api
{
    protected $noNeedLogin = ['decorate_list'];
    protected $noNeedRight = ['*'];

    /**
     * 列表
     *
     */
    public function decorate_list()
    {
        $type = input_post('type',1); // 类型:1=座驾,2=头饰,3=尾灯,4=气泡

        // 获取基本信息
        $where = [];
        $where['type'] = $type;
        $where['status'] = 1;
        $list = Db::name('decorate')->where($where)->autopage()->order('sort desc')->select();

        $list = list_domain_image($list,['base_image','play_image','android_image']);

        $this->success("success",$list);
    }

    /**
     * 购买并加入我的背包
     */
    public function buy_one() {
        $did = input_post('did',''); //装扮ID
        if (!$did) {
            $this->error();
        }
        // 判断用户钻石余额是否充足
        $walletinfo = model('wallet')->getWallet($this->auth->id);

        // 获取购买装扮需要的价格
        $decorate = Db::name('decorate')->where(['id'=>$did])->find();
        if(!$decorate) {
            $this->error("装扮信息获取失败！");
        }
        if($walletinfo['gold'] < $decorate['price']) {
            $this->error("您的余额不足，请先充值！");
        }
        // 进行购买逻辑
        Db::startTrans();

        // 添加到背包
        $data = [
            'user_id' => $this->auth->id,
            'decorate_id' => $decorate['id'],
            'decorate_type' => $decorate['type'],
            'is_using' => 0,
            'end_time' => time() + ($decorate['days'] * 86400),
            'createtime' => time(),
            'updatetime' => time(),
        ];

        $log_id = Db::name('user_decorate')->insertGetId($data);
        if(!$log_id){
            Db::rollback();
            $this->error('购买失败');
        }

        $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$decorate['price'],31,'','user_decorate',$log_id);
        if($rs['status'] === false){
            Db::rollback();
            $this->error($rs['msg']);
        }


        Db::commit();
        $this->success("购买成功！");
    }

    //获取用户装扮
    public function my_decorate_list()
    {
        $uid = $this->auth->id;
        $type = input_post('type',1);

        $map = [
            'a.end_time' => ['gt',time()],
            'a.decorate_type' => $type,
            'a.user_id' => $uid,
        ];

        $list = Db::name('user_decorate')
            ->alias('a')
            ->field('a.id,a.is_using,a.end_time,b.name,b.base_image,b.play_image,b.android_image')
            ->join('decorate b', 'a.decorate_id = b.id')
            ->where($map)->order('a.id desc')->autopage()->select();

        $list = list_domain_image($list,['base_image','play_image','android_image']);

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

    //获得某用户的某类型正在使用的装扮
    public function get_user_onetype_decorate(){
        $uid = input_post('uid');
        $type = input_post('type',1);

        if(!$uid){
            $this->error();
        }

        //获取用户气泡
        $map = [
            'a.user_id' => $uid,
            'a.is_using' => 1,
            'a.decorate_type' => $type,
            'a.end_time' => ['gt',time()],
        ];

        $info = Db::name('user_decorate')->alias('a')
            ->field('a.id,a.is_using,a.end_time,b.name,b.base_image,b.play_image,b.android_image')
            ->join('decorate b', 'a.decorate_id = b.id')
            ->where($map)->order('a.id desc')->find();
        $info = info_domain_image($info,['base_image','play_image','android_image']);

        $this->success('success',$info);
    }
    //设置装扮
    public function set_user_decorate()
    {
        $did = input_post('did',''); //用户装扮ID
        if (!$did) {
            $this->error();
        }

        $map = [
            'user_id' => $this->auth->id,
            'id'    => $did,
        ];
        $info = Db::name('user_decorate')->where($map)->find();
        if (empty($info)) {
            $this->error('装扮不存在');
        }
        if ($info['end_time'] < time()) {
            $this->error('装扮已过期');
        }

        Db::startTrans();
        //清理该类型装扮使用状态
        $map = [
            'user_id' => $this->auth->id,
            'decorate_type' => $info['decorate_type'],
        ];

        $data = [];
        $data['is_using'] = 0;
        $data['updatetime'] = time();
        $reslut = Db::name('user_decorate')->where($map)->update($data);
        if (!$reslut) {
            Db::rollback();
            $this->error('设置失败');
        }

        //设置使用中状态
        $map = [
//            'user_id' => $this->auth->id,
            'id'    => $did,
        ];
        $data = [];
        $data['is_using'] = 1;
        $data['updatetime'] = time();
        $reslut = Db::name('user_decorate')->where($map)->update($data);
        if (!$reslut) {
            Db::rollback();
            $this->error('设置失败');
        }
        // 提交事务
        Db::commit();
        $this->success('设置成功');

    }
    //取消装扮
    public function cancel_user_decorate()
    {
        $did = input_post('did',''); //装扮ID
        if (!$did) {
            $this->error();
        }

        $map = [
            'user_id' => $this->auth->id,
            'decorate_id'    => $did,
        ];

        $data = [];
        $data['is_using'] = 0;
        $data['updatetime'] = time();

        $reslut = Db::name('user_decorate')->where($map)->update($data);

        $this->success('设置成功');
    }
}
