* @version : HUOSDK 9.0 */ namespace huoIdentify\model; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use think\Cache; class IdentifyGameModel extends CommonModel { protected $name = 'identify_game'; protected $pk = 'id'; /* 开启自动写入时间戳字段 */ protected $autoWriteTimestamp = true; protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_GAME_PREFIX; protected $type = [ 'id' => 'integer', 'app_id' => 'integer', 'app_key' => 'string', 'app_secret' => 'string', 'name' => 'string', 'ext_info' => 'array', 'create_time' => 'timestamp', 'update_time' => 'timestamp', ]; /** * 获取单条记录缓存key * * @param int $id ID * * @return string :string */ protected function getSingleCacheKey($id) { return $this->cache_key_prefix.$id; } public function getCacheKeyByAppId($app_id) { return $this->cache_key_prefix.'_ai_'.$app_id; } /** * 通过ID获取信息 * * @param int $id 主键ID * * @return array */ public function getInfoById($id) { $_data = parent::getInfoById($id); return $_data; } /** * 加入数据库 * * @param array $data * * @return bool|int */ public function addData($data) { $_data = $data; $_rs = parent::addData($_data); if (empty($_rs)) { return false; } return $_rs; } /** * 更新单条数据 * * @param array $data 数据 * @param int $id ID * * @return bool */ public function updateData($data, $id) { $_data = $data; $_old_data = $this->getInfoById($id); $_ext_info = get_val($_old_data, 'ext_info', []); $_ext_info = array_merge($_ext_info, $_data['ext_info'] ?? []); $_data = array_merge($_old_data, $_data); $_data['ext_info'] = $_ext_info; $_rs = parent::updateData($_data, $id); if (false === $_rs) { return false; } return true; } /** * 删除单条数据 * * @param $id * @param bool $is_complete * * @return bool|int */ public function deleteData($id, $is_complete = true) { $_old_data = $this->getInfoById($id); $_rs = parent::deleteData($id, $is_complete); if (false !== $_rs) { $_cache_key = $this->getSingleCacheKey($id); Cache::rm($_cache_key); $_cache_key = $this->getCacheKeyByAppId($_old_data['app_id']); Cache::rm($_cache_key); } return $_rs; } /** * 通过app_id获取ID * * @param string $app_id app_id * * @return int */ public function getIdByAppId($app_id) { $_cache_key = $this->getCacheKeyByAppId($app_id); $_id = Cache::get($_cache_key); if (!empty($_id)) { return $_id; } /* 取默认 */ $_map = [ 'app_id' => $app_id ]; $_id = $this->where($_map)->value('id'); if (empty($_id)) { $_id = 0; } Cache::set($_cache_key, $_id); return $_id; } public function getInfoByAppId($app_id) { $_id = $this->getIdByAppId($app_id); if (empty($_id)) { return []; } return $this->getInfoById($_id); } /** * 通过ID获取app_key * * @param int $id ID * * @return string */ public function getAppKeyById($id) { $_data = $this->getInfoById($id); return get_val($_data, 'app_key', ''); } /** * 通过ID获取name * * @param int $id ID * * @return string */ public function getNameById($id) { $_data = $this->getInfoById($id); return get_val($_data, 'name', ''); } /** * 通过ID获取app_secret * * @param int $id ID * * @return string */ public function getAppSecretById($id) { $_data = $this->getInfoById($id); return get_val($_data, 'app_secret', ''); } /** * 通过ID获取app_id * * @param int $id ID * * @return string */ public function getAppIdById($id) { $_data = $this->getInfoById($id); return get_val($_data, 'app_id', 0); } /** * 关联应用 */ public function game() { $_field = [ 'id', 'apple_id', 'name', 'icon', 'classify' ]; return $this->belongsTo('\huoIdentify\model\GameModel', 'app_id', 'id')->field($_field); } public function getIdsByWhere($where, $column = 'app_id') { return $this->where($where)->column($column); } public function getList($where = [], $field = [], $order = 'id desc') { $_list = $this->where($where)->field($field)->order($order)->select(); if (is_object($_list)) { $_list = $_list->toArray(); } return $_list; } }