subaccount.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.Others != "" {
  99. query = query.Where("phone_number LIKE ? OR sub_account_name LIKE ?", "%"+req.Others+"%", "%"+req.Others+"%")
  100. }
  101. // 查询总数
  102. var total int64
  103. countQuery := query.Count(&total)
  104. if countQuery.Error != nil {
  105. return http_model.SubAccountData{}, countQuery.Error
  106. }
  107. // 添加分页逻辑
  108. pageSize := req.PageSize
  109. if pageSize == 0 {
  110. pageSize = 10 // 设置默认页大小
  111. }
  112. pageNum := req.PageNum
  113. if pageNum == 0 {
  114. pageNum = 1 // 设置默认页码
  115. }
  116. offset := (pageNum - 1) * pageSize
  117. // 执行分页查询
  118. result := query.Offset(offset).Limit(pageSize).Find(&subaccountInfo)
  119. if result.Error != nil {
  120. return http_model.SubAccountData{}, result.Error
  121. }
  122. // 收集所有 SuperAdminId
  123. var userIDs []int64
  124. for _, acc := range subaccountInfo {
  125. userIDs = append(userIDs, acc.SuperAdminId)
  126. }
  127. // 查询所有相关用户
  128. var users []gorm_model.YounggeeUser
  129. db.Where("id IN (?)", userIDs).Find(&users)
  130. // 创建用户 ID 到用户名的映射
  131. userMap := make(map[int64]string)
  132. for _, user := range users {
  133. userMap[user.ID] = user.Username
  134. }
  135. // 构造返回结果
  136. var subaccountInfoPointers []*http_model.SubAccountDetailResponse
  137. for _, acc := range subaccountInfo {
  138. response := &http_model.SubAccountDetailResponse{
  139. SubAccountInfo: acc,
  140. Creater: userMap[acc.SuperAdminId], // 从映射中获取用户名
  141. }
  142. subaccountInfoPointers = append(subaccountInfoPointers, response)
  143. }
  144. subaccountInfolist := http_model.SubAccountData{
  145. SubAccountInfo: subaccountInfoPointers,
  146. Total: conv.MustString(total, ""),
  147. }
  148. return subaccountInfolist, nil
  149. }
  150. func StopSubAccount(ctx context.Context, req *http_model.StopSubAccountRequest) (*http_model.StopSubAccountResponse, error) {
  151. db := GetReadDB(ctx)
  152. SubaccountInfo := gorm_model.YounggeeSubAccount{}
  153. whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: req.SubAccountId}
  154. result := db.Where(&whereCondition).First(&SubaccountInfo)
  155. if result.Error != nil {
  156. return nil, result.Error
  157. }
  158. updateData := gorm_model.YounggeeSubAccount{
  159. AccountStatus: 2,
  160. }
  161. updateResult := db.Model(&SubaccountInfo).Where("sub_account_id = ?", req.SubAccountId).Updates(updateData)
  162. if updateResult.Error != nil {
  163. fmt.Println("updateResult.Error:", updateResult.Error)
  164. return nil, updateResult.Error
  165. }
  166. response := &http_model.StopSubAccountResponse{
  167. SubAccountName: SubaccountInfo.SubAccountName,
  168. SubAccountID: SubaccountInfo.SubAccountId,
  169. }
  170. return response, nil
  171. }
  172. func DeleteSubAccount(ctx context.Context, req *http_model.DeleteSubAccountRequest) error {
  173. db := GetReadDB(ctx)
  174. // 开始事务
  175. tx := db.Begin()
  176. var younggee_user gorm_model.YounggeeSubAccount
  177. err := tx.Where("sub_account_id = ?", req.SubAccountId).First(&younggee_user).Error
  178. if err != nil {
  179. fmt.Println("FindYounggeeSubAccountError:", err)
  180. tx.Rollback()
  181. return err
  182. }
  183. // 删除 YounggeeSubAccount
  184. err = tx.Where("sub_account_id = ?", req.SubAccountId).Delete(&younggee_user).Error
  185. if err != nil {
  186. fmt.Println("DeleteYounggeeSubAccountError:", err)
  187. tx.Rollback()
  188. return err
  189. }
  190. // 删除 YounggeeUser
  191. var user gorm_model.YounggeeUser
  192. if younggee_user.UserId != 0 {
  193. err = tx.Where("id = ?", younggee_user.UserId).Delete(&user).Error
  194. if err != nil {
  195. fmt.Println("DeleteYounggeeUserError:", err)
  196. tx.Rollback()
  197. return err
  198. }
  199. }
  200. // 提交事务
  201. tx.Commit()
  202. return nil
  203. }
  204. func GetSubAccdDetail(ctx context.Context, req *http_model.SubAccShowRequest) (*http_model.GetSubAccountData, error) {
  205. db := GetReadDB(ctx)
  206. var subaccount gorm_model.YounggeeSubAccount
  207. err := db.Model(gorm_model.YounggeeSubAccount{}).Where("sub_account_id = ?", req.SubaccountId).Find(&subaccount).Error
  208. if err != nil {
  209. return nil, err
  210. }
  211. response := &http_model.GetSubAccountData{
  212. SubAccountName: subaccount.SubAccountName,
  213. Phonenumber: subaccount.PhoneNumber,
  214. JobId: subaccount.JobId,
  215. }
  216. return response, nil
  217. }