package dao import ( "Cold_Logistic/internal/pkg/common/constant" "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" "time" ) // OrderTransportTaskStore 运单任务表 type OrderTransportTaskStore interface { DbBaseStore BatchSave(ctx context.Context, values []*models.OrderTransportTask, omit ...string) error GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.OrderTransportTask, error) Page(ctx context.Context, dto OrderTransportTaskPageDTO) ([]OrderTransportTaskPageVO, int64, error) FinishedTask(ctx context.Context, id int) error } var _ OrderTransportTaskStore = &orderTransportTask{} type orderTransportTask struct { dbBase } func newOrderTransportTask(ds *DataStore) *orderTransportTask { return &orderTransportTask{dbBase: dbBase{ store: ds, baseEntity: &models.OrderTransportTask{}, }} } func (ds *DataStore) OrderTransportTask() OrderTransportTaskStore { return newOrderTransportTask(ds) } func (a *orderTransportTask) BatchSave(ctx context.Context, values []*models.OrderTransportTask, 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 *orderTransportTask) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.OrderTransportTask, error) { res := make(map[int]models.OrderTransportTask, len(ids)) if len(ids) == 0 { return res, nil } var list []models.OrderTransportTask 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 OrderTransportTaskPageDTO struct { Page core.Page OrderNo string Status int } type OrderTransportTaskPageVO struct { OrderId int `gorm:"column:id" json:"orderId"` // OrderNo string `gorm:"column:order_no" json:"orderNo"` // 单号 Status int `gorm:"column:status" json:"status"` // 状态 SenderName string `gorm:"column:sender_name" json:"senderName"` // 寄件人姓名 SenderPhone string `gorm:"column:sender_phone" json:"senderPhone"` // 寄件人电话 ProvinceName string `gorm:"column:province_name" json:"provinceName"` // 省中文名 CityName string `gorm:"column:city_name" json:"cityName"` // 市中文名 RegionName string `gorm:"column:region_name" json:"regionName"` // 区中文名 Address string `gorm:"column:address" json:"address"` // 详细地址 } func (a *orderTransportTask) Page(ctx context.Context, dto OrderTransportTaskPageDTO) ([]OrderTransportTaskPageVO, int64, error) { db := a.store.optionDB(ctx, a.store.withAccountDataPermis(ctx, "dro.driver_account_id"), a.store.withCompanyDataPermis(ctx, "eo.pid")) stmt := db.Table(a.baseEntity.TableName()+" AS dro"). Joins("LEFT JOIN express_order AS eo ON eo.id = dro.order_id"). Joins("LEFT JOIN address_book AS book ON book.id = eo.sender_address_id"). Where("dro.deleted = ?", models.DeleteNo). Where("eo.deleted = ?", models.DeleteNo). Where("status = ?", dto.Status) stmt.Select("eo.id, eo.order_no, dro.status, book.name AS sender_name, " + "book.phone AS sender_phone, book.province_name, book.city_name, book.region_name, book.address ") if dto.OrderNo != "" { stmt.Where("eo.name LIKE ?", sqlutil.LikeFormat(dto.OrderNo)) } var total int64 var ret []OrderTransportTaskPageVO 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("eo.id ASC") if err := stmt.Find(&ret).Error; err != nil { return nil, 0, err } return ret, total, nil } func (a *orderTransportTask) FinishedTask(ctx context.Context, id int) error { up := models.OrderTransportTask{ Status: constant.TaskStatusFinished, FinishedTime: models.NewMyTime(time.Now()), } err := a.store.optionDB(ctx).Model(a.baseEntity). Where("deleted = ?", models.DeleteNo). Where("id = ?", id). Updates(up).Error return err }