From 3737ab3dd0cc753be986638316c96cb3114601e4 Mon Sep 17 00:00:00 2001 From: zhangzengfei <zhangzengfei@smartai.com> Date: 星期日, 29 九月 2024 16:20:46 +0800 Subject: [PATCH] fix db column --- db/repository.go | 280 ------------------------------------------------------- 1 files changed, 2 insertions(+), 278 deletions(-) diff --git a/db/repository.go b/db/repository.go index 5cdc1d8..f948b01 100644 --- a/db/repository.go +++ b/db/repository.go @@ -1,290 +1,14 @@ package db -import ( - "basic.com/valib/logger.git" - "errors" - "gorm.io/gorm" -) - // 鏌ヨ灏忓尯琛� func GetCommunityIDs() ([]string, error) { - db, err := ConnectDB() - if err != nil { - return nil, err - } - // 鏌ヨ鏁版嵁 + var db = DB var communityIDs []string - result := db.Table("domain_unit").Where("domainType = ?", 1).Pluck("id", &communityIDs) + result := db.Table("domain_unit").Where("domain_type = ?", 1).Pluck("id", &communityIDs) if result.Error != nil { return nil, result.Error } return communityIDs, nil -} - -// 鏌ヨ鍏ㄩ儴鏁版嵁 -func GetAllData() ([]PersonnelStatusRule, error) { - db, err := ConnectDB() - if err != nil { - return nil, err - } - - var rules []PersonnelStatusRule - if err := db.Find(&rules).Error; err != nil { - return nil, err - } - - return rules, nil -} - -// 鏌ヨ浣忔埛鏃堕棿鏁版嵁 -func GetResidentData(status, communityID string) ([]Resident, error) { - db, err := ConnectDB() - if err != nil { - return nil, err - } - - var residents []Resident - - // 鎵ц鏌ヨ - rows, err := db.Table("person_status"). - Select("person_status.documentNumber", "person_status.communityID", "snapshot_count_summary.last_appearance_time", "snapshot_count_summary.created_at"). - Joins("INNER JOIN snapshot_count_summary ON person_status.documentNumber = snapshot_count_summary.document_number AND person_status.communityID = snapshot_count_summary.community_id"). - Where("person_status.status = ? AND person_status.communityID = ?", status, communityID). - 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 { - return nil, err - } - residents = append(residents, resident) - } - if err := rows.Err(); err != nil { - return nil, err - } - - return residents, nil -} - -// 鏌ヨ浜虹墿灞炴�� -func GetDBPersonStatusData(id string) ([]PersonStatus, error) { - db, err := ConnectDB() - if err != nil { - return nil, err - } - - // 鏌ヨ鏁版嵁 - var personStatusList []PersonStatus - if err := db.Table("person_status"). - Select("documentNumber, status, frequentAddress"). - Where("communityID = ?", id). - Find(&personStatusList).Error; err != nil { - return nil, err - } - - return personStatusList, nil -} - -// 鏍规嵁绀惧尯id鍜屼綇鎴峰睘鎬ф煡璇綇鎴锋。妗堢紪鍙� -func GetDocNumberFromPersonStatus(id, status string) ([]string, error) { - db, err := ConnectDB() - if err != nil { - return nil, err - } - - // 鏌ヨ鏁版嵁 - var personStatusList []PersonStatus - if err := db.Table("person_status"). - Select("documentNumber, status, frequentAddress"). - Where("communityID = ? AND status = ?", id, status). - Find(&personStatusList).Error; err != nil { - return nil, err - } - - docNum := make([]string, 0) - for _, ps := range personStatusList { - docNum = append(docNum, ps.DocumentNumber) - } - - return docNum, nil -} - -// 鏌ヨ浜虹墿韬唤灞炴�ц〃 -func GetLabelManageIdentity(IdentityType int) ([]LabelManage, error) { - db, err := ConnectDB() - if err != nil { - return nil, err - } - - // 鏌ヨ鏁版嵁 - var labelManageIdentity []LabelManage - if err := db.Table("label_manage"). - Select("id, name, valid_days"). - Where("type = ?", IdentityType). - Find(&labelManageIdentity).Error; err != nil { - return nil, err - } - - return labelManageIdentity, nil -} - -// UpdatePersonInfo 鏇存柊鎴栨彃鍏ュ涓汉鍛樹俊鎭� -func UpdateMoveInout(personsMoveInout []MoveInout) error { - // 鏁版嵁搴撹繛鎺ヤ俊鎭� - db, err := ConnectDB() - if err != nil { - return err - } - // 閬嶅巻浜哄憳淇℃伅 - for _, personMoveInout := range personsMoveInout { - - // 妫�鏌ヨ褰曟槸鍚﹀瓨鍦� - var existingPerson MoveInout - err := db.Where("document_number = ? AND community_id = ?", personMoveInout.DocumentNumber, personMoveInout.CommunityID).First(&existingPerson).Error - if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { - logger.Error("Query person error:", err, personMoveInout.DocumentNumber, personMoveInout.CommunityID) - //fmt.Println("asdasfasfasf") - continue - //return err - } - - // 濡傛灉璁板綍瀛樺湪锛屽垯鏇存柊 - if existingPerson.DocumentNumber != "" { - if existingPerson.Status != "Verified" { - err := db.Model(&MoveInout{}). - Where("document_number = ? AND community_id = ?", personMoveInout.DocumentNumber, personMoveInout.CommunityID). - Updates(map[string]interface{}{ - "status": personMoveInout.Status, - "move_out_date": personMoveInout.MoveOutDate, - }).Error - if err != nil { - return err - } - } else { - err := db.Model(&MoveInout{}). - Where("document_number = ? AND community_id = ?", personMoveInout.DocumentNumber, personMoveInout.CommunityID). - Updates(map[string]interface{}{ - "move_out_date": personMoveInout.MoveOutDate, - }).Error - if err != nil { - return err - } - } - } else { - // 濡傛灉璁板綍涓嶅瓨鍦紝鍒欐彃鍏ユ柊璁板綍 - err := db.Create(&personsMoveInout).Error - if err != nil { - return err - } - } - - } - - return nil -} - -// UpdatePersonInfo 鏇存柊鎴栨彃鍏ュ涓汉鍛樿韩浠戒俊鎭� -func UpdateDBPersonLabel(personsIdentity []Identity) error { - // 鏁版嵁搴撹繛鎺ヤ俊鎭� - db, err := ConnectDB() - if err != nil { - return err - } - // 閬嶅巻浜哄憳淇℃伅 - for _, personIdentity := range personsIdentity { - - // 妫�鏌ヨ褰曟槸鍚﹀瓨鍦� - var existingPerson Identity - err := db.Where("dbtablepersons_id = ? AND community_id = ? AND label_id = ?", - personIdentity.DocumentNumber, - personIdentity.CommunityID, - personIdentity.LabelId, - ).First(&existingPerson).Error - if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { - logger.Error("Query person error:", err, - personIdentity.DocumentNumber, - personIdentity.CommunityID, - personIdentity.LabelId) - //fmt.Println("asdasfasfasf") - continue - //return err - } - - // 濡傛灉璁板綍瀛樺湪锛屽垯鏇存柊 - if existingPerson.DocumentNumber != "" { - err := db.Model(&Identity{}). - Where("dbtablepersons_id = ? AND community_id = ? AND label_id = ?", - personIdentity.DocumentNumber, - personIdentity.CommunityID, - personIdentity.LabelId, - ). - Updates(map[string]interface{}{ - "expire_time": personIdentity.ExpireTime, - }).Error - if err != nil { - return err - } - } else { - // 濡傛灉璁板綍涓嶅瓨鍦紝鍒欐彃鍏ユ柊璁板綍 - err := db.Create(&personIdentity).Error - if err != nil { - return err - } - } - - } - - return nil -} - -// UpdatePersonInfo 鏇存柊鎴栨彃鍏ュ涓汉鍛樹俊鎭� -func UpdatePersonInfo(persons []PersonStatus, communityID string) error { - // 鏁版嵁搴撹繛鎺ヤ俊鎭� - db, err := ConnectDB() - if err != nil { - return err - } - // 閬嶅巻浜哄憳淇℃伅 - for _, person := range persons { - - // 妫�鏌ヨ褰曟槸鍚﹀瓨鍦� - var existingPerson PersonStatus - err := db.Where("documentNumber = ? AND communityID = ?", 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 - } - - // 濡傛灉璁板綍瀛樺湪锛屽垯鏇存柊 - if existingPerson.DocumentNumber != "" { - err := db.Model(&PersonStatus{}). - Where("documentNumber = ? AND communityID = ?", person.DocumentNumber, communityID). - Updates(map[string]interface{}{ - "status": person.Status, - "frequentAddress": person.FrequentAddress, - }).Error - if err != nil { - return err - } - } else { - // 濡傛灉璁板綍涓嶅瓨鍦紝鍒欐彃鍏ユ柊璁板綍 - err := db.Create(&person).Error - if err != nil { - return err - } - } - - } - - return nil } -- Gitblit v1.8.0