123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package dao
- import (
- "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"
- )
- // OrderLogisticLogStore 订单物流日志表
- type OrderLogisticLogStore interface {
- DbBaseStore
- BatchSave(ctx context.Context, values []*models.OrderLogisticLog, omit ...string) error
- GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.OrderLogisticLog, error)
- Page(ctx context.Context, dto OrderLogisticLogPageDTO) ([]OrderLogisticLogPageVO, int64, error)
- FindAllByOrderId(ctx context.Context, orderId int) ([]models.OrderLogisticLog, error)
- }
- var _ OrderLogisticLogStore = &orderLogisticLog{}
- type orderLogisticLog struct {
- dbBase
- }
- func newOrderLogisticLog(ds *DataStore) *orderLogisticLog {
- return &orderLogisticLog{dbBase: dbBase{
- store: ds,
- baseEntity: &models.OrderLogisticLog{},
- }}
- }
- func (ds *DataStore) OrderLogisticLog() OrderLogisticLogStore {
- return newOrderLogisticLog(ds)
- }
- func (a *orderLogisticLog) BatchSave(ctx context.Context, values []*models.OrderLogisticLog, omit ...string) error {
- if len(values) == 0 {
- return nil
- }
- db := a.store.optionDB(ctx)
- //tokenInfo := ctxtoken.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 *orderLogisticLog) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.OrderLogisticLog, error) {
- res := make(map[int]models.OrderLogisticLog, len(ids))
- if len(ids) == 0 {
- return res, nil
- }
- var list []models.OrderLogisticLog
- 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 OrderLogisticLogPageDTO struct {
- Page core.Page
- Name string // 名称
- }
- type OrderLogisticLogPageVO struct {
- Id int `gorm:"column:id" json:"id"` //
- OrderId int `gorm:"column:order_id" json:"orderId"` //订单id
- OrderNo string `gorm:"column:order_no" json:"orderNo"` //订单号
- Location string `gorm:"column:location" json:"location"` //当前所在地
- Phone string `gorm:"column:phone" json:"phone"` //联系方式
- Transporter string `gorm:"column:transporter" json:"transporter"` //运输员
- }
- func (a *orderLogisticLog) Page(ctx context.Context, dto OrderLogisticLogPageDTO) ([]OrderLogisticLogPageVO, int64, error) {
- db := a.store.optionDB(ctx)
- stmt := db.Model(&models.OrderLogisticLog{}).
- Where("deleted = ?", models.DeleteNo)
- //stmt.Select("id")
- //if dto.Name != "" {
- // stmt.Where("name LIKE ?", "%"+dto.Name+"%")
- //}
- var total int64
- var ret []OrderLogisticLogPageVO
- 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 *orderLogisticLog) FindAllByOrderId(ctx context.Context, orderId int) ([]models.OrderLogisticLog, error) {
- ret := make([]models.OrderLogisticLog, 0, 3)
- err := a.store.optionDB(ctx).Model(&models.OrderLogisticLog{}).
- Where("deleted = ?", models.DeleteNo).
- Where("order_id = ?", orderId).
- Order("id DESC").
- Find(&ret).Error
- return ret, errors.WithStackOnce(err)
- }
|