subaccount.go 7.3 KB

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