* @version : HUOSDK 8.5 */ namespace huoIdentify\model; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use think\Cache; class IdentifyAgentModel extends CommonModel { protected $name = 'identify_agent'; protected $pk = 'id'; protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_AGENT_PREFIX; /* 开启自动写入时间戳字段 */ protected $autoWriteTimestamp = true; /** * 获取单条记录缓存key * * @param int $id ID * * @return string :string */ protected function getSingleCacheKey($id) { return $this->cache_key_prefix.$id; } /** * 根据渠道id获取缓存key * * @param int $agent_id 渠道id * * @return string :string */ protected function getCacheKeyByAgentId($agent_id) { return $this->cache_key_prefix.'agent_'.$agent_id; } /** * 添加数据 * * @param array $data 需要添加的数据 * * @return false|int 添加失败返回 false 添加成功 返回添加的ID */ public function addData($data) { $_data = $data; $_id = parent::addData($_data); if (false === $_id) { return false; } 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['agent_id']) && $_old_data['agent_id'] != $_data['agent_id']) { /* 更新了渠道id 需要删除旧缓存 */ $_cache_key = $this->getCacheKeyByAgentId($_old_data['agent_id']); 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->getCacheKeyByAgentId($_old_data['agent_id']); Cache::rm($_cache_key); return $_rs; } /** * 根据渠道id获取id * * @param int $agent_id 渠道id * * @return int */ public function getIdByAgentId($agent_id) { $_cache_key = $this->getCacheKeyByAgentId($agent_id); $_id = Cache::get($_cache_key); if (!empty($_id)) { return $_id; } $_map = [ 'agent_id' => $agent_id ]; $_id = $this->where($_map)->value('id'); if (empty($_id)) { return CommonConst::CONST_ZERO; } Cache::set($_cache_key, $_id); return $_id; } /** * 根据渠道id获取实名信息 * * @param int $agent_id 渠道id * * @return array */ public function getInfoByAgentId($agent_id) { $_id = $this->getIdByAgentId($agent_id); return $this->getInfoById($_id); } /** * 根据渠道id获取证件号 * * @param $agent_id * * @return mixed|string */ public function getIdCardByAgentId($agent_id) { $_info = $this->getInfoByAgentId($agent_id); return get_val($_info, 'id_card', ''); } /** * 根据证件号获取渠道id * * @param $id_card * * @return array */ public function getAgentIdsByIdCard($id_card) { $_map = [ 'id_card' => $id_card ]; $_agent_ids = $this->where($_map)->column('agent_id'); return $_agent_ids; } /** * 根据证件号获取数量 * * @param $id_card * * @param $real_name * * @return int|string */ public function getCntByIdCardRealName($id_card, $real_name) { $_map = [ 'id_card' => $id_card, 'real_name' => $real_name, ]; $_cnt = $this->getCnt($_map); return $_cnt; } }