subaccount.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. //收集所有JobID
  130. var JobIDs []int
  131. for _, acc := range subaccountInfo {
  132. JobIDs = append(JobIDs, acc.JobId)
  133. }
  134. // 查询所有相关用户
  135. var users []gorm_model.YounggeeUser
  136. db.Where("id IN (?)", userIDs).Find(&users)
  137. // 创建用户 ID 到用户名的映射
  138. userMap := make(map[int64]string)
  139. for _, user := range users {
  140. userMap[user.ID] = user.Username
  141. }
  142. var Jobs []gorm_model.YounggeeJob
  143. db.Where("job_id IN (?)", JobIDs).Find(&Jobs)
  144. //创建岗位id到jobname的映射
  145. JobMap := make(map[int]string)
  146. for _, job := range Jobs {
  147. JobMap[job.JobId] = job.JobName
  148. }
  149. // 构造返回结果
  150. var subaccountInfoPointers []*http_model.SubAccountDetailResponse
  151. for _, acc := range subaccountInfo {
  152. response := &http_model.SubAccountDetailResponse{
  153. SubAccountInfo: acc,
  154. Creater: userMap[acc.SuperAdminId], // 从映射中获取用户名
  155. JobName: JobMap[acc.JobId],
  156. }
  157. subaccountInfoPointers = append(subaccountInfoPointers, response)
  158. }
  159. subaccountInfolist := http_model.SubAccountData{
  160. SubAccountInfo: subaccountInfoPointers,
  161. Total: conv.MustString(total, ""),
  162. }
  163. return subaccountInfolist, nil
  164. }
  165. func StopSubAccount(ctx context.Context, req *http_model.StopSubAccountRequest) (*http_model.StopSubAccountResponse, error) {
  166. db := GetReadDB(ctx)
  167. SubaccountInfo := gorm_model.YounggeeSubAccount{}
  168. whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: req.SubAccountId}
  169. result := db.Where(&whereCondition).First(&SubaccountInfo)
  170. if result.Error != nil {
  171. return nil, result.Error
  172. }
  173. updateData := gorm_model.YounggeeSubAccount{
  174. AccountStatus: 2,
  175. }
  176. updateResult := db.Model(&SubaccountInfo).Where("sub_account_id = ?", req.SubAccountId).Updates(updateData)
  177. if updateResult.Error != nil {
  178. fmt.Println("updateResult.Error:", updateResult.Error)
  179. return nil, updateResult.Error
  180. }
  181. response := &http_model.StopSubAccountResponse{
  182. SubAccountName: SubaccountInfo.SubAccountName,
  183. SubAccountID: SubaccountInfo.SubAccountId,
  184. }
  185. return response, nil
  186. }
  187. func DeleteSubAccount(ctx context.Context, req *http_model.DeleteSubAccountRequest) error {
  188. db := GetReadDB(ctx)
  189. // 开始事务
  190. tx := db.Begin()
  191. var younggee_user gorm_model.YounggeeSubAccount
  192. err := tx.Where("sub_account_id = ?", req.SubAccountId).First(&younggee_user).Error
  193. if err != nil {
  194. fmt.Println("FindYounggeeSubAccountError:", err)
  195. tx.Rollback()
  196. return err
  197. }
  198. // 删除 YounggeeSubAccount
  199. err = tx.Where("sub_account_id = ?", req.SubAccountId).Delete(&younggee_user).Error
  200. if err != nil {
  201. fmt.Println("DeleteYounggeeSubAccountError:", err)
  202. tx.Rollback()
  203. return err
  204. }
  205. // 删除 YounggeeUser
  206. var user gorm_model.YounggeeUser
  207. if younggee_user.UserId != 0 {
  208. err = tx.Where("id = ?", younggee_user.UserId).Delete(&user).Error
  209. if err != nil {
  210. fmt.Println("DeleteYounggeeUserError:", err)
  211. tx.Rollback()
  212. return err
  213. }
  214. }
  215. // 提交事务
  216. tx.Commit()
  217. return nil
  218. }
  219. func GetSubAccdDetail(ctx context.Context, req *http_model.SubAccShowRequest) (*http_model.GetSubAccountData, error) {
  220. db := GetReadDB(ctx)
  221. var subaccount gorm_model.YounggeeSubAccount
  222. err := db.Model(gorm_model.YounggeeSubAccount{}).Where("sub_account_id = ?", req.SubaccountId).Find(&subaccount).Error
  223. if err != nil {
  224. return nil, err
  225. }
  226. response := &http_model.GetSubAccountData{
  227. SubAccountName: subaccount.SubAccountName,
  228. Phonenumber: subaccount.PhoneNumber,
  229. JobId: subaccount.JobId,
  230. }
  231. return response, nil
  232. }
  233. // FindSubAccountById 根据手机号码查询子账号
  234. func FindSubAccountById(ctx context.Context, id int64) (*gorm_model.YounggeeSubAccount, error) {
  235. db := GetReadDB(ctx)
  236. var subaccount gorm_model.YounggeeSubAccount
  237. err := db.Model(gorm_model.YounggeeSubAccount{}).Where("user_id = ?", id).Find(&subaccount).Error
  238. if err != nil {
  239. return nil, err
  240. }
  241. return &subaccount, nil
  242. }