package service import ( "cold-logistics/app/admin/model" "cold-logistics/app/admin/service/dto" "cold-logistics/common/actions" cDto "cold-logistics/common/dto" "cold-logistics/common/global" "errors" "fmt" "gogs.baozhida.cn/zoie/OAuth-core/service" "gorm.io/gorm" ) type WaybillLogistics struct { service.Service } // GetPage 获取WaybillLogistics列表 func (e *WaybillLogistics) GetPage(c *dto.WaybillLogisticsGetPageReq, list *[]model.WaybillLogistics, count *int64) error { var err error var data model.WaybillLogistics status := []int{ model.WaybillStatusTruck, model.WaybillStatusStorage, model.WaybillStatusTruckOut, model.WaybillStatusStorageOut, model.WaybillStatusVanning, model.WaybillStatusVanningOut, model.WaybillStatusReceipt, } err = e.Orm.Model(&data). Scopes( cDto.MakeCondition(c.GetNeedSearch()), ). Where("status in (?)", status). Preload("User"). Preload("Warehouse.User").Preload("Car.User").Preload("CoolerBox"). Find(list). Count(count).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.GetFailedErr } return nil } // Update 修改Waybill对象 func (e *WaybillLogistics) Update(c *dto.WaybillLogisticsUpdateReq, p *actions.DataPermission) error { var err error tx := e.Orm.Begin() defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() for _, logistics := range c.WaybillLogisticsList { var waybillLogisticsModel = model.WaybillLogistics{} // 查询运单是否存在 err = e.Orm.Scopes(actions.Permission(waybillLogisticsModel.TableName(), p)). First(&waybillLogisticsModel, logistics.Id).Error if err != nil { e.Log.Errorf("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.UpdateNotFoundOrNoPermissionErr } return global.UpdateFailedErr } waybillLogisticsModel.CreatedAt = logistics.Time waybillLogisticsModel.UpdateBy = c.UpdateBy err = tx.Save(&waybillLogisticsModel).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.UpdateFailedErr } if waybillLogisticsModel.Status == model.WaybillStatusReceipt { // 修改运单签收时间 var waybillModel = model.Waybill{} // 查询运单是否存在 err = tx.Scopes(actions.Permission(waybillModel.TableName(), p)). Where("waybill_no = ?", waybillLogisticsModel.WaybillNo). First(&waybillModel).Error if err != nil { e.Log.Errorf("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return errors.New(fmt.Sprintf("运单号%s不存在", waybillLogisticsModel.WaybillNo)) } return errors.New(fmt.Sprintf("运单号%s查询失败", waybillLogisticsModel.WaybillNo)) } waybillModel.ReceiptTime = logistics.Time err = tx.Save(&waybillModel).Error if err != nil { e.Log.Errorf("db error: %s", err) return errors.New(fmt.Sprintf("保存运单信息失败:%s", err)) } } } return nil }