69 lines
2.4 KiB
Java
69 lines
2.4 KiB
Java
package com.kimgo.wepush.service;
|
||
|
||
|
||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||
import com.kimgo.wepush.config.UserConfig;
|
||
import com.kimgo.wepush.mapper.ApiSettingMapper;
|
||
import com.kimgo.wepush.model.ApiSetting;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
import org.springframework.stereotype.Service;
|
||
|
||
@Service
|
||
public class ApiSettingService {
|
||
private final Logger logger = LoggerFactory.getLogger(ApiSettingService.class);
|
||
private ApiSetting apiSetting;
|
||
private final ApiSettingMapper apiSettingMapper;
|
||
private final UserConfig userConfig;
|
||
|
||
public ApiSettingService(ApiSettingMapper apiSettingMapper, UserConfig userConfig) {
|
||
this.apiSettingMapper = apiSettingMapper;
|
||
this.userConfig = userConfig;
|
||
}
|
||
|
||
public ApiSetting getApiSetting() {
|
||
if (apiSetting == null) {
|
||
queryApiSetting();
|
||
}
|
||
return apiSetting;
|
||
}
|
||
|
||
/**
|
||
* 查询API设置信息。
|
||
* 该方法不接受参数,也不返回值,但会根据用户配置的电话号码查询数据库中的API设置信息,并更新相关的成员变量。
|
||
* 主要步骤包括:
|
||
* 1. 从用户配置中获取电话号码。
|
||
* 2. 构建查询条件。
|
||
* 3. 根据查询条件从数据库中查询唯一的API设置信息。
|
||
* 4. 记录查询结果的日志信息。
|
||
* 5. 如果查询结果为空,则更新API设置为null,并记录错误日志。
|
||
* 6. 最后,更新API设置成员变量为查询结果,并记录相应日志。
|
||
*/
|
||
private void queryApiSetting() {
|
||
// 从用户配置中获取电话号码
|
||
String phoneNumberToSearch = userConfig.getPhoneNumber();
|
||
|
||
// 构建查询条件
|
||
QueryWrapper<ApiSetting> wrapper = new QueryWrapper<>();
|
||
wrapper.eq("phone_number", phoneNumberToSearch);
|
||
|
||
// 根据查询条件查询API设置信息
|
||
ApiSetting result = apiSettingMapper.selectOne(wrapper);
|
||
// 记录查询结果
|
||
logger.info("result: {}", result);
|
||
|
||
// 处理查询结果为空的情况
|
||
if (result == null) {
|
||
apiSetting = null;
|
||
// 记录错误日志
|
||
logger.error("updateAccessToken error");
|
||
}
|
||
|
||
// 更新API设置成员变量
|
||
apiSetting = result;
|
||
// 记录更新日志
|
||
logger.info("get apiSetting from mysql,accessToken: {}", apiSetting);
|
||
}
|
||
|
||
}
|