123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- 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;
-
- protected function getSingleCacheKey($id) {
- return $this->cache_key_prefix.$id;
- }
-
- protected function getCacheKeyByIp($ip) {
- if (false == is_int($ip)) {
- $ip = ip2long($ip);
- }
- return $this->cache_key_prefix.'ip_'.$ip;
- }
-
- 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();
-
- return $_id;
- }
-
- 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;
- }
-
- 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);
-
- return true;
- }
-
- 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;
- }
-
- public function getIdByMemId($mem_id) {
- if (empty($mem_id)) {
- return CommonConst::CONST_ZERO;
- }
- $_map = [
- 'mem_id' => $mem_id
- ];
- return $this->where($_map)->value('id');
- }
-
- 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;
- }
- }
|