IdentifyMotLogic.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * IdentifyMotLogic.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\IdentifyMotModel;
  15. use huolib\constant\CacheConst;
  16. use huolib\constant\CommonConst;
  17. use think\Cache;
  18. class IdentifyMotLogic extends CommonModel {
  19. protected $update_time = CommonConst::MINUTE_SECONDS_10; //写入数据库间隔时间 10分钟 60s
  20. protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_MOT_PREFIX; //缓存前缀
  21. /***
  22. * 根据玩家id和日期获取缓存key
  23. *
  24. * @param int $mem_id 玩家id
  25. * @param string $date 日期 年月日 空者获取今日日期
  26. *
  27. * @return string
  28. */
  29. public function getCacheKeyByMemId($mem_id, $date = '') {
  30. $_date = date('Ymd');
  31. if (!empty($date)) {
  32. $_date = date('Ymd', strtotime($date));
  33. }
  34. $_cache_key = $this->cache_key_prefix.$mem_id.$_date;
  35. return $_cache_key;
  36. }
  37. /**
  38. * 保存缓存数据
  39. *
  40. * @param $key
  41. * @param $data
  42. */
  43. public function saveCacheData($key, $data) {
  44. $_expire = CommonConst::CONST_7_DAY_SECONDS; //缓存7天
  45. Cache::set($key, $data, $_expire);
  46. }
  47. /***
  48. * 根据id_card获取统计数据
  49. *
  50. * @param int $mem_id 玩家id
  51. *
  52. * @return mixed
  53. */
  54. public function getInfoByMemId($mem_id) {
  55. if (empty($mem_id)) {
  56. return [];
  57. }
  58. $_cache_key = $this->getCacheKeyByMemId($mem_id);
  59. $_data = Cache::get($_cache_key);
  60. if (!empty($_data)) {
  61. return $_data;
  62. }
  63. $_model = new IdentifyMotModel();
  64. $_info = $_model->getInfoByMemId($mem_id);
  65. if (empty($_info)) {
  66. $_data = [
  67. 'mem_id' => $mem_id
  68. ];
  69. $_model->addData($_data);
  70. $_info = $_model->getInfoByMemId($mem_id);
  71. }
  72. $_next_update_time = time() + $this->update_time;
  73. $_info['next_update_time'] = $_next_update_time;
  74. $this->saveCacheData($_cache_key, $_info);
  75. return $_info;
  76. }
  77. /**
  78. * 根据id_card更新统计数据
  79. *
  80. * @param array $data 更新数据
  81. * @param int $mem_id 玩家id
  82. *
  83. * @return bool
  84. */
  85. public function updateByMemId($data, $mem_id) {
  86. $_cache_key = $this->getCacheKeyByMemId($mem_id);
  87. $_time = time();
  88. $_old_data = $this->getInfoByMemId($mem_id);
  89. $_data = array_merge($_old_data, $data);
  90. $_update_time = $_data['next_update_time']; //获取当前缓存的更新时间 入库失败则回退
  91. $_data['update_time'] = $_time;
  92. $this->saveCacheData($_cache_key, $_data); //先更新缓存
  93. if ($_update_time <= $_time) {
  94. $_data['next_update_time'] = $_time + $this->update_time;
  95. $_rs = (new IdentifyMotModel())->updateByMemId($_data, $mem_id);
  96. if (false === $_rs) {
  97. /* 入库失败 */
  98. $_data['next_update_time'] = $_update_time; //还原更新时间
  99. }
  100. $this->saveCacheData($_cache_key, $_data); //更新缓存
  101. }
  102. return true;
  103. }
  104. /**
  105. * 更新时长
  106. *
  107. * @param int $time 时长
  108. * @param int $mem_id 玩家id
  109. *
  110. * @return bool
  111. */
  112. public function updateOnlineTimeByMemId($time, $mem_id) {
  113. $_data = $this->getInfoByMemId($mem_id);
  114. $_data['online_duration'] += $time;
  115. $_rs = $this->updateByMemId($_data, $mem_id);
  116. return $_rs;
  117. }
  118. }