|
|
package com.ibeetl.jlw.service;
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.net.url.UrlQuery;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.http.Header;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import cn.jlw.aliPay.utils.StringUtils;
|
|
|
import cn.jlw.util.IpUtils;
|
|
|
import com.ibeetl.admin.core.entity.CoreUser;
|
|
|
import com.ibeetl.admin.core.service.CoreBaseService;
|
|
|
import com.ibeetl.admin.core.util.PlatformException;
|
|
|
import com.ibeetl.jlw.dao.SysLogDao;
|
|
|
import com.ibeetl.jlw.entity.Student;
|
|
|
import com.ibeetl.jlw.entity.SysLog;
|
|
|
import org.beetl.sql.core.SqlId;
|
|
|
import org.beetl.sql.core.engine.PageQuery;
|
|
|
import org.beetl.sql.core.query.LambdaQuery;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import static cn.jlw.Interceptor.AuthenticationInterceptor.getToken;
|
|
|
import static cn.jlw.util.CacheUserUtil.getStudent;
|
|
|
import static com.ibeetl.admin.core.util.servlet.ServletUtils.getRequest;
|
|
|
import static com.ibeetl.admin.core.util.user.CacheUserUtil.getUser;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* SysLog Service
|
|
|
*/
|
|
|
|
|
|
@Service
|
|
|
@Transactional
|
|
|
public class SysLogService extends CoreBaseService<SysLog> {
|
|
|
|
|
|
@Autowired
|
|
|
private SysLogDao sysLogDao;
|
|
|
|
|
|
public PageQuery<SysLog> queryByCondition(PageQuery query) {
|
|
|
PageQuery ret = sysLogDao.queryByCondition(query);
|
|
|
queryListAfter(ret.getList());
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
public void deleteSysLog(String ids) {
|
|
|
try {
|
|
|
sysLogDao.deleteSysLogByIds(ids);
|
|
|
} catch (Exception e) {
|
|
|
throw new PlatformException("批量删除SysLog失败", e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public boolean saveToLog(SysLog sysLog) {
|
|
|
String params = sysLog.getParams();
|
|
|
String result = sysLog.getResult();
|
|
|
if(StringUtils.isBlank(params)){
|
|
|
params = "";
|
|
|
}
|
|
|
if(StringUtils.isBlank(result)){
|
|
|
result = "";
|
|
|
}
|
|
|
if (params.getBytes().length < 5000 || result.getBytes().length < 10000 ) {
|
|
|
if (params.getBytes().length < 5000 && result.getBytes().length >= 10000) {
|
|
|
if(!result.contains("\"code\":\"200\"") && !result.contains("\\\"code\\\":\\\"200\\\"")){
|
|
|
if(result.contains("会话过期")){
|
|
|
sysLog.setResult("{\\\"code\\\":\\\"500\\\",\\\"msg\\\":\\\"会话过期\\\"}");
|
|
|
}else {
|
|
|
sysLog.setResult("{\\\"code\\\":\\\"500\\\",\\\"msg\\\":\\\"异常\\\"}");
|
|
|
}
|
|
|
}else {
|
|
|
sysLog.setResult(null);
|
|
|
}
|
|
|
// sysLogDao.insert(sysLog);
|
|
|
if (result.getBytes().length < 65535) {
|
|
|
sysLogDao.saveToText(sysLog.getId(), null, result);
|
|
|
} else if (result.getBytes().length < 16777215) {
|
|
|
sysLogDao.saveToMediumText(sysLog.getId(), null, result);
|
|
|
} else {
|
|
|
sysLogDao.saveToLongText(sysLog.getId(), null, result);
|
|
|
}
|
|
|
} else if (params.getBytes().length >= 5000 && result.getBytes().length < 10000) {
|
|
|
sysLog.setParams(null);
|
|
|
sysLogDao.insert(sysLog);
|
|
|
if (params.getBytes().length < 65535) {
|
|
|
sysLogDao.saveToText(sysLog.getId(), params, null);
|
|
|
} else if (params.getBytes().length < 16777215) {
|
|
|
sysLogDao.saveToMediumText(sysLog.getId(), params, null);
|
|
|
} else {
|
|
|
sysLogDao.saveToLongText(sysLog.getId(), params, null);
|
|
|
}
|
|
|
} else if (params.getBytes().length < 5000 && result.getBytes().length < 10000) {
|
|
|
sysLogDao.insert(sysLog);
|
|
|
}
|
|
|
} else {
|
|
|
sysLog.setParams(null);
|
|
|
if(!result.contains("\"code\":\"200\"") && !result.contains("\\\"code\\\":\\\"200\\\"")){
|
|
|
if(result.contains("超时")){
|
|
|
sysLog.setResult("{\\\"code\\\":\\\"500\\\",\\\"msg\\\":\\\"超时\\\"}");
|
|
|
}else {
|
|
|
sysLog.setResult("{\\\"code\\\":\\\"500\\\",\\\"msg\\\":\\\"异常\\\"}");
|
|
|
}
|
|
|
}else {
|
|
|
sysLog.setResult(null);
|
|
|
}
|
|
|
sysLogDao.insert(sysLog);
|
|
|
if (params.getBytes().length < 65535 && result.getBytes().length < 65535) {
|
|
|
sysLogDao.saveToText(sysLog.getId(), params, result);
|
|
|
} else if (params.getBytes().length < 16777215 && result.getBytes().length < 16777215) {
|
|
|
sysLogDao.saveToMediumText(sysLog.getId(), params, result);
|
|
|
} else {
|
|
|
sysLogDao.saveToLongText(sysLog.getId(), params, result);
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
public List<SysLog> getValues(Object paras) {
|
|
|
return sqlManager.select(SqlId.of("jlw.sysLog.getSysLogValues"), SysLog.class, paras);
|
|
|
}
|
|
|
|
|
|
public List<Map<Integer, Integer>> onLineNum(Date startTime, Date endTime) {
|
|
|
return sysLogDao.onLineNum(startTime, endTime);
|
|
|
}
|
|
|
|
|
|
public List<Map<String, Object>> visitGoodsNum(Date startTime, Date endTime) {
|
|
|
return sysLogDao.visitGoodsNum(startTime, endTime);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 从跳转的路径中,取参数。并构建成一个SysLog导入到日志表中
|
|
|
* @param realUrl
|
|
|
*/
|
|
|
public void applicationLogPoint(String realUrl) {
|
|
|
Map params = new HashMap(8);
|
|
|
|
|
|
// 记录下,realUrl中的参数列表
|
|
|
if (null != realUrl && realUrl.contains("?")) {
|
|
|
UrlQuery urlQuery = UrlQuery.of(realUrl.split("\\?")[1], Charset.defaultCharset());
|
|
|
Map<String, Object> tempMap = new HashMap<>();
|
|
|
for (Map.Entry<CharSequence, CharSequence> entry : urlQuery.getQueryMap().entrySet()) {
|
|
|
tempMap.put(entry.getKey().toString(), entry.getValue());
|
|
|
}
|
|
|
params.putAll(tempMap);
|
|
|
}
|
|
|
mockLog(realUrl, params, null);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 模拟日志,用于操作日志埋点
|
|
|
*/
|
|
|
public void mockLog(String realUrl, Object params, Object result) {
|
|
|
|
|
|
CoreUser user = getUser();
|
|
|
Student student = getStudent();
|
|
|
|
|
|
HttpServletRequest request = getRequest();
|
|
|
JSONObject paramsJSON = JSONUtil.parseObj(params);
|
|
|
paramsJSON.set("_referer", request.getHeader(Header.REFERER.getValue()));
|
|
|
|
|
|
SysLog model = new SysLog();
|
|
|
Date nowDate = new Date();
|
|
|
|
|
|
// 这里固定该接口的请求路径
|
|
|
model.setMethod("com.ibeetl.jlw.service.SysLogService.mockLog");
|
|
|
model.setRequestUrl(realUrl);
|
|
|
model.setParams(paramsJSON.toString());
|
|
|
model.setResult(JSONUtil.parseObj(result).toString());
|
|
|
model.setIp(IpUtils.getIpAddr(request));
|
|
|
model.setCreateTime(nowDate);
|
|
|
model.setResultTime(nowDate);
|
|
|
Object token = getToken(request);
|
|
|
model.setSession(request.getRequestedSessionId());
|
|
|
model.setUserId(user.getId());
|
|
|
|
|
|
if(null != token) {
|
|
|
model.setToken(token.toString());
|
|
|
}
|
|
|
|
|
|
if (null != student) {
|
|
|
model.setStudentId(student.getStudentId());
|
|
|
}
|
|
|
|
|
|
insert(model);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 设置用户的登录次数和最后登录时间等信息
|
|
|
* 这样查询日志表来统计登录信息,不太好,但是没得时间了
|
|
|
* @param userId
|
|
|
* @param dbUser
|
|
|
*/
|
|
|
public void setLoginLogInfo(Long userId, CoreUser dbUser) {
|
|
|
if (ObjectUtil.isAllEmpty(userId, dbUser)) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
LambdaQuery<SysLog> sysLogLambdaQuery = sysLogDao.createLambdaQuery();
|
|
|
|
|
|
|
|
|
long loginCount = sysLogLambdaQuery.andLike(SysLog::getRequestUrl, "%login%").andLike(SysLog::getResult, "%成功%").andEq(SysLog::getUserId, userId).count();
|
|
|
// 登录次数
|
|
|
dbUser.set("loginCount", loginCount);
|
|
|
|
|
|
|
|
|
//最后登陆时间
|
|
|
SysLog lastLoginLog = sysLogLambdaQuery.andLike(SysLog::getRequestUrl, "%login%").andLike(SysLog::getResult, "%成功%")
|
|
|
.andEq(SysLog::getUserId, userId).desc(SysLog::getResultTime).limit(0, 1).single("result_time");
|
|
|
|
|
|
// 最后登录时间,默认置空
|
|
|
dbUser.set("lastLoginTime", "-");
|
|
|
if (lastLoginLog != null) {
|
|
|
dbUser.set("lastLoginTime", lastLoginLog.getResultTime());
|
|
|
}
|
|
|
|
|
|
|
|
|
// 登录天数
|
|
|
List<SysLog> loginDaysLogList = sysLogLambdaQuery.andLike(SysLog::getRequestUrl, "%login%").andLike(SysLog::getResult, "%成功%")
|
|
|
.andEq(SysLog::getUserId, userId).desc(SysLog::getResultTime).select(SysLog::getResultTime);
|
|
|
|
|
|
long loginDays = CollectionUtil.emptyIfNull(loginDaysLogList).stream().map(item -> DateUtil.formatDate(item.getResultTime())).distinct().count();
|
|
|
dbUser.set("loginDays", loginDays);
|
|
|
}
|
|
|
|
|
|
} |