<?php
/**
* 任务和成就配置相关
*/
defined('IN_WEB') or die( 'Include Error!');
include_once dirname(__FILE__) . "/Base.php";
class Task extends Base
{
	private $_config; //配置文件

	private $_table;
	private $_cjtable;//成就配置文件

	public function __construct()
	{
		$this->_config = include PATH_CFG.'config.task.php';
		$this->_table = otable::taskConfig();
		$this->_cjtable = otable::achievementConfig();
	}

	/**
	*	获取配置文件
	*/
	public function getConfig()
	{
		$sql = " SELECT * FROM {$this->_table} ORDER BY sort DESC";
        $info = oo::commonOprDb('common')->getAll($sql, 1);
        return json_encode($info);
	}


	/**
	*	获取成就配置文件
	*/
	public function getCjConfig()
	{
		$sql = " SELECT * FROM {$this->_cjtable} ";
        $info = oo::commonOprDb('common')->getAll($sql, 1);
        return json_encode($info);
	}
	
	/**
	*	获取单个记录
	*/
	public function getOne($param)
	{
		$tid = oo::functions()->uint($param['tid']);
		$sql = " SELECT * FROM {$this->_table} WHERE id = {$tid} LIMIT 1 ";
        $info = oo::commonOprDb('common')->getOne($sql, MYSQLI_ASSOC);
        return json_encode($info);
	}

	/**
	*	获取单个记录
	*/
	public function getOnecj($param)
	{
		$tid = oo::functions()->uint($param['tid']);
		$sql = " SELECT * FROM {$this->_cjtable} WHERE id = {$tid} LIMIT 1 ";
        $info = oo::commonOprDb('common')->getOne($sql, MYSQLI_ASSOC);
        return json_encode($info);
	}

	/**
	*	编辑
	*/
	public function editTask($param)
	{
		$tid = oo::functions()->uint($param['tid']);
        $tc_zh_name = oo::functions()->escape($param['tc_zh_name']);
        $tc_name = oo::functions()->escape($param['tc_name']);
        $tc_sort = oo::functions()->uint($param['tc_sort']);
        $tc_goto = oo::functions()->uint($param['tc_goto']);
        $rewardNum = json_decode($param['rewardNum'], 1);
        $rewardMoney = json_decode($param['rewardMoney'], 1);

        $i = 1;
        $tc_subtask = array();
        foreach ($rewardNum as $key => $nums) {
        	$tc_subtask[$i] = array('num' => $nums, 'reward' => array('money' => $rewardMoney[$key]));
        	$i++;
        }
        $tc_subtask = json_encode($tc_subtask);

        $sql = " UPDATE {$this->_table} SET zh_name = '{$tc_zh_name}', name = '{$tc_name}', sort = {$tc_sort}, goto = {$tc_goto}, subtask = '{$tc_subtask}' WHERE id = {$tid} LIMIT 1 ";
        oo::commonOprDb('common')->query($sql);
        $ret = oo::commonOprDb('common')->affectedRows();
        if ($ret) {
        	//删除缓存
        	$key = okeys::taskConfig();
			oo::commonOprRedis('common')->delete($key);
        }

        return json_encode(array('num' => $ret));
	}

	/**
	*	编辑
	*/
	public function editCj($param)
	{
		$tid = oo::functions()->uint($param['tid']);
        $zh_name = oo::functions()->escape($param['zh_name']);
        $name = oo::functions()->escape($param['name']);
        $sort = oo::functions()->uint($param['sort']);
        $rewardNum = json_decode($param['rewardNum'], 1);
        $rewardMoney = json_decode($param['rewardMoney'], 1);

        $i = 1;
        $tc_subtask = array();
        foreach ($rewardNum as $key => $nums) {
        	$tc_subtask[$i] = array($nums, $rewardMoney[$key]);
        	$i++;
        }
        $tc_subtask = json_encode($tc_subtask);

        $sql = " UPDATE {$this->_cjtable} SET zh_name = '{$zh_name}', name = '{$name}', sort = {$sort}, subtask = '{$tc_subtask}' WHERE id = {$tid} LIMIT 1 ";
        oo::commonOprDb('common')->query($sql);
        $ret = oo::commonOprDb('common')->affectedRows();
        if ($ret) {
        	$key = okeys::achievementConfig();
			oo::commonOprRedis('common')->delete($key);
        }

        return json_encode(array('num' => $ret));
	}
}

?>