123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- /**
- * HpVisitorModel.php UTF-8
- * 首页访问表
- *
- * @date : 2018/9/20 15:04
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @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;
- }
- }
|