CameraServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package com.ruoyi.system.service.impl;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONArray;
  4. import com.alibaba.fastjson2.JSONObject;
  5. import com.alibaba.fastjson2.TypeReference;
  6. import com.ruoyi.common.core.domain.AjaxResult;
  7. import com.ruoyi.common.core.redis.RedisCache;
  8. import com.ruoyi.common.exception.ServiceException;
  9. import com.ruoyi.common.utils.DateUtils;
  10. import com.ruoyi.common.utils.SecurityUtils;
  11. import com.ruoyi.common.utils.StringUtils;
  12. import com.ruoyi.common.utils.hkws.GetCameraPreviewURL;
  13. import com.ruoyi.system.domain.*;
  14. import com.ruoyi.system.domain.vo.CameraInfoVo;
  15. import com.ruoyi.system.mapper.*;
  16. import com.ruoyi.system.service.CameraService;
  17. import com.ruoyi.system.service.IClockEquipRecordService;
  18. import lombok.SneakyThrows;
  19. import org.apache.poi.ss.usermodel.DateUtil;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import java.time.OffsetDateTime;
  23. import java.time.ZoneOffset;
  24. import java.time.ZonedDateTime;
  25. import java.time.format.DateTimeFormatter;
  26. import java.util.*;
  27. import java.util.stream.Collectors;
  28. import static com.ruoyi.common.constant.CommonConstants.KQ_START_TIME;
  29. import static com.ruoyi.common.constant.CommonConstants.OPEN_DOOR;
  30. /**
  31. * 监控Service业务层处理
  32. *
  33. * @author ruoyi
  34. * @date 2024-06-25
  35. */
  36. @Service
  37. public class CameraServiceImpl implements CameraService {
  38. @Autowired
  39. private EquipmentConfigurationMapper equipmentConfigurationMapper;
  40. @Autowired
  41. private DutyScheduleMapper dutyScheduleMapper;
  42. @Autowired
  43. private IClockEquipRecordService clockEquipRecordService;
  44. @Autowired
  45. private ClockEquipRecordMapper clockEquipRecordMapper;
  46. @Autowired
  47. private ClockUserInfoMapper clockUserInfoMapper;
  48. @Autowired
  49. private RedisCache redisCache;
  50. @Autowired
  51. private ClockRecordMapper clockRecordMapper;
  52. @Override
  53. public AjaxResult selectCameraList(CameraInfoVo cameraInfoVo) {
  54. //Map<String,JSONObject> map = new HashMap<>();
  55. /**
  56. * STEP3:设置接口的URI地址
  57. */
  58. String url = "/api/resource/v2/camera/search";
  59. /**
  60. * STEP5:组装请求参数
  61. */
  62. String body = JSONArray.toJSONString(cameraInfoVo);
  63. /*JSONObject jsonBody = new JSONObject();
  64. jsonBody.put("cameraIndexCode", "8e59cc5559904589938be47888e5214a");
  65. jsonBody.put("streamType", 0);
  66. jsonBody.put("protocol", "httpflv");
  67. jsonBody.put("transmode", 1);
  68. jsonBody.put("expand", "streamform=ps");*/
  69. /* jsonBody.put("pageNo", 1);
  70. jsonBody.put("pageSize", 30);*/
  71. String result = GetCameraPreviewURL.GetCameraPreviewURL(url,body);
  72. JSONObject jsonObject = JSON.parseObject(result);
  73. //将门禁设备单独列出
  74. //Map<String, Object> map = jsonObject.to(new TypeReference<Map<String, Object>>() {});
  75. JSONObject data = (JSONObject) jsonObject.get("data");
  76. JSONArray itemsArray = data.getJSONArray("list");
  77. List<JSONObject> itemsList = itemsArray.toJavaList(JSONObject.class);
  78. List<JSONObject> accessList = new ArrayList<>();
  79. //查询门禁设备名字
  80. List<EquipmentConfiguration> equipmentList = equipmentConfigurationMapper.selectEquipmentConfigurationList(new EquipmentConfiguration());
  81. for (JSONObject object : itemsList) {
  82. for (EquipmentConfiguration equipmentConfiguration : equipmentList) {
  83. if(object.get("name").equals(equipmentConfiguration.getEquipmentName())){
  84. accessList.add(object);
  85. }
  86. }
  87. }
  88. data.put("access",accessList);
  89. return AjaxResult.success(jsonObject);
  90. }
  91. @Override
  92. public AjaxResult video(String cameraIndexCode) {
  93. if(StringUtils.isEmpty(cameraIndexCode)){
  94. throw new ServiceException("参数错误");
  95. }
  96. /**
  97. * STEP3:设置接口的URI地址
  98. */
  99. String url = "/api/video/v2/cameras/previewURLs";
  100. /**
  101. * STEP5:组装请求参数
  102. */
  103. JSONObject jsonBody = new JSONObject();
  104. jsonBody.put("cameraIndexCode", cameraIndexCode);
  105. jsonBody.put("streamType", 0);
  106. jsonBody.put("protocol", "httpflv");
  107. jsonBody.put("transmode", 1);
  108. jsonBody.put("expand", "streamform=ps");
  109. String result = GetCameraPreviewURL.GetCameraPreviewURL(url,jsonBody.toString());
  110. JSONObject jsonObject = JSON.parseObject(result);
  111. return AjaxResult.success(jsonObject);
  112. }
  113. @SneakyThrows
  114. @Override
  115. public void configureTasks() {
  116. //查询设备 name -> 1号楼2F-
  117. /**
  118. * STEP3:查询门禁点事件v2
  119. */
  120. String url = "/api/acs/v2/door/events";
  121. /**
  122. * STEP5:组装请求参数
  123. */
  124. /**
  125. * STEP3:设置接口的URI地址
  126. * indexCode -> c9a07d45adc247908d73d4d3a41470a4 name -> 1号楼2F-2_门_1
  127. * indexCode -> 86e6e58f29fe44ad80b18a0bc3a24080 name -> 1号楼2F-1_门_1
  128. * indexCode -> 5490b0c100dd4367a8fb548b1de33043 name -> 5号楼1F_门_1
  129. * indexCode -> 0e530bc2f5234929b6bf5562e1e4932c name -> 后门_门_1
  130. * */
  131. String startTime = redisCache.getCacheObject(KQ_START_TIME);
  132. if(StringUtils.isEmpty(startTime)){
  133. startTime = "2025-01-01T00:00:00+08:00";
  134. }
  135. ZoneOffset offset = ZoneOffset.of("+08:00"); // 中国时区偏移量
  136. ZonedDateTime now = ZonedDateTime.now(offset); // 注意这里用的是ZoneOffset而不是ZoneId
  137. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX"); // XXX 表示时区偏移量,但不显示区域名称如Asia/Shanghai
  138. String formatted = now.format(formatter);
  139. int[] eventTypes = {196893};
  140. String[] doorIndexCodes = {"c9a07d45adc247908d73d4d3a41470a4","86e6e58f29fe44ad80b18a0bc3a24080"};
  141. JSONObject jsonBody = new JSONObject();
  142. jsonBody.put("pageNo", 1);
  143. jsonBody.put("pageSize", "1000");
  144. jsonBody.put("doorIndexCodes", doorIndexCodes);
  145. jsonBody.put("eventTypes", eventTypes);
  146. jsonBody.put("startTime", startTime);
  147. jsonBody.put("endTime", formatted);
  148. //jsonBody.put("doorName", "考勤");
  149. jsonBody.put("sort", "eventTime");
  150. jsonBody.put("order", "asc");
  151. String result = GetCameraPreviewURL.GetCameraPreviewURL(url,jsonBody.toString());
  152. JSONObject jsonObject = JSON.parseObject(result);
  153. JSONObject data = (JSONObject) jsonObject.get("data");
  154. JSONArray itemsArray = data.getJSONArray("list");
  155. List<JSONObject> itemsList = itemsArray.toJavaList(JSONObject.class);
  156. List<JSONObject> accessList = new ArrayList<>();
  157. for (JSONObject object : itemsList) {
  158. String name = (String)object.get("personName");
  159. if(StringUtils.isNotEmpty(name)){
  160. accessList.add(object);
  161. }
  162. }
  163. if(accessList==null){
  164. return;
  165. }
  166. Map<String,List<JSONObject>> map = accessList.stream().collect(Collectors.groupingBy(obj -> obj.getString("personName")));
  167. //查询打卡时间
  168. DutySchedule dutySchedule = new DutySchedule();
  169. dutySchedule.setIsEquip("Y");
  170. List<DutySchedule> dutySchedules = dutyScheduleMapper.selectDutyScheduleList(dutySchedule);
  171. if(dutySchedules==null || dutySchedules.size()<1){
  172. return;
  173. }
  174. dutySchedule = dutySchedules.get(0);
  175. for (String key : map.keySet()) {
  176. //查询人员的信息
  177. ClockUserInfo clockUserInfo = clockUserInfoMapper.selectClockUserInfoByName(key);
  178. List<JSONObject> keyValue = map.get(key);
  179. for (int i = 0; i < keyValue.size(); i++) {
  180. JSONObject object = map.get(key).get(i);
  181. if(object.get("eventTime").equals(startTime)){
  182. continue;
  183. }
  184. String dateTime = DateUtils.bDateToString((String) object.get("eventTime"));
  185. ClockRecord clockEquipRecord = new ClockRecord();
  186. if(clockUserInfo!=null){
  187. clockEquipRecord.setUserId(clockUserInfo.getUserId());
  188. clockEquipRecord.setDeptName(clockUserInfo.getDeptName());
  189. clockEquipRecord.setPhonenumber(clockUserInfo.getPhonenumber());
  190. clockEquipRecord.setIdCard(clockUserInfo.getIdCard());
  191. }
  192. //clockEquipRecord.setUserName((String) object.get("personId"));
  193. clockEquipRecord.setUserName((String) object.get("personName"));
  194. //clockEquipRecord.setIdCard((String) object.get("certNo"));
  195. clockEquipRecord.setWeek(String.valueOf(DateUtils.dayForWeek(dateTime)));
  196. clockEquipRecord.setRecordDt(dateTime);
  197. String[] dt = dateTime.split(" ");
  198. clockEquipRecord.setRecordDate(dt[0]);
  199. clockEquipRecord.setRecordTime(dt[1]);
  200. clockEquipRecord.setRecordLocations((String) object.get("readerDevName"));
  201. //判断是否是正常打卡 1:正常打卡,2:外勤打卡,3:迟到,4:早退',
  202. //查询库里是否有key的数据,有的话就说明不是上班卡,没有的话就是第一个卡
  203. ClockRecord clockEquip = new ClockRecord();
  204. clockEquip.setUserName((String) object.get("personName"));
  205. clockEquip.setRecordDate(dt[0]);
  206. List<ClockRecord> clockEquipRecords = clockRecordMapper.selectClockRecordList(clockEquip);
  207. //里面存在数据
  208. if(clockEquipRecords!=null && clockEquipRecords.size()>0){
  209. //先将之前的的下班卡改为无效(名字)
  210. //clockEquipRecordMapper.updateClockEquipRecordEffective(dt[0],(String) object.get("personName"));
  211. clockEquipRecord.setType("2");
  212. //clockEquipRecord.setEffective("1");
  213. //判断下班是否早退
  214. int res = dt[1].compareTo(dutySchedule.getEndWorkTime());
  215. if(res>0 || res==0){
  216. //正常打卡
  217. clockEquipRecord.setIsOutwork("1");
  218. }else{
  219. clockEquipRecord.setIsOutwork("4");
  220. }
  221. }else{
  222. //不存在数据
  223. clockEquipRecord.setType("1");
  224. //clockEquipRecord.setEffective("1");
  225. //判断上班是否迟到
  226. int res = dt[1].compareTo(dutySchedule.getStartWorkTime());
  227. if(res>0 || res==0){
  228. //迟到
  229. clockEquipRecord.setIsOutwork("3");
  230. }else{
  231. clockEquipRecord.setIsOutwork("1");
  232. }
  233. }
  234. clockEquipRecord.setSource("设备");
  235. clockEquipRecord.setCreateTime(DateUtils.getNowDate());
  236. clockRecordMapper.insertClockRecord(clockEquipRecord);
  237. }
  238. }
  239. //将最后一条有效数据时间存入redis
  240. redisCache.setCacheObject(KQ_START_TIME,accessList.get(accessList.size()-1).get("eventTime"));
  241. }
  242. /*public static void main(String[] args) {
  243. // 获取当前时间
  244. ZoneOffset offset = ZoneOffset.of("+08:00"); // 中国时区偏移量
  245. ZonedDateTime now = ZonedDateTime.now(offset); // 注意这里用的是ZoneOffset而不是ZoneId
  246. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX"); // XXX 表示时区偏移量,但不显示区域名称如Asia/Shanghai
  247. String formatted = now.format(formatter);
  248. System.out.println(formatted);
  249. // 打印ISO 8601格式的当前时间
  250. //System.out.println(now);
  251. //String url = "/api/resource/v2/door/search";
  252. String url = "/api/acs/v2/door/events";
  253. int[] eventTypes = {196893};
  254. String[] doorIndexCodes = {"c9a07d45adc247908d73d4d3a41470a4","86e6e58f29fe44ad80b18a0bc3a24080"};
  255. JSONObject jsonBody = new JSONObject();
  256. jsonBody.put("pageNo", 1);
  257. jsonBody.put("pageSize", "1000");
  258. jsonBody.put("doorIndexCodes", doorIndexCodes);
  259. jsonBody.put("eventTypes", eventTypes);
  260. //jsonBody.put("doorName", "考勤");
  261. String result = GetCameraPreviewURL.GetCameraPreviewURL(url,jsonBody.toString());
  262. JSONObject jsonObject = JSON.parseObject(result);
  263. JSONObject data = (JSONObject) jsonObject.get("data");
  264. JSONArray itemsArray = data.getJSONArray("list");
  265. List<JSONObject> itemsList = itemsArray.toJavaList(JSONObject.class);
  266. Map<String,List<JSONObject>> map = itemsList.stream().collect(Collectors.groupingBy(obj -> obj.getString("personId")));
  267. List<JSONObject> accessList = new ArrayList<>();
  268. for (JSONObject object : itemsList) {
  269. String name = (String)object.get("doorName");
  270. if(name.contains("考勤")){
  271. accessList.add(object);
  272. }
  273. }
  274. System.out.println(accessList.toString());
  275. }*/
  276. }