123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package dao
- import (
- "Cold_Logistic/internal/pkg/common/global"
- "Cold_Logistic/internal/server/infra/models"
- "context"
- "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
- "time"
- )
- // AccountStore 小程序账号表
- type AccountStore interface {
- DbBaseStore
- BatchSave(ctx context.Context, values []*models.Account, omit ...string) error
- GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.Account, error)
- FindByOpenid(ctx context.Context, openid string, accType int) (models.Account, error)
- FindByUuid(ctx context.Context, uuid string) (models.Account, error)
- FindByUuids(ctx context.Context, uuid []string) (map[string]models.Account, error)
- FindTokenKeyById(ctx context.Context, id int) (string, error)
- }
- var _ AccountStore = &account{}
- type account struct {
- dbBase
- }
- func newAccount(ds *DataStore) *account {
- return &account{dbBase: dbBase{
- store: ds,
- baseEntity: &models.Account{},
- }}
- }
- func (ds *DataStore) Account() AccountStore {
- return newAccount(ds)
- }
- func (a *account) BatchSave(ctx context.Context, values []*models.Account, omit ...string) error {
- if len(values) == 0 {
- return nil
- }
- db := a.store.optionDB(ctx)
- tokenInfo := global.GetTokenInfoFromContext(ctx)
- for i := range values {
- if values[i].Id == 0 {
- values[i].CreatedBy = tokenInfo.AccountId
- values[i].CreatedTime.Time = time.Now()
- values[i].Deleted = models.DeleteNo
- } else {
- values[i].UpdatedBy = tokenInfo.AccountId
- values[i].UpdatedTime.Time = time.Now()
- }
- }
- return db.Table(a.baseEntity.TableName()).Omit(omit...).Save(&values).Error
- }
- func (a *account) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.Account, error) {
- res := make(map[int]models.Account, len(ids))
- if len(ids) == 0 {
- return res, nil
- }
- var list []models.Account
- db := a.store.optionDB(ctx)
- err := db.Table(a.baseEntity.TableName()).
- Where("deleted = ?", models.DeleteNo).
- Where("id IN (?)", ids).
- Omit(omit...).
- Find(&list).Error
- if err != nil {
- return res, err
- }
- for i := range list {
- res[list[i].Id] = list[i]
- }
- return res, nil
- }
- func (a *account) FindByOpenid(ctx context.Context, openid string, accType int) (models.Account, error) {
- db := a.store.optionDB(ctx)
- ent := models.Account{}
- err := db.Model(a.baseEntity).Where("deleted = ?", models.DeleteNo).
- Where("openid = ? AND account_type = ?", openid, accType).
- Find(&ent).Error
- if err != nil {
- return ent, errors.WithStackOnce(err)
- }
- return ent, nil
- }
- func (a *account) FindByUuid(ctx context.Context, uuid string) (models.Account, error) {
- db := a.store.optionDB(ctx)
- ent := models.Account{}
- err := db.Model(a.baseEntity).Where("deleted = ?", models.DeleteNo).
- Where("uuid = ?", uuid).
- Find(&ent).Limit(1).Error
- if err != nil {
- return ent, errors.WithStackOnce(err)
- }
- return ent, nil
- }
- func (a *account) FindByUuids(ctx context.Context, uuid []string) (map[string]models.Account, error) {
- db := a.store.optionDB(ctx)
- ent := make([]models.Account, 0, len(uuid))
- err := db.Model(a.baseEntity).Where("deleted = ?", models.DeleteNo).
- Where("uuid IN(?)", uuid).
- Find(&ent).Error
- if err != nil {
- return nil, errors.WithStackOnce(err)
- }
- ret := make(map[string]models.Account, len(ent))
- for _, v := range ent {
- ret[v.Uuid] = v
- }
- return ret, nil
- }
- func (a *account) FindTokenKeyById(ctx context.Context, id int) (string, error) {
- db := a.store.optionDB(ctx)
- var key string
- err := db.Model(a.baseEntity).Where("deleted = ?", models.DeleteNo).
- Where("id = ?", id).
- Pluck("tokey", &key).Error
- return key, errors.WithStackOnce(err)
- }
|