* @version : HuoMp 1.0 */ namespace huomp\model\agent; use huolib\constant\CacheConst; use huomp\model\common\CommonModel; use think\Cache; class AgentAdsCfgModel extends CommonModel { protected $name = 'agent_ads_cfg'; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; protected $cache_key_prefix = CacheConst::CACHE_AGENT_ADS_CFG_PREFIX; /** * 添加数据 * * @param $data * * @return bool */ public function addData($data) { if (empty($data) || empty($data['agent_id'])) { return false; } $_data = $data; $_obj = self::create($_data, true); if ($_obj) { return true; } return false; } /** * 更新数据 * * @param array $data 数据 * @param int $id 应用ID * * @return bool */ public function updateData($data, $id) { $_old_data = $this->getInfoById($id); $_map['id'] = $id; $_data = $data; $_rs = $this->allowField(true)->isUpdate(true)->save($_data, $_map); if (false === $_rs) { return false; } $_cache_key = $this->getCacheKeyByAgentId($_old_data['agent_id']); Cache::rm($_cache_key); return true; } /*** * 获取数据 * * @param $agent_id * * @return mixed */ public function getInfoByAgentId($agent_id) { $_cache_key = $this->getCacheKeyByAgentId($agent_id); $_data = Cache::get($_cache_key); if (!empty($_data)) { return $_data; } $_map = ['agent_id' => $agent_id]; $_data = $this->where($_map)->find(); if (is_object($_data)) { $_data = $_data->toArray(); } if (!empty($_data)) { Cache::set($_cache_key, $_data); } return $_data; } /** * 删除数据 * * @param $id * * @return bool */ public function deleteData($id) { $_old_data = $this->getInfoById($id); $_map['id'] = $id; $_rs = $this->where($_map)->delete(); if (false == $_rs) { return false; } $_cache_key = $this->getCacheKeyByAgentId($_old_data['agent_id']); Cache::rm($_cache_key); return true; } /** * 获取缓存key * * @param $agent_id * * @return string */ public function getCacheKeyByAgentId($agent_id) { return $this->cache_key_prefix.$agent_id; } }