* @version : HUOSDK-IDENTITY 1.0 */ namespace huoIdentify\model; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use think\Cache; class IdentifyPiModel extends CommonModel { protected $name = 'identify_pi'; protected $pk = 'id'; protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_PI_PREFIX; /* 开启自动写入时间戳字段 */ protected $autoWriteTimestamp = true; protected $type = [ 'id' => 'integer', 'real_name' => 'string', 'id_card' => 'string', 'identify_pi' => 'string', 'identify_from' => 'string', 'create_time' => 'timestamp', ]; /** * 获取单条记录缓存key * * @param int $id ID * * @return string :string */ protected function getSingleCacheKey($id) { return $this->cache_key_prefix.$id; } /** * 根据身份证号获取数量缓存key * * @param string $id_card 身份证号 * * @return string :string */ protected function getCacheKeyByIdCard($id_card) { return $this->cache_key_prefix.'c_'.$id_card; } /** * 根据PI获取缓存key * * @param string $identify_pi 中宣部PI * * @return string :string */ protected function getCacheKeyByIdentifyPi($identify_pi) { return $this->cache_key_prefix.'pi_'.$identify_pi; } /** * 添加数据 * * @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['id_card'])) { $_cache_key = $this->getCacheKeyByIdCard($_data['id_card']); Cache::set($_cache_key, $_id); } if (!empty($_data['identify_pi'])) { $_cache_key = $this->getCacheKeyByIdentifyPi($_data['identify_pi']); Cache::set($_cache_key, $_id); } 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['id_card']) && $_old_data['id_card'] != $_data['id_card']) { /* 更新了身份证号 需要删除旧缓存 */ $_cache_key = $this->getCacheKeyByIdCard($_old_data['id_card']); Cache::rm($_cache_key); } if (isset($_data['identify_pi']) && $_old_data['identify_pi'] != $_data['identify_pi']) { /* 更新了身份证号 需要删除旧缓存 */ $_cache_key = $this->getCacheKeyByIdentifyPi($_old_data['identify_pi']); 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); $_cache_key = $this->getCacheKeyByIdCard($_old_data['id_card']); Cache::rm($_cache_key); $_cache_key = $this->getCacheKeyByIdentifyPi($_old_data['identify_pi']); Cache::rm($_cache_key); return $_rs; } /** * 根据身份证号获取id * * @param string $id_card 身份证号 * * @return int */ public function getIdByIdCard($id_card) { $_cache_key = $this->getCacheKeyByIdCard($id_card); if (Cache::has($_cache_key)) { return Cache::get($_cache_key); } $_map = [ 'id_card' => $id_card ]; $_id = $this->where($_map)->value($this->pk); if (empty($_id)) { $_id = CommonConst::CONST_ZERO; } Cache::set($_cache_key, $_id); return $_id; } /** * 根据身份证号获取实名信息 * * @param string $id_card 身份证号 * * @return array */ public function getInfoByIdCard($id_card) { $_id = $this->getIdByIdCard($id_card); return $this->getInfoById($_id); } /** * 根据PI获取id * * @param string $identify_pi 中宣部PI * * @return int */ public function getIdByIdentifyPi($identify_pi) { $_cache_key = $this->getCacheKeyByIdentifyPi($identify_pi); if (Cache::has($_cache_key)) { return Cache::get($_cache_key); } $_map = [ 'identify_pi' => $identify_pi ]; $_id = $this->where($_map)->value($this->pk); if (empty($_id)) { $_id = CommonConst::CONST_ZERO; } Cache::set($_cache_key, $_id); return $_id; } /** * 根据PI获取实名信息 * * @param string $identify_pi 中宣部PI * * @return array */ public function getInfoByIdentifyPi($identify_pi) { $_id = $this->getIdByIdentifyPi($identify_pi); return $this->getInfoById($_id); } }