account.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package domainservice
  2. import (
  3. "Cold_Logistic/internal/pkg/common/constant"
  4. "Cold_Logistic/internal/pkg/common/global"
  5. "Cold_Logistic/internal/server/infra/dao"
  6. "Cold_Logistic/internal/server/infra/models"
  7. "context"
  8. "encoding/json"
  9. "fmt"
  10. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  11. "time"
  12. )
  13. func ProcessDriver(ctx context.Context, carId int, driver global.ClodAccount) (acc int, car int, err error) {
  14. store := dao.NewDataStore(global.CommonConnectRepoInst.StoreDB)
  15. account, err := store.Account().FindByUuid(ctx, driver.Uuid)
  16. if err != nil {
  17. return 0, 0, errors.WithStackOnce(err)
  18. }
  19. if account.Id == 0 {
  20. account = models.Account{
  21. AccountType: constant.AccountPlatform,
  22. Name: driver.Name,
  23. Pid: driver.Pid,
  24. Uuid: driver.Uuid,
  25. RoleType: constant.RoleDriver,
  26. CarId: carId,
  27. }
  28. if err = store.Car().Create(ctx, &account); err != nil {
  29. return 0, 0, errors.WithStackOnce(err)
  30. }
  31. } else {
  32. if carId > 0 {
  33. account.CarId = carId
  34. }
  35. if driver.Name != "" {
  36. account.Name = driver.Name
  37. }
  38. if err = store.Account().UpdateById(ctx, &account); err != nil {
  39. return 0, 0, errors.WithStackOnce(err)
  40. }
  41. }
  42. return account.Id, account.CarId, nil
  43. }
  44. func ProcessWareHouse(ctx context.Context, warehouseId int, houses []global.ClodAccount) error {
  45. store := dao.NewDataStore(global.CommonConnectRepoInst.StoreDB)
  46. uuids := make([]string, 0, len(houses))
  47. for _, v := range houses {
  48. uuids = append(uuids, v.Uuid)
  49. }
  50. accMap, err := store.Account().FindByUuids(ctx, uuids)
  51. if err != nil {
  52. return errors.WithStackOnce(err)
  53. }
  54. addAcc := make([]*models.Account, 0, len(houses)-len(accMap))
  55. update := make([]*models.Account, 0)
  56. for _, v := range houses {
  57. if _, ok := accMap[v.Uuid]; !ok {
  58. addAcc = append(addAcc, &models.Account{
  59. AccountType: constant.AccountPlatform,
  60. Name: v.Name,
  61. Uuid: v.Uuid,
  62. RoleType: constant.RoleWarehouse,
  63. WarehouseId: warehouseId,
  64. Pid: v.Pid,
  65. })
  66. } else {
  67. acc := accMap[v.Uuid]
  68. if warehouseId > 0 {
  69. acc.WarehouseId = warehouseId
  70. }
  71. if v.Name != "" {
  72. acc.Name = v.Name
  73. }
  74. update = append(update, &acc)
  75. }
  76. }
  77. if len(addAcc) > 0 {
  78. if err = store.Account().BatchSave(ctx, addAcc); err != nil {
  79. return errors.WithStackOnce(err)
  80. }
  81. }
  82. if len(update) > 0 {
  83. if err = store.Account().BatchSave(ctx, update); err != nil {
  84. return errors.WithStackOnce(err)
  85. }
  86. }
  87. return nil
  88. }
  89. const (
  90. accountHashKey = "AccountInfo"
  91. accInfoKey = "acc_%v"
  92. tokenKey = "Token_%v"
  93. )
  94. func SetTokenAndInfo(ctx context.Context, account models.Account, token string, exp time.Duration) (res global.TokenInfo, err error) {
  95. tokenInfo := global.TokenInfo{
  96. AccountId: account.Id,
  97. AccountUuid: account.Uuid,
  98. Pid: account.Pid,
  99. AccountType: account.AccountType,
  100. Role: account.RoleType,
  101. Name: account.Name,
  102. Phone: account.Phone,
  103. Gender: account.Gender,
  104. CompanyName: account.CompanyName,
  105. Openid: account.Openid,
  106. IsFirst: account.FirstLogin,
  107. UsePid: account.UsePid,
  108. PowerId: account.PowerId,
  109. CarId: account.CarId,
  110. WarehouseId: account.WarehouseId,
  111. }
  112. key := fmt.Sprintf(accInfoKey, tokenInfo.AccountId)
  113. client := global.CommonConnectRepoInst.Redis
  114. bt, err := tokenInfo.MarshalBinary()
  115. if err != nil {
  116. return res, errors.WithStackOnce(err)
  117. }
  118. _, err = client.Set(ctx, fmt.Sprintf(tokenKey, account.Id), token, exp).Result()
  119. if err != nil {
  120. return res, errors.WithStackOnce(err)
  121. }
  122. _, err = client.HSet(ctx, accountHashKey, key, bt).Result()
  123. return tokenInfo, errors.WithStackOnce(err)
  124. }
  125. func UpdateTokenInfo(ctx context.Context, account models.Account) (res global.TokenInfo, err error) {
  126. tokenInfo := global.TokenInfo{
  127. AccountId: account.Id,
  128. AccountUuid: account.Uuid,
  129. Pid: account.Pid,
  130. AccountType: account.AccountType,
  131. Role: account.RoleType,
  132. Name: account.Name,
  133. Phone: account.Phone,
  134. Gender: account.Gender,
  135. CompanyName: account.CompanyName,
  136. Openid: account.Openid,
  137. IsFirst: account.FirstLogin,
  138. UsePid: account.UsePid,
  139. PowerId: account.PowerId,
  140. CarId: account.CarId,
  141. WarehouseId: account.WarehouseId,
  142. }
  143. key := fmt.Sprintf(accInfoKey, tokenInfo.AccountId)
  144. client := global.CommonConnectRepoInst.Redis
  145. bt, err := tokenInfo.MarshalBinary()
  146. if err != nil {
  147. return res, errors.WithStackOnce(err)
  148. }
  149. _, err = client.HSet(ctx, accountHashKey, key, bt).Result()
  150. return tokenInfo, errors.WithStackOnce(err)
  151. }
  152. func UpdateToken(ctx context.Context, accountId int, token string, exp time.Duration) error {
  153. client := global.CommonConnectRepoInst.Redis
  154. _, err := client.Set(ctx, fmt.Sprintf(tokenKey, accountId), token, exp).Result()
  155. return errors.WithStackOnce(err)
  156. }
  157. func GetToken(ctx context.Context, accountId int) (res string, err error) {
  158. key := fmt.Sprintf(tokenKey, accountId)
  159. client := global.CommonConnectRepoInst.Redis
  160. token, err := client.Get(ctx, key).Result()
  161. if err != nil {
  162. return "", errors.WithStackOnce(err)
  163. }
  164. return token, nil
  165. }
  166. func GetTokenInfoById(ctx context.Context, accountId int) (res global.TokenInfo, err error) {
  167. filedKey := fmt.Sprintf("acc_%d", accountId)
  168. client := global.CommonConnectRepoInst.Redis
  169. info, _ := client.HGet(ctx, accountHashKey, filedKey).Result()
  170. if info != "" {
  171. if err = json.Unmarshal([]byte(info), &res); err != nil {
  172. return res, errors.WithStackOnce(err)
  173. }
  174. } else {
  175. dataStore := dao.NewDataStore(global.CommonConnectRepoInst.StoreDB)
  176. account := models.Account{}
  177. err = dataStore.Account().FirstById(ctx, &account, accountId)
  178. if err != nil {
  179. return res, errors.WithStackOnce(err)
  180. }
  181. res, err = UpdateTokenInfo(ctx, account)
  182. if err != nil {
  183. return res, errors.WithStackOnce(err)
  184. }
  185. }
  186. return res, nil
  187. }