| | |
| | | ) |
| | | |
| | | // 计算抓拍天数 |
| | | func CalculateCaptureDays(details []db.CaptureDetail) int { |
| | | func CalculateCaptureDays(details []db.CaptureDetail) (int, int) { |
| | | // 使用 map 来存储每天是否有抓拍记录 |
| | | captureMap := make(map[string]bool) |
| | | for _, detail := range details { |
| | | //pointDate := "" |
| | | //pointTime := "" |
| | | //pointDirection := "" |
| | | overnightCount := 0 |
| | | for i, detail := range details { |
| | | // 解析抓拍日期 |
| | | layout := "2006-01-02 15:04:05" |
| | | captureTime, err := time.Parse(layout, detail.CaptureDate) |
| | |
| | | fmt.Println("解析抓拍日期时出错:", err) |
| | | continue |
| | | } |
| | | //fmt.Println(captureTime, detail.Direction) |
| | | // 获取日期部分 |
| | | date := captureTime.Format("2006-01-02") |
| | | //time := captureTime.Format("15:04:05") |
| | | // 在 map 中标记这一天有抓拍记录 |
| | | captureMap[date] = true |
| | | if i == len(details)-1 { |
| | | break |
| | | } |
| | | |
| | | // 第一个验证条件:当前为 in,时间在下午 16 点以后 |
| | | currTime, _ := time.Parse("2006-01-02 15:04:05", detail.CaptureDate) |
| | | if detail.Direction == "in" && currTime.Hour() >= 16 { |
| | | // 第二个验证条件:下一个为 out,时间在上午 5 点之后 12 点之前 |
| | | nextDetail := details[i+1] |
| | | nextTime, _ := time.Parse("2006-01-02 15:04:05", nextDetail.CaptureDate) |
| | | nextDay := nextTime.AddDate(0, 0, -1).Format("2006-01-02") |
| | | if nextDetail.Direction == "out" && nextTime.Hour() >= 5 && nextTime.Hour() < 12 && nextDay == detail.CaptureDate[:10] { |
| | | overnightCount++ |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 统计有抓拍记录的天数 |
| | |
| | | captureDays++ |
| | | } |
| | | |
| | | return captureDays |
| | | return captureDays, overnightCount |
| | | } |
| | | |
| | | // 设置状态 |
| | |
| | | } |
| | | |
| | | // SetFrequentAddress 方法计算出现最频繁的出行地址并设置为常用地址 |
| | | func SetFrequentAddress(c *db.CaptureInfo) { |
| | | func GetFrequentAddress(captureDetail []db.CaptureDetail) string { |
| | | outAddressCounts := make(map[string]int) |
| | | inAddressCounts := make(map[string]int) |
| | | // 统计每个出行地址的出现次数 |
| | | for _, detail := range c.CaptureDetail { |
| | | for _, detail := range captureDetail { |
| | | if detail.Direction == "out" { |
| | | outAddressCounts[detail.CaptureAddress]++ |
| | | } |
| | |
| | | } |
| | | } |
| | | } |
| | | // 将出现次数最多的出行地址设置为常用地址 |
| | | c.FrequentAddress = frequentAddress |
| | | return frequentAddress |
| | | } |
| | | |
| | | // processData 函数处理数据,根据要求过滤掉数据并根据规则更新状态 |
| | | func ProcessData(captureInfos []db.CaptureInfo, personStatus []db.PersonStatus, ruleInfos []db.PersonnelStatusRule, communityID string) []db.PersonStatus { |
| | | func ProcessData(captureInfos []db.CaptureInfo, personStatus []*db.PersonStatus, ruleInfos []db.PersonnelStatusRule, communityID string) []db.PersonStatus { |
| | | filteredInfos := make([]db.PersonStatus, 0) |
| | | |
| | | // 构建快速查找索引,方便查找对应的人员状态和规则 |
| | |
| | | ruleIndex := make(map[string]db.PersonnelStatusRule) |
| | | |
| | | for _, person := range personStatus { |
| | | statusIndex[person.DocumentNumber] = person |
| | | statusIndex[person.DocumentNumber] = *person |
| | | } |
| | | |
| | | for _, rule := range ruleInfos { |
| | | ruleIndex[rule.Name] = rule |
| | | } |
| | | |
| | | //fmt.Println("statusIndex: ", statusIndex) |
| | | //fmt.Println("ruleIndex: ", ruleIndex) |
| | | // 处理每个抓拍信息 |
| | | for _, info := range captureInfos { |
| | | //fmt.Println("info", info.DocumentNumber, info.Status, info.FrequentAddress) |
| | | //fmt.Println("person", statusIndex[info.DocumentNumber].DocumentNumber, statusIndex[info.DocumentNumber].Status, statusIndex[info.DocumentNumber].FrequentAddress) |
| | | // 检查是否存在对应的人员状态 |
| | | person, ok := statusIndex[info.DocumentNumber] |
| | | if !ok { |
| | | // 不存在对应的人员状态为新数据 |
| | | filteredInfos = append(filteredInfos, db.PersonStatus{CommunityID: communityID, DocumentNumber: info.DocumentNumber, Status: info.Status, FrequentAddress: info.FrequentAddress}) |
| | | continue |
| | | } |
| | | |
| | | person := statusIndex[info.DocumentNumber] |
| | | //fmt.Println("person: ", person.DocumentNumber, person.Status, person.FrequentAddress, person.LastAppearanceTime, person.LastAppearanceStatusTime) |
| | | // 判断状态和常用地址是否相等,如果相等则忽略 |
| | | if (info.Status == person.Status || info.CaptureDays <= ruleIndex[person.DocumentNumber].DetectionDaysEnd) && |
| | | info.FrequentAddress == person.FrequentAddress { |
| | | continue |
| | | } |
| | | |
| | | //if (info.Status == person.Status || info.CaptureDays <= ruleIndex[person.DocumentNumber].DetectionDaysEnd) && |
| | | // info.FrequentAddress == person.FrequentAddress { |
| | | // continue |
| | | //} |
| | | // 更新过滤后的信息列表 |
| | | filteredInfos = append(filteredInfos, db.PersonStatus{CommunityID: communityID, DocumentNumber: info.DocumentNumber, Status: info.Status, FrequentAddress: info.FrequentAddress}) |
| | | //fmt.Println("LastAppearanceTime: ", person.LastAppearanceTime) |
| | | filteredInfos = append(filteredInfos, db.PersonStatus{CommunityID: communityID, DocumentNumber: info.DocumentNumber, Status: info.Status, FrequentAddress: info.FrequentAddress, LastAppearanceStatusTime: person.LastAppearanceTime}) |
| | | |
| | | } |
| | | |
| | | //fmt.Println("filteredInfos: ", filteredInfos) |
| | | return filteredInfos |
| | | } |