* @version : HUOSDK 8.0 */ namespace huo\model\game; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use huolib\constant\OrderConst; use think\Cache; class GameServerSwitchModel extends CommonModel { protected $name = 'game_server_switch'; protected $autoWriteTimestamp = true; protected $key = CacheConst::CACHE_GAME_SERVER_PAY_SWITCH_PREFIX; /** * 获取缓存key * * @param $app_id * @param $server_id * * @return string */ protected function getKey($app_id, $server_id) { $_key = $this->key.$app_id.'_'.$server_id; return $_key; } public function addData($data) { $_data = []; $_data['app_id'] = get_val($data, 'app_id', ''); $_data['server_id'] = get_val($data, 'server_id', 0); $_data['server_name'] = get_val($data, 'server_name', ''); $_data['is_switch'] = get_val($data, 'is_switch', OrderConst::PAY_SWITCH_YES); if ($_obj = self::create($_data, true)) { $_data['id'] = $_obj->id; $_key = $this->getKey($_data['app_id'], $_data['server_id']); Cache::set($_key, json_encode($_data), CommonConst::CONST_DAY_SECONDS); return $_data; } else { return false; } } /** * 获取详情 * * @param $app_id * @param $server_id * * @return mixed */ public function getInfo($app_id, $server_id) { $_key = $this->getKey($app_id, $server_id); $_data = Cache::get($_key); if (!empty($_data)) { return json_decode($_data, true); } $_map = [ 'app_id' => $app_id, 'server_id' => $server_id, ]; $_data = $this->where($_map)->find(); if (is_object($_data)) { $_data = $_data->toArray(); } if (empty($_data)) { return []; } Cache::set($_key, json_encode($_data), CommonConst::CONST_DAY_SECONDS); return $_data; } /** * 修改信息 * * @param $app_id * @param $server_id * @param $data * * @return bool */ public function updateDataGss($app_id, $server_id, $data) { $_key = $this->getKey($app_id, $server_id); $_data = $this->getInfo($app_id, $server_id); $_data = array_merge($_data, $data); $_map = [ 'app_id' => $app_id, 'server_id' => $server_id, ]; $_rs = $this->where($_map)->update($_data); if (false == $_rs) { return false; } Cache::set($_key, json_encode($_data), CommonConst::CONST_DAY_SECONDS); return true; } /** * 根据ID更新数据 * * @param $id * @param $data * * @return bool */ public function updateById($id, $data) { $_data = parent::getInfoById($id); $_data = array_merge($_data, $data); $_map = ['id' => $id]; $_rs = $this->where($_map)->update($_data); if (false == $_rs) { return false; } $_key = $this->getKey($_data['app_id'], $_data['server_id']); Cache::set($_key, json_encode($_data), CommonConst::CONST_DAY_SECONDS); return true; } /** * 获取所有区服id_name * * @param $app_id * * @return array */ public function getIdName($app_id) { $_map = [ 'app_id' => $app_id, ]; $_array = $this->where($_map)->column('server_name', 'id'); return $_array; } /** * 获取标记为不切换的id * * @param $app_id * * @return array */ public function getSwitchNo($app_id) { $_map = [ 'app_id' => $app_id, 'is_switch' => OrderConst::PAY_SWITCH_NO ]; $_array = $this->where($_map)->column('id'); return $_array; } }