IdentifyDayDotLogic.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * IdentifyDayDotLogic.php UTF-8
  4. * 设备每日在线时长统计逻辑处理
  5. *
  6. * @date : 2019/11/29 21:40
  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\IdentifyDayDotModel;
  15. use huolib\constant\CacheConst;
  16. use huolib\constant\CommonConst;
  17. use think\Cache;
  18. class IdentifyDayDotLogic extends CommonModel {
  19. protected $update_time = CommonConst::MINUTE_SECONDS_10; //写入数据库间隔时间 10分钟 60s
  20. protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_DAY_DOT_PREFIX; //缓存前缀
  21. /**
  22. * 获取缓存key
  23. *
  24. * @param string $date 日期
  25. * @param int $mem_id 玩家ID
  26. * @param string $device_id 设备ID android 为imei ios 为idfa h5 为 永久cookie标识 huoh5开头标识
  27. *
  28. * @return string
  29. */
  30. protected function getCacheKeyByDateMemDevice($date, $mem_id, $device_id) {
  31. $date = date('Ymd', strtotime($date));
  32. return $this->cache_key_prefix.$date.$mem_id.$device_id;
  33. }
  34. /**
  35. * 保存缓存数据
  36. *
  37. * @param $key
  38. * @param $data
  39. */
  40. public function saveCacheData($key, $data) {
  41. $_expire = CommonConst::CONST_7_DAY_SECONDS; //缓存7天
  42. Cache::set($key, $data, $_expire);
  43. }
  44. /**
  45. * 获取信息
  46. *
  47. * @param string $date 日期
  48. * @param int $mem_id 玩家ID
  49. * @param string $device_id 设备ID android 为imei ios 为idfa h5 为 永久cookie标识 huoh5开头标识
  50. *
  51. * @return array|false|mixed
  52. */
  53. public function getInfoByDateMemDevice($date, $mem_id, $device_id) {
  54. $date = date('Y-m-d', strtotime($date));
  55. /* 缓存操作 */
  56. $_cache_key = $this->getCacheKeyByDateMemDevice($date, $mem_id, $device_id);
  57. $_data = Cache::get($_cache_key);
  58. if (!empty($_data)) {
  59. return $_data;
  60. }
  61. $_idd_model = new IdentifyDayDotModel();
  62. $_data = $_idd_model->getInfoByDateMemDevice($date, $mem_id, $device_id);
  63. if (empty($_data)) {
  64. $_data = [
  65. 'date' => $date,
  66. 'device_id' => $device_id,
  67. 'mem_id' => $mem_id
  68. ];
  69. $_idd_model->addData($_data);
  70. $_data = $_idd_model->getInfoByDateMemDevice($date, $mem_id, $device_id);
  71. }
  72. $_next_update_time = time() + $this->update_time;
  73. $_data['next_update_time'] = $_next_update_time;
  74. $this->saveCacheData($_cache_key, $_data);
  75. return $_data;
  76. }
  77. /**
  78. * 更新信息
  79. *
  80. * @param array $data 更新数据
  81. * @param string $date 日期
  82. * @param int $mem_id 玩家ID
  83. * @param string $device_id 设备ID android 为imei ios 为idfa h5 为 永久cookie标识 huoh5开头标识
  84. *
  85. * @return bool
  86. */
  87. public function updateDataByDateMemDevice($data, $date, $mem_id, $device_id) {
  88. $_cache_key = $this->getCacheKeyByDateMemDevice($date, $mem_id, $device_id);
  89. $_old_data = $this->getInfoByDateMemDevice($date, $mem_id, $device_id);
  90. $_data = array_merge($_old_data, $data);
  91. $_time = time();
  92. $_update_time = $_data['next_update_time']; //获取当前缓存的更新时间 入库失败则回退
  93. $_data['update_time'] = $_time;
  94. $this->saveCacheData($_cache_key, $_data); //先更新缓存
  95. if ($_update_time <= $_time) {
  96. $_data['next_update_time'] = $_time + $this->update_time;
  97. $_rs = (new IdentifyDayDotModel())->updateData($_data, $_data['id']);
  98. if (false !== $_rs) {
  99. /* 入库失败 */
  100. $_data['next_update_time'] = $_update_time; //还原更新时间
  101. }
  102. $this->saveCacheData($_cache_key, $_data); //更新缓存
  103. }
  104. return true;
  105. }
  106. /**
  107. * 更新时长
  108. *
  109. * @param int $time 时长
  110. * @param string $device_id 设备ID android 为imei ios 为idfa h5 为 永久cookie标识 huoh5开头标识
  111. * @param int $mem_id 玩家id
  112. *
  113. * @return bool
  114. */
  115. public function updateOnlineTimeByDevice($time, $device_id, $mem_id) {
  116. $_date = date('Y-m-d');
  117. $_data = $this->getInfoByDateMemDevice($_date, $mem_id, $device_id);
  118. $_data['online_duration'] += $time;
  119. $_rs = $this->updateDataByDateMemDevice($_data, $_date, $mem_id, $device_id);
  120. return $_rs;
  121. }
  122. }