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" "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors" "time" ) // ExpressOrderStore 快递单表 type ExpressOrderStore interface { DbBaseStore MyPage(ctx context.Context, dto ExpressOrderPageDTO) ([]MyOrderPageVO, int64, error) ManagePage(ctx context.Context, dto ExpressOrderPageDTO) ([]ManagePageVO, int64, error) FindByOrderNo(ctx context.Context, orderNo string) (models.ExpressOrder, error) UpdateInTransitById(ctx context.Context, id, logisticId int) error FindOrderSendAddressById(ctx context.Context, id int) (models.AddressBook, error) FindOrderConsigneeAddressById(ctx context.Context, id int) (models.AddressBook, error) StaticNumByTime(ctx context.Context, dto StaticOrderNumDTO) ([]StaticNumData, error) StaticNumByUser(ctx context.Context, dto StaticOrderNumDTO) ([]StaticNumByUserData, int64, error) } var _ ExpressOrderStore = &expressOrder{} type expressOrder struct { dbBase } func newExpressOrder(ds *DataStore) *expressOrder { return &expressOrder{dbBase: dbBase{ store: ds, baseEntity: &models.ExpressOrder{}, }} } func (ds *DataStore) ExpressOrder() ExpressOrderStore { return newExpressOrder(ds) } type ExpressOrderPageDTO struct { Page core.Page OrderNo string // 订单号 OrderStatus []int PlaceOrderStartTime models.MyTime PlaceOrderEndTime models.MyTime } type MyOrderPageVO struct { OrderId int `gorm:"column:id" json:"orderId"` // OrderNo string `gorm:"column:order_no" json:"orderNo"` // 单号 OrderSubNo string `gorm:"-" json:"orderSubNo"` // 订单揽件码 OrderStatus int `gorm:"column:order_status" json:"orderStatus"` // 订单状态 LogisticName string `gorm:"column:logistic_name" json:"logisticName"` // 物流名称 Location string `gorm:"column:location" json:"location"` // 当前所在地 LocationTime models.MyTime `gorm:"column:location_time" json:"locationTime"` // 所在地时间 ContactPerson string `gorm:"column:contact_person" json:"contactPerson"` // 联系人 ContactPhone string `gorm:"column:contact_phone" json:"contactPhone"` // 联系电话 Extend string `gorm:"column:extend" json:"extend"` // } // MyPage 我的寄件列表 func (a *expressOrder) MyPage(ctx context.Context, dto ExpressOrderPageDTO) ([]MyOrderPageVO, int64, error) { db := a.store.optionDB(ctx, a.store.withAccountDataPermis(ctx, "eo.created_by")) stmt := db.Table(a.baseEntity.TableName()+" AS eo"). Joins("LEFT JOIN logistic_company AS logistic ON logistic.id = eo.logistic_id"). Joins("LEFT JOIN order_logistic_log AS log ON log.id = eo.latest_log_id"). Where("eo.deleted = ?", models.DeleteNo) stmt.Select("eo.id, eo.order_no, eo.order_status, logistic.name AS logistic_name, " + "log.created_time AS location_time, log.location, log.contact_phone, log.contact_person, log.extend") if dto.OrderNo != "" { stmt.Where("eo.order_no LIKE ?", sqlutil.LikeFormat(dto.OrderNo)) } if len(dto.OrderStatus) > 0 { stmt.Where("eo.order_status IN(?)", dto.OrderStatus) } if !dto.PlaceOrderStartTime.IsZero() { stmt.Where("eo.created_time >= ?", dto.PlaceOrderStartTime) } if !dto.PlaceOrderEndTime.IsZero() { stmt.Where("eo.created_time <= ?", dto.PlaceOrderEndTime) } var total int64 var ret []MyOrderPageVO 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 desc") if err := stmt.Find(&ret).Error; err != nil { return nil, 0, err } return ret, total, nil } type ManagePageVO struct { OrderId int `gorm:"column:id" json:"orderId"` // OrderNo string `gorm:"column:order_no" json:"orderNo"` // 单号 OrderStatus int `gorm:"column:order_status" json:"orderStatus"` // 订单状态 SenderAddressId int `gorm:"column:sender_address_id" json:"-"` // SenderName string `gorm:"-" json:"senderName"` // 寄件人 ConsigneeAddressId int `gorm:"column:consignee_address_id" json:"-"` // ConsigneeName string `gorm:"column:consignee_name" json:"consigneeName"` // 收件人 PlaceOrderTime models.MyTime `gorm:"column:create_time" json:"placeOrderTime"` // 下单时间 } // ManagePage 管理后台订单列表 func (a *expressOrder) ManagePage(ctx context.Context, dto ExpressOrderPageDTO) ([]ManagePageVO, int64, error) { db := a.store.optionDB(ctx, a.store.withCompanyDataPermis(ctx, "pid")) stmt := db.Model(a.baseEntity).Where("deleted = ?", models.DeleteNo) stmt.Select("id, order_no, order_status, sender_address_id, consignee_address_id, created_time") if dto.OrderNo != "" { stmt.Where("order_no LIKE ?", sqlutil.LikeFormat(dto.OrderNo)) } if len(dto.OrderStatus) > 0 { stmt.Where("order_status IN (?)", dto.OrderStatus) } if !dto.PlaceOrderStartTime.IsZero() { stmt.Where("created_time >= ?", dto.PlaceOrderStartTime) } if !dto.PlaceOrderEndTime.IsZero() { stmt.Where("created_time <= ?", dto.PlaceOrderEndTime) } var total int64 var ret []ManagePageVO 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 *expressOrder) FindByOrderNo(ctx context.Context, orderNo string) (models.ExpressOrder, error) { ret := models.ExpressOrder{} err := a.store.optionDB(ctx).Model(&models.ExpressOrder{}). Where("deleted = ?", models.DeleteNo). Where("order_no = ?", orderNo).Find(&ret).Error return ret, errors.WithStackOnce(err) } func (a *expressOrder) UpdateInTransitById(ctx context.Context, id, logisticId int) error { up := map[string]interface{}{ "updated_time": models.NewMyTime(time.Now()), "updated_by": global.GetTokenInfoFromContext(ctx).AccountId, "order_status": constant.OrderStatusInTransit, "logistic_id": logisticId, "order_sub_no": "", } err := a.store.optionDB(ctx).Model(&models.ExpressOrder{}). Where("deleted = ?", models.DeleteNo). Where("id = ?", id). Updates(&up).Error return errors.WithStackOnce(err) } func (a *expressOrder) FindOrderSendAddressById(ctx context.Context, id int) (models.AddressBook, error) { stmt := a.store.optionDB(ctx).Table(a.baseEntity.TableName()+" AS eo"). Joins("address_book AS book ON book.id = eo.sender_address_id"). Where("eo.deleted = ?", models.DeleteNo). Where("id = ?", id) ret := models.AddressBook{} stmt.Select("province_name, city_name, region_name, address") err := stmt.Find(&ret).Error return ret, errors.WithStackOnce(err) } func (a *expressOrder) FindOrderConsigneeAddressById(ctx context.Context, id int) (models.AddressBook, error) { stmt := a.store.optionDB(ctx).Table(a.baseEntity.TableName()+" AS eo"). Joins("address_book AS book ON book.id = eo.consignee_address_id"). Where("eo.deleted = ?", models.DeleteNo). Where("id = ?", id) ret := models.AddressBook{} stmt.Select("province_name, city_name, region_name, address") err := stmt.Find(&ret).Error return ret, errors.WithStackOnce(err) } type StaticOrderNumDTO struct { Page core.Page StatType models.TimeStatType // 年,月,天 TimeStart models.MyTime TimeEnd models.MyTime OrderStatus int } type StaticNumData struct { XKey string `gorm:"column:x_key" json:"xKey"` OrderCount int64 `gorm:"column:order_count" json:"orderCount"` } func (a *expressOrder) StaticNumByTime(ctx context.Context, dto StaticOrderNumDTO) ([]StaticNumData, error) { stmt := a.store.optionDB(ctx, a.store.withCompanyDataPermis(ctx, "pid")). Model(a.baseEntity).Where("deleted = ?", models.DeleteNo) group := dto.StatType.Format("created_time", "x_key") stmt.Select(group, "COUNT(*) AS order_count") if !dto.TimeStart.IsZero() { stmt.Where("created_time >= ?", dto.TimeStart) } if !dto.TimeEnd.IsZero() { stmt.Where("created_time <= ?", dto.TimeEnd) } if dto.OrderStatus > 0 { stmt.Where("order_status = ?", dto.OrderStatus) } stmt.Order("x_key") ret := make([]StaticNumData, 0) err := stmt.Find(&ret).Error return ret, errors.WithStackOnce(err) } type StaticNumByUserData struct { XKey string `gorm:"column:x_key" json:"xKey"` XTime string `gorm:"column:x_time" json:"xTime"` OrderCount int64 `gorm:"column:order_count" json:"orderCount"` } func (a *expressOrder) StaticNumByUser(ctx context.Context, dto StaticOrderNumDTO) ([]StaticNumByUserData, int64, error) { stmt := a.store.optionDB(ctx, a.store.withCompanyDataPermis(ctx, "express_order.pid")). Table(a.baseEntity.TableName()). Joins("LEFT JOIN account ON account.id = express_order.created_by"). Where("express_order.deleted = ?", models.DeleteNo) group := dto.StatType.Format("express_order.created_time", "express_order.x_time") stmt.Select("account.name AS x_key", group, "COUNT(*) AS order_count") if !dto.TimeStart.IsZero() { stmt.Where("express_order.created_time >= ?", dto.TimeStart) } if !dto.TimeEnd.IsZero() { stmt.Where("express_order.created_time <= ?", dto.TimeEnd) } if dto.OrderStatus > 0 { stmt.Where("express_order.order_status = ?", dto.OrderStatus) } var total int64 ret := make([]StaticNumByUserData, 0) if err := stmt.Count(&total).Error; err != nil { return nil, 0, err } if total == 0 { return ret, 0, nil } stmt.Order("x_key, x_time") stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset) err := stmt.Find(&ret).Error return ret, total, errors.WithStackOnce(err) }