* @version : HUOSDK-IDENTITY 1.0 */ namespace huoIdentify\model; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use think\Cache; class IdentifyInQueueModel extends CommonModel { protected $name = 'identify_in_queue'; protected $pk = 'id'; /* 开启自动写入时间戳字段 */ protected $autoWriteTimestamp = true; protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_IN_QUEUE_PREFIX; protected $type = [ 'id' => 'integer', 'uid' => 'string', 'channel_code' => 'string', 'real_name' => 'string', 'id_card' => 'string', 'identify_pi' => '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; } /** * 获取单条记录缓存key * * @param $uid * @param $channel_code * @param $id_card * * @return string :string */ protected function getCacheKeyByUidCodeCard($uid, $channel_code, $id_card) { $uid = (string)$uid; $channel_code = (string)$channel_code; $id_card = (string)$id_card; $_array = [$uid, $channel_code, $id_card]; return $this->cache_key_prefix.'unc_'.md5(json_encode($_array)); } /** * 加入数据库 * * @param array $data * * @return bool|int */ public function addData($data) { $_data = $data; $_rs = parent::addData($_data); if (empty($_rs)) { return false; } $_cache_key = $this->getCacheKeyByUidCodeCard( $_data['uid'], $_data['channel_code'], $_data['id_card'] ); Cache::set($_cache_key, $_rs); return $_rs; } /** * 通过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) { $_data = $data; $_rs = parent::updateData($_data, $id); if (false === $_rs) { return false; } /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($id); Cache::rm($_single_cache_key); return true; } /** * 删除单条数据 * * @param $id * @param bool $is_complete * * @return bool|int */ public function deleteData($id, $is_complete = true) { $_old_data = $this->getInfoById($id); $_rs = parent::deleteData($id, $is_complete); if (false !== $_rs) { $_cache_key = $this->getSingleCacheKey($id); Cache::rm($_cache_key); $_cache_key = $this->getCacheKeyByUidCodeCard( $_old_data['uid'], $_old_data['channel_code'], $_old_data['id_card'] ); Cache::set($_cache_key, 0); } return $_rs; } /** * 根据uid real_name id_card 获取id * * @param $uid * @param $channel_code * @param $id_card * * @return int|mixed */ public function getIdByUidCodeCard($uid, $channel_code, $id_card) { $_cache_key = $this->getCacheKeyByUidCodeCard($uid, $channel_code, $id_card); if (Cache::has($_cache_key)) { return Cache::get($_cache_key); } $_map = [ 'uid' => $uid, 'channel_code' => $channel_code, 'id_card' => $id_card, ]; $_id = $this->where($_map)->value($this->pk); if (empty($_id)) { $_id = 0; } Cache::set($_cache_key, $_id); return $_id; } }