* @version : HuoMp 1.0 */ namespace huomp\model\fill; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use huomp\model\common\CommonModel; use think\Cache; class MemFakerModel extends CommonModel { protected $cache_key_prefix = CacheConst::CACHE_MEM_RANK_FAKER_PREFIX; protected $tag = CacheConst::CACHE_MEM_RANK_FAKER_TAG; protected $table = 'mp_mem_faker'; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; /** * @param array $data * * @return bool|mixed */ public function addData($data = []) { if ($_obj = self::create($data, true)) { Cache::clear($this->tag); return $_obj->id; } else { return false; } } /** * @param array $data * @param int $id * * @return bool|mixed */ public function updateData($data, $id) { $_map['id'] = $id; $_data = $data; $_rs = self::update($_data, $_map, true); if (false === $_rs) { return false; } else { $_cache_key = $this->cache_key_prefix.$id; Cache::rm($_cache_key); return true; } } /** * @param int $id * * @return array|false */ public function getDetail($id) { $_cache_key = $this->cache_key_prefix.$id; $_data = Cache::get($_cache_key); if (!empty($_data)) { return json_decode($_data, true); } $_map['id'] = $id; $_data = $this->where($_map)->find(); if (empty($_data)) { return []; } if (is_object($_data)) { $_data = $_data->toArray(); } if (!empty($_data)) { Cache::tag($this->tag, $_cache_key)->set($_cache_key, json_encode($_data)); } return $_data; } /** * 获取列表 * * @param array $ids * * @return array */ public function getList($ids = []) { $_map = []; if (!empty($ids)) { $_map['id'] = ['in', $ids]; } $_map['is_delete'] = CommonConst::CONST_NOT_DELETE; return self::where($_map)->select()->toArray(); } /** * 获取头像列表 */ public function getAvatars() { $_cache_key = $this->cache_key_prefix.'avatars'; $_data = Cache::get($_cache_key); if (!empty($_data)) { return json_decode($_data, true); } $_avatars = $this->column('avatar'); if (!empty($_avatars)) { Cache::tag($this->tag, $_cache_key)->set($_cache_key, json_encode($_avatars)); } return $_avatars; } }