123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- /**
- * AgentGameSwitchModel.php UTF-8
- * 渠道游戏切量控制
- *
- * @date : 2019/11/11 15:27
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : chenbingling <cbl@huosdk.com>
- * @version : HUOSDK 8.5
- */
- namespace huoAgentSwitch\model;
- use huo\model\common\CommonModel;
- use huolib\constant\CacheConst;
- use huolib\constant\CommonConst;
- use think\Cache;
- class AgentGameSwitchModel extends CommonModel {
- protected $name = 'agent_game_switch';
- protected $cache_key_prefix = CacheConst::CACHE_AGENT_GAME_SWITCH_PREFIX;
- /**
- * 获取单条记录缓存key
- *
- * @param int $id ID
- *
- * @return string
- */
- protected function getSingleCacheKey($id) {
- return $this->cache_key_prefix.$id;
- }
- /**
- * 根据mem_id 获取缓存key
- *
- * @param int $agent_id 渠道id
- * @param int $app_id 游戏id
- *
- * @return string
- */
- public function getCacheKeyByAgentApp($agent_id, $app_id) {
- $_key = $this->cache_key_prefix.'agent_'.$agent_id.'_app_'.$app_id;
- return $_key;
- }
- /**
- * 添加数据
- *
- * @param array $data 需要添加的数据
- *
- * @return false|int 添加失败返回 false 添加成功 返回添加的ID
- */
- public function addData($data) {
- if (empty($data)) {
- return false;
- }
- $_data = $data;
- $_model = new static();
- $_rs = $_model->allowField(true)->isUpdate(false)->save($_data, []);
- if (false !== $_rs) {
- return $_model->getLastInsID();
- }
- return false;
- }
- /**
- * 通过ID获取信息
- *
- * @param int $id 主键ID
- *
- * @return array
- */
- public function getInfoById($id) {
- /* 缓存操作 */
- $_single_cache_key = $this->getSingleCacheKey($id);
- $_data = Cache::get($_single_cache_key);
- if (!empty($_data)) {
- return $_data;
- }
- $_data = parent::getInfoById($id);
- if (empty($_data)) {
- return [];
- }
- Cache::set($_single_cache_key, $_data);
- return $_data;
- }
- /**
- * 更新数据
- *
- * @param array $data 数据
- * @param int|array $id 主集ID
- *
- * @return bool
- */
- public function updateData($data, $id) {
- $_old_data = $this->getInfoById($id);
- $_map = ['id' => $id];
- $_data = $data;
- $_model = new static();
- $_rs = $_model->allowField(true)->isUpdate(true)->save($_data, $_map);
- if (false === $_rs) {
- return false;
- }
- /* 缓存操作 */
- $_single_cache_key = $this->getSingleCacheKey($id);
- Cache::rm($_single_cache_key);
- if ((isset($data['app_id']) && $data['app_id'] != $_old_data['app_id'])
- || (isset($data['agent_id']) && $data['agent_id'] != $_old_data['agent_id'])) {
- $_cache_key = $this->getCacheKeyByAgentApp($_old_data['agent_id'], $_old_data['app_id']);
- Cache::rm($_cache_key);
- }
- return true;
- }
- /**
- * 删除单条数据 渠道数据不能删除
- *
- * @param int $id ID
- *
- * @param bool $is_complete
- *
- * @return bool
- */
- public function deleteData($id, $is_complete = true) {
- $_old_data = $this->getInfoById($id);
- $_rs = parent::deleteData($id, $is_complete);
- if (false == $_rs) {
- return false;
- }
- /* 缓存操作 */
- $_single_cache_key = $this->getSingleCacheKey($id);
- Cache::rm($_single_cache_key);
- $_cache_key = $this->getCacheKeyByAgentApp($_old_data['agent_id'], $_old_data['app_id']);
- Cache::rm($_cache_key);
- return $_rs;
- }
- /**
- * 根据渠道和游戏获取id
- *
- * @param int $agent_id 渠道id
- * @param int $app_id 游戏id
- *
- * @return int|mixed
- */
- public function getIdByAgentApp($agent_id, $app_id) {
- $_cache_key = $this->getCacheKeyByAgentApp($agent_id, $app_id);
- $_id = Cache::get($_cache_key);
- if (!empty($_id)) {
- return $_id;
- }
- $_map = ['agent_id' => $agent_id, 'app_id' => $app_id];
- $_id = $this->where($_map)->value('id');
- if (empty($_id)) {
- return CommonConst::CONST_ZERO;
- }
- Cache::set($_cache_key, $_id);
- return $_id;
- }
- /**
- * 根据渠道和游戏获取数据
- *
- * @param int $agent_id 渠道id
- * @param int $app_id 游戏id
- *
- * @return array
- */
- public function getInfoByAgentApp($agent_id, $app_id) {
- $_id = $this->getIdByAgentApp($agent_id, $app_id);
- return $this->getInfoById($_id);
- }
- }
|