* @version : HuoMp 1.0 */ namespace huomp\model\homepage; use huolib\constant\CacheConst; use huomp\model\common\CommonModel; use think\Cache; class HpVisitorModel extends CommonModel { protected $table = 'mp_hp_visitor'; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; protected $cache_key_prefix = CacheConst::CACHE_HOME_VISITOR_PREFIX; public function visitor() { return $this->belongsTo('huo\model\member\MemberModel', 'mem_id', 'id')->setEagerlyType(0); } /** * 添加数据 * * @param $data * * @return bool */ public function addData($data) { if (empty($data)) { return false; } $_data = $data; $_obj = self::create($_data, true); if ($_obj) { return true; } return false; } /** * 更新数据 * * @param int $parent_mem_id * @param int $mem_id * * @return bool */ public function replaceData($parent_mem_id, $mem_id) { $_data = $this->getInfoByMemSub($parent_mem_id, $mem_id); if (!empty($_data)) { $_data['update_time'] = time(); return $this->updateData($_data, $_data['id']); } else { $_data['parent_mem_id'] = $parent_mem_id; $_data['mem_id'] = $mem_id; $_data['create_time'] = time(); $_data['update_time'] = $_data['create_time']; return $this->addData($_data); } } /** * 更新数据 * * @param array $data 数据 * @param int $id ID * * @return bool */ public function updateData($data, $id) { $_map['id'] = $id; $_data = $data; $_rs = self::update($_data, $_map, true); if (false == $_rs) { return false; } $_cache_data = $this->getInfoByMemSub($_data['parent_mem_id'], $_data['mem_id']); $_cache_data = array_merge($_cache_data, $_data); $_cache_key = $this->cache_key_prefix.$data['parent_mem_id'].$data['mem_id']; Cache::set($_cache_key, $_cache_data); return true; } /** * 获取Banner图 * * @param int $parent_mem_id * @param int $mem_id * * @return array|bool|false */ public function getInfoByMemSub($parent_mem_id, $mem_id) { $_cache_key = $this->cache_key_prefix.$parent_mem_id.$mem_id; $_data = Cache::get($_cache_key); if (!empty($_data)) { return $_data; } $_map['parent_mem_id'] = $parent_mem_id; $_map['mem_id'] = $mem_id; $_info = $this->where($_map)->find(); if (false === $_info) { return false; } if (is_object($_info)) { $_info = $_info->toArray(); } Cache::set($_cache_key, $_info); return $_info; } }