IdentifyAgentModel.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * IdentifyAgentModel.php UTF-8
  4. * https://doc.huosdk.com/web/#/170?page_id=13606
  5. * 渠道实名信息表
  6. *
  7. * @date : 2020/06/20 11:47
  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 think\Cache;
  18. class IdentifyAgentModel extends CommonModel {
  19. protected $name = 'identify_agent';
  20. protected $pk = 'id';
  21. protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_AGENT_PREFIX;
  22. /* 开启自动写入时间戳字段 */
  23. protected $autoWriteTimestamp = true;
  24. /**
  25. * 获取单条记录缓存key
  26. *
  27. * @param int $id ID
  28. *
  29. * @return string :string
  30. */
  31. protected function getSingleCacheKey($id) {
  32. return $this->cache_key_prefix.$id;
  33. }
  34. /**
  35. * 根据渠道id获取缓存key
  36. *
  37. * @param int $agent_id 渠道id
  38. *
  39. * @return string :string
  40. */
  41. protected function getCacheKeyByAgentId($agent_id) {
  42. return $this->cache_key_prefix.'agent_'.$agent_id;
  43. }
  44. /**
  45. * 添加数据
  46. *
  47. * @param array $data 需要添加的数据
  48. *
  49. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  50. */
  51. public function addData($data) {
  52. $_data = $data;
  53. $_id = parent::addData($_data);
  54. if (false === $_id) {
  55. return false;
  56. }
  57. return $_id;
  58. }
  59. /**
  60. * 通过ID获取信息
  61. *
  62. * @param int $id 主键ID
  63. *
  64. * @return array
  65. */
  66. public function getInfoById($id) {
  67. /* 缓存操作 */
  68. $_single_cache_key = $this->getSingleCacheKey($id);
  69. $_data = Cache::get($_single_cache_key);
  70. if (!empty($_data)) {
  71. return $_data;
  72. }
  73. $_data = parent::getInfoById($id);
  74. if (empty($_data)) {
  75. return [];
  76. }
  77. Cache::set($_single_cache_key, $_data);
  78. return $_data;
  79. }
  80. /**
  81. * 更新单条数据
  82. *
  83. * @param array $data 数据
  84. * @param int $id ID
  85. *
  86. * @return bool
  87. */
  88. public function updateData($data, $id) {
  89. $_old_data = $this->getInfoById($id);
  90. $_map[$this->pk] = $id;
  91. $_data = $data;
  92. $_rs = $this->allowField(true)->isUpdate(true)->save($_data, $_map);
  93. if (false === $_rs) {
  94. return false;
  95. }
  96. /* 缓存操作 */
  97. $_single_cache_key = $this->getSingleCacheKey($id);
  98. Cache::rm($_single_cache_key);
  99. if (isset($_data['agent_id']) && $_old_data['agent_id'] != $_data['agent_id']) {
  100. /* 更新了渠道id 需要删除旧缓存 */
  101. $_cache_key = $this->getCacheKeyByAgentId($_old_data['agent_id']);
  102. Cache::rm($_cache_key);
  103. }
  104. return true;
  105. }
  106. /**
  107. * 删除单条数据
  108. *
  109. * @param int $id ID
  110. * @param bool $is_complete 是否完成删除
  111. *
  112. * @return bool
  113. */
  114. public function deleteData($id, $is_complete = true) {
  115. $_old_data = $this->getInfoById($id);
  116. $_rs = parent::deleteData($id, $is_complete);
  117. if (false == $_rs) {
  118. return false;
  119. }
  120. /* 缓存操作 */
  121. $_single_cache_key = $this->getSingleCacheKey($id);
  122. Cache::rm($_single_cache_key);
  123. /* 删除渠道id 缓存 */
  124. $_cache_key = $this->getCacheKeyByAgentId($_old_data['agent_id']);
  125. Cache::rm($_cache_key);
  126. return $_rs;
  127. }
  128. /**
  129. * 根据渠道id获取id
  130. *
  131. * @param int $agent_id 渠道id
  132. *
  133. * @return int
  134. */
  135. public function getIdByAgentId($agent_id) {
  136. $_cache_key = $this->getCacheKeyByAgentId($agent_id);
  137. $_id = Cache::get($_cache_key);
  138. if (!empty($_id)) {
  139. return $_id;
  140. }
  141. $_map = [
  142. 'agent_id' => $agent_id
  143. ];
  144. $_id = $this->where($_map)->value('id');
  145. if (empty($_id)) {
  146. return CommonConst::CONST_ZERO;
  147. }
  148. Cache::set($_cache_key, $_id);
  149. return $_id;
  150. }
  151. /**
  152. * 根据渠道id获取实名信息
  153. *
  154. * @param int $agent_id 渠道id
  155. *
  156. * @return array
  157. */
  158. public function getInfoByAgentId($agent_id) {
  159. $_id = $this->getIdByAgentId($agent_id);
  160. return $this->getInfoById($_id);
  161. }
  162. /**
  163. * 根据渠道id获取证件号
  164. *
  165. * @param $agent_id
  166. *
  167. * @return mixed|string
  168. */
  169. public function getIdCardByAgentId($agent_id) {
  170. $_info = $this->getInfoByAgentId($agent_id);
  171. return get_val($_info, 'id_card', '');
  172. }
  173. /**
  174. * 根据证件号获取渠道id
  175. *
  176. * @param $id_card
  177. *
  178. * @return array
  179. */
  180. public function getAgentIdsByIdCard($id_card) {
  181. $_map = [
  182. 'id_card' => $id_card
  183. ];
  184. $_agent_ids = $this->where($_map)->column('agent_id');
  185. return $_agent_ids;
  186. }
  187. /**
  188. * 根据证件号获取数量
  189. *
  190. * @param $id_card
  191. *
  192. * @param $real_name
  193. *
  194. * @return int|string
  195. */
  196. public function getCntByIdCardRealName($id_card, $real_name) {
  197. $_map = [
  198. 'id_card' => $id_card,
  199. 'real_name' => $real_name,
  200. ];
  201. $_cnt = $this->getCnt($_map);
  202. return $_cnt;
  203. }
  204. }