HikvisionLeaveTask.java 6.1 KB

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