subaccount.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/caixw/lib.go/conv"
  6. "time"
  7. "youngee_m_api/model/gorm_model"
  8. "youngee_m_api/model/http_model"
  9. )
  10. func CreateSubAccount(ctx context.Context, req *http_model.CreateSubAccountRequest) (http_model.YounggeeSubAccountsRes, error) {
  11. db := GetReadDB(ctx)
  12. fmt.Println("req", req.Code)
  13. // 开始事务
  14. tx := db.Begin()
  15. if tx.Error != nil {
  16. return http_model.YounggeeSubAccountsRes{}, tx.Error
  17. }
  18. now := time.Now()
  19. fmt.Println("now:", now)
  20. younggee_user := gorm_model.YounggeeUser{
  21. Phone: req.PhoneNumber,
  22. User: req.SubAccountName,
  23. Role: "5",
  24. LastLoginTime: now,
  25. UpdatedAt: now,
  26. CreatedAt: now,
  27. }
  28. err := tx.Create(&younggee_user).Error
  29. fmt.Println("err:", err)
  30. if err != nil {
  31. tx.Rollback()
  32. return http_model.YounggeeSubAccountsRes{}, err
  33. }
  34. user := gorm_model.YounggeeSubAccount{
  35. PhoneNumber: req.PhoneNumber,
  36. SubAccountName: req.SubAccountName,
  37. JobId: req.Job,
  38. SubAccountType: 2,
  39. AccountStatus: 1,
  40. SuperAdminId: req.AccountID,
  41. UserId: younggee_user.ID,
  42. }
  43. err = tx.Create(&user).Error
  44. if err != nil {
  45. tx.Rollback()
  46. return http_model.YounggeeSubAccountsRes{}, err
  47. }
  48. // 提交事务
  49. tx.Commit()
  50. // 构造返回结果
  51. res := http_model.YounggeeSubAccountsRes{
  52. JobID: user.JobId,
  53. PhoneNumber: user.PhoneNumber,
  54. SubAccountName: user.SubAccountName,
  55. }
  56. return res, nil
  57. }
  58. func UPdateSubaccountInfo(ctx context.Context, req *http_model.UpdateSubaccountInfoRequest) (*http_model.YounggeeSubAccountsRes, error) {
  59. db := GetReadDB(ctx)
  60. // 查找子账号
  61. SubaccountInfo := gorm_model.YounggeeSubAccount{}
  62. whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: req.SubAccountID}
  63. result := db.Where(&whereCondition).First(&SubaccountInfo)
  64. if result.Error != nil {
  65. return nil, result.Error
  66. }
  67. // 更新子账号信息
  68. updateData := map[string]interface{}{
  69. "phone_number": req.PhoneNumber,
  70. "sub_account_name": req.SubAccountName,
  71. "job_id": req.Job,
  72. "sub_account_type": 2,
  73. }
  74. updateResult := db.Model(&SubaccountInfo).Where("sub_account_id = ?", req.SubAccountID).Updates(updateData)
  75. if updateResult.Error != nil {
  76. fmt.Println("updateResult.Error:", updateResult.Error)
  77. return nil, updateResult.Error
  78. }
  79. // 返回更新后的结果
  80. response := &http_model.YounggeeSubAccountsRes{
  81. PhoneNumber: SubaccountInfo.PhoneNumber,
  82. SubAccountName: SubaccountInfo.SubAccountName,
  83. JobID: SubaccountInfo.JobId,
  84. }
  85. return response, nil
  86. }
  87. func GetSubAccountDetail(ctx context.Context, req *http_model.SubAccountDetailRequest) (http_model.SubAccountData, error) {
  88. db := GetReadDB(ctx)
  89. var subaccountInfo []gorm_model.YounggeeSubAccount
  90. // 构建查询条件
  91. query := db.Model(&gorm_model.YounggeeSubAccount{}).Where("sub_account_type = ?", 2)
  92. if req.JobId != nil {
  93. query = query.Where("job_id = ?", *req.JobId)
  94. }
  95. if req.AccountStatus != nil {
  96. query = query.Where("account_status = ?", *req.AccountStatus)
  97. }
  98. if req.PhoneNumber != nil {
  99. query = query.Where("phone_number = ?", *req.PhoneNumber)
  100. }
  101. if req.SubAccountName != nil {
  102. query = query.Where("account_name = ?", *req.SubAccountName)
  103. }
  104. // 查询总数
  105. var total int64
  106. countQuery := query.Count(&total)
  107. if countQuery.Error != nil {
  108. return http_model.SubAccountData{}, countQuery.Error
  109. }
  110. // 添加分页逻辑
  111. pageSize := req.PageSize
  112. if pageSize == 0 {
  113. pageSize = 10 // 设置默认页大小
  114. }
  115. pageNum := req.PageNum
  116. if pageNum == 0 {
  117. pageNum = 1 // 设置默认页码
  118. }
  119. offset := (pageNum - 1) * pageSize
  120. // 执行分页查询
  121. result := query.Offset(offset).Limit(pageSize).Find(&subaccountInfo)
  122. if result.Error != nil {
  123. return http_model.SubAccountData{}, result.Error
  124. }
  125. // 收集所有 SuperAdminId
  126. var userIDs []int64
  127. for _, acc := range subaccountInfo {
  128. userIDs = append(userIDs, acc.SuperAdminId)
  129. }
  130. // 查询所有相关用户
  131. var users []gorm_model.YounggeeUser
  132. db.Where("id IN (?)", userIDs).Find(&users)
  133. // 创建用户 ID 到用户名的映射
  134. userMap := make(map[int64]string)
  135. for _, user := range users {
  136. userMap[user.ID] = user.Username
  137. }
  138. // 构造返回结果
  139. var subaccountInfoPointers []*http_model.SubAccountDetailResponse
  140. for _, acc := range subaccountInfo {
  141. response := &http_model.SubAccountDetailResponse{
  142. SubAccountInfo: acc,
  143. Creater: userMap[acc.SuperAdminId], // 从映射中获取用户名
  144. }
  145. subaccountInfoPointers = append(subaccountInfoPointers, response)
  146. }
  147. subaccountInfolist := http_model.SubAccountData{
  148. SubAccountInfo: subaccountInfoPointers,
  149. Total: conv.MustString(total, ""),
  150. }
  151. return subaccountInfolist, nil
  152. }
  153. func StopSubAccount(ctx context.Context, req *http_model.StopSubAccountRequest) (*http_model.StopSubAccountResponse, error) {
  154. db := GetReadDB(ctx)
  155. SubaccountInfo := gorm_model.YounggeeSubAccount{}
  156. whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: req.SubAccountId}
  157. result := db.Where(&whereCondition).First(&SubaccountInfo)
  158. if result.Error != nil {
  159. return nil, result.Error
  160. }
  161. updateData := gorm_model.YounggeeSubAccount{
  162. AccountStatus: 2,
  163. }
  164. updateResult := db.Model(&SubaccountInfo).Where("sub_account_id = ?", req.SubAccountId).Updates(updateData)
  165. if updateResult.Error != nil {
  166. fmt.Println("updateResult.Error:", updateResult.Error)
  167. return nil, updateResult.Error
  168. }
  169. response := &http_model.StopSubAccountResponse{
  170. SubAccountName: SubaccountInfo.SubAccountName,
  171. SubAccountID: SubaccountInfo.SubAccountId,
  172. }
  173. return response, nil
  174. }
  175. func DeleteSubAccount(ctx context.Context, req *http_model.DeleteSubAccountRequest) error {
  176. db := GetReadDB(ctx)
  177. // 开始事务
  178. tx := db.Begin()
  179. var younggee_user gorm_model.YounggeeSubAccount
  180. err := tx.Where("sub_account_id = ?", req.SubAccountId).First(&younggee_user).Error
  181. if err != nil {
  182. fmt.Println("FindYounggeeSubAccountError:", err)
  183. tx.Rollback()
  184. return err
  185. }
  186. // 删除 YounggeeSubAccount
  187. err = tx.Where("sub_account_id = ?", req.SubAccountId).Delete(&younggee_user).Error
  188. if err != nil {
  189. fmt.Println("DeleteYounggeeSubAccountError:", err)
  190. tx.Rollback()
  191. return err
  192. }
  193. // 删除 YounggeeUser
  194. var user gorm_model.YounggeeUser
  195. if younggee_user.UserId != 0 {
  196. err = tx.Where("id = ?", younggee_user.UserId).Delete(&user).Error
  197. if err != nil {
  198. fmt.Println("DeleteYounggeeUserError:", err)
  199. tx.Rollback()
  200. return err
  201. }
  202. }
  203. // 提交事务
  204. tx.Commit()
  205. return nil
  206. }