Просмотр исходного кода

定时获取亲密度的数据

liu 2 лет назад
Родитель
Сommit
a8c33a64cc

+ 7 - 0
src/main/java/com/template/api/SmartRelationControllerAPI.java

@@ -0,0 +1,7 @@
+package com.template.api;
+
+import org.springframework.web.bind.annotation.RequestMapping;
+
+@RequestMapping("/api/smartRelation")
+public interface SmartRelationControllerAPI {
+}

+ 28 - 0
src/main/java/com/template/controller/SmartRelationController.java

@@ -0,0 +1,28 @@
+package com.template.controller;
+
+
+import com.template.annotation.DESRespondSecret;
+import com.template.api.SmartRelationControllerAPI;
+import com.template.model.pojo.SmartRelation;
+import com.template.services.SmartRelationService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * <p>
+ *  前端控制器
+ * </p>
+ *
+ * @author ceshi
+ * @since 2024-06-28
+ */
+@RestController
+@DESRespondSecret
+public class SmartRelationController implements SmartRelationControllerAPI {
+
+    @Autowired
+    SmartRelationService smartRelationService;
+}
+

+ 16 - 0
src/main/java/com/template/mapper/SmartRelationMapper.java

@@ -0,0 +1,16 @@
+package com.template.mapper;
+
+import com.template.model.pojo.SmartRelation;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * <p>
+ *  Mapper 接口
+ * </p>
+ *
+ * @author ceshi
+ * @since 2024-06-28
+ */
+public interface SmartRelationMapper extends BaseMapper<SmartRelation> {
+
+}

+ 73 - 0
src/main/java/com/template/model/pojo/SmartRelation.java

@@ -0,0 +1,73 @@
+package com.template.model.pojo;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import java.util.Date;
+import com.baomidou.mybatisplus.annotation.Version;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.FieldFill;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.annotation.TableField;
+import java.io.Serializable;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+
+/**
+ * <p>
+ * 
+ * </p>
+ *
+ * @author ceshi
+ * @since 2024-06-28
+ */
+@Data
+@EqualsAndHashCode(callSuper = false)
+@Accessors(chain = true)
+@ApiModel(value="SmartRelation对象", description="")
+public class SmartRelation implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId(value = "id", type = IdType.AUTO)
+    private Integer id;
+
+    @ApiModelProperty(value = "用户id")
+    private Integer userId;
+
+    @ApiModelProperty(value = "名字")
+    private String name;
+
+    @ApiModelProperty(value = "关联人id")
+    private Integer relationId;
+
+    @ApiModelProperty(value = "关联人名称")
+    private String relationName;
+
+    @ApiModelProperty(value = "次数")
+    private Integer count;
+
+    @ApiModelProperty(value = "创建时间")
+    @TableField(fill = FieldFill.INSERT)
+    private Date createTime;
+
+    @ApiModelProperty(value = "更新时间")
+    @TableField(fill = FieldFill.INSERT_UPDATE)
+    private Date updateTime;
+
+    @ApiModelProperty(value = "创建人员")
+    @TableField(fill = FieldFill.INSERT)
+    private String createUser;
+
+    @ApiModelProperty(value = "更新人员")
+    @TableField(fill = FieldFill.INSERT_UPDATE)
+    private String updateUser;
+
+    @ApiModelProperty(value = "逻辑删除 未删除:0;删除:1")
+    @TableField(fill = FieldFill.INSERT)
+    @TableLogic
+    private Integer deleted;
+
+
+}

+ 3 - 0
src/main/java/com/template/services/SmartAccessService.java

@@ -6,6 +6,7 @@ import com.template.model.result.PageUtils;
 import com.template.model.vo.SmartAccessVo;
 import org.springframework.stereotype.Repository;
 
+import java.time.LocalDateTime;
 import java.util.List;
 
 /**
@@ -25,4 +26,6 @@ public interface SmartAccessService extends IService<SmartAccess> {
     List<SmartAccessVo> getPageExport(String keyWord, Integer gradeId, Integer classId, Integer departmentId,String openType,String resultStatus, String startTime, String endTime);
 
     PageUtils<SmartAccessVo> getAccessPage(Integer currentPage, Integer pageCount, String keyWord, Integer gradeId, Integer classId, String resultStatus, String inOut, String startTime, String endTime);
+
+    List<SmartAccess> toDatelist(LocalDateTime start, LocalDateTime end);
 }

+ 2 - 0
src/main/java/com/template/services/SmartFaceDiscernService.java

@@ -42,4 +42,6 @@ public interface SmartFaceDiscernService extends IService<SmartFaceDiscern> {
     PageUtils<FaceManagementVo> faceManagement(int currentPage, int pageCount, String keyWord,String gradeId,String classId,String startTime,String endTime);
 
     List<FaceManagementVo> faceManagementExport(String keyWord, String gradeId, String classId, String startTime, String endTime);
+
+    List<SmartFaceDiscern> toDateList(LocalDateTime start, LocalDateTime end);
 }

+ 16 - 0
src/main/java/com/template/services/SmartRelationService.java

@@ -0,0 +1,16 @@
+package com.template.services;
+
+import com.template.model.pojo.SmartRelation;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * <p>
+ *  服务类
+ * </p>
+ *
+ * @author ceshi
+ * @since 2024-06-28
+ */
+public interface SmartRelationService extends IService<SmartRelation> {
+
+}

