package db import ( "context" "fmt" "github.com/caixw/lib.go/conv" "time" "youngee_m_api/model/gorm_model" "youngee_m_api/model/http_model" ) func CreateSubAccount(ctx context.Context, req *http_model.CreateSubAccountRequest) (http_model.YounggeeSubAccountsRes, error) { db := GetReadDB(ctx) fmt.Println("req", req.Code) // 开始事务 tx := db.Begin() if tx.Error != nil { return http_model.YounggeeSubAccountsRes{}, tx.Error } now := time.Now() fmt.Println("now:", now) younggee_user := gorm_model.YounggeeUser{ Phone: req.PhoneNumber, User: req.SubAccountName, Role: "5", LastLoginTime: now, UpdatedAt: now, CreatedAt: now, } err := tx.Create(&younggee_user).Error fmt.Println("err:", err) if err != nil { tx.Rollback() return http_model.YounggeeSubAccountsRes{}, err } user := gorm_model.YounggeeSubAccount{ PhoneNumber: req.PhoneNumber, SubAccountName: req.SubAccountName, JobId: req.Job, SubAccountType: 2, AccountStatus: 1, SuperAdminId: req.AccountID, UserId: younggee_user.ID, } err = tx.Create(&user).Error if err != nil { tx.Rollback() return http_model.YounggeeSubAccountsRes{}, err } // 提交事务 tx.Commit() // 构造返回结果 res := http_model.YounggeeSubAccountsRes{ JobID: user.JobId, PhoneNumber: user.PhoneNumber, SubAccountName: user.SubAccountName, } return res, nil } func UPdateSubaccountInfo(ctx context.Context, req *http_model.UpdateSubaccountInfoRequest) (*http_model.YounggeeSubAccountsRes, error) { db := GetReadDB(ctx) // 查找子账号 SubaccountInfo := gorm_model.YounggeeSubAccount{} whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: req.SubAccountID} result := db.Where(&whereCondition).First(&SubaccountInfo) if result.Error != nil { return nil, result.Error } // 更新子账号信息 updateData := map[string]interface{}{ "phone_number": req.PhoneNumber, "sub_account_name": req.SubAccountName, "job_id": req.Job, "sub_account_type": 2, } updateResult := db.Model(&SubaccountInfo).Where("sub_account_id = ?", req.SubAccountID).Updates(updateData) if updateResult.Error != nil { fmt.Println("updateResult.Error:", updateResult.Error) return nil, updateResult.Error } // 返回更新后的结果 response := &http_model.YounggeeSubAccountsRes{ PhoneNumber: SubaccountInfo.PhoneNumber, SubAccountName: SubaccountInfo.SubAccountName, JobID: SubaccountInfo.JobId, } return response, nil } func GetSubAccountDetail(ctx context.Context, req *http_model.SubAccountDetailRequest) (http_model.SubAccountData, error) { db := GetReadDB(ctx) var subaccountInfo []gorm_model.YounggeeSubAccount // 构建查询条件 query := db.Model(&gorm_model.YounggeeSubAccount{}).Where("sub_account_type = ?", 2) if req.JobId != nil { query = query.Where("job_id = ?", *req.JobId) } if req.AccountStatus != nil { query = query.Where("account_status = ?", *req.AccountStatus) } if req.PhoneNumber != nil { query = query.Where("phone_number = ?", *req.PhoneNumber) } if req.SubAccountName != nil { query = query.Where("account_name = ?", *req.SubAccountName) } // 查询总数 var total int64 countQuery := query.Count(&total) if countQuery.Error != nil { return http_model.SubAccountData{}, countQuery.Error } // 添加分页逻辑 pageSize := req.PageSize if pageSize == 0 { pageSize = 10 // 设置默认页大小 } pageNum := req.PageNum if pageNum == 0 { pageNum = 1 // 设置默认页码 } offset := (pageNum - 1) * pageSize // 执行分页查询 result := query.Offset(offset).Limit(pageSize).Find(&subaccountInfo) if result.Error != nil { return http_model.SubAccountData{}, result.Error } // 收集所有 SuperAdminId var userIDs []int64 for _, acc := range subaccountInfo { userIDs = append(userIDs, acc.SuperAdminId) } // 查询所有相关用户 var users []gorm_model.YounggeeUser db.Where("id IN (?)", userIDs).Find(&users) // 创建用户 ID 到用户名的映射 userMap := make(map[int64]string) for _, user := range users { userMap[user.ID] = user.Username } // 构造返回结果 var subaccountInfoPointers []*http_model.SubAccountDetailResponse for _, acc := range subaccountInfo { response := &http_model.SubAccountDetailResponse{ SubAccountInfo: acc, Creater: userMap[acc.SuperAdminId], // 从映射中获取用户名 } subaccountInfoPointers = append(subaccountInfoPointers, response) } subaccountInfolist := http_model.SubAccountData{ SubAccountInfo: subaccountInfoPointers, Total: conv.MustString(total, ""), } return subaccountInfolist, nil } func StopSubAccount(ctx context.Context, req *http_model.StopSubAccountRequest) (*http_model.StopSubAccountResponse, error) { db := GetReadDB(ctx) SubaccountInfo := gorm_model.YounggeeSubAccount{} whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: req.SubAccountId} result := db.Where(&whereCondition).First(&SubaccountInfo) if result.Error != nil { return nil, result.Error } updateData := gorm_model.YounggeeSubAccount{ AccountStatus: 2, } updateResult := db.Model(&SubaccountInfo).Where("sub_account_id = ?", req.SubAccountId).Updates(updateData) if updateResult.Error != nil { fmt.Println("updateResult.Error:", updateResult.Error) return nil, updateResult.Error } response := &http_model.StopSubAccountResponse{ SubAccountName: SubaccountInfo.SubAccountName, SubAccountID: SubaccountInfo.SubAccountId, } return response, nil } func DeleteSubAccount(ctx context.Context, req *http_model.DeleteSubAccountRequest) error { db := GetReadDB(ctx) // 开始事务 tx := db.Begin() var younggee_user gorm_model.YounggeeSubAccount err := tx.Where("sub_account_id = ?", req.SubAccountId).First(&younggee_user).Error if err != nil { fmt.Println("FindYounggeeSubAccountError:", err) tx.Rollback() return err } // 删除 YounggeeSubAccount err = tx.Where("sub_account_id = ?", req.SubAccountId).Delete(&younggee_user).Error if err != nil { fmt.Println("DeleteYounggeeSubAccountError:", err) tx.Rollback() return err } // 删除 YounggeeUser var user gorm_model.YounggeeUser if younggee_user.UserId != 0 { err = tx.Where("id = ?", younggee_user.UserId).Delete(&user).Error if err != nil { fmt.Println("DeleteYounggeeUserError:", err) tx.Rollback() return err } } // 提交事务 tx.Commit() return nil }