package com.fr.plugin; import com.fr.plugin.beans.UserLogBean; import com.fr.plugin.dao.LogDao; import com.fr.plugin.dao.RelationLogDao; import com.fr.plugin.entitys.AccessLogEntity; import com.fr.plugin.entitys.RelationLogEntity; import com.fr.plugin.transform.ExecuteFunctionRecord; import com.fr.plugin.transform.FunctionRecorder; import com.fr.stable.db.accessor.DBAccessor; import com.fr.stable.db.action.DBAction; import com.fr.stable.db.dao.DAOContext; import com.fr.stable.query.QueryFactory; import com.fr.stable.query.condition.QueryCondition; import com.fr.stable.query.restriction.RestrictionFactory; import com.fr.third.org.apache.commons.lang3.time.DateFormatUtils; import java.util.Date; import java.util.List; import java.util.UUID; @FunctionRecorder(localeKey = "logmac") public class LogMacFunction { @ExecuteFunctionRecord public void save(UserLogBean bean) throws Exception { DBAccessor accessor = LogMacDBAccess.getAccessor(); RelationLogEntity logEntity = new RelationLogEntity(); logEntity.setTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")); logEntity.setMacAddr(bean.getMac()); logEntity.setUserName(bean.getName()); logEntity.setDeviceName(bean.getDeviceName()); logEntity.setId(UUID.randomUUID().toString()); final RelationLogEntity count = accessor.runQueryAction(new DBAction() { public RelationLogEntity run(DAOContext content) throws Exception { QueryCondition queryCondition = QueryFactory.create(); queryCondition.addRestriction(RestrictionFactory.eq("userName", logEntity.getUserName())); return content.getDAO(RelationLogDao.class).findOne(queryCondition); } }); accessor.runDMLAction(new DBAction() { public RelationLogEntity run(DAOContext content) throws Exception { if (count != null) { count.setMacAddr(bean.getMac()); count.setDeviceName(bean.getDeviceName()); content.getDAO(RelationLogDao.class).update(count); } else { content.getDAO(RelationLogDao.class).add(logEntity); } return null; } }); } public RelationLogEntity findRelation(String name) throws Exception { DBAccessor accessor = LogMacDBAccess.getAccessor(); return accessor.runQueryAction(new DBAction() { public RelationLogEntity run(DAOContext content) throws Exception { QueryCondition queryCondition = QueryFactory.create(); queryCondition.addRestriction(RestrictionFactory.eq("userName", name)); return content.getDAO(RelationLogDao.class).findOne(queryCondition); } }); } public void addLoginHistory(RelationLogEntity relation) throws Exception { AccessLogEntity logEntity = new AccessLogEntity(); logEntity.setMacAddr(relation.getMacAddr()); logEntity.setUserName(relation.getUserName()); logEntity.setDeviceName(relation.getDeviceName()); logEntity.setId(UUID.randomUUID().toString()); logEntity.setTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd")); DBAccessor accessor = LogMacDBAccess.getAccessor(); accessor.runDMLAction(new DBAction() { public RelationLogEntity run(DAOContext content) throws Exception { LogDao dao = content.getDAO(LogDao.class); QueryCondition queryCondition = QueryFactory.create(); queryCondition.addRestriction(RestrictionFactory.eq("time", logEntity.getTime())).addRestriction(RestrictionFactory.eq("macAddr", logEntity.getMacAddr())).addRestriction(RestrictionFactory.eq("userName", logEntity.getUserName())); List accessLogEntities = dao.find(queryCondition); //如果不存在则插入,保障一天,一个用户,一个MAC只插入1条 if (accessLogEntities.isEmpty()) { dao.add(logEntity); } return null; } }); } }