IdentifyDayIdotLogic.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * IdentifyDayIdotLogic.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\IdentifyDayIdotModel;
  15. use huolib\constant\CacheConst;
  16. use huolib\constant\CommonConst;
  17. use think\Cache;
  18. class IdentifyDayIdotLogic extends CommonModel {
  19. protected $update_time = CommonConst::MINUTE_SECONDS_10; //写入数据库间隔时间 10分钟 60s
  20. protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_DAY_IDOT_PREFIX; //缓存前缀
  21. /**
  22. * 获取缓存key
  23. *
  24. * @param string $date 日期
  25. * @param int $mem_id 玩家ID
  26. * @param string $id_card 证件号
  27. *
  28. * @return string
  29. */
  30. protected function getCacheKeyByDateMemIdCard($date, $mem_id, $id_card) {
  31. $date = date('Ymd', strtotime($date));
  32. return $this->cache_key_prefix.$date.$mem_id.$id_card;
  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 $id_card 证件号
  50. *
  51. * @return array|false
  52. */
  53. public function getInfoByDateMemIdCard($date, $mem_id, $id_card) {
  54. $date = date('Y-m-d', strtotime($date));
  55. /* 缓存操作 */
  56. $_cache_key = $this->getCacheKeyByDateMemIdCard($date, $mem_id, $id_card);
  57. $_data = Cache::get($_cache_key);
  58. if (!empty($_data)) {
  59. return $_data;
  60. }
  61. $_idi_model = new IdentifyDayIdotModel();
  62. $_data = $_idi_model->getInfoByDateMemIdCard($date, $mem_id, $id_card);
  63. if (empty($_data)) {
  64. $_data = [
  65. 'date' => $date,
  66. 'id_card' => $id_card,
  67. 'mem_id' => $mem_id
  68. ];
  69. $_idi_model->addData($_data);
  70. $_data = $_idi_model->getInfoByDateMemIdCard($date, $mem_id, $id_card);
  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 $id_card 证件号
  84. *
  85. * @return bool
  86. */
  87. public function updateDataByDateMemIdCard($data, $date, $mem_id, $id_card) {
  88. $_cache_key = $this->getCacheKeyByDateMemIdCard($date, $mem_id, $id_card);
  89. $_old_data = $this->getInfoByDateMemIdCard($date, $mem_id, $id_card);
  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 IdentifyDayIdotModel())->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 $id_card 证件号
  111. * @param int $mem_id 玩家ID
  112. *
  113. * @return bool
  114. */
  115. public function updateOnlineTimeByIdCard($time, $id_card, $mem_id) {
  116. $_date = date('Y-m-d');
  117. $_data = $this->getInfoByDateMemIdCard($_date, $mem_id, $id_card);
  118. $_data['online_duration'] += $time;
  119. $_rs = $this->updateDataByDateMemIdCard($_data, $_date, $mem_id, $id_card);
  120. return $_rs;
  121. }
  122. /**
  123. * 更新金额
  124. *
  125. * @param float $money 金额
  126. * @param string $id_card 证件号
  127. * @param int $mem_id 玩家ID
  128. *
  129. * @return bool
  130. */
  131. public function updateSumMoneyByIdCard($money, $id_card, $mem_id) {
  132. $_date = date('Y-m-d');
  133. $_data = $this->getInfoByDateMemIdCard($_date, $mem_id, $id_card);
  134. $_data['sum_money'] += $money;
  135. $_rs = $this->updateDataByDateMemIdCard($_data, $_date, $mem_id, $id_card);
  136. return $_rs;
  137. }
  138. }