123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- /**
- * IdentifyPiModel.php UTF-8
- * 实名认证pi表
- *
- * @date : 2021/5/6 10:13
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : chenbingling <cbl@huosdk.com>
- * @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);
- }
- }
|