SmartFaceDiscernServiceImpl.java 11 KB

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