123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace huoIdentify\logic;
- use huo\model\common\CommonModel;
- use huoIdentify\model\IdentifyDotModel;
- use huolib\constant\CacheConst;
- use huolib\constant\CommonConst;
- use think\Cache;
- class IdentifyDotLogic extends CommonModel {
- protected $update_time = CommonConst::MINUTE_SECONDS_10;
- protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_DOT_PREFIX;
-
- public function getCacheKeyByDeviceDate($device_id, $date = '') {
- $_date = date('Ymd');
- if (!empty($date)) {
- $_date = date('Ymd', strtotime($date));
- }
- $_cache_key = $this->cache_key_prefix.$device_id.$_date;
- return $_cache_key;
- }
-
- public function saveCacheData($key, $data) {
- $_expire = CommonConst::CONST_7_DAY_SECONDS;
- Cache::set($key, $data, $_expire);
- }
-
- public function getInfoByDevice($device_id) {
- if (empty($device_id)) {
- return [];
- }
- $_cache_key = $this->getCacheKeyByDeviceDate($device_id);
- $_data = Cache::get($_cache_key);
- if (!empty($_data)) {
- return $_data;
- }
- $_model = new IdentifyDotModel();
- $_info = $_model->getInfoByDevice($device_id);
- if (empty($_info)) {
- $_data = [
- 'device_id' => $device_id
- ];
- $_model->addData($_data);
- $_info = $_model->getInfoByDevice($device_id);
- }
- $_next_update_time = time() + $this->update_time;
- $_info['next_update_time'] = $_next_update_time;
- $this->saveCacheData($_cache_key, $_info);
- return $_info;
- }
-
- public function updateByDevice($data, $device_id) {
- $_cache_key = $this->getCacheKeyByDeviceDate($device_id);
- $_old_data = $this->getInfoByDevice($device_id);
- $_data = array_merge($_old_data, $data);
- $_time = time();
- $_update_time = $_data['next_update_time'];
- $_data['update_time'] = $_time;
- $this->saveCacheData($_cache_key, $_data);
- if ($_update_time <= $_time) {
- $_data['next_update_time'] = $_time + $this->update_time;
- $_rs = (new IdentifyDotModel())->updateByDevice($_data, $device_id);
- if (false === $_rs) {
-
- $_data['next_update_time'] = $_update_time;
- }
- $this->saveCacheData($_cache_key, $_data);
- }
- return true;
- }
-
- public function updateOnlineTimeByDevice($time, $device_id) {
- $_data = $this->getInfoByDevice($device_id);
- $_data['online_duration'] += $time;
- $_rs = $this->updateByDevice($_data, $device_id);
- return $_rs;
- }
- }
|