浏览代码

人员进出统计

chejianzheng 1 月之前
父节点
当前提交
87d4e46cf4

+ 0 - 7
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/mapper/PersonInOutMapper.java

@@ -3,9 +3,6 @@ package com.hfln.portal.infrastructure.mapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.hfln.portal.infrastructure.po.PersonInOutInfo;
 import org.apache.ibatis.annotations.Mapper;
-import org.apache.ibatis.annotations.Select;
-
-import java.time.LocalDate;
 
 /**
  *  Mapper 接口
@@ -13,8 +10,4 @@ import java.time.LocalDate;
 @Mapper
 public interface PersonInOutMapper extends BaseMapper<PersonInOutInfo> {
 
-    @Select("" +
-            "SELECT COUNT(*) FROM " +
-            "(SELECT dev_id FROM tbl_person_in_out WHERE tenant_id = #{tenantId} AND create_time >= #{startTime} GROUP BY dev_id HAVING COUNT(*) > 3)")
-    Long countActive(Long tenantId, LocalDate startTime);
 }

+ 14 - 14
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/mqtt/MqttSubHandle.java

@@ -21,7 +21,9 @@ import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
 import java.math.BigDecimal;
+import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -367,23 +369,21 @@ public class MqttSubHandle {
                 new LambdaUpdateWrapper<DevInfo>()
                         .eq(DevInfo::getClientId, clientId)
                         .set(DevInfo::getExistFlag, presence == 1 ? DevInfo.Constants.ExistFlag.EXIST : DevInfo.Constants.ExistFlag.NOT_EXIST)
-                        .set(DevInfo::getPresenceChangeTime,
-//                                obj.get("timestamp") != null
-//                                        ? LocalDateTime.ofInstant(Instant.ofEpochMilli(obj.getLongValue("timestamp")), ZoneId.of("UTC"))
-//                                        :
-                                        LocalDateTime.now())
+                        .set(DevInfo::getPresenceChangeTime, LocalDateTime.now())
         );
 
+
+        PersonInOutInfo personInOut = personInOutService.queryOneByDevId(dev.getDevId(), DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDate.now()));
         if (presence == 1) {
-            PersonInOutInfo personInOut = new PersonInOutInfo();
-            personInOut.setTenantId(dev.getTenantId());
-            personInOut.setDevId(dev.getDevId());
-            personInOut.setEnterTime(LocalDateTime.now());
-            personInOutService.save(personInOut);
-        } else if (presence == 0) {
-            PersonInOutInfo personInOut = personInOutService.queryOneByDevId(dev.getDevId());
-            if (personInOut != null) {
-                personInOut.setLeaveTime(LocalDateTime.now());
+            if (personInOut == null) {
+                personInOut = new PersonInOutInfo();
+                personInOut.setTenantId(dev.getTenantId());
+                personInOut.setDevId(dev.getDevId());
+                personInOut.setCountDate(DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDate.now()));
+                personInOut.setCount(1);
+                personInOutService.save(personInOut);
+            } else {
+                personInOut.setCount(personInOut.getCount() + 1);
                 personInOutService.updateById(personInOut);
             }
         }

+ 6 - 6
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/po/PersonInOutInfo.java

@@ -6,8 +6,6 @@ import com.baomidou.mybatisplus.annotation.TableName;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 
-import java.time.LocalDateTime;
-
 /**
  * 人员进出表
  */
@@ -33,12 +31,14 @@ public class PersonInOutInfo extends BasePO {
     private Long devId;
 
     /**
-     * 进入时间
+     * 统计日期 yyyyMMdd
      */
-    private LocalDateTime enterTime;
+    private String countDate;
+
 
     /**
-     * 离开时间
+     * 统计次数
      */
-    private LocalDateTime leaveTime;
+    private Integer count;
+
 } 

+ 1 - 1
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/PersonInOutService.java

@@ -5,7 +5,7 @@ import com.hfln.portal.infrastructure.po.PersonInOutInfo;
 
 public interface PersonInOutService extends IService<PersonInOutInfo> {
 
-    PersonInOutInfo queryOneByDevId(Long devId);
+    PersonInOutInfo queryOneByDevId(Long devId, String countDate);
 
     Long countActive(Long tenantId);
 }

+ 9 - 4
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/impl/PersonInOutServiceImpl.java

@@ -9,22 +9,27 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
 import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
 
 @Service
 @Slf4j
 public class PersonInOutServiceImpl extends ServiceImpl<PersonInOutMapper, PersonInOutInfo> implements PersonInOutService {
     @Override
-    public PersonInOutInfo queryOneByDevId(Long devId) {
+    public PersonInOutInfo queryOneByDevId(Long devId, String countDate) {
 
         LambdaUpdateWrapper<PersonInOutInfo> wrapper = new LambdaUpdateWrapper<>();
         wrapper.eq(PersonInOutInfo::getDevId, devId)
-                .orderByDesc(PersonInOutInfo::getCreateTime)
-                .last("limit 1");
+                .eq(PersonInOutInfo::getCountDate, countDate);
         return this.baseMapper.selectOne(wrapper);
     }
 
     @Override
     public Long countActive(Long tenantId) {
-        return this.baseMapper.countActive(tenantId, LocalDate.now());
+
+        LambdaUpdateWrapper<PersonInOutInfo> wrapper = new LambdaUpdateWrapper<>();
+        wrapper.eq(PersonInOutInfo::getTenantId, tenantId)
+                .eq(PersonInOutInfo::getCountDate, DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDate.now()))
+                .ge(PersonInOutInfo::getCount, 3);
+        return this.baseMapper.selectCount(wrapper);
     }
 }