SmartFaceDiscernServiceImpl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package com.template.services.impl;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.Wrapper;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
  7. import com.template.common.utils.*;
  8. import com.template.model.pojo.SmartFaceDiscern;
  9. import com.template.mapper.SmartFaceDiscernMapper;
  10. import com.template.model.pojo.SmartNotification;
  11. import com.template.model.pojo.SmartUser;
  12. import com.template.services.SmartFaceDiscernService;
  13. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  14. import com.template.services.SmartNotificationService;
  15. import com.template.services.SmartUserService;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.scheduling.annotation.Scheduled;
  18. import org.springframework.stereotype.Service;
  19. import java.net.URLDecoder;
  20. import java.net.URLEncoder;
  21. import java.time.LocalDateTime;
  22. import java.time.format.DateTimeFormatter;
  23. import java.util.List;
  24. import static com.template.common.utils.AesTestOne.decrypt;
  25. /**
  26. * <p>
  27. * 服务实现类
  28. * </p>
  29. *
  30. * @author ceshi
  31. * @since 2024-01-18
  32. */
  33. @Service
  34. public class SmartFaceDiscernServiceImpl extends ServiceImpl<SmartFaceDiscernMapper, SmartFaceDiscern> implements SmartFaceDiscernService {
  35. @Autowired
  36. SmartUserService smartUserService;
  37. @Autowired
  38. SmartNotificationService smartNotificationService;
  39. // static String appid = "sc5efc60f2bd373df9";
  40. static String appid = "sc1eb792c1ea3237e9";
  41. // static String appSecret = "fe0d767a2a394d1b81ccda6fc0ce5ecc";
  42. static String appSecret = "61afd794ed3244e995c5e894e5788193";
  43. // static String schoolCode = "SL1701743624375793";
  44. static String schoolCode = "SL1704880825203963";
  45. private Integer page = 1;
  46. private Integer size = 50;
  47. // @Scheduled(cron = "0 0/50 * * * ? ")
  48. @Scheduled(cron = "0 0/20 * * * ? ")
  49. public void baiS() {
  50. try {
  51. getFaceDiscern(page, size);
  52. } catch (Exception e) {
  53. throw new RuntimeException(e);
  54. }
  55. }
  56. public void getFaceDiscern(Integer pageindex, Integer pagesize) throws Exception {
  57. // String url = "http://schoolopenapi.szymzh.com/openapi/record/searchrecord";
  58. String url = "https://schoolopenapi.ymiot.net/openapi/record/searchrecord";
  59. JSONObject jsonObject = new JSONObject();
  60. jsonObject.put("appid", appid);
  61. String timestamp = TimeExchange.DateNowTimeStamo();
  62. jsonObject.put("timestamp", timestamp);
  63. jsonObject.put("schoolno", schoolCode);
  64. JSONObject data = new JSONObject();
  65. data.put("pageindex", pageindex + "");
  66. data.put("pagesize", pagesize + "");
  67. // 时间
  68. DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  69. LocalDateTime now = LocalDateTime.now();
  70. LocalDateTime stateTime = now.withHour(0).withMinute(0).withSecond(0);
  71. LocalDateTime endTime = now.withHour(23).withMinute(59).withSecond(59);
  72. // 开始时间
  73. data.put("record_begintime", stateTime.format(dateTimeFormatter1));
  74. // data.put("record_begintime", "2024-01-22 00:00:00");
  75. // 结束时间
  76. data.put("record_endtime", endTime.format(dateTimeFormatter1));
  77. // data.put("record_endtime", "2024-01-22 23:59:59");
  78. String dataString = data.toJSONString();
  79. // 加密
  80. String aesStr = URLEncoder.encode(AesTestOne.encrypt(dataString, appSecret), "UTF-8");
  81. jsonObject.put("data", aesStr);
  82. String stringA = "appid=" + appid + "&data=" + dataString + "&schoolno=" + schoolCode + "&timestamp=" + timestamp + "&key=" + appSecret;
  83. String sign = CommonUtil.MD5(stringA);
  84. jsonObject.put("sign", sign);
  85. String s = RequestUtils.httpPost(url, jsonObject.toJSONString());
  86. JSONObject jsonObject1 = JSONObject.parseObject(s);
  87. String data1 = jsonObject1.getString("data");
  88. // URL解码
  89. String decodedUrl = URLDecoder.decode(data1, "UTF-8");
  90. String decrypt = decrypt(decodedUrl, appSecret);
  91. // System.out.println("decrypt = " + decrypt);
  92. if (ObjectUtils.isNotEmpty(decrypt)&& decrypt!="") {
  93. JSONObject jsonObject2 = JSONObject.parseObject(decrypt);
  94. if (ObjectUtils.isNotEmpty(jsonObject2)) {
  95. // 获取总数
  96. Integer total = Integer.valueOf(jsonObject2.getString("total"));
  97. // System.out.println("total = " + total);
  98. if (total > pageindex * pagesize) {
  99. page = pageindex + 1;
  100. } else {
  101. page = pageindex - 1;
  102. }
  103. JSONArray data2 = jsonObject2.getJSONArray("data");
  104. for (int i = 0; i < data2.size(); i++) {
  105. JSONObject jsonObject3 = data2.getJSONObject(i);
  106. SmartFaceDiscern smartFaceDiscern = new SmartFaceDiscern();
  107. // 事件
  108. String deviceName = jsonObject3.getString("device_name");
  109. smartFaceDiscern.setType(deviceName);
  110. // 地点
  111. String deviceArea = jsonObject3.getString("device_area");
  112. smartFaceDiscern.setLocation(deviceArea);
  113. // 图片
  114. String recordPhoto = jsonObject3.getString("record_photo");
  115. smartFaceDiscern.setImage(recordPhoto);
  116. // 时间
  117. String recordAddtime = jsonObject3.getString("record_addtime");
  118. smartFaceDiscern.setDateTime(recordAddtime);
  119. // 名字
  120. String student_name = jsonObject3.getString("student_name");
  121. smartFaceDiscern.setName(student_name);
  122. // 学号
  123. String student_number = jsonObject3.getString("student_number");
  124. // 编号
  125. String student_no = jsonObject3.getString("student_no");
  126. LambdaQueryWrapper<SmartUser> wrapper = new LambdaQueryWrapper<>();
  127. wrapper.eq(SmartUser::getBsStudentNo, student_no);
  128. wrapper.eq(SmartUser::getName, student_name);
  129. SmartUser smartUser = smartUserService.getOne(wrapper);
  130. if (ObjectUtils.isNotEmpty(smartUser)) {
  131. smartFaceDiscern.setUserId(smartUser.getId());
  132. // 判断是否已经添加
  133. LambdaQueryWrapper<SmartFaceDiscern> wrapperFD = new LambdaQueryWrapper<>();
  134. wrapperFD.eq(SmartFaceDiscern::getDateTime, recordAddtime)
  135. .eq(SmartFaceDiscern::getUserId, smartUser.getId());
  136. SmartFaceDiscern one = this.getOne(wrapperFD);
  137. if (ObjectUtils.isEmpty(one)) {
  138. this.save(smartFaceDiscern);
  139. // 通过学生id找到关联的家长affiliate,并找到公众号,如果openid为空则不传
  140. List<SmartUser> userList = smartUserService.getAffiliateList(smartUser.getId());
  141. if (ObjectUtils.isNotEmpty(userList) && userList.size() > 0) {
  142. for (SmartUser user : userList) {
  143. String gzhOpenId = user.getGzhOpenId();
  144. if (ObjectUtils.isNotEmpty(gzhOpenId)) {
  145. String pushType = "";
  146. if (deviceName.contains("进校")) {
  147. pushType = "进入大门";
  148. } else if (deviceName.contains("出校")) {
  149. pushType = "离开大门";
  150. }
  151. // 时间格式是 yyyy-MM-dd HH:mm:ss,当前时间格式有问题
  152. LocalDateTime date = LocalDateTime.now();
  153. String format = date.format(dateTimeFormatter1);
  154. // 公众号信息推送
  155. Message2.send(gzhOpenId, pushType, deviceArea, format);
  156. SmartNotification smartNotification = new SmartNotification();
  157. smartNotification.setUserId(smartUser.getId());
  158. smartNotification.setUserName(smartUser.getName());
  159. smartNotification.setTypeName(pushType);
  160. smartNotification.setLocation(deviceArea);
  161. smartNotification.setImage(recordPhoto);
  162. smartNotification.setDateTime(recordAddtime);
  163. smartNotification.setType(2);
  164. smartNotificationService.save(smartNotification);
  165. }
  166. }
  167. }
  168. }
  169. }else {
  170. smartFaceDiscern.setUserId(0);
  171. // 判断是否已经添加
  172. LambdaQueryWrapper<SmartFaceDiscern> wrapperFD = new LambdaQueryWrapper<>();
  173. wrapperFD.eq(SmartFaceDiscern::getDateTime, recordAddtime)
  174. .eq(SmartFaceDiscern::getName, student_name)
  175. .eq(SmartFaceDiscern::getType,deviceName );
  176. SmartFaceDiscern one = this.getOne(wrapperFD);
  177. if (ObjectUtils.isEmpty(one)) {
  178. this.save(smartFaceDiscern);
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. @Override
  186. public List<SmartFaceDiscern> track(String stateTime, String endTime, Integer id) {
  187. LambdaQueryWrapper<SmartFaceDiscern> wrapper = new LambdaQueryWrapper<>();
  188. wrapper.between(SmartFaceDiscern::getDateTime, stateTime, endTime)
  189. .eq(SmartFaceDiscern::getUserId, id)
  190. .orderByDesc(SmartFaceDiscern::getDateTime);
  191. List<SmartFaceDiscern> list = this.list(wrapper);
  192. return list;
  193. }
  194. }