123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- package com.jsh.erp.service.impl;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.jsh.erp.constants.BusinessConstants;
- import com.jsh.erp.datasource.entities.Log;
- import com.jsh.erp.datasource.entities.LogExample;
- import com.jsh.erp.datasource.mappers.LogMapper;
- import com.jsh.erp.datasource.mappers.LogMapperEx;
- import com.jsh.erp.datasource.vo.LogVo4List;
- import com.jsh.erp.exception.JshException;
- import com.jsh.erp.service.LogService;
- import com.jsh.erp.service.RedisService;
- import com.jsh.erp.service.UserService;
- import com.jsh.erp.utils.PageUtils;
- import com.jsh.erp.utils.StringUtil;
- import com.jsh.erp.utils.Tools;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import java.util.Date;
- import java.util.List;
- import static com.jsh.erp.utils.Tools.getLocalIp;
- @Service
- public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {
- private Logger logger = LoggerFactory.getLogger(LogServiceImpl.class);
- @Resource
- private LogMapper logMapper;
- @Resource
- private LogMapperEx logMapperEx;
- @Resource
- private UserService userService;
- @Resource
- private RedisService redisService;
- @Override
- public Log getLog(long id)throws Exception {
- Log result=null;
- try{
- result=logMapper.selectByPrimaryKey(id);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- public List<Log> getLog()throws Exception {
- LogExample example = new LogExample();
- List<Log> list=null;
- try{
- list=logMapper.selectByExample(example);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public List<LogVo4List> select(String operation, String userInfo, String clientIp, String tenantLoginName, String tenantType,
- String beginTime, String endTime, String content)throws Exception {
- List<LogVo4List> list=null;
- try{
- beginTime = Tools.parseDayToTime(beginTime,BusinessConstants.DAY_FIRST_TIME);
- endTime = Tools.parseDayToTime(endTime,BusinessConstants.DAY_LAST_TIME);
- PageUtils.startPage();
- list=logMapperEx.selectByConditionLog(operation, userInfo, clientIp, tenantLoginName, tenantType, beginTime, endTime,
- content);
- if (null != list) {
- for (LogVo4List log : list) {
- log.setCreateTimeStr(Tools.getCenternTime(log.getCreateTime()));
- }
- }
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- @Override
- public int insertLog(JSONObject obj, HttpServletRequest request) throws Exception{
- Log log = JSONObject.parseObject(obj.toJSONString(), Log.class);
- int result=0;
- try{
- result=logMapper.insertSelective(log);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int updateLog(JSONObject obj, HttpServletRequest request)throws Exception {
- Log log = JSONObject.parseObject(obj.toJSONString(), Log.class);
- int result=0;
- try{
- result=logMapper.updateByPrimaryKeySelective(log);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int deleteLog(Long id, HttpServletRequest request)throws Exception {
- int result=0;
- try{
- result=logMapper.deleteByPrimaryKey(id);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int batchDeleteLog(String ids, HttpServletRequest request)throws Exception {
- List<Long> idList = StringUtil.strToLongList(ids);
- LogExample example = new LogExample();
- example.createCriteria().andIdIn(idList);
- int result=0;
- try{
- result=logMapper.deleteByExample(example);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- return result;
- }
- @Override
- public void insertLog(String moduleName, String content, HttpServletRequest request)throws Exception{
- try{
- Long userId = userService.getUserId(request);
- if(userId!=null) {
- String clientIp = getLocalIp(request);
- String createTime = Tools.getNow3();
- Long count = logMapperEx.getCountByIpAndDate(userId, moduleName, clientIp, createTime);
- if(count > 0) {
- //如果某个用户某个IP在同1秒内连续操作两遍,此时需要删除该redis记录,使其退出,防止恶意攻击
- redisService.deleteObjectByUserAndIp(userId, clientIp);
- } else {
- Log log = new Log();
- log.setUserId(userId);
- log.setOperation(moduleName);
- log.setClientIp(getLocalIp(request));
- log.setCreateTime(new Date());
- Byte status = 0;
- log.setStatus(status);
- log.setContent(content);
- logMapper.insertSelective(log);
- }
- }
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- }
- @Override
- public void insertLogWithUserId(Long userId, Long tenantId, String moduleName, String content, HttpServletRequest request)throws Exception{
- try{
- if(userId!=null) {
- Log log = new Log();
- log.setUserId(userId);
- log.setOperation(moduleName);
- log.setClientIp(getLocalIp(request));
- log.setCreateTime(new Date());
- Byte status = 0;
- log.setStatus(status);
- log.setContent(content);
- log.setTenantId(tenantId);
- logMapperEx.insertLogWithUserId(log);
- }
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- }
- }
|