<?php
namespace app\api\controller;

use app\common\model\TabConf;
use think\Db;
use think\Exception;
use wanghua\general_utility_tools_php\Mmodel;
use wanghua\general_utility_tools_php\tool\Tools;

/**
 * 用户游戏道具
 * Class Usergameprop
 * @package app\api\controller
 */
class Usergameprop extends BaseHttpApi
{
    /**
     * 用户获得游戏道具接口
     * api /api/usergameprop/gainGameProp
     *
     * @param string $openid 用户openid
     * @param int $gameprop_id 道具id
     * @param int $num 道具数量
     *
     */
    public function gainGameProp()
    {
        Tools::log_to_write_txt(['获取道具 入参：' => input()]);
        return Mmodel::catchJson(function (){
            $openid = input('openid', '');
            $gameprop_id = input('gameprop_id/d', 0);
            $num = input('num/d', 1);

            if (empty($openid) || empty($gameprop_id)) {
                return Tools::set_fail('openid或gameprop_id不能为空');
            }

            // 检查gameprop_id的有效性
            $gamepropExist = Db::table('fa_gameprop')->where('id', $gameprop_id)->find();
            if (!$gamepropExist) {
                return Tools::set_fail('无效的gameprop_id');
            }

            // 检查用户是否已拥有该道具
            $propExist = Db::table('fa_usergameprop')
                ->where('openid', $openid)
                ->where('gameprop_id', $gameprop_id)
                ->find();

            if ($propExist) {
                // 更新道具数量
                Db::table('fa_usergameprop')
                    ->where('openid', $openid)
                    ->where('gameprop_id', $gameprop_id)
                    ->inc('num', $num)
                    ->update();
            } else {
                // 新增道具记录
                $gameproptype_id = $gamepropExist['gameproptype_id'];
                $newData = [
                    'openid' => $openid,
                    'gameproptype_id' => $gameproptype_id,
                    'name' => $gamepropExist['name'],
                    'gameprop_id' => $gameprop_id,
                    'num' => $num,
                    'image' => $gamepropExist['image'],
                ];
                Db::table('fa_usergameprop')->insert($newData);
            }

            Tools::log_to_write_txt(['获取道具 结果：' => '成功']);
            return Tools::set_ok('道具处理成功');
        });
    }

    /**
     * 查询用户获得的游戏道具列表接口
     * api/usergameprop/listUserGameProps
     *
     */
    public function listUserGameProps()
    {
        Tools::log_to_write_txt(['查询道具列表 入参：' => input()]);

        try {
            $openid = api_user_openid();

            // 查询用户道具列表并格式化时间
            $props = Db::table('fa_usergameprop')
                ->where('openid', $openid)
                ->order('gameprop_id asc')
                ->select();

            Tools::log_to_write_txt(['查询道具列表 结果：' => count($props)]);
            return json(Tools::set_ok('查询成功', $props));
        } catch (Exception $e) {
            Tools::error_txt_log($e);
            return json(Tools::set_fail('错误', $e->getMessage()));
        }
    }

    /**
     * desc：用户消耗游戏道具
     *
     * api/usergameprop/cutGameProp
     * 参数：
     * usergameprop_id 用户的游戏道具id
     * num 消耗数量（默认1）
     *
     * author：wh
     */
    function cutGameProp()
    {
        return Mmodel::catchJson(function (){
            $num = input('num',1);
            $openid = api_user_openid();
            $usergameprop_id = input('usergameprop_id/d', 0);//用户的游戏道具
            if (empty($openid) || empty($usergameprop_id)) {
                return Tools::set_fail('参数错误');
            }
            $propExist = Db::table(TabConf::$fa_usergameprop)
                ->where('openid', $openid)
                ->where('id', $usergameprop_id)
                ->find();
            if(empty($propExist)){
                return Tools::set_fail('道具不存在');
            }
            if($propExist['num'] <= 0){
                return Tools::set_fail('道具不足');
            }
            if($propExist['num'] - $num < 0){
                return Tools::set_fail('道具不足.');
            }
            Db::table(TabConf::$fa_usergameprop)
                ->where('id', $usergameprop_id)
                ->setDec('num', $num);
            return Tools::set_ok('消耗成功');
        });
    }
}
