* @version : HUOSDK 8.5 */ namespace huoIdentify\logic; use huo\model\common\CommonModel; use huoIdentify\model\IdentifyIdotModel; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use huolib\tool\Time; use think\Cache; class IdentifyIdotLogic extends CommonModel { protected $update_time = CommonConst::MINUTE_SECONDS_10; //写入数据库间隔时间 10分钟 60s protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_IDOT_PREFIX; //缓存前缀 /*** * 根据id_card和日期获取缓存key * * @param string $id_card 证件号 * @param string $date 日期 年月日 空者获取今日日期 * * @return string */ public function getCacheKeyByIdCardDate($id_card, $date = '') { $_date = date('Ymd'); if (!empty($date)) { $_date = date('Ymd', strtotime($date)); } $_cache_key = $this->cache_key_prefix.$id_card.$_date; return $_cache_key; } /** * 保存缓存数据 * * @param $key * @param $data */ public function saveCacheData($key, $data) { $_expire = CommonConst::CONST_7_DAY_SECONDS; //缓存7天 Cache::set($key, $data, $_expire); } /*** * 根据id_card获取统计数据 * * @param string $id_card 证件号 * * @return mixed */ public function getInfoByIdCard($id_card) { if (empty($id_card)) { return []; } $_cache_key = $this->getCacheKeyByIdCardDate($id_card); $_data = Cache::get($_cache_key); if (!empty($_data)) { return $_data; } $_model = new IdentifyIdotModel(); $_info = $_model->getInfoByIdCard($id_card); if (empty($_info)) { $_data = [ 'id_card' => $id_card ]; $_model->addData($_data); $_info = $_model->getInfoByIdCard($id_card); } $_last_update_date = date('Ymd', $_info['update_time']); $_today_date = date('Ymd'); if ($_last_update_date != $_today_date) { /* 如果上次更新时间不是今天,则清空今日数据 */ $_info['day_online_duration'] = CommonConst::CONST_ZERO; $_info['day_money'] = CommonConst::CONST_ZERO; list($_week_start_time, $_week_end_time) = Time::week(); if ($_week_start_time > $_data['update_time']) { /* 上次更新时间是上一周则清空本周数据 */ $_info['week_online_duration'] = CommonConst::CONST_ZERO; $_info['week_money'] = CommonConst::CONST_ZERO; } list($_month_start_time, $_mont_end_time) = Time::month(); if ($_month_start_time > $_data['update_time']) { /* 下次时间上一月则清空本月数据 */ $_info['month_online_duration'] = CommonConst::CONST_ZERO; $_info['month_money'] = CommonConst::CONST_ZERO; } } $_next_update_time = time() + $this->update_time; $_info['next_update_time'] = $_next_update_time; $this->saveCacheData($_cache_key, $_info); return $_info; } /** * 根据id_card更新统计数据 * * @param array $data 更新数据 * @param string $id_card 证件号 * * @return bool */ public function updateByIdCard($data, $id_card) { $_cache_key = $this->getCacheKeyByIdCardDate($id_card); $_old_data = $this->getInfoByIdCard($id_card); $_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) { $_update_data = $_data; $_data['next_update_time'] = $_time + $this->update_time; $_next_date = date('Ymd', $_data['next_update_time']); $_today_date = date('Ymd'); if ($_next_date != $_today_date) { /* 如果下次更新时间不是今天,则清空今日数据 */ $_update_data['day_online_duration'] = CommonConst::CONST_ZERO; $_update_data['day_money'] = CommonConst::CONST_ZERO; list($_week_start_time, $_week_end_time) = Time::week(); if ($_week_end_time < $_data['next_update_time']) { /* 下次时间是下一周则清空本周数据 */ $_update_data['week_online_duration'] = CommonConst::CONST_ZERO; $_update_data['week_money'] = CommonConst::CONST_ZERO; } list($_month_start_time, $_mont_end_time) = Time::month(); if ($_mont_end_time < $_data['next_update_time']) { /* 下次时间是下一月则清空本月数据 */ $_update_data['month_online_duration'] = CommonConst::CONST_ZERO; $_update_data['month_money'] = CommonConst::CONST_ZERO; } } $_rs = (new IdentifyIdotModel())->updateByIdCard($_update_data, $id_card); if (false === $_rs) { /* 入库失败 */ $_data['next_update_time'] = $_update_time; //还原更新时间 } $this->saveCacheData($_cache_key, $_data); //更新缓存 } return true; } /** * 更新时长 * * @param int $time 时长 * @param string $id_card 证件号 * * @return bool */ public function updateOnlineTimeByIdCard($time, $id_card) { $_data = $this->getInfoByIdCard($id_card); $_data['day_online_duration'] += $time; $_data['week_online_duration'] += $time; $_data['month_online_duration'] += $time; $_data['online_duration'] += $time; $_rs = $this->updateByIdCard($_data, $id_card); return $_rs; } /** * 更新金额 * * @param float $money 金额 * @param string $id_card 证件号 * * @return bool */ public function updateSumMoneyByIdCard($money, $id_card) { $_data = $this->getInfoByIdCard($id_card); $_data['day_money'] += $money; $_data['week_money'] += $money; $_data['month_money'] += $money; $_data['sum_money'] += $money; $_rs = $this->updateByIdCard($_data, $id_card); return $_rs; } }