+ 9 - 0
src/main/java/com/template/services/impl/SmartAccessServiceImpl.java

@@ -14,6 +14,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.time.LocalDateTime;
 import java.util.List;
 
 /**
@@ -63,4 +64,12 @@ public class SmartAccessServiceImpl extends ServiceImpl<SmartAccessMapper, Smart
         IPage<SmartAccessVo> datas = smartAccessMapper.getAccessPage(page,keyWord,gradeId,classId,resultStatus,inOut,startTime,endTime);
         return new PageUtils(datas);
     }
+
+    @Override
+    public List<SmartAccess> toDatelist(LocalDateTime start, LocalDateTime end) {
+        LambdaQueryWrapper<SmartAccess> wrapper=new LambdaQueryWrapper<>();
+        wrapper.between(SmartAccess::getDateTime,start,end);
+        List<SmartAccess> list = this.list(wrapper);
+        return list;
+    }
 }

+ 9 - 0
src/main/java/com/template/services/impl/SmartFaceDiscernServiceImpl.java

@@ -2,6 +2,7 @@ package com.template.services.impl;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
@@ -310,4 +311,12 @@ public class SmartFaceDiscernServiceImpl extends ServiceImpl<SmartFaceDiscernMap
         List<FaceManagementVo> vos = smartFaceDiscernMapper.faceManagementExport(keyWord,gradeId,classId,startTime,endTime);
         return vos;
     }
+
+    @Override
+    public List<SmartFaceDiscern> toDateList(LocalDateTime start, LocalDateTime end) {
+        LambdaQueryWrapper<SmartFaceDiscern> wrapper=new LambdaQueryWrapper<>();
+        wrapper.between(SmartFaceDiscern::getDateTime,start,end);
+        List<SmartFaceDiscern> list = this.list(wrapper);
+        return list;
+    }
 }

+ 165 - 0
src/main/java/com/template/services/impl/SmartRelationServiceImpl.java

@@ -0,0 +1,165 @@
+package com.template.services.impl;
+
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
+import com.template.config.ScheduleConfig;
+import com.template.model.pojo.SmartAccess;
+import com.template.model.pojo.SmartFaceDiscern;
+import com.template.model.pojo.SmartRelation;
+import com.template.mapper.SmartRelationMapper;
+import com.template.model.pojo.SmartUser;
+import com.template.services.SmartAccessService;
+import com.template.services.SmartFaceDiscernService;
+import com.template.services.SmartRelationService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+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.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.List;
+
+/**
+ * <p>
+ * 服务实现类
+ * </p>
+ *
+ * @author ceshi
+ * @since 2024-06-28
+ */
+@Service
+public class SmartRelationServiceImpl extends ServiceImpl<SmartRelationMapper, SmartRelation> implements SmartRelationService {
+
+    @Resource
+    private ScheduleConfig scheduleConfig;
+
+    @Autowired
+    SmartAccessService smartAccessService;
+
+    @Autowired
+    SmartFaceDiscernService smartFaceDiscernService;
+
+    @Autowired
+    SmartUserService smartUserService;
+
+    @Async
+    @Scheduled(cron = "0 0 1 * * ? ")//每天凌晨一点
+    public void getAccessSmartRelation() {
+        if (scheduleConfig.getIsOpen().equals("1")) {
+            DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+            LocalDateTime now = LocalDateTime.now();
+            LocalDateTime start = now.withHour(0).withMinute(0).withSecond(0).minusDays(1);
+            LocalDateTime end = now.withHour(23).withMinute(59).withSecond(59).minusDays(1);
+//            找到昨天的所有数据
+            List<SmartAccess> accessList = smartAccessService.toDatelist(start, end);
+
+            for (SmartAccess smartAccess : accessList) {
+                String dateTime = smartAccess.getDateTime();
+                LocalDateTime date = LocalDateTime.parse(dateTime, pattern);
+                LocalDateTime startTime = date.minusSeconds(10);
+                LocalDateTime endTime = date.plusSeconds(10);
+                Integer userId = smartAccess.getUserId();
+                SmartUser smartUser = smartUserService.getSmartById(userId);
+                List<SmartAccess> accesses = smartAccessService.toDatelist(startTime, endTime);
+                if (ObjectUtils.isNotEmpty(accesses) && accesses.size() > 0) {
+
+                    for (SmartAccess access : accesses) {
+                        Integer userId1 = access.getUserId();
+                        SmartUser smartById = smartUserService.getSmartById(userId1);
+//                        判断是否已添加,有的话则加1
+                        LambdaQueryWrapper<SmartRelation> wrapper = new LambdaQueryWrapper<>();
+                        wrapper.eq(SmartRelation::getUserId, userId)
+                                .eq(SmartRelation::getRelationId, userId1);
+                        SmartRelation one = this.getOne(wrapper);
+                        if (ObjectUtils.isEmpty(one)) {
+                            SmartRelation smartRelation = new SmartRelation();
+                            smartRelation.setUserId(userId);
+                            smartRelation.setRelationId(userId1);
+                            if (ObjectUtils.isNotEmpty(smartUser)) {
+                                smartRelation.setName(smartUser.getName());
+                            }
+                            if (ObjectUtils.isNotEmpty(smartById)) {
+                                smartRelation.setRelationName(smartById.getName());
+                            }
+                            smartRelation.setCount(0);
+                            this.save(smartRelation);
+                        } else {
+                            Integer count = one.getCount();
+                            count = count + 1;
+                            one.setCount(count);
+                            this.updateById(one);
+                        }
+
+                    }
+
+                }
+
+            }
+
+        }
+    }
+
+    @Async
+    @Scheduled(cron = "0 0 2 * * ? ")//每天凌晨一点
+    public void getFaceDiscernListSmartRelation() {
+        if (scheduleConfig.getIsOpen().equals("1")) {
+            DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+            LocalDateTime now = LocalDateTime.now();
+            LocalDateTime start = now.withHour(0).withMinute(0).withSecond(0).minusDays(1);
+            LocalDateTime end = now.withHour(23).withMinute(59).withSecond(59).minusDays(1);
+//            找到昨天的所有数据
+            List<SmartFaceDiscern> faceDiscernList = smartFaceDiscernService.toDateList(start, end);
+
+            for (SmartFaceDiscern faceDiscern : faceDiscernList) {
+                String dateTime = faceDiscern.getDateTime();
+                LocalDateTime date = LocalDateTime.parse(dateTime, pattern);
+                LocalDateTime startTime = date.minusSeconds(10);
+                LocalDateTime endTime = date.plusSeconds(10);
+                Integer userId = faceDiscern.getUserId();
+                SmartUser smartUser = smartUserService.getSmartById(userId);
+                List<SmartFaceDiscern> faceDiscerns = smartFaceDiscernService.toDateList(startTime, endTime);
+                if (ObjectUtils.isNotEmpty(faceDiscerns) && faceDiscerns.size() > 0) {
+
+                    for (SmartFaceDiscern faceDiscern1 : faceDiscerns) {
+                        Integer userId1 = faceDiscern1.getUserId();
+                        SmartUser smartById = smartUserService.getSmartById(userId1);
+//                        判断是否已添加,有的话则加1
+                        LambdaQueryWrapper<SmartRelation> wrapper = new LambdaQueryWrapper<>();
+                        wrapper.eq(SmartRelation::getUserId, userId)
+                                .eq(SmartRelation::getRelationId, userId1);
+                        SmartRelation one = this.getOne(wrapper);
+                        if (ObjectUtils.isEmpty(one)) {
+                            SmartRelation smartRelation = new SmartRelation();
+                            smartRelation.setUserId(userId);
+                            smartRelation.setRelationId(userId1);
+                            if (ObjectUtils.isNotEmpty(smartUser)) {
+                                smartRelation.setName(smartUser.getName());
+                            }
+                            if (ObjectUtils.isNotEmpty(smartById)) {
+                                smartRelation.setRelationName(smartById.getName());
+                            }
+                            smartRelation.setCount(0);
+                            this.save(smartRelation);
+                        } else {
+                            Integer count = one.getCount();
+                            count = count + 1;
+                            one.setCount(count);
+                            this.updateById(one);
+                        }
+
+                    }
+
+                }
+
+            }
+
+        }
+    }
+
+
+}