| | |
| | | "basic.com/valib/logger.git" |
| | | "errors" |
| | | "gorm.io/gorm" |
| | | "strconv" |
| | | ) |
| | | |
| | | // 查询小区表 |
| | |
| | | if result.Error != nil { |
| | | return nil, result.Error |
| | | } |
| | | |
| | | return device, nil |
| | | } |
| | | |
| | | func GetPublicHouseData() ([]PublicHouse, error) { |
| | | // 查询数据 |
| | | var db = DB |
| | | var ph []PublicHouse |
| | | result := db.Table("public_house").Where("1 = 1").Find(&ph) |
| | | if result.Error != nil { |
| | | return nil, result.Error |
| | | } |
| | | return ph, nil |
| | | } |
| | | |
| | | // 查询全部数据 |
| | |
| | | } |
| | | |
| | | // 查询人物属性 |
| | | func GetDBPersonStatusData(id string) ([]PersonStatus, error) { |
| | | func GetDBPersonStatus(id, communityId string) string { |
| | | var db = DB |
| | | // 查询数据 |
| | | var personStatusList []PersonStatus |
| | | if err := db.Table("person_status"). |
| | | Select("documentNumber, status, frequentAddress"). |
| | | Where("communityID = ?", id). |
| | | Find(&personStatusList).Error; err != nil { |
| | | return nil, err |
| | | } |
| | | |
| | | // 查询数据 |
| | | 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 |
| | | } |
| | | |
| | |
| | | func GetAgeById(id string) (int, error) { |
| | | var db = DB |
| | | // 查询数据 |
| | | var age int |
| | | var age string |
| | | if err := db.Table("dbtablepersons"). |
| | | Select("age"). |
| | | Where("id = ?", id). |
| | |
| | | return 0, err |
| | | } |
| | | |
| | | return age, nil |
| | | return strconv.Atoi(age) |
| | | } |
| | | |
| | | // 查询人物身份证号 |
| | | func GetIdCardById(id string) string { |
| | | var db = DB |
| | | // 查询数据 |
| | | var idCard string |
| | | db.Table("dbtablepersons"). |
| | | Select("idCard"). |
| | | Where("id = ?", id). |
| | | Find(&idCard) |
| | | |
| | | return idCard |
| | | } |
| | | |
| | | //// 根据社区id和住户属性查询住户档案编号 |
| | |
| | | // 根据dbtablepersons表id查询目标档案年龄 |
| | | func QueryAgeById(id string) (int, error) { |
| | | var db = DB |
| | | var age int |
| | | var age string |
| | | err := db.Table("dbtablepersons"). |
| | | Select("age"). |
| | | Where("id = ?", id). |
| | |
| | | if err != nil { |
| | | return 0, err |
| | | } |
| | | return age, nil |
| | | |
| | | return strconv.Atoi(age) |
| | | } |
| | | |
| | | // UpdatePersonInfo 更新或插入多个人员信息 |
| | |
| | | var db = DB |
| | | // 遍历人员信息 |
| | | for _, person := range persons { |
| | | |
| | | // 检查记录是否存在 |
| | | var existingPerson PersonStatus |
| | | err := db.Where("document_number = ? AND community_id = ?", person.DocumentNumber, communityID).First(&existingPerson).Error |
| | | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| | | logger.Error("Query person error:", err, person.DocumentNumber, communityID) |
| | | //fmt.Println("asdasfasfasf") |
| | | continue |
| | | //return err |
| | | 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 |
| | | } |
| | | |
| | | // 如果记录存在,则更新 |
| | | if existingPerson.DocumentNumber != "" { |
| | | err := db.Model(&PersonStatus{}). |
| | | Where("document_number = ? AND community_id = ?", person.DocumentNumber, communityID). |
| | | Updates(map[string]interface{}{ |
| | | "status": person.Status, |
| | | "frequent_address": person.FrequentAddress, |
| | | }).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | } else { |
| | | // 如果记录不存在,则插入新记录 |
| | | err := db.Create(&person).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | return nil |