<?php
/**
*	兑换码相关
*/
defined( 'IN_WEB') or die( 'Include Error!');

class Exchange
{
	
	/**
	* 获取兑换码列表
	*/
	public function getList($param){
	    $start = oo::functions()->uint($param['start']);
	    $length = oo::functions()->uint($param['length']);
		$table = otable::exchangeCode();
        $w="";
        if($param['redeemCode']!=""){
            $w = " where code='".$param['redeemCode']."' ";
        }
		$sql = " SELECT * FROM {$table} {$w} ORDER BY `time` DESC  LIMIT {$start}, {$length}";
		$list = oo::commonOprDb('api')->getAll($sql, MYSQLI_ASSOC);
		$sql2 = " SELECT count(1) as total FROM {$table}";
		$list2 = oo::commonOprDb('api')->getOne($sql2, MYSQLI_ASSOC);
		$data['list'] = $list;
		$data['total'] = $list2['total'];
		return json_encode($data);
	}

	/**
	*	创建兑换码
	*/
	public function createCode($param){
        $reward = $param['reward'];
		$limit  = oo::functions()->uint($param['limit']);
		$day    = oo::functions()->escape($param['day']);
        $type   = oo::functions()->uint($param['userType']);
        $remake = $param['remake'];
        $userLimit = $param['userLimit']??"";
        $rewardArr = json_decode($reward,1);
        if($rewardArr['spins'] > 20000){
            return json_encode(['code'=>-1]);
        }
		return oo::commonOprModel('exchangecode')->createCode($reward, $limit,$day,$type,$remake,$userLimit);
	}

}