IdentifyPiModel.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * IdentifyPiModel.php UTF-8
  4. * 实名认证pi表
  5. *
  6. * @date : 2021/5/6 10:13
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK-IDENTITY 1.0
  11. */
  12. namespace huoIdentify\model;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\CacheConst;
  15. use huolib\constant\CommonConst;
  16. use think\Cache;
  17. class IdentifyPiModel extends CommonModel {
  18. protected $name = 'identify_pi';
  19. protected $pk = 'id';
  20. protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_PI_PREFIX;
  21. /* 开启自动写入时间戳字段 */
  22. protected $autoWriteTimestamp = true;
  23. protected $type
  24. = [
  25. 'id' => 'integer',
  26. 'real_name' => 'string',
  27. 'id_card' => 'string',
  28. 'identify_pi' => 'string',
  29. 'identify_from' => 'string',
  30. 'create_time' => 'timestamp',
  31. ];
  32. /**
  33. * 获取单条记录缓存key
  34. *
  35. * @param int $id ID
  36. *
  37. * @return string :string
  38. */
  39. protected function getSingleCacheKey($id) {
  40. return $this->cache_key_prefix.$id;
  41. }
  42. /**
  43. * 根据身份证号获取数量缓存key
  44. *
  45. * @param string $id_card 身份证号
  46. *
  47. * @return string :string
  48. */
  49. protected function getCacheKeyByIdCard($id_card) {
  50. return $this->cache_key_prefix.'c_'.$id_card;
  51. }
  52. /**
  53. * 根据PI获取缓存key
  54. *
  55. * @param string $identify_pi 中宣部PI
  56. *
  57. * @return string :string
  58. */
  59. protected function getCacheKeyByIdentifyPi($identify_pi) {
  60. return $this->cache_key_prefix.'pi_'.$identify_pi;
  61. }
  62. /**
  63. * 添加数据
  64. *
  65. * @param array $data 需要添加的数据
  66. *
  67. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  68. */
  69. public function addData($data) {
  70. $_data = $data;
  71. $_id = parent::addData($_data);
  72. if (false === $_id) {
  73. return false;
  74. }
  75. if (!empty($_data['id_card'])) {
  76. $_cache_key = $this->getCacheKeyByIdCard($_data['id_card']);
  77. Cache::set($_cache_key, $_id);
  78. }
  79. if (!empty($_data['identify_pi'])) {
  80. $_cache_key = $this->getCacheKeyByIdentifyPi($_data['identify_pi']);
  81. Cache::set($_cache_key, $_id);
  82. }
  83. return $_id;
  84. }
  85. /**
  86. * 通过ID获取信息
  87. *
  88. * @param int $id 主键ID
  89. *
  90. * @return array
  91. */
  92. public function getInfoById($id) {
  93. /* 缓存操作 */
  94. $_single_cache_key = $this->getSingleCacheKey($id);
  95. $_data = Cache::get($_single_cache_key);
  96. if (!empty($_data)) {
  97. return $_data;
  98. }
  99. $_data = parent::getInfoById($id);
  100. if (empty($_data)) {
  101. return [];
  102. }
  103. Cache::set($_single_cache_key, $_data);
  104. return $_data;
  105. }
  106. /**
  107. * 更新单条数据
  108. *
  109. * @param array $data 数据
  110. * @param int $id ID
  111. *
  112. * @return bool
  113. */
  114. public function updateData($data, $id) {
  115. $_old_data = $this->getInfoById($id);
  116. $_map[$this->pk] = $id;
  117. $_data = $data;
  118. $_rs = $this->allowField(true)->isUpdate(true)->save($_data, $_map);
  119. if (false === $_rs) {
  120. return false;
  121. }
  122. /* 缓存操作 */
  123. $_single_cache_key = $this->getSingleCacheKey($id);
  124. Cache::rm($_single_cache_key);
  125. if (isset($_data['id_card']) && $_old_data['id_card'] != $_data['id_card']) {
  126. /* 更新了身份证号 需要删除旧缓存 */
  127. $_cache_key = $this->getCacheKeyByIdCard($_old_data['id_card']);
  128. Cache::rm($_cache_key);
  129. }
  130. if (isset($_data['identify_pi']) && $_old_data['identify_pi'] != $_data['identify_pi']) {
  131. /* 更新了身份证号 需要删除旧缓存 */
  132. $_cache_key = $this->getCacheKeyByIdentifyPi($_old_data['identify_pi']);
  133. Cache::rm($_cache_key);
  134. }
  135. return true;
  136. }
  137. /**
  138. * 删除单条数据
  139. *
  140. * @param int $id ID
  141. * @param bool $is_complete 是否完成删除
  142. *
  143. * @return bool
  144. */
  145. public function deleteData($id, $is_complete = true) {
  146. $_old_data = $this->getInfoById($id);
  147. $_rs = parent::deleteData($id, $is_complete);
  148. if (false == $_rs) {
  149. return false;
  150. }
  151. /* 缓存操作 */
  152. $_single_cache_key = $this->getSingleCacheKey($id);
  153. Cache::rm($_single_cache_key);
  154. $_cache_key = $this->getCacheKeyByIdCard($_old_data['id_card']);
  155. Cache::rm($_cache_key);
  156. $_cache_key = $this->getCacheKeyByIdentifyPi($_old_data['identify_pi']);
  157. Cache::rm($_cache_key);
  158. return $_rs;
  159. }
  160. /**
  161. * 根据身份证号获取id
  162. *
  163. * @param string $id_card 身份证号
  164. *
  165. * @return int
  166. */
  167. public function getIdByIdCard($id_card) {
  168. $_cache_key = $this->getCacheKeyByIdCard($id_card);
  169. if (Cache::has($_cache_key)) {
  170. return Cache::get($_cache_key);
  171. }
  172. $_map = [
  173. 'id_card' => $id_card
  174. ];
  175. $_id = $this->where($_map)->value($this->pk);
  176. if (empty($_id)) {
  177. $_id = CommonConst::CONST_ZERO;
  178. }
  179. Cache::set($_cache_key, $_id);
  180. return $_id;
  181. }
  182. /**
  183. * 根据身份证号获取实名信息
  184. *
  185. * @param string $id_card 身份证号
  186. *
  187. * @return array
  188. */
  189. public function getInfoByIdCard($id_card) {
  190. $_id = $this->getIdByIdCard($id_card);
  191. return $this->getInfoById($_id);
  192. }
  193. /**
  194. * 根据PI获取id
  195. *
  196. * @param string $identify_pi 中宣部PI
  197. *
  198. * @return int
  199. */
  200. public function getIdByIdentifyPi($identify_pi) {
  201. $_cache_key = $this->getCacheKeyByIdentifyPi($identify_pi);
  202. if (Cache::has($_cache_key)) {
  203. return Cache::get($_cache_key);
  204. }
  205. $_map = [
  206. 'identify_pi' => $identify_pi
  207. ];
  208. $_id = $this->where($_map)->value($this->pk);
  209. if (empty($_id)) {
  210. $_id = CommonConst::CONST_ZERO;
  211. }
  212. Cache::set($_cache_key, $_id);
  213. return $_id;
  214. }
  215. /**
  216. * 根据PI获取实名信息
  217. *
  218. * @param string $identify_pi 中宣部PI
  219. *
  220. * @return array
  221. */
  222. public function getInfoByIdentifyPi($identify_pi) {
  223. $_id = $this->getIdByIdentifyPi($identify_pi);
  224. return $this->getInfoById($_id);
  225. }
  226. }