* @version : HUOOA 1.0 */ namespace huo\model\agent; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use think\Cache; class AgentCpaLogModel extends CommonModel { protected $name = 'agent_cpa_log'; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; protected $cache_key_prefix = CacheConst::CACHE_AGENT_CPA_LOG_PREFIX; /** * 获取单条记录缓存key * * @param int $id ID * * @return string */ protected function getSingleCacheKey($id) { return $this->cache_key_prefix.$id; } /** * 获取单条记录缓存key * * @param $ip * * @return string */ protected function getCacheKeyByIp($ip) { if (false == is_int($ip)) { $ip = ip2long($ip); } return $this->cache_key_prefix.'ip_'.$ip; } /** * 添加数据 * * @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 false; } $_id = $_model->getLastInsID(); /* TAG缓存操作 */ return $_id; } /** * 通过ID获取信息 * * @param int $id 主键ID * * @return array|false */ 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 $id ID * * @return bool */ public function updateData($data, $id) { $_old_data = $this->getInfoById($id); $_data = $data; $_map = ['id' => $id]; $_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); $_cache_key = $this->getCacheKeyByIp($_old_data['ip']); Cache::rm($_cache_key); /* TAG缓存操作 */ return true; } /** * 删除单条数据 * * @param int $id ID * @param bool $is_complete 是否完成删除 * * @return bool */ public function deleteData($id, $is_complete = true) { $_old_data = $this->getInfoById($id); $_map['id'] = $id; $_rs = $this->useGlobalScope(false)->where($_map)->delete(); if (false == $_rs) { return false; } /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($id); Cache::rm($_single_cache_key); $_cache_key = $this->getCacheKeyByIp($_old_data['ip']); Cache::rm($_cache_key); return $_rs; } /*** * 根据玩家id获取id * * @param $mem_id * * @return int */ public function getIdByMemId($mem_id) { if (empty($mem_id)) { return CommonConst::CONST_ZERO; } $_map = [ 'mem_id' => $mem_id ]; return $this->where($_map)->value('id'); } /** * 根据ip获取id * * @param $ip * * @return mixed */ public function getIdByIp($ip) { $_cache_key = $this->getCacheKeyByIp($ip); $_id = Cache::get($_cache_key); if (!empty($_id)) { return $_id; } $_map = [ 'ip' => $ip ]; $_id = $this->where($_map)->value('id'); if (empty($_id)) { return CommonConst::CONST_ZERO; } Cache::set($_cache_key, $_id, CommonConst::CONST_DAY_SECONDS); return $_id; } }