123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package dao
- import (
- "Cold_Logistic/internal/pkg/common/global"
- "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"
- "time"
- )
- // LogisticCompanyStore 物流公司表
- type LogisticCompanyStore interface {
- DbBaseStore
- Page(ctx context.Context, dto LogisticCompanyPageDTO) ([]LogisticCompanyPageVO, int64, error)
- CountMyLogistic(ctx context.Context, name string, pid int) (int64, error)
- UpdateDefaultByCreateBy(ctx context.Context, isDefault int, excludeId ...int) error
- FindNameById(ctx context.Context, id int) (string, error)
- FindMyByPid(ctx context.Context, pid int) (models.LogisticCompany, error)
- }
- var _ LogisticCompanyStore = &logisticCompany{}
- type logisticCompany struct {
- dbBase
- }
- func newLogisticCompany(ds *DataStore) *logisticCompany {
- return &logisticCompany{dbBase: dbBase{
- store: ds,
- baseEntity: &models.LogisticCompany{},
- }}
- }
- func (ds *DataStore) LogisticCompany() LogisticCompanyStore {
- return newLogisticCompany(ds)
- }
- type LogisticCompanyPageDTO struct {
- Page core.Page
- }
- type LogisticCompanyPageVO struct {
- LogisticId int `gorm:"column:id" json:"logisticId"` //
- Name string `gorm:"column:name" json:"name"` //名称
- IsDefault int `gorm:"column:is_default" json:"isDefault"` //是否默认:1-是 - 2-否
- Pid int `gorm:"column:pid" json:"pid"` //冷链系统公司Id
- }
- func (a *logisticCompany) Page(ctx context.Context, dto LogisticCompanyPageDTO) ([]LogisticCompanyPageVO, int64, error) {
- db := a.store.optionDB(ctx, a.store.withAccountDataPermis(ctx, "created_by"))
- stmt := db.Model(&models.LogisticCompany{}).
- Where("deleted = ?", models.DeleteNo)
- stmt.Select("id, name, is_default, pid")
- var total int64
- var ret []LogisticCompanyPageVO
- 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
- }
- // CountMyLogistic 统计我的物流
- func (a *logisticCompany) CountMyLogistic(ctx context.Context, name string, pid int) (int64, error) {
- db := a.store.optionDB(ctx, a.store.withAccountDataPermis(ctx, "created_by"))
- stmt := db.Model(&models.LogisticCompany{}).
- Where("deleted = ?", models.DeleteNo)
- if name != "" {
- stmt.Where("name = ?", name)
- }
- if pid > 0 {
- stmt.Where("pid = ?", pid)
- }
- var total int64
- err := stmt.Count(&total).Error
- return total, errors.WithStackOnce(err)
- }
- // UpdateDefaultByCreateBy 修改我的默认选中
- func (a *logisticCompany) UpdateDefaultByCreateBy(ctx context.Context, isDefault int, excludeId ...int) error {
- accId := global.GetTokenInfoFromContext(ctx).AccountId
- ent := models.LogisticCompany{
- UpdatedTime: models.NewMyTime(time.Now()),
- UpdatedBy: accId,
- IsDefault: isDefault,
- }
- stmt := a.store.optionDB(ctx).Table(a.baseEntity.TableName()).
- Where("deleted = ?", models.DeleteNo).
- Where("created_by = ?", accId)
- if len(excludeId) > 0 {
- stmt.Where("id NOT IN(?)", excludeId)
- }
- err := stmt.Updates(&ent).Error
- return errors.WithStackOnce(err)
- }
- // FindNameById 查名字
- func (a *logisticCompany) FindNameById(ctx context.Context, id int) (string, error) {
- var name string
- err := a.store.optionDB(ctx).Table(a.baseEntity.TableName()).
- Where("deleted = ?", models.DeleteNo).
- Where("id = ?", id).Pluck("name", &name).Error
- return name, errors.WithStackOnce(err)
- }
- func (a *logisticCompany) FindMyByPid(ctx context.Context, pid int) (models.LogisticCompany, error) {
- var ret models.LogisticCompany
- err := a.store.optionDB(ctx, a.store.withAccountDataPermis(ctx, "created_by")).
- Model(a.baseEntity).
- Where("deleted = ?", models.DeleteNo).
- Where("pid = ?", pid).Find(&ret).Error
- return ret, errors.WithStackOnce(err)
- }
|