Browse Source

学生教师拉取数据

brealinxx 9 months ago
parent
commit
85101912a5

+ 4 - 6
src/main/java/com/xjrsoft/module/hikvision/util/DataUtil.java

@@ -468,10 +468,6 @@ public class DataUtil {
         String existingPlatesSql = "SELECT source_id FROM hikvision_data";
         List<String> existingPlates = db.query(existingPlatesSql, String.class);
 
-        for (String h : existingPlates){
-            System.out.println(h + "qqq");
-        }
-
         JsonArray dataArray = new JsonArray();
         String result = null;
         int clientId = 1;
@@ -483,7 +479,9 @@ public class DataUtil {
             JsonObject paramJson = new JsonObject();
             paramJson.addProperty("clientId", clientId);
             paramJson.addProperty("plateNo", carMessageApply.getCarNumber());
-            paramJson.addProperty("personId", faceImportMapper.GetHikvisonPersonId(Long.valueOf(Long.parseLong(carMessageApply.getId().toString()))));
+            System.out.println(faceImportMapper.GetHikvisonPersonId(carMessageApply.getId().toString()) + "heihei");
+            System.out.println(faceImportMapper.GetHikvisonPersonId(carMessageApply.getId().toString()) + "xixi");
+            paramJson.addProperty("personId", faceImportMapper.GetHikvisonPersonId(carMessageApply.getId().toString()));
 
             int vehicleTypeNum;
             switch (carMessageApply.getVehicleType()) {
@@ -513,7 +511,7 @@ public class DataUtil {
             paramJsonTime.addProperty("plateNo", carMessageApply.getCarNumber());
             paramJsonTime.addProperty("startTime", String.valueOf(carMessageApply.getStartTime()));
             paramJsonTime.addProperty("endTime", String.valueOf(carMessageApply.getEndTime()));
-            String aa = apiUtil.doPost(apiPathTime, String.valueOf(paramJsonTime), tagMap, null);
+            apiUtil.doPost(apiPathTime, String.valueOf(paramJsonTime), tagMap, null);
         }
 
         JsonElement parse = jsonParser.parse(result);

+ 74 - 70
src/main/java/com/xjrsoft/module/hikvision/util/Teacher_Student_OutIn_RecordUtil.java

@@ -1,113 +1,117 @@
 package com.xjrsoft.module.hikvision.util;
 
-import cn.dev33.satoken.annotation.SaCheckPermission;
 import cn.hutool.db.Db;
 import com.google.gson.Gson;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
-import io.swagger.annotations.ApiOperation;
-import lombok.var;
-import org.springframework.web.bind.annotation.GetMapping;
+import com.xjrsoft.common.constant.GlobalConstant;
+import com.xjrsoft.common.utils.DatasourceUtil;
+import com.xjrsoft.module.teacher.mapper.FaceImportMapper;
+import org.springframework.beans.factory.annotation.Autowired;
 
+import javax.sql.DataSource;
 import java.sql.SQLException;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicInteger;
 
 
 public class Teacher_Student_OutIn_RecordUtil {
-//    void tearcherInsertRecord(Db db, Long userId, String recordTime, Long facePhoto,int status) throws SQLException {
-//
-//        String sql = "INSERT INTO teacher_out_in_record(create_date, ...) " +
-//                "VALUES(now(), '"  + userId + "', '" + teacherId + "', '" +
-//                classId + "', '" + recordTime + "', '" + facePhoto + "', '" + status + "', '" + entry.getValue() + "')";
-//        db.execute(sql);
-//    }
-
-    public void studentInsertRecord(Db db, Long userId, Long teacherId, Long classId, String recordTime,Long facePhoto, int status) throws SQLException {
-        String sql = "INSERT INTO teacher_out_in_record(create_date, ...) " +
-                "VALUES(now(), '"  + userId + "', '" + teacherId + "', '" +
-                classId + "', '" + recordTime + "', '" + facePhoto + "', '" + status + "', '" + "')";
+    public void teacherInsertRecord(Db db, Long userId, String recordTime, String facePhoto,int status, String eventId) throws SQLException {
+
+        String sql = "INSERT INTO teacher_out_in_record(create_date, user_id, record_time, face_photo, eventId, status) " +
+                "VALUES(now(), '"  + userId + "', '" + recordTime + "', '" +
+                facePhoto + "', '" + eventId + "', '" +status + "')";
         db.execute(sql);
     }
 
+    public void studentInsertRecord(Db db, Long userId, Long teacherId, Long classId, String facePhoto, String recordTime, int status, String eventId) throws SQLException {
+        String sql = "INSERT INTO student_out_in_record(create_date, user_id, teacher_id, class_id, face_photo, record_time , eventId,status) " +
+                "VALUES(now(), '" + userId + "', '" + teacherId + "', '" +
+                classId + "', '" + facePhoto + "', '" + recordTime + "', '" + eventId + "', '" + status + "')";
+        db.execute(sql);
+    }
+
+
     public void visitInsertRecord(Db db, Long reservation_school_people_id, String recordTime,Long facePhoto, int status) throws SQLException {
-        String sql = "INSERT INTO visitor_out_in_record(create_date, ...) " +
+        String sql = "INSERT INTO visitor_out_in_record(create_date, reservation_school_people_id, record_time, face_photo, status) " +
                 "VALUES(now(), '"  + reservation_school_people_id + "', '" +
-                 recordTime + "', '" + facePhoto + "', '" + status + "', '" + "')";
+                 recordTime + "', '" + facePhoto + "', '" + status +  "')";
         db.execute(sql);
     }
 
-
-    @GetMapping(value = "/adada")
-    @ApiOperation(value = "新www价项")
-    @SaCheckPermission("evaluateitem:add")
-    public String GetAccessConsole(){
+    public String GetDoorEvents(Db use, FaceImportMapper faceImportMapper) throws SQLException {
         ApiUtil apiUtil = new ApiUtil();
-        JsonObject paramJson = new JsonObject();
-        paramJson.addProperty("pageNo", 1);
-        paramJson.addProperty("pageSize", 100);
 
-        Map<String, String> querys = new HashMap<>();
-        querys.put("tagId", "frs");
+        String apiPath = "/api/acs/v2/door/events";
+        JsonArray responseBuilder = new JsonArray();
 
-        String apiPath = "/api/resource/v2/acsDevice/search";
-        String response = apiUtil.doPost(apiPath, String.valueOf(paramJson), querys);
+        AtomicInteger pageNo = new AtomicInteger(1);
+        int pageSize = 100;
+        boolean hasMorePages = true;
 
-        return GetDoorEvents(response);
-    }
+        while (hasMorePages) {
+            JsonObject paramJson = new JsonObject();
+            paramJson.addProperty("pageNo", pageNo.get());
+            paramJson.addProperty("pageSize", pageSize);
 
-    public String GetDoorEvents(String accessPointResponse){
-        ApiUtil apiUtil = new ApiUtil();
+            String response = apiUtil.doPost(apiPath, String.valueOf(paramJson), null);
+            JsonElement responseElement = new Gson().fromJson(response, JsonElement.class);
+            JsonArray dataList = responseElement.getAsJsonObject().getAsJsonObject("data").getAsJsonArray("list");
 
-        JsonObject accessPointJson = new Gson().fromJson(accessPointResponse, JsonObject.class);
-        JsonArray dataList = accessPointJson.getAsJsonObject("data").getAsJsonArray("list");
-        String apiPath = "/api/acs/v2/door/events";
-        String response = null;
-        for(JsonElement element : dataList){
-            JsonObject item = element.getAsJsonObject();
-            JsonObject paramJson = new JsonObject();
-            JsonArray doorIndexCodes = new JsonArray();
-            paramJson.addProperty("pageNo", 1);
-            paramJson.addProperty("pageSize", 10);
-            doorIndexCodes.add(item.get("indexCode"));
-            paramJson.add("doorIndexCodes", doorIndexCodes);
-            response = apiUtil.doPost(apiPath, String.valueOf(paramJson), null);
-            // 执行四中人员的导入
-            stProcessRecords(response);
-
-//            doorIndexCodes.add(item.get("indexCode").getAsString());
-            System.out.println(response);
-        }
+            for (JsonElement item : dataList) {
+                responseBuilder.add(item);
+            }
 
-        return response;
+            JsonObject responseJson = new Gson().fromJson(response, JsonObject.class);
+            int totalCount = responseJson.getAsJsonObject("data").get("totalPage").getAsInt();
+            hasMorePages = pageNo.getAndIncrement() < totalCount;
+        }
+        // Process the records
+        InsertTeacherStudentRecords(use, responseBuilder, faceImportMapper);
+        return responseBuilder.toString();
     }
 
-    public void stProcessRecords(String doorEventsResponse) {
-        JsonObject doorEventsJson = new Gson().fromJson(doorEventsResponse, JsonObject.class);
-        JsonArray dataList = doorEventsJson.getAsJsonObject("data").getAsJsonArray("list");
 
-        for (JsonElement element : dataList) {
+    public void InsertTeacherStudentRecords(Db use, JsonArray doorEventsResponse, FaceImportMapper faceImportMapper) throws SQLException {
+//        for (JsonElement e : doorEventsResponse){
+        Teacher_Student_OutIn_RecordUtil teacherStudentOutInRecordUtil = new Teacher_Student_OutIn_RecordUtil();
+        List<String> teacherEventIdList = faceImportMapper.GetTeacherUrlList();
+        List<String> studentEventIdList = faceImportMapper.GetStudentUrlList();
+
+        for (JsonElement element : doorEventsResponse) {
             JsonObject item = element.getAsJsonObject();
-            if (item.get("studentId").isJsonNull()) {
-                // 老师记录
-                String personId = item.get("personId").isJsonNull() ? null : item.get("personId").getAsString();
-                int inAndOutType = item.get("inAndOutType").getAsInt();
 
+            Long personId;
+            try {
+                personId = item.get("personId").isJsonNull() ? null : item.get("personId").getAsLong();
+            }catch (Exception e){
+                continue;
+            }
+            if (personId == null) continue;
+
+            int status = item.get("inAndOutType").getAsInt();
+            String photo = item.get("picUri").isJsonNull() ? null : item.get("picUri").getAsString();
+            String recordTime = item.get("eventTime").isJsonNull() ? null : item.get("eventTime").getAsString();
+            String eventId = item.get("eventId").isJsonNull() ? null : item.get("eventId").getAsString();
+
+            if (!Objects.equals(faceImportMapper.IsStudentTypeByPersonId(personId), "学生")) {
+                // 老师记录
+                if (teacherEventIdList.contains(item.get("eventId").getAsString())) continue;
+                teacherStudentOutInRecordUtil.teacherInsertRecord(use, personId,recordTime,photo,status,eventId);
             } else {
                 // 学生记录
-                String studentId = item.get("studentId").getAsString();
-                String personId = item.get("personId").isJsonNull() ? null : item.get("personId").getAsString();
-                int status = item.get("inAndOutType").getAsInt();
-                String receiveTime = item.get("receiveTime").getAsString();
-
-                // 执行mapper获取例如班主任等数据 然后执行
-//                Teacher_Student_OutIn_RecordUtil teacherStudentOutInRecordUtil = new Teacher_Student_OutIn_RecordUtil();
-//                teacherStudentOutInRecordUtil.studentInsertRecord(db...);
+                if (studentEventIdList.contains(item.get("eventId").getAsString())) continue;
+                teacherStudentOutInRecordUtil.studentInsertRecord(use, personId, faceImportMapper.GetTeacherIdByPersonId(personId), faceImportMapper.GetClassIdByPersonId(personId), photo, recordTime, status,eventId);
             }
         }
+//        }
     }
 
+
     void GetVisitRecord(){
         ApiUtil apiUtil = new ApiUtil();
         JsonObject paramJson = new JsonObject();

+ 57 - 0
src/main/java/com/xjrsoft/module/job/InsertOutInRecordTask.java

@@ -0,0 +1,57 @@
+package com.xjrsoft.module.job;
+
+import cn.hutool.db.Db;
+import cn.hutool.extra.spring.SpringUtil;
+import com.xjrsoft.common.constant.GlobalConstant;
+import com.xjrsoft.common.utils.DatasourceUtil;
+import com.xjrsoft.module.hikvision.entity.HikvisionData;
+import com.xjrsoft.module.hikvision.util.DataUtil;
+import com.xjrsoft.module.hikvision.util.Teacher_Student_OutIn_RecordUtil;
+import com.xjrsoft.module.schedule.entity.JianyueData;
+import com.xjrsoft.module.teacher.mapper.FaceImportMapper;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.sql.DataSource;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @author dzx
+ * @date 2024/5/8
+ */
+@Component
+@Slf4j
+public class InsertOutInRecordTask {
+    @Autowired
+    private FaceImportMapper faceImportMapper;
+
+    Teacher_Student_OutIn_RecordUtil teacherStudentOutInRecordUtil = new Teacher_Student_OutIn_RecordUtil();
+
+    @Scheduled(cron = "0 */15 * * * ?")
+    public void RefreshConnectionPool() {
+        String active = SpringUtil.getActiveProfile();
+        if(!"prod".equals(active)){
+            log.info("非正式环境,无法执行数据推送");
+            return;
+        }
+        log.info("开始推送海康威视基础数据");
+        DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
+        try {
+            Db use = Db.use(datasource);
+
+            //教师&学生拉取数据
+            teacherStudentOutInRecordUtil.GetDoorEvents(use, faceImportMapper);
+
+            log.info("数据推送完成");
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+        }
+    }
+}
+

+ 10 - 1
src/main/java/com/xjrsoft/module/teacher/mapper/FaceImportMapper.java

@@ -3,6 +3,8 @@ package com.xjrsoft.module.teacher.mapper;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
+import java.util.List;
+
 
 @Mapper
 public interface FaceImportMapper {
@@ -12,6 +14,13 @@ public interface FaceImportMapper {
     String GetStudentUserIdById(@Param("id") Long id);
     String GetStudentHikvisionImgById(@Param("id") Long id);
 
-    Long GetHikvisonPersonId(@Param("id") Long id);
+    String GetHikvisonPersonId(@Param("id") String id);
+
+    String IsStudentTypeByPersonId(@Param("id") Long id);
+    Long GetTeacherIdByPersonId(@Param("id") Long id);
+    Long GetClassIdByPersonId(@Param("id") Long id);
+
+    List<String> GetStudentUrlList();
+    List<String> GetTeacherUrlList();
 }
 

+ 38 - 2
src/main/resources/mapper/teacher/TeacherFaceImportMapper.xml

@@ -41,13 +41,49 @@
         and t.status = 1
     </select>
 
-    <select id="GetHikvisonPersonId" parameterType="long" resultType="java.lang.Long">
+    <select id="GetHikvisonPersonId" parameterType="string" resultType="java.lang.String">
         select t.hikvision_id
         from hikvision_data t
-        left join car_message_apply t1 on t1.user_id = t.source_id
+        left join car_message_apply t1 on t1.id = t.source_id
         <if test="id != null">
             where t1.id = #{id}
         </if>
         and t1.status = 1
     </select>
+
+    <select id="IsStudentTypeByPersonId" parameterType="long" resultType="java.lang.String">
+        select t.nick_name
+        from xjr_user t
+        <if test="id != null">
+            where t.id = #{id}
+        </if>
+    </select>
+
+    <select id="GetTeacherIdByPersonId" parameterType="long" resultType="java.lang.Long">
+        select t1.teacher_id
+        from base_student_school_roll t
+        left join base_class t1 on t.class_id = t1.id
+        <if test="id != null">
+            where t.user_id = #{id}
+        </if>
+    </select>
+
+    <select id="GetClassIdByPersonId" parameterType="long" resultType="java.lang.Long">
+        select t1.id
+        from base_student_school_roll t
+        left join base_class t1 on t.class_id = t1.id
+        <if test="id != null">
+            where t.user_id = #{id}
+        </if>
+    </select>
+
+    <select id="GetStudentUrlList"  resultType="java.lang.String">
+        select t.eventId
+        from student_out_in_record t
+    </select>
+
+    <select id="GetTeacherUrlList"  resultType="java.lang.String">
+        select t.eventId
+        from teacher_out_in_record t
+    </select>
 </mapper>

+ 11 - 3
src/test/java/com/xjrsoft/module/job/HikvisionBaseDataTaskTest.java

@@ -8,6 +8,7 @@ import com.google.gson.JsonParser;
 import com.xjrsoft.XjrSoftApplication;
 import com.xjrsoft.common.constant.GlobalConstant;
 import com.xjrsoft.common.utils.DatasourceUtil;
+import com.xjrsoft.module.evaluate.controller.EvaluateItemController;
 import com.xjrsoft.module.hikvision.entity.HikvisionData;
 import com.xjrsoft.module.hikvision.util.ApiUtil;
 import com.xjrsoft.module.hikvision.util.DataUtil;
@@ -15,8 +16,10 @@ import com.xjrsoft.module.organization.entity.Department;
 import com.xjrsoft.module.schedule.entity.JianyueData;
 import com.xjrsoft.module.teacher.entity.XjrUser;
 import com.xjrsoft.module.teacher.mapper.FaceImportMapper;
+import com.xjrsoft.module.hikvision.util.Teacher_Student_OutIn_RecordUtil;
 import lombok.var;
 import org.junit.jupiter.api.Test;
+import org.junit.platform.commons.logging.LoggerFactory;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
@@ -30,6 +33,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.logging.Logger;
 
 /**
  * @author dzx
@@ -41,6 +45,9 @@ class HikvisionBaseDataTaskTest {
     @Autowired
     private FaceImportMapper faceImportMapper;
 
+    @Autowired
+    EvaluateItemController evaluateItemController;
+
     @Test
     void test() throws Exception {
         DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
@@ -77,9 +84,10 @@ class HikvisionBaseDataTaskTest {
 //
         //推送车辆
 
-        String carTableName = "car_message_apply";
-        Map<String, String> baseClass = dataMap.get(carTableName);
-        dataUtil.insertCar(use, carTableName,baseClass, faceImportMapper);
+//        String carTableName = "car_message_apply";
+//        Map<String, String> baseClass = dataMap.get(carTableName);
+//        dataUtil.insertCar(use, carTableName,baseClass, faceImportMapper);
+
 
 //        tableName = "base_class";
 //        Map<String, String> baseClass = dataMap.get(tableName);