IdentifyMemModel.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * IdentifyMemModel.php UTF-8
  4. * http://doc.huosdk.com/web/#/170?page_id=10980
  5. * 玩家实名信息表
  6. *
  7. * @date : 2019/11/29 15:37
  8. *
  9. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  10. * @author : chenbingling <cbl@huosdk.com>
  11. * @version : HUOSDK 8.5
  12. */
  13. namespace huoIdentify\model;
  14. use huo\model\common\CommonModel;
  15. use huolib\constant\CacheConst;
  16. use huolib\constant\CommonConst;
  17. use huolib\constant\IdentifyConst;
  18. use think\Cache;
  19. class IdentifyMemModel extends CommonModel {
  20. protected $name = 'identify_mem';
  21. protected $pk = 'id';
  22. protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_MEM_PREFIX;
  23. /* 开启自动写入时间戳字段 */
  24. protected $autoWriteTimestamp = true;
  25. protected $type
  26. = [
  27. 'id' => 'integer',
  28. 'mem_id' => 'integer',
  29. 'identify_type' => 'integer',
  30. 'real_name' => 'string',
  31. 'id_card' => 'string',
  32. 'identify_pi' => 'string',
  33. 'identify_from' => 'string',
  34. 'create_time' => 'timestamp',
  35. 'update_time' => 'timestamp',
  36. ];
  37. /**
  38. * 获取单条记录缓存key
  39. *
  40. * @param int $id ID
  41. *
  42. * @return string :string
  43. */
  44. protected function getSingleCacheKey($id) {
  45. return $this->cache_key_prefix.$id;
  46. }
  47. /**
  48. * 根据玩家id获取缓存key
  49. *
  50. * @param int $mem_id 玩家id
  51. *
  52. * @return string :string
  53. */
  54. protected function getCacheKeyByMemId($mem_id) {
  55. return $this->cache_key_prefix.'mem_'.$mem_id;
  56. }
  57. /**
  58. * 根据身份证号获取数量缓存key
  59. *
  60. * @param string $id_card 身份证号
  61. *
  62. * @return string :string
  63. */
  64. protected function getCntCacheKeyByIdCard($id_card) {
  65. return $this->cache_key_prefix.'cntc_'.$id_card;
  66. }
  67. /**
  68. * 获取玩家实名信息上报认证状态缓存key
  69. *
  70. * @param $mem_id
  71. * @param string $id_card 身份证号
  72. *
  73. * @return string :string
  74. */
  75. protected function getReportStatusCacheKeyByMemIdIdCard($mem_id, $id_card) {
  76. return $this->cache_key_prefix.'rs_m_'.$mem_id.'_ic_'.$id_card;
  77. }
  78. /**
  79. * 添加数据
  80. *
  81. * @param array $data 需要添加的数据
  82. *
  83. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  84. */
  85. public function addData($data) {
  86. $_data = $data;
  87. $_id = parent::addData($_data);
  88. if (false === $_id) {
  89. return false;
  90. }
  91. if (!empty($_data['mem_id'])) {
  92. $_cache_key = $this->getCacheKeyByMemId($_data['mem_id']);
  93. Cache::set($_cache_key, $_id);
  94. }
  95. if (!empty($_data['id_card'])) {
  96. $_cache_key = $this->getCntCacheKeyByIdCard($_data['id_card']);
  97. Cache::rm($_cache_key);
  98. }
  99. return $_id;
  100. }
  101. /**
  102. * 通过ID获取信息
  103. *
  104. * @param int $id 主键ID
  105. *
  106. * @return array
  107. */
  108. public function getInfoById($id) {
  109. /* 缓存操作 */
  110. $_single_cache_key = $this->getSingleCacheKey($id);
  111. $_data = Cache::get($_single_cache_key);
  112. if (!empty($_data)) {
  113. return $_data;
  114. }
  115. $_data = parent::getInfoById($id);
  116. if (empty($_data)) {
  117. return [];
  118. }
  119. Cache::set($_single_cache_key, $_data);
  120. return $_data;
  121. }
  122. /**
  123. * 更新单条数据
  124. *
  125. * @param array $data 数据
  126. * @param int $id ID
  127. *
  128. * @return bool
  129. */
  130. public function updateData($data, $id) {
  131. $_old_data = $this->getInfoById($id);
  132. $_map[$this->pk] = $id;
  133. $_data = $data;
  134. $_rs = $this->allowField(true)->isUpdate(true)->save($_data, $_map);
  135. if (false === $_rs) {
  136. return false;
  137. }
  138. /* 缓存操作 */
  139. $_single_cache_key = $this->getSingleCacheKey($id);
  140. Cache::rm($_single_cache_key);
  141. if (isset($_data['mem_id']) && $_old_data['mem_id'] != $_data['mem_id']) {
  142. /* 更新了玩家id 需要删除旧缓存 */
  143. $_cache_key = $this->getCacheKeyByMemId($_old_data['mem_id']);
  144. Cache::rm($_cache_key);
  145. }
  146. if (isset($_data['id_card']) && $_old_data['id_card'] != $_data['id_card']) {
  147. $_cache_key = $this->getCntCacheKeyByIdCard($_data['id_card']);
  148. Cache::rm($_cache_key);
  149. $_cache_key = $this->getCntCacheKeyByIdCard($_old_data['id_card']);
  150. Cache::rm($_cache_key);
  151. }
  152. return true;
  153. }
  154. /**
  155. * 删除单条数据
  156. *
  157. * @param int $id ID
  158. * @param bool $is_complete 是否完成删除
  159. *
  160. * @return bool
  161. */
  162. public function deleteData($id, $is_complete = true) {
  163. $_old_data = $this->getInfoById($id);
  164. $_rs = parent::deleteData($id, $is_complete);
  165. if (false == $_rs) {
  166. return false;
  167. }
  168. /* 缓存操作 */
  169. $_single_cache_key = $this->getSingleCacheKey($id);
  170. Cache::rm($_single_cache_key);
  171. /* 删除玩家id 缓存 */
  172. $_cache_key = $this->getCacheKeyByMemId($_old_data['mem_id']);
  173. Cache::rm($_cache_key);
  174. $_cache_key = $this->getCntCacheKeyByIdCard($_old_data['id_card']);
  175. Cache::rm($_cache_key);
  176. return $_rs;
  177. }
  178. /**
  179. * 根据玩家id获取id
  180. *
  181. * @param int $mem_id 玩家id
  182. *
  183. * @return int
  184. */
  185. public function getIdByMemId($mem_id) {
  186. $_cache_key = $this->getCacheKeyByMemId($mem_id);
  187. if (Cache::has($_cache_key)) {
  188. return Cache::get($_cache_key);
  189. }
  190. $_map = [
  191. 'mem_id' => $mem_id
  192. ];
  193. $_id = $this->where($_map)->value('id');
  194. if (empty($_id)) {
  195. $_id = CommonConst::CONST_ZERO;
  196. }
  197. Cache::set($_cache_key, $_id);
  198. return $_id;
  199. }
  200. /**
  201. * 根据玩家id获取实名信息
  202. *
  203. * @param int $mem_id 玩家id
  204. *
  205. * @return array
  206. */
  207. public function getInfoByMemId($mem_id) {
  208. $_id = $this->getIdByMemId($mem_id);
  209. return $this->getInfoById($_id);
  210. }
  211. /**
  212. * 根据玩家id获取证件号
  213. *
  214. * @param $mem_id
  215. *
  216. * @return mixed|string
  217. */
  218. public function getIdCardByMemId($mem_id) {
  219. $_info = $this->getInfoByMemId($mem_id);
  220. return get_val($_info, 'id_card', '');
  221. }
  222. /**
  223. * 根据证件号获取玩家id
  224. *
  225. * @param $id_card
  226. *
  227. * @return array
  228. */
  229. public function getMemIdsByIdCard($id_card) {
  230. $_map = [
  231. 'id_card' => $id_card
  232. ];
  233. $_mem_ids = $this->where($_map)->column('mem_id');
  234. return $_mem_ids;
  235. }
  236. /**
  237. * 根据证件号获取数量
  238. *
  239. * @param $id_card
  240. *
  241. * @return int|string
  242. */
  243. public function getCntByIdCard($id_card) {
  244. $_cache_key = $this->getCntCacheKeyByIdCard($id_card);
  245. if (Cache::has($_cache_key)) {
  246. return Cache::get($_cache_key);
  247. }
  248. $_map = [
  249. 'id_card' => $id_card
  250. ];
  251. $_cnt = $this->getCnt($_map);
  252. Cache::set($_cache_key, $_cnt);
  253. return $_cnt;
  254. }
  255. /**
  256. * 根据证件号获取信息
  257. *
  258. * @param $id_card
  259. *
  260. * @return array
  261. */
  262. public function getInfoByIdCard($id_card) {
  263. $_map = ['id_card' => $id_card];
  264. $_id = $this->where($_map)->order('identify_pi DESC, id ASC')->value('id');
  265. if (empty($_id)) {
  266. return [];
  267. }
  268. return $this->getInfoById($_id);
  269. }
  270. /**
  271. * 设置上报实名失败
  272. *
  273. * @param $mem_id
  274. * @param $id_card
  275. *
  276. * @return mixed
  277. */
  278. public function setReportFailByMemIdIdCard($mem_id, $id_card) {
  279. $_rs_cache_key = $this->getReportStatusCacheKeyByMemIdIdCard($mem_id, $id_card);
  280. return Cache::set($_rs_cache_key, IdentifyConst::IDENTITY_STATUS_NO);
  281. }
  282. /**
  283. * 获取上报实名状态
  284. *
  285. * @param $mem_id
  286. * @param $id_card
  287. *
  288. * @return mixed
  289. */
  290. public function getReportStatusByMemIdIdCard($mem_id, $id_card) {
  291. $_rs_cache_key = $this->getReportStatusCacheKeyByMemIdIdCard($mem_id, $id_card);
  292. $_status = Cache::get($_rs_cache_key);
  293. /* 获取后清理 */
  294. Cache::rm($_rs_cache_key);
  295. return $_status;
  296. }
  297. /**
  298. * 关联玩家
  299. */
  300. public function mem() {
  301. $_field = [
  302. 'id',
  303. 'username'
  304. ];
  305. return $this->belongsTo('\huoIdentify\model\MemberModel', 'mem_id', 'id')->field($_field);
  306. }
  307. /**
  308. * 通过条件获取数量
  309. *
  310. * @param array $where 条件
  311. *
  312. * @return int
  313. */
  314. public function getCnt($where = []) {
  315. $_map = $where;
  316. $_cnt = $this->useGlobalScope(false)->where($_map)->count();
  317. if (empty($_cnt)) {
  318. return 0;
  319. }
  320. return $_cnt;
  321. }
  322. }