IdentifyIdotLogic.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * IdentifyIdotLogic.php UTF-8
  4. * 实名信息在线时长统计逻辑处理
  5. *
  6. * @date : 2019/11/29 17:43
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK 8.5
  11. */
  12. namespace huoIdentify\logic;
  13. use huo\model\common\CommonModel;
  14. use huoIdentify\model\IdentifyIdotModel;
  15. use huolib\constant\CacheConst;
  16. use huolib\constant\CommonConst;
  17. use huolib\tool\Time;
  18. use think\Cache;
  19. class IdentifyIdotLogic extends CommonModel {
  20. protected $update_time = CommonConst::MINUTE_SECONDS_10; //写入数据库间隔时间 10分钟 60s
  21. protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_IDOT_PREFIX; //缓存前缀
  22. /***
  23. * 根据id_card和日期获取缓存key
  24. *
  25. * @param string $id_card 证件号
  26. * @param string $date 日期 年月日 空者获取今日日期
  27. *
  28. * @return string
  29. */
  30. public function getCacheKeyByIdCardDate($id_card, $date = '') {
  31. $_date = date('Ymd');
  32. if (!empty($date)) {
  33. $_date = date('Ymd', strtotime($date));
  34. }
  35. $_cache_key = $this->cache_key_prefix.$id_card.$_date;
  36. return $_cache_key;
  37. }
  38. /**
  39. * 保存缓存数据
  40. *
  41. * @param $key
  42. * @param $data
  43. */
  44. public function saveCacheData($key, $data) {
  45. $_expire = CommonConst::CONST_7_DAY_SECONDS; //缓存7天
  46. Cache::set($key, $data, $_expire);
  47. }
  48. /***
  49. * 根据id_card获取统计数据
  50. *
  51. * @param string $id_card 证件号
  52. *
  53. * @return mixed
  54. */
  55. public function getInfoByIdCard($id_card) {
  56. if (empty($id_card)) {
  57. return [];
  58. }
  59. $_cache_key = $this->getCacheKeyByIdCardDate($id_card);
  60. $_data = Cache::get($_cache_key);
  61. if (!empty($_data)) {
  62. return $_data;
  63. }
  64. $_model = new IdentifyIdotModel();
  65. $_info = $_model->getInfoByIdCard($id_card);
  66. if (empty($_info)) {
  67. $_data = [
  68. 'id_card' => $id_card
  69. ];
  70. $_model->addData($_data);
  71. $_info = $_model->getInfoByIdCard($id_card);
  72. }
  73. $_last_update_date = date('Ymd', $_info['update_time']);
  74. $_today_date = date('Ymd');
  75. if ($_last_update_date != $_today_date) {
  76. /* 如果上次更新时间不是今天,则清空今日数据 */
  77. $_info['day_online_duration'] = CommonConst::CONST_ZERO;
  78. $_info['day_money'] = CommonConst::CONST_ZERO;
  79. list($_week_start_time, $_week_end_time) = Time::week();
  80. if ($_week_start_time > $_data['update_time']) {
  81. /* 上次更新时间是上一周则清空本周数据 */
  82. $_info['week_online_duration'] = CommonConst::CONST_ZERO;
  83. $_info['week_money'] = CommonConst::CONST_ZERO;
  84. }
  85. list($_month_start_time, $_mont_end_time) = Time::month();
  86. if ($_month_start_time > $_data['update_time']) {
  87. /* 下次时间上一月则清空本月数据 */
  88. $_info['month_online_duration'] = CommonConst::CONST_ZERO;
  89. $_info['month_money'] = CommonConst::CONST_ZERO;
  90. }
  91. }
  92. $_next_update_time = time() + $this->update_time;
  93. $_info['next_update_time'] = $_next_update_time;
  94. $this->saveCacheData($_cache_key, $_info);
  95. return $_info;
  96. }
  97. /**
  98. * 根据id_card更新统计数据
  99. *
  100. * @param array $data 更新数据
  101. * @param string $id_card 证件号
  102. *
  103. * @return bool
  104. */
  105. public function updateByIdCard($data, $id_card) {
  106. $_cache_key = $this->getCacheKeyByIdCardDate($id_card);
  107. $_old_data = $this->getInfoByIdCard($id_card);
  108. $_data = array_merge($_old_data, $data);
  109. $_time = time();
  110. $_update_time = $_data['next_update_time']; //获取当前缓存的更新时间 入库失败则回退
  111. $_data['update_time'] = $_time;
  112. $this->saveCacheData($_cache_key, $_data); //先更新缓存
  113. if ($_update_time <= $_time) {
  114. $_update_data = $_data;
  115. $_data['next_update_time'] = $_time + $this->update_time;
  116. $_next_date = date('Ymd', $_data['next_update_time']);
  117. $_today_date = date('Ymd');
  118. if ($_next_date != $_today_date) {
  119. /* 如果下次更新时间不是今天,则清空今日数据 */
  120. $_update_data['day_online_duration'] = CommonConst::CONST_ZERO;
  121. $_update_data['day_money'] = CommonConst::CONST_ZERO;
  122. list($_week_start_time, $_week_end_time) = Time::week();
  123. if ($_week_end_time < $_data['next_update_time']) {
  124. /* 下次时间是下一周则清空本周数据 */
  125. $_update_data['week_online_duration'] = CommonConst::CONST_ZERO;
  126. $_update_data['week_money'] = CommonConst::CONST_ZERO;
  127. }
  128. list($_month_start_time, $_mont_end_time) = Time::month();
  129. if ($_mont_end_time < $_data['next_update_time']) {
  130. /* 下次时间是下一月则清空本月数据 */
  131. $_update_data['month_online_duration'] = CommonConst::CONST_ZERO;
  132. $_update_data['month_money'] = CommonConst::CONST_ZERO;
  133. }
  134. }
  135. $_rs = (new IdentifyIdotModel())->updateByIdCard($_update_data, $id_card);
  136. if (false === $_rs) {
  137. /* 入库失败 */
  138. $_data['next_update_time'] = $_update_time; //还原更新时间
  139. }
  140. $this->saveCacheData($_cache_key, $_data); //更新缓存
  141. }
  142. return true;
  143. }
  144. /**
  145. * 更新时长
  146. *
  147. * @param int $time 时长
  148. * @param string $id_card 证件号
  149. *
  150. * @return bool
  151. */
  152. public function updateOnlineTimeByIdCard($time, $id_card) {
  153. $_data = $this->getInfoByIdCard($id_card);
  154. $_data['day_online_duration'] += $time;
  155. $_data['week_online_duration'] += $time;
  156. $_data['month_online_duration'] += $time;
  157. $_data['online_duration'] += $time;
  158. $_rs = $this->updateByIdCard($_data, $id_card);
  159. return $_rs;
  160. }
  161. /**
  162. * 更新金额
  163. *
  164. * @param float $money 金额
  165. * @param string $id_card 证件号
  166. *
  167. * @return bool
  168. */
  169. public function updateSumMoneyByIdCard($money, $id_card) {
  170. $_data = $this->getInfoByIdCard($id_card);
  171. $_data['day_money'] += $money;
  172. $_data['week_money'] += $money;
  173. $_data['month_money'] += $money;
  174. $_data['sum_money'] += $money;
  175. $_rs = $this->updateByIdCard($_data, $id_card);
  176. return $_rs;
  177. }
  178. }