| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- package com.template.services.impl;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
- import com.template.common.utils.*;
- import com.template.config.ScheduleConfig;
- import com.template.model.pojo.SmartFaceDiscern;
- import com.template.mapper.SmartFaceDiscernMapper;
- import com.template.model.pojo.SmartNotification;
- import com.template.model.pojo.SmartUser;
- import com.template.services.SmartFaceDiscernService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.template.services.SmartNotificationService;
- import com.template.services.SmartUserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.scheduling.annotation.Async;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.net.URLDecoder;
- import java.net.URLEncoder;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import static com.template.common.utils.AesTestOne.decrypt;
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author ceshi
- * @since 2024-01-18
- */
- @Service
- public class SmartFaceDiscernServiceImpl extends ServiceImpl<SmartFaceDiscernMapper, SmartFaceDiscern> implements SmartFaceDiscernService {
- @Autowired
- SmartUserService smartUserService;
- @Autowired
- SmartNotificationService smartNotificationService;
- @Resource
- private ScheduleConfig scheduleConfig;
- // static String appid = "sc5efc60f2bd373df9";
- static String appid = "sc1eb792c1ea3237e9";
- // static String appSecret = "fe0d767a2a394d1b81ccda6fc0ce5ecc";
- static String appSecret = "61afd794ed3244e995c5e894e5788193";
- // static String schoolCode = "SL1701743624375793";
- static String schoolCode = "SL1704880825203963";
- private Integer page = 1;
- private Integer size = 50;
- // @Scheduled(cron = "0 0/50 * * * ? ")
- @Async
- @Scheduled(cron = "0 0/20 * * * ? ")
- public void baiS() {
- if(scheduleConfig.getIsOpen().equals("1")){
- try {
- getFaceDiscern(page, size);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- }
- public void getFaceDiscern(Integer pageindex, Integer pagesize) throws Exception {
- // String url = "http://schoolopenapi.szymzh.com/openapi/record/searchrecord";
- String url = "https://schoolopenapi.ymiot.net/openapi/record/searchrecord";
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("appid", appid);
- String timestamp = TimeExchange.DateNowTimeStamo();
- jsonObject.put("timestamp", timestamp);
- jsonObject.put("schoolno", schoolCode);
- JSONObject data = new JSONObject();
- data.put("pageindex", pageindex + "");
- data.put("pagesize", pagesize + "");
- // 时间
- DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- LocalDateTime now = LocalDateTime.now();
- LocalDateTime stateTime = now.withHour(0).withMinute(0).withSecond(0);
- LocalDateTime endTime = now.withHour(23).withMinute(59).withSecond(59);
- // 开始时间
- data.put("record_begintime", stateTime.format(dateTimeFormatter1));
- // data.put("record_begintime", "2024-01-22 00:00:00");
- // 结束时间
- data.put("record_endtime", endTime.format(dateTimeFormatter1));
- // data.put("record_endtime", "2024-01-22 23:59:59");
- String dataString = data.toJSONString();
- // 加密
- String aesStr = URLEncoder.encode(AesTestOne.encrypt(dataString, appSecret), "UTF-8");
- jsonObject.put("data", aesStr);
- String stringA = "appid=" + appid + "&data=" + dataString + "&schoolno=" + schoolCode + "×tamp=" + timestamp + "&key=" + appSecret;
- String sign = CommonUtil.MD5(stringA);
- jsonObject.put("sign", sign);
- String s = RequestUtils.httpPost(url, jsonObject.toJSONString());
- JSONObject jsonObject1 = JSONObject.parseObject(s);
- String data1 = jsonObject1.getString("data");
- // URL解码
- String decodedUrl = URLDecoder.decode(data1, "UTF-8");
- String decrypt = decrypt(decodedUrl, appSecret);
- // logger.info("decrypt = " + decrypt);
- if (ObjectUtils.isNotEmpty(decrypt)&& decrypt!="") {
- JSONObject jsonObject2 = JSONObject.parseObject(decrypt);
- if (ObjectUtils.isNotEmpty(jsonObject2)) {
- // 获取总数
- Integer total = Integer.valueOf(jsonObject2.getString("total"));
- // logger.info("total = " + total);
- if (total > pageindex * pagesize) {
- page = pageindex + 1;
- } else {
- page = pageindex - 1;
- }
- JSONArray data2 = jsonObject2.getJSONArray("data");
- // 去重
- HashSet<SmartFaceDiscern> smartFaceDiscerns = new HashSet<>();
- for (int i = 0; i < data2.size(); i++) {
- JSONObject jsonObject3 = data2.getJSONObject(i);
- SmartFaceDiscern smartFaceDiscern = new SmartFaceDiscern();
- // 事件
- String deviceName = jsonObject3.getString("device_name");
- smartFaceDiscern.setType(deviceName);
- // 地点
- String deviceArea = jsonObject3.getString("device_area");
- smartFaceDiscern.setLocation(deviceArea);
- // 图片
- String recordPhoto = jsonObject3.getString("record_photo");
- smartFaceDiscern.setImage(recordPhoto);
- // 时间
- String recordAddtime = jsonObject3.getString("record_addtime");
- smartFaceDiscern.setDateTime(recordAddtime);
- // 名字
- String student_name = jsonObject3.getString("student_name");
- smartFaceDiscern.setName(student_name);
- // 学号
- String student_number = jsonObject3.getString("student_number");
- // 编号
- String student_no = jsonObject3.getString("student_no");
- LambdaQueryWrapper<SmartUser> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(SmartUser::getBsStudentNo, student_no);
- wrapper.eq(SmartUser::getName, student_name);
- SmartUser smartUser = smartUserService.getOne(wrapper);
- if (ObjectUtils.isNotEmpty(smartUser)) {
- smartFaceDiscern.setUserId(smartUser.getId());
- } else {
- smartFaceDiscern.setUserId(0);
- }
- smartFaceDiscerns.add(smartFaceDiscern);
- }
- for (SmartFaceDiscern smartFaceDiscern : smartFaceDiscerns) {
- Integer userId = smartFaceDiscern.getUserId();
- String deviceName = smartFaceDiscern.getType();
- String deviceArea = smartFaceDiscern.getLocation();
- String recordPhoto = smartFaceDiscern.getImage();
- String recordAddtime = smartFaceDiscern.getDateTime();
- String student_name = smartFaceDiscern.getName();
- if (userId != 0) {
- // 判断是否已经添加
- LambdaQueryWrapper<SmartFaceDiscern> wrapperFD = new LambdaQueryWrapper<>();
- wrapperFD.eq(SmartFaceDiscern::getDateTime, smartFaceDiscern.getDateTime())
- .eq(SmartFaceDiscern::getUserId, userId);
- List<SmartFaceDiscern> list = this.list(wrapperFD);
- if (ObjectUtils.isEmpty(list) && list.size() == 0) {
- this.save(smartFaceDiscern);
- // 通过学生id找到关联的家长affiliate,并找到公众号,如果openid为空则不传
- List<SmartUser> userList = smartUserService.getAffiliateList(userId);
- if (ObjectUtils.isNotEmpty(userList) && userList.size() > 0) {
- SmartUser smartUser = smartUserService.getById(userId);
- for (SmartUser user : userList) {
- String gzhOpenId = user.getGzhOpenId();
- if (ObjectUtils.isNotEmpty(gzhOpenId)) {
- String pushType = "";
- if (deviceName.contains("进校")) {
- pushType = "进入大门";
- } else if (deviceName.contains("出校")) {
- pushType = "离开大门";
- }
- // 时间格式是 yyyy-MM-dd HH:mm:ss,当前时间格式有问题
- LocalDateTime date = LocalDateTime.now();
- String format = date.format(dateTimeFormatter1);
- // 公众号信息推送
- Message2.send(gzhOpenId, pushType, deviceArea, format);
- SmartNotification smartNotification = new SmartNotification();
- smartNotification.setUserId(smartUser.getId());
- smartNotification.setUserName(smartUser.getName());
- smartNotification.setTypeName(pushType);
- smartNotification.setLocation(deviceArea);
- smartNotification.setImage(recordPhoto);
- smartNotification.setDateTime(recordAddtime);
- smartNotification.setType(2);
- smartNotificationService.save(smartNotification);
- }
- }
- }
- }
- } else {
- smartFaceDiscern.setUserId(0);
- // 判断是否已经添加
- LambdaQueryWrapper<SmartFaceDiscern> wrapperFD = new LambdaQueryWrapper<>();
- wrapperFD.eq(SmartFaceDiscern::getDateTime, recordAddtime)
- .eq(SmartFaceDiscern::getName, student_name)
- .eq(SmartFaceDiscern::getType, deviceName);
- List<SmartFaceDiscern> list = this.list(wrapperFD);
- if (ObjectUtils.isEmpty(list) && list.size() == 0) {
- this.save(smartFaceDiscern);
- }
- }
- }
- }
- }
- }
- @Override
- public List<SmartFaceDiscern> track(String stateTime, String endTime, Integer id) {
- LambdaQueryWrapper<SmartFaceDiscern> wrapper = new LambdaQueryWrapper<>();
- wrapper.between(SmartFaceDiscern::getDateTime, stateTime, endTime)
- .eq(SmartFaceDiscern::getUserId, id)
- .orderByDesc(SmartFaceDiscern::getDateTime);
- List<SmartFaceDiscern> list = this.list(wrapper);
- return list;
- }
- }
|