SmartFaceDiscernServiceImpl.java 13 KB

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