<?php
/**
 * 商品相关
 */
defined('IN_WEB') or die('Include Error!');
class Diamond
{
    /**
     * 获取钻石商品兑换列表
     * @param $param
     * @return false|string
     */
    public function getDiamondGoods($param){
        $data = array('code' => -1, 'list' => []);
        $uid = oo::functions()->uint($param['uid']);//后期要根据玩家类型展示不同的商品
        if( empty($uid) ){
            return json_encode($data);
        }
        $data = oo::commonOprModel('diamond')->getShopDiamondWithClient($uid);
        return json_encode($data, JSON_NUMERIC_CHECK);
    }
	
    /**
     * 获取玩家钻石兑换信息
     * @param $param
     * @return false|string
     */
  	public function getExchangeStageInfo($param){
        $uid = oo::functions()->uint($param['uid']);
        if( empty($uid) ){
            return oo::response(-1,[],'参数错误');
        }
        $version = oo::commonOprModel('user')->getUserVersion($uid);
        if(oo::compareVersion($version, '1.7.7')){
            $data = oo::commonOprModel('diamond')->getNewUserExchangeInfo($uid);
        }else{
            $data = oo::commonOprModel('diamond')->getUserExchangeInfo($uid);
        }
        return json_encode($data, JSON_NUMERIC_CHECK);
    }

    /**
     * 钻石兑换商品
     */
  	public function exchange($param){
        $uid = oo::functions()->uint($param['uid']);
        $pid = oo::functions()->uint($param['gid']);
        $stage = oo::functions()->uint($param['stage']);
        if( empty($uid) || !in_array($pid, [1, 2, 3,6])){
            return oo::response(-1,[],'参数错误');
        }
        $version = oo::commonOprModel('user')->getUserVersion($uid);
        if(!oo::compareVersion($version,"1.8.4")){
            $ret = oo::commonOprModel('diamond')->canExchange($uid, $pid, $stage, $version);
            if(!$ret){
                return oo::response(-1,[],'未满足兑换条件');
            }
        }
        $ret = oo::commonOprModel('diamond')->diamondExchange($uid, $pid, $stage, $version);
        if(!$ret){
            return oo::response(-1,[],'兑换失败');
        }
        $data = oo::commonOprModel('diamond')->getExchangedWithClient($uid, $pid, $stage, $version); //返回当前阶兑换记录
        return oo::response(1, $data);
    }

    /**
     * 钻石兑换信息清除
     */
    public function clearExchanged($param) {
        $uid = oo::functions()->uint($param['uid']);
        if(!in_array($uid, ['273497', 273537])){
            return oo::response(-1,[],'清除失败');
        }
        oo::commonOprRedis("user")->delete(okeys::userDdiamond($uid));
        return oo::response(1);
    }

    /**
     * 金币/体力不足钻石兑换列表
     */
    public function notEnonghExchangeList($param) {
        $uid = oo::functions()->uint($param['uid']);
        $data = oo::commonOprModel('diamond')->notEnoughWithClient($uid);
        return json_encode($data, JSON_NUMERIC_CHECK);
    }

    /**
     * 金币/体力不足钻石兑换列表
     */
    public function notEnonghExchange($param) {
        $uid = oo::functions()->uint($param['uid']);
        $gid = oo::functions()->uint($param['gid']);
        $data = oo::commonOprModel('diamond')->getNotEnoughDiamond($uid, $gid);
        if(empty($data['price'])){
            return oo::response(errorCode::REQUEST_PARAM_ERROR);
        }
        $assetsInfo = oo::commonOprModel('member')->getUserAssetsInfo($uid);
        if($assetsInfo['diamond'] < $data['price']){
            return oo::response(errorCode::DIAMOND_NOT_ENOUGH);
        }

        if(isset($data['gbaseReward']['spins'])){
            $drid = 381;
            $grid = 383;
            $prefix = '体力';
            $str = $prefix.'不足钻石兑换,兑换id:'.$gid;
        }else{
            $drid = 380;
            $grid = 382;
            $prefix = '金币';
            $str = $prefix.'不足钻石兑换,兑换id:'.$gid;
        }

        //扣除钻石
        $ret = oo::commonOprModel('member')->optProperty($uid,['diamond' => $data['price']],'-',$drid,$str);

        if(!$ret){
            oo::logs()->debug3(['uid'=> $uid,'price'=>$data['price'], 'msg' => 'diamond exchange reduce error'],'getDiamond');
            return oo::response(errorCode::SYSTEM_ERROR);
        }

        $reward = oo::commonOprModel('diamond')->getReward($uid, $data['gbaseReward']);
        $ret = oo::commonOprModel('member')->optProperty(
            $uid,$reward,'+',$grid,$prefix.'不足钻石兑换,兑换id:'.$gid
        );
        if(!$ret){
            oo::logs()->debug3(['uid'=> $uid,'price'=>$data['price'], 'msg' => 'diamond exchange add error'],'getDiamond');
            return oo::response(errorCode::DIAMOND_NOT_ENOUGH);
        }
        $diamond = abs($assetsInfo['diamond'] - $data['price']);
        $info = oo::commonOprModel('member')->getUserAssetsInfo($uid);
        return oo::response(errorCode::SUCCESS, ['diamond' => $info['diamond'], 'money' => $info['money'], 'spins' => $info['spins']]);
    }
}
