package domainservice import ( "Cold_Logistic/internal/pkg/common/constant" "Cold_Logistic/internal/pkg/common/global" "Cold_Logistic/internal/server/infra/dao" "Cold_Logistic/internal/server/infra/models" "context" "encoding/json" "fmt" "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors" "time" ) func ProcessDriver(ctx context.Context, carId int, driver global.ClodAccount) (acc int, car int, err error) { store := dao.NewDataStore(global.CommonConnectRepoInst.StoreDB) account, err := store.Account().FindByUuid(ctx, driver.Uuid) if err != nil { return 0, 0, errors.WithStackOnce(err) } if account.Id == 0 { account = models.Account{ AccountType: constant.AccountPlatform, Name: driver.Name, Pid: driver.Pid, Uuid: driver.Uuid, RoleType: constant.RoleDriver, CarId: carId, } if err = store.Car().Create(ctx, &account); err != nil { return 0, 0, errors.WithStackOnce(err) } } else { if carId > 0 { account.CarId = carId } if driver.Name != "" { account.Name = driver.Name } if err = store.Account().UpdateById(ctx, &account); err != nil { return 0, 0, errors.WithStackOnce(err) } } return account.Id, account.CarId, nil } func ProcessWareHouse(ctx context.Context, warehouseId int, houses []global.ClodAccount) error { store := dao.NewDataStore(global.CommonConnectRepoInst.StoreDB) uuids := make([]string, 0, len(houses)) for _, v := range houses { uuids = append(uuids, v.Uuid) } accMap, err := store.Account().FindByUuids(ctx, uuids) if err != nil { return errors.WithStackOnce(err) } addAcc := make([]*models.Account, 0, len(houses)-len(accMap)) update := make([]*models.Account, 0) for _, v := range houses { if _, ok := accMap[v.Uuid]; !ok { addAcc = append(addAcc, &models.Account{ AccountType: constant.AccountPlatform, Name: v.Name, Uuid: v.Uuid, RoleType: constant.RoleWarehouse, WarehouseId: warehouseId, Pid: v.Pid, }) } else { acc := accMap[v.Uuid] if warehouseId > 0 { acc.WarehouseId = warehouseId } if v.Name != "" { acc.Name = v.Name } update = append(update, &acc) } } if len(addAcc) > 0 { if err = store.Account().BatchSave(ctx, addAcc); err != nil { return errors.WithStackOnce(err) } } if len(update) > 0 { if err = store.Account().BatchSave(ctx, update); err != nil { return errors.WithStackOnce(err) } } return nil } const ( accountHashKey = "AccountInfo" accInfoKey = "acc_%v" tokenKey = "Token_%v" ) func SetTokenAndInfo(ctx context.Context, account models.Account, token string, exp time.Duration) (res global.TokenInfo, err error) { tokenInfo := global.TokenInfo{ AccountId: account.Id, AccountUuid: account.Uuid, Pid: account.Pid, AccountType: account.AccountType, Role: account.RoleType, Name: account.Name, Phone: account.Phone, Gender: account.Gender, CompanyName: account.CompanyName, Openid: account.Openid, IsFirst: account.FirstLogin, UsePid: account.UsePid, PowerId: account.PowerId, CarId: account.CarId, WarehouseId: account.WarehouseId, } key := fmt.Sprintf(accInfoKey, tokenInfo.AccountId) client := global.CommonConnectRepoInst.Redis bt, err := tokenInfo.MarshalBinary() if err != nil { return res, errors.WithStackOnce(err) } _, err = client.Set(ctx, fmt.Sprintf(tokenKey, account.Id), token, exp).Result() if err != nil { return res, errors.WithStackOnce(err) } _, err = client.HSet(ctx, accountHashKey, key, bt).Result() return tokenInfo, errors.WithStackOnce(err) } func UpdateTokenInfo(ctx context.Context, account models.Account) (res global.TokenInfo, err error) { tokenInfo := global.TokenInfo{ AccountId: account.Id, AccountUuid: account.Uuid, Pid: account.Pid, AccountType: account.AccountType, Role: account.RoleType, Name: account.Name, Phone: account.Phone, Gender: account.Gender, CompanyName: account.CompanyName, Openid: account.Openid, IsFirst: account.FirstLogin, UsePid: account.UsePid, PowerId: account.PowerId, CarId: account.CarId, WarehouseId: account.WarehouseId, } key := fmt.Sprintf(accInfoKey, tokenInfo.AccountId) client := global.CommonConnectRepoInst.Redis bt, err := tokenInfo.MarshalBinary() if err != nil { return res, errors.WithStackOnce(err) } _, err = client.HSet(ctx, accountHashKey, key, bt).Result() return tokenInfo, errors.WithStackOnce(err) } func UpdateToken(ctx context.Context, accountId int, token string, exp time.Duration) error { client := global.CommonConnectRepoInst.Redis _, err := client.Set(ctx, fmt.Sprintf(tokenKey, accountId), token, exp).Result() return errors.WithStackOnce(err) } func GetToken(ctx context.Context, accountId int) (res string, err error) { key := fmt.Sprintf(tokenKey, accountId) client := global.CommonConnectRepoInst.Redis token, err := client.Get(ctx, key).Result() if err != nil { return "", errors.WithStackOnce(err) } return token, nil } func GetTokenInfoById(ctx context.Context, accountId int) (res global.TokenInfo, err error) { filedKey := fmt.Sprintf("acc_%d", accountId) client := global.CommonConnectRepoInst.Redis info, _ := client.HGet(ctx, accountHashKey, filedKey).Result() if info != "" { if err = json.Unmarshal([]byte(info), &res); err != nil { return res, errors.WithStackOnce(err) } } else { dataStore := dao.NewDataStore(global.CommonConnectRepoInst.StoreDB) account := models.Account{} err = dataStore.Account().FirstById(ctx, &account, accountId) if err != nil { return res, errors.WithStackOnce(err) } res, err = UpdateTokenInfo(ctx, account) if err != nil { return res, errors.WithStackOnce(err) } } return res, nil }