SmartFaceDiscernServiceImpl.java 12 KB

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