package dao import ( "Cold_Logistic/internal/pkg/common/global" "Cold_Logistic/internal/pkg/utils/sqlutil" "Cold_Logistic/internal/server/infra/models" "context" "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core" "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors" "gorm.io/datatypes" "time" ) // WarehouseStore 仓库表 type WarehouseStore interface { DbBaseStore BatchSave(ctx context.Context, values []*models.Warehouse, omit ...string) error GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.Warehouse, error) Page(ctx context.Context, dto WarehousePageDTO) ([]models.Warehouse, int64, error) FindByManageUuid(ctx context.Context, uuid string) (models.Warehouse, error) CountByNameOrSnCode(ctx context.Context, name, snCode string, exclude ...int) (int64, error) } var _ WarehouseStore = &warehouse{} type warehouse struct { dbBase } func newWarehouse(ds *DataStore) *warehouse { return &warehouse{dbBase: dbBase{ store: ds, baseEntity: &models.Warehouse{}, }} } func (ds *DataStore) Warehouse() WarehouseStore { return newWarehouse(ds) } func (a *warehouse) BatchSave(ctx context.Context, values []*models.Warehouse, 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 *warehouse) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.Warehouse, error) { res := make(map[int]models.Warehouse, len(ids)) if len(ids) == 0 { return res, nil } var list []models.Warehouse 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 } type WarehousePageDTO struct { Page core.Page Name string SnCode string } func (a *warehouse) Page(ctx context.Context, dto WarehousePageDTO) ([]models.Warehouse, int64, error) { db := a.store.optionDB(ctx, a.store.withCompanyDataPermis(ctx, "pid")) stmt := db.Model(&models.Warehouse{}). Where("deleted = ?", models.DeleteNo) stmt.Select("id, name, sn_code, address, manager_uuid") if dto.Name != "" { stmt.Where("name LIKE ?", sqlutil.LikeFormat(dto.Name)) } if dto.SnCode != "" { stmt.Where("sn_code LIKE ?", sqlutil.LikeFormat(dto.SnCode)) } var total int64 var ret []models.Warehouse if err := stmt.Count(&total).Error; err != nil { return nil, 0, err } if total == 0 { return ret, total, nil } stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset) stmt.Order("id desc") if err := stmt.Find(&ret).Error; err != nil { return nil, 0, err } return ret, total, nil } func (a *warehouse) FindByManageUuid(ctx context.Context, uuid string) (models.Warehouse, error) { ret := models.Warehouse{} err := a.store.optionDB(ctx).Model(&models.Warehouse{}). Where("deleted = ?", models.DeleteNo). Where(datatypes.JSONArrayQuery("manager_uuid").Contains(uuid)). Find(&ret).Error return ret, errors.WithStackOnce(err) } func (a *warehouse) CountByNameOrSnCode(ctx context.Context, name, snCode string, exclude ...int) (int64, error) { var ret int64 stmt := a.store.optionDB(ctx).Model(&models.Warehouse{}). Where("deleted = ?", models.DeleteNo). Where("(name = ? OR sn_code = ?)", name, snCode) if len(exclude) > 0 { stmt.Where("id NOT IN (?)", exclude) } err := stmt.Count(&ret).Error return ret, errors.WithStackOnce(err) }