package com.greentech.gateservice.task; import com.greentech.gateservice.entity.GateOplog; import com.greentech.gateservice.mapper.GateOplogMapper; import com.greentech.gateservice.util.LoginModule; import com.netsdk.common.Res; import com.netsdk.lib.NetSDKLib; import com.netsdk.lib.ToolKits; import com.sun.jna.Memory; import lombok.Data; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestMapping; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.util.*; @RequestMapping("crud") @Data @Component @Service public class GateLogService { @Autowired GateOplogMapper gateOplogMapper; @Value("${customer.project}") private int project; @Scheduled(cron = "0 0 * * * *") public void syncLog() { System.out.println("--开始执行日志查询同步任务--"); String format = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setTimeZone(TimeZone.getTimeZone("GMT+0")); //获取前一小时 LocalDateTime now = LocalDateTime.now(); LocalDateTime startTime = now.minusHours(1); NetSDKLib.NET_TIME s = new NetSDKLib.NET_TIME(); s.setTime(startTime.getYear(),startTime.getMonthValue(),startTime.getDayOfMonth(),startTime.getHour(),0,0); NetSDKLib.NET_TIME e = new NetSDKLib.NET_TIME(); e.setTime(startTime.getYear(),startTime.getMonthValue(),startTime.getDayOfMonth(),startTime.getHour(),59,59); synchronized(LoginModule.lock){ for (NetSDKLib.LLong key : LoginModule.m_stDeviceInfo.keySet()) { //获取handle句柄和设备信息 NetSDKLib.NET_DEVICEINFO_Ex value = LoginModule.m_stDeviceInfo.get(key); System.out.println("Key: " + key + ", Value: " + value); //查询逻辑 NetSDKLib.NET_IN_FIND_RECORD_PARAM inParam = new NetSDKLib.NET_IN_FIND_RECORD_PARAM(); inParam.emType = NetSDKLib.EM_NET_RECORD_TYPE.NET_RECORD_ACCESSCTLCARDREC_EX; //查询条件 NetSDKLib.FIND_RECORD_ACCESSCTLCARDREC_CONDITION_EX condition = new NetSDKLib.FIND_RECORD_ACCESSCTLCARDREC_CONDITION_EX(); condition.bTimeEnable = 1; condition.stStartTime = s; condition.stEndTime = e; inParam.pQueryCondition = new Memory(condition.size()); ToolKits.SetStructDataToPointer(condition, inParam.pQueryCondition, 0); NetSDKLib.NET_OUT_FIND_RECORD_PARAM outParam = new NetSDKLib.NET_OUT_FIND_RECORD_PARAM(); boolean f = LoginModule.netsdk.CLIENT_FindRecord(key,inParam,outParam,5000); if (!f) { System.err.println("查询日志失败1!"); return; } System.out.println("CLIENT_FindRecord success!"); int max = 100; NetSDKLib.NET_IN_FIND_NEXT_RECORD_PARAM inNextParam = new NetSDKLib.NET_IN_FIND_NEXT_RECORD_PARAM(); inNextParam.lFindeHandle = outParam.lFindeHandle; inNextParam.nFileCount = max; NetSDKLib.NET_OUT_FIND_NEXT_RECORD_PARAM outNextParam = new NetSDKLib.NET_OUT_FIND_NEXT_RECORD_PARAM(); outNextParam.nMaxRecordNum = max; NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC[] rets = new NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC[max]; for (int i = 0; i < max; i++) { rets[i] = new NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARDREC(); } outNextParam.pRecordList = new Memory(rets[0].size() * max); ToolKits.SetStructArrToPointerData(rets, outNextParam.pRecordList); boolean f2 = LoginModule.netsdk.CLIENT_FindNextRecord(inNextParam, outNextParam, 5000); if (!f2) { System.err.println("查询日志失败2!"); return; } System.out.println("CLIENT_FindNextRecord success!"); ToolKits.GetPointerDataToStructArr(outNextParam.pRecordList, rets); System.out.println("开始时间:"+s.toStringTimeEx() + "结束时间:"+e.toStringTimeEx()); System.out.println("查询到的结果数量:"+outNextParam.nRetRecordNum+"最大数量:"+outNextParam.nMaxRecordNum); for (int i = 0; i < outNextParam.nRetRecordNum; i++) { String szCardName = new String(rets[i].szCardName); String openMethod = Res.string().getOpenMethods(rets[i].emMethod); System.out.println("人名:" + szCardName + "时间:" + rets[i].stuTime.toStringTimeEx() + "开门方式" + openMethod); System.out.println("开门方向:"+rets[i].emDirection); //bStatus 开门结果 GateOplog opLog = new GateOplog(); opLog.setGateName(""); opLog.setProjectId(project); opLog.setUserName(szCardName); opLog.setEventType(openMethod); if (rets[i].emDirection == NetSDKLib.NET_ENUM_DIRECTION_ACCESS_CTL.NET_ENUM_DIRECTION_ENTRY) { opLog.setStatus("进门"); } else if (rets[i].emDirection == NetSDKLib.NET_ENUM_DIRECTION_ACCESS_CTL.NET_ENUM_DIRECTION_EXIT) { opLog.setStatus("出门"); } else { opLog.setStatus(""); } opLog.setSerialNum(new String(value.sSerialNumber)); Date d = new Date(); try { d = sdf.parse(rets[i].stuTime.toStringTimeEx()); }catch (ParseException excep) { System.err.println("解析时间发生错误: " + excep.getMessage()); } opLog.setCreatedTime(d); opLog.setCheckType(""); byte isDelete = 0; opLog.setIsDelete(isDelete); gateOplogMapper.insert(opLog); } //断开查询句柄 LoginModule.netsdk.CLIENT_FindRecordClose(outParam.lFindeHandle); System.out.println("查询结束"); } } System.out.println("--结束执行日志查询同步任务--"); } }