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
|
}
|