HikvisionLeaveTask.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package com.xjrsoft.module.job;
  2. import cn.hutool.extra.spring.SpringUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.google.gson.JsonArray;
  5. import com.google.gson.JsonElement;
  6. import com.google.gson.JsonObject;
  7. import com.google.gson.JsonParser;
  8. import com.xjrsoft.module.hikvision.entity.HikvisionData;
  9. import com.xjrsoft.module.hikvision.mapper.HikvisionDataMapper;
  10. import com.xjrsoft.module.hikvision.util.ApiUtil;
  11. import com.xjrsoft.module.student.entity.StudentLeave;
  12. import com.xjrsoft.module.student.service.IStudentLeaveService;
  13. import com.xjrsoft.module.teacher.entity.WfTeacherleave;
  14. import com.xjrsoft.module.teacher.service.IWfTeacherleaveService;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.scheduling.annotation.Async;
  18. import org.springframework.scheduling.annotation.Scheduled;
  19. import org.springframework.stereotype.Component;
  20. import java.time.LocalDate;
  21. import java.util.*;
  22. /**
  23. * 请假结束之后,删除下发到海康的一卡通权限
  24. *
  25. * @author dzx
  26. * @date 2024/5/8
  27. */
  28. @Component
  29. @Slf4j
  30. public class HikvisionLeaveTask {
  31. @Autowired
  32. private IStudentLeaveService studentLeaveService;
  33. @Autowired
  34. private IWfTeacherleaveService teacherleaveService;
  35. @Autowired
  36. private HikvisionDataMapper hikvisionDataMapper;
  37. @Async
  38. @Scheduled(cron = "0 */15 * * * ?")
  39. public void execute() {
  40. doExecute();
  41. }
  42. public void doExecute() {
  43. String active = SpringUtil.getActiveProfile();
  44. if (!"prod".equals(active)) {
  45. log.info("非正式环境,无法执行数据推送");
  46. return;
  47. }
  48. List<String> sourceIds = new ArrayList<>();
  49. List<StudentLeave> studentList = studentLeaveService.list(
  50. new QueryWrapper<StudentLeave>().lambda()
  51. .isNotNull(StudentLeave::getHikvisionResult)
  52. .lt(StudentLeave::getEndDate, LocalDate.now())
  53. );
  54. for (StudentLeave studentLeave : studentList) {
  55. sourceIds.add(studentLeave.getStudentUserId().toString());
  56. }
  57. List<WfTeacherleave> teacherList = teacherleaveService.list(
  58. new QueryWrapper<WfTeacherleave>().lambda()
  59. .isNotNull(WfTeacherleave::getHikvisionResult)
  60. .lt(WfTeacherleave::getLeaveEndTime, new Date())
  61. );
  62. for (WfTeacherleave teacherleave : teacherList) {
  63. String[] split = teacherleave.getUserId().split(",");
  64. sourceIds.addAll(Arrays.asList(split));
  65. }
  66. if (sourceIds.isEmpty()) {
  67. return;
  68. }
  69. List<HikvisionData> hikvisionData = hikvisionDataMapper.selectList(
  70. new QueryWrapper<HikvisionData>().lambda()
  71. .in(HikvisionData::getSourceId, sourceIds)
  72. );
  73. ApiUtil apiUtil = new ApiUtil();
  74. String apiPath = "/api/acps/v1/auth_config/delete";
  75. JsonObject paramJson = new JsonObject();
  76. JsonArray personDatas = new JsonArray();
  77. JsonObject personData = new JsonObject();
  78. JsonArray indexCodes = new JsonArray();
  79. for (HikvisionData el : hikvisionData) {
  80. indexCodes.add(el.getHikvisionId());
  81. }
  82. personData.addProperty("personDataType", "person");
  83. personData.add("indexCodes", indexCodes);
  84. personDatas.add(personData);
  85. paramJson.add("personDatas", personDatas);
  86. JsonArray resourceInfos = selectResource(apiUtil);
  87. paramJson.add("resourceInfos", resourceInfos);
  88. Map<String, String> header = new HashMap<>();
  89. header.put("tagId", "studentleave");
  90. //调用接口获取到返回内容,并将其存到数据库中
  91. String result = apiUtil.doPost(apiPath, paramJson.toString(), null, header);
  92. //删除成功后,重新下载
  93. //1、创建任务
  94. paramJson = new JsonObject();
  95. paramJson.addProperty("taskType", 1);
  96. apiPath = "/api/acps/v1/download/configuration/task/add";
  97. String doPost = apiUtil.doPost(apiPath, paramJson.toString(), null, null);
  98. JsonParser jsonParser = new JsonParser();
  99. JsonObject resultJson = jsonParser.parse(doPost).getAsJsonObject();
  100. if ("0".equals(resultJson.get("code").getAsString()) && "success".equals(resultJson.get("msg").getAsString())) {
  101. String taskId = resultJson.get("data").getAsJsonObject().get("taskId").getAsString();
  102. //2、下载
  103. apiPath = "/api/acps/v1/download/configuration/data/add";
  104. paramJson = new JsonObject();
  105. paramJson.add("resourceInfos", resourceInfos);
  106. paramJson.addProperty("taskId", taskId);
  107. apiUtil.doPost(apiPath, paramJson.toString(), null, null);
  108. }
  109. }
  110. JsonArray selectResource(ApiUtil apiUtil) {
  111. String apiPath = "/api/irds/v2/resource/resourcesByParams";
  112. JsonObject jsonObject = new JsonObject();
  113. jsonObject.addProperty("pageNo", 1);
  114. jsonObject.addProperty("pageSize", 500);
  115. jsonObject.addProperty("resourceType", "door");
  116. String result = apiUtil.doPost(apiPath, jsonObject.toString(), null, null);
  117. JsonParser parser = new JsonParser();
  118. JsonObject resultJson = parser.parse(result).getAsJsonObject();
  119. JsonArray resourceInfos = new JsonArray();
  120. if ("0".equals(resultJson.get("code").getAsString()) && "success".equals(resultJson.get("msg").getAsString())) {
  121. JsonArray list = resultJson.get("data").getAsJsonObject().get("list").getAsJsonArray();
  122. for (JsonElement jsonElement : list) {
  123. JsonObject listOne = jsonElement.getAsJsonObject();
  124. JsonObject resourceInfo = new JsonObject();
  125. resourceInfo.add("resourceIndexCode", listOne.get("indexCode"));
  126. resourceInfo.add("resourceType", listOne.get("resourceType"));
  127. JsonArray channelNos = new JsonArray();
  128. channelNos.add(listOne.get("channelNo"));
  129. resourceInfo.add("channelNos", channelNos);
  130. resourceInfos.add(resourceInfo);
  131. }
  132. }
  133. return resourceInfos;
  134. }
  135. }