zhangzengfei
2024-09-29 3737ab3dd0cc753be986638316c96cb3114601e4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package db
 
import (
    "basic.com/valib/logger.git"
    "gorm.io/gorm"
    "time"
)
 
type PersonStatus struct {
    ID                       string `gorm:"primary_key;column:id;type:varchar(255);" json:"id"`
    CreatedAt                time.Time
    UpdatedAt                time.Time
    DeletedAt                gorm.DeletedAt `gorm:"index" json:"-"`
    OrgId                    string         `gorm:"column:org_id"`                      // 派出所id
    CommunityID              string         `gorm:"column:community_id"`                // 小区id
    DocumentNumber           string         `gorm:"column:document_number"`             // 档案编号
    DaysAppeared             int            `gorm:"column:days_appeared"`               // 出现天数
    Count                    int            `gorm:"column:count"`                       // 抓拍次数
    Status                   string         `gorm:"column:status"`                      // 标签
    LastAppearanceTime       int64          `gorm:"column:last_appearance_time"`        // 最后出现时间
    LastAppearanceStatusTime int64          `gorm:"column:last_appearance_status_time"` // 最后出现时间
    LastLocation             string         `gorm:"column:last_location"`               // 最后出现地点
    FrequentAddress          string         `gorm:"column:frequent_address"`            // 常住地址
}
 
func (PersonStatus) TableName() string {
    return "snapshot_count_summary"
}
 
type Resident struct {
    CommunityId        string `gorm:"column:community_id"`
    DocumentNumber     string `gorm:"column:document_number"`
    LastAppearanceTime int64  `gorm:"column:last_appearance_time"`
    CreateAt           string `gorm:"column:create_at"`
}
 
// 查询住户时间数据
func GetResidentData(status, communityID string) ([]Resident, error) {
    var residents []Resident
    //var db = DB.Debug()
    var db = DB
    // 执行查询
    rows, err := db.Table("snapshot_count_summary").
        Select("document_number", "community_id", "last_appearance_time", "created_at").
        Where("status = ? AND community_id = ?", status, communityID).
        //Where("snapshot_count_summary.created_at is not null").
        Rows()
    if err != nil {
        return nil, err
    }
    defer rows.Close()
 
    // 遍历查询结果
    for rows.Next() {
        var resident Resident
        err := rows.Scan(&resident.DocumentNumber, &resident.CommunityId, &resident.LastAppearanceTime, &resident.CreateAt)
        if err != nil {
            logger.Error("err: ", err)
            return nil, err
        }
        //fmt.Println("resident111: ", resident)
        residents = append(residents, resident)
    }
    if err := rows.Err(); err != nil {
        return nil, err
    }
 
    return residents, nil
}
 
// 查询人物属性
func GetDBPersonStatus(id, communityId string) string {
    var db = DB
 
    // 查询数据
    var personStatus string
    db.Table("snapshot_count_summary").
        Select("status").
        Where("community_id = ? and document_number = ?", communityId, id).
        Find(&personStatus)
 
    return personStatus
}
 
// 查询小区档案表 (原查询任务属性)
func QueryPersonStatusWithPagination(community_id string, timeThreshold int64) ([]*PersonStatus, error) {
    var db = DB
    var personStatusList []*PersonStatus
    result := db.Select("document_number, status, frequent_address, last_appearance_time, last_appearance_status_time").
        Where("community_id = ? AND last_appearance_time != last_appearance_status_time AND last_appearance_time > ?", community_id, timeThreshold).
        Find(&personStatusList)
    if result.Error != nil {
        logger.Error(result.Error)
        return nil, result.Error
    }
    return personStatusList, nil
}
 
// UpdatePersonInfo 更新或插入多个人员信息
func UpdatePersonInfo(persons []PersonStatus, communityID string) error {
    var db = DB
    // 遍历人员信息
    for _, person := range persons {
        err := db.Model(&PersonStatus{}).
            Where("document_number = ? AND community_id = ?", person.DocumentNumber, communityID).
            Updates(map[string]interface{}{
                "status":                   person.Status,
                "frequent_address":         person.FrequentAddress,
                "LastAppearanceStatusTime": person.LastAppearanceStatusTime,
            }).Error
        if err != nil {
            return err
        }
    }
 
    return nil
}