* @version : HUOSDK 8.5 */ namespace huoIdentify\model; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use huolib\constant\IdentifyConst; use think\Cache; class IdentifyMemModel extends CommonModel { protected $name = 'identify_mem'; protected $pk = 'id'; protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_MEM_PREFIX; /* 开启自动写入时间戳字段 */ protected $autoWriteTimestamp = true; protected $type = [ 'id' => 'integer', 'mem_id' => 'integer', 'identify_type' => 'integer', 'real_name' => 'string', 'id_card' => 'string', 'identify_pi' => 'string', 'identify_from' => 'string', 'create_time' => 'timestamp', 'update_time' => 'timestamp', ]; /** * 获取单条记录缓存key * * @param int $id ID * * @return string :string */ protected function getSingleCacheKey($id) { return $this->cache_key_prefix.$id; } /** * 根据玩家id获取缓存key * * @param int $mem_id 玩家id * * @return string :string */ protected function getCacheKeyByMemId($mem_id) { return $this->cache_key_prefix.'mem_'.$mem_id; } /** * 根据身份证号获取数量缓存key * * @param string $id_card 身份证号 * * @return string :string */ protected function getCntCacheKeyByIdCard($id_card) { return $this->cache_key_prefix.'cntc_'.$id_card; } /** * 获取玩家实名信息上报认证状态缓存key * * @param $mem_id * @param string $id_card 身份证号 * * @return string :string */ protected function getReportStatusCacheKeyByMemIdIdCard($mem_id, $id_card) { return $this->cache_key_prefix.'rs_m_'.$mem_id.'_ic_'.$id_card; } /** * 添加数据 * * @param array $data 需要添加的数据 * * @return false|int 添加失败返回 false 添加成功 返回添加的ID */ public function addData($data) { $_data = $data; $_id = parent::addData($_data); if (false === $_id) { return false; } if (!empty($_data['mem_id'])) { $_cache_key = $this->getCacheKeyByMemId($_data['mem_id']); Cache::set($_cache_key, $_id); } if (!empty($_data['id_card'])) { $_cache_key = $this->getCntCacheKeyByIdCard($_data['id_card']); Cache::rm($_cache_key); } return $_id; } /** * 通过ID获取信息 * * @param int $id 主键ID * * @return array */ 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); $_map[$this->pk] = $id; $_data = $data; $_rs = $this->allowField(true)->isUpdate(true)->save($_data, $_map); if (false === $_rs) { return false; } /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($id); Cache::rm($_single_cache_key); if (isset($_data['mem_id']) && $_old_data['mem_id'] != $_data['mem_id']) { /* 更新了玩家id 需要删除旧缓存 */ $_cache_key = $this->getCacheKeyByMemId($_old_data['mem_id']); Cache::rm($_cache_key); } if (isset($_data['id_card']) && $_old_data['id_card'] != $_data['id_card']) { $_cache_key = $this->getCntCacheKeyByIdCard($_data['id_card']); Cache::rm($_cache_key); $_cache_key = $this->getCntCacheKeyByIdCard($_old_data['id_card']); Cache::rm($_cache_key); } return true; } /** * 删除单条数据 * * @param int $id ID * @param bool $is_complete 是否完成删除 * * @return bool */ public function deleteData($id, $is_complete = true) { $_old_data = $this->getInfoById($id); $_rs = parent::deleteData($id, $is_complete); if (false == $_rs) { return false; } /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($id); Cache::rm($_single_cache_key); /* 删除玩家id 缓存 */ $_cache_key = $this->getCacheKeyByMemId($_old_data['mem_id']); Cache::rm($_cache_key); $_cache_key = $this->getCntCacheKeyByIdCard($_old_data['id_card']); Cache::rm($_cache_key); return $_rs; } /** * 根据玩家id获取id * * @param int $mem_id 玩家id * * @return int */ public function getIdByMemId($mem_id) { $_cache_key = $this->getCacheKeyByMemId($mem_id); if (Cache::has($_cache_key)) { return Cache::get($_cache_key); } $_map = [ 'mem_id' => $mem_id ]; $_id = $this->where($_map)->value('id'); if (empty($_id)) { $_id = CommonConst::CONST_ZERO; } Cache::set($_cache_key, $_id); return $_id; } /** * 根据玩家id获取实名信息 * * @param int $mem_id 玩家id * * @return array */ public function getInfoByMemId($mem_id) { $_id = $this->getIdByMemId($mem_id); return $this->getInfoById($_id); } /** * 根据玩家id获取证件号 * * @param $mem_id * * @return mixed|string */ public function getIdCardByMemId($mem_id) { $_info = $this->getInfoByMemId($mem_id); return get_val($_info, 'id_card', ''); } /** * 根据证件号获取玩家id * * @param $id_card * * @return array */ public function getMemIdsByIdCard($id_card) { $_map = [ 'id_card' => $id_card ]; $_mem_ids = $this->where($_map)->column('mem_id'); return $_mem_ids; } /** * 根据证件号获取数量 * * @param $id_card * * @return int|string */ public function getCntByIdCard($id_card) { $_cache_key = $this->getCntCacheKeyByIdCard($id_card); if (Cache::has($_cache_key)) { return Cache::get($_cache_key); } $_map = [ 'id_card' => $id_card ]; $_cnt = $this->getCnt($_map); Cache::set($_cache_key, $_cnt); return $_cnt; } /** * 根据证件号获取信息 * * @param $id_card * * @return array */ public function getInfoByIdCard($id_card) { $_map = ['id_card' => $id_card]; $_id = $this->where($_map)->order('identify_pi DESC, id ASC')->value('id'); if (empty($_id)) { return []; } return $this->getInfoById($_id); } /** * 设置上报实名失败 * * @param $mem_id * @param $id_card * * @return mixed */ public function setReportFailByMemIdIdCard($mem_id, $id_card) { $_rs_cache_key = $this->getReportStatusCacheKeyByMemIdIdCard($mem_id, $id_card); return Cache::set($_rs_cache_key, IdentifyConst::IDENTITY_STATUS_NO); } /** * 获取上报实名状态 * * @param $mem_id * @param $id_card * * @return mixed */ public function getReportStatusByMemIdIdCard($mem_id, $id_card) { $_rs_cache_key = $this->getReportStatusCacheKeyByMemIdIdCard($mem_id, $id_card); $_status = Cache::get($_rs_cache_key); /* 获取后清理 */ Cache::rm($_rs_cache_key); return $_status; } /** * 关联玩家 */ public function mem() { $_field = [ 'id', 'username' ]; return $this->belongsTo('\huoIdentify\model\MemberModel', 'mem_id', 'id')->field($_field); } /** * 通过条件获取数量 * * @param array $where 条件 * * @return int */ public function getCnt($where = []) { $_map = $where; $_cnt = $this->useGlobalScope(false)->where($_map)->count(); if (empty($_cnt)) { return 0; } return $_cnt; } }