<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 道具商城,联盟
 */
class Lianmeng extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    //道具列表
    public function decorate_list(){

        $type = input_post('type',1);

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

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

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

    /**
     * 购买并加入我的背包
     */
    public function buy_one() {
        $number = input_post('number',1,'intval'); //数量
        if(!$number){
            $this->error();
        }

        $did = input_post('did',''); //装扮ID
        if (!$did) {
            $this->error();
        }
        // 判断用户金币余额是否充足
        $walletinfo = model('wallet')->getWallet($this->auth->id);

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

        // 添加到背包
        for($i=1;$i<=$number;$i++){
            $data[] = [
                'user_id' => $this->auth->id,
                'decorate_id' => $decorate['id'],
                'is_using' => 0,
                'createtime' => time(),
                'updatetime' => time(),
            ];
        }

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

        //扣钱
        $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$total_price,33,'购买联盟道具','user_decorate_lianmeng',0);
        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.user_id'  => $uid,
            'a.is_using' => 0,
            'b.type'     => $type,
        ];

        $list = Db::name('user_decorate_lianmeng')
            ->alias('a')
            ->field('a.id,a.decorate_id,b.name,b.type,b.base_image,count(a.decorate_id) as number')
            ->join('decorate_lianmeng b', 'a.decorate_id = b.id','LEFT')
            ->where($map)->group('a.decorate_id')->order('a.id desc')->autopage()->select();

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

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

    //使用道具
    public function use_decorate()
    {
        $plat_name = input_post('plat_name',''); //平台用户名
        if (!$plat_name) {
            $this->error();
        }

        $id = input_post('id',0); //用户装扮ID
        if (!$id) {
            $this->error();
        }

        $map = [
            'user_id' => $this->auth->id,
            'id'    => $id,
        ];
        $info = Db::name('user_decorate_lianmeng')->where($map)->find();
        if (empty($info)) {
            $this->error('道具不存在');
        }
        if ($info['is_using'] != 0) {
            $this->error('道具已使用');
        }

        Db::startTrans();


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

    }
}