Browse Source

FUNC:添加运单app

zoie 11 months ago
parent
commit
48380f1581

+ 34 - 0
app/admin/controller/waybill.go

@@ -168,6 +168,40 @@ func (e WaybillController) Insert(c *gin.Context) {
 	e.OK(req.GetId(), "创建成功")
 }
 
+// AppletInsert 添加运单app
+// @Summary 添加运单app
+// @Description 添加运单app
+// @Tags 运单
+// @Accept  application/json
+// @Product application/json
+// @Param data body dto.WaybillInsertReq true "data"
+// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
+// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
+// @Router /api/waybill [post]
+// @Security Bearer
+func (e WaybillController) AppletInsert(c *gin.Context) {
+	s := service.Waybill{}
+	req := dto.WaybillInsertReq{}
+	err := e.MakeContext(c).
+		MakeOrm().
+		Bind(&req, binding.JSON).
+		MakeService(&s.Service).
+		Errors
+	if err != nil {
+		e.Logger.Error(err)
+		e.Error(500, err, err.Error())
+		return
+	}
+	p := actions.GetPermissionFromContext(c)
+
+	err = s.AppletInsert(&req, p)
+	if err != nil {
+		e.Error(500, err, err.Error())
+		return
+	}
+	e.OK(req.GetId(), "添加成功")
+}
+
 // Update 修改运单
 // @Summary 修改运单
 // @Description 修改运单

+ 1 - 0
app/admin/router/waybill.go

@@ -20,6 +20,7 @@ func registerWaybillRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddle
 		r.GET("", cont.GetPage)
 		r.GET("/:id", cont.Get)
 		r.POST("", cont.Insert)
+		r.POST("/applet", cont.AppletInsert)
 		r.POST("/delivery", cont.Delivery)
 		r.PUT("", cont.Update)
 		r.DELETE("", cont.Delete)

+ 5 - 4
app/admin/service/dto/waybill.go

@@ -137,9 +137,9 @@ func (s *WaybillUpdateReq) GetId() interface{} {
 }
 
 type WaybillDeliveryReq struct {
-	WaybillIds       []int `json:"waybillIds" comment:"编码"`      // 运单id
-	Type             int   `json:"type"  gorm:"size:128"`        // 派单类型 3司机 2仓管
-	PrintUserId      int   `json:"printUserId"  gorm:"size:128"` // 制单人
+	WaybillIds       []int `json:"waybillIds"`  // 运单id
+	Type             int   `json:"type"`        // 派单类型 3司机 2仓管
+	PrintUserId      int   `json:"printUserId"` // 制单人
 	model2.ControlBy `swaggerignore:"true"`
 	model2.DeptBy    `swaggerignore:"true"`
 }
@@ -167,7 +167,8 @@ func (s *WaybillDeleteReq) GetId() interface{} {
 
 // 运单出入库/上下车
 type WaybillInOutReq struct {
-	WaybillNoList []string `json:"waybillNoList"  gorm:"size:128"` // 订单编号
+	StartTime     model2.Time `json:"startTime"`
+	WaybillNoList []string    `json:"waybillNoList"  gorm:"size:128"` // 订单编号
 }
 
 type WaybillGetAppletPageReq struct {

+ 182 - 22
app/admin/service/waybill.go

@@ -180,6 +180,113 @@ func (e *Waybill) Insert(c *dto.WaybillInsertReq) error {
 
 }
 
+// AppletInsert 员工添加运单
+func (e *Waybill) AppletInsert(c *dto.WaybillInsertReq, p *actions.DataPermission) error {
+	var err error
+	var data model.Waybill
+
+	tx := e.Orm.Begin()
+	defer func() {
+		if err != nil {
+			tx.Rollback()
+		} else {
+			tx.Commit()
+		}
+	}()
+
+	var userModel = model.SysUser{}
+	// 查询运单是否存在
+	err = tx.Scopes(actions.Permission(userModel.TableName(), p)).
+		First(&userModel, p.UserId).Error
+	if err != nil {
+		e.Log.Errorf("db error: %s", err)
+		if errors.Is(err, gorm.ErrRecordNotFound) {
+			return global.GetNotFoundErr
+		}
+		return global.CreateFailedErr
+	}
+	if userModel.Type != model.SysUserTypeDriver && userModel.Type != model.SysUserTypeWarehouse {
+		err = errors.New("无权添加!")
+		return err
+	}
+	var status = model.WaybillStatusWaitDelivery
+	var car = model.Car{}
+	if userModel.Type == model.SysUserTypeDriver {
+		status = model.WaybillStatusWaitTruck
+		// 查询车辆库信息
+		err = tx.Scopes(actions.Permission(car.TableName(), p)).
+			Where("user_id = ?", p.UserId).
+			First(&car).Error
+		if err != nil {
+			e.Log.Errorf("db error: %s", err)
+			return errors.New("获取车辆信息失败")
+		}
+	}
+
+	var warehouse = model.Warehouse{}
+	if userModel.Type == model.SysUserTypeWarehouse {
+		status = model.WaybillStatusWaitStorage
+		// 查询仓库信息
+		err = tx.Scopes(actions.Permission(warehouse.TableName(), p)).
+			Where("user_id = ?", p.UserId).
+			First(&warehouse).Error
+		if err != nil {
+			e.Log.Errorf("db error: %s", err)
+			return errors.New("获取仓库信息失败")
+		}
+	}
+	data.PrintUserId = p.UserId
+
+	var no string
+	for {
+		no = time.Now().Format("200601021504") + utils.GetRandString(6, "0123456789", 0)
+		var j int64
+		err = e.Orm.Model(&data).Where("waybill_no = ?", no).Count(&j).Error
+		if err != nil {
+			continue
+		}
+		if j == 0 {
+			break
+		}
+	}
+
+	// 添加运单
+	data.DeptId = p.DeptId
+	data.CreateBy = p.UserId
+	data.WaybillNo = no
+	c.Generate(&data)
+	data.Status = status
+	err = tx.Create(&data).Error
+	if err != nil {
+		e.Log.Errorf("db error: %s", err)
+		return global.CreateFailedErr
+	}
+	c.Id = data.Id
+
+	// 添加物流
+	Logistics := model.WaybillLogistics{
+		WaybillNo:   data.WaybillNo,
+		Status:      data.Status,
+		CarId:       car.Id,
+		WarehouseId: warehouse.Id,
+		UserId:      p.UserId,
+		ControlBy: model2.ControlBy{
+			CreateBy: p.UserId,
+		},
+		DeptBy: model2.DeptBy{
+			DeptId: p.DeptId,
+		},
+	}
+	err = tx.Create(&Logistics).Error
+	if err != nil {
+		e.Log.Errorf("db error: %s", err)
+		return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
+	}
+
+	return nil
+
+}
+
 // Update 修改Waybill对象
 func (e *Waybill) Update(c *dto.WaybillUpdateReq, p *actions.DataPermission) error {
 	var err error
@@ -243,7 +350,8 @@ func (e *Waybill) Delivery(c *dto.WaybillDeliveryReq, p *actions.DataPermission)
 		if waybillModel.Status != model.WaybillStatusWaitDelivery &&
 			waybillModel.Status != model.WaybillStatusWaitTruck &&
 			waybillModel.Status != model.WaybillStatusWaitStorage {
-			return errors.New(fmt.Sprintf("运单状态为%s,禁止操作!", model.WaybillStatusMap[waybillModel.Status]))
+			err = errors.New(fmt.Sprintf("运单状态为%s,禁止操作!", model.WaybillStatusMap[waybillModel.Status]))
+			return err
 		}
 
 		var car = model.Car{}
@@ -264,7 +372,7 @@ func (e *Waybill) Delivery(c *dto.WaybillDeliveryReq, p *actions.DataPermission)
 			waybillModel.Status = model.WaybillStatusWaitStorage
 			// 查询仓库信息
 			err = tx.Scopes(actions.Permission(warehouse.TableName(), p)).
-				Where("user_id = ?",c.PrintUserId).
+				Where("user_id = ?", c.PrintUserId).
 				First(&warehouse).Error
 			if err != nil {
 				e.Log.Errorf("db error: %s", err)
@@ -383,17 +491,26 @@ func (e *Waybill) WarehouseIn(c *dto.WaybillInOutReq, p *actions.DataPermission)
 		if waybillModel.WarehouseId == warehouse.Id && waybillModel.Status == model.WaybillStatusStorage {
 			continue
 		}
+		if waybillModel.Status == model.WaybillStatusTruck || (waybillModel.WarehouseId != warehouse.Id && waybillModel.Status == model.WaybillStatusStorage) {
+			err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法入库!", waybillNo, model.WaybillStatusMap[waybillModel.Status]))
+			return err
+		}
+
 		waybillModel.Status = model.WaybillStatusStorage
 		waybillModel.WarehouseId = warehouse.Id
-		err = tx.Save(waybillModel).Error
+		err = tx.Save(&waybillModel).Error
 		if err != nil {
 			e.Log.Errorf("db error: %s", err)
 			return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
 		}
 		// 获取传感器信息
-		deviceSensorList, count, err := nats_server.Cold_CompanyDeviceSensor_List_ByKey(warehouse.Sn)
+		// 获取传感器信息
+		var deviceSensorList = []nats_server.DeviceSensor_R{}
+		var count int64
+		deviceSensorList, count, err = nats_server.Cold_CompanyDeviceSensor_List_ByKey(warehouse.Sn)
 		if err != nil || count == 0 {
-			return errors.New(fmt.Sprintf("查询设备定位信息失败:%s", err.Error()))
+			err = errors.New("查询设备定位信息失败")
+			return err
 		}
 		var lng, Lat string
 		if len(deviceSensorList[0].T_DeviceSensorData.T_site) > 0 {
@@ -418,6 +535,9 @@ func (e *Waybill) WarehouseIn(c *dto.WaybillInOutReq, p *actions.DataPermission)
 			DeptBy: model2.DeptBy{
 				DeptId: p.DeptId,
 			},
+			ModelTime: model2.ModelTime{
+				CreatedAt: c.StartTime,
+			},
 		}
 		err = tx.Create(&Logistics).Error
 		if err != nil {
@@ -431,7 +551,7 @@ func (e *Waybill) WarehouseIn(c *dto.WaybillInOutReq, p *actions.DataPermission)
 			WarehouseId: warehouse.Id,
 			UserId:      p.UserId,
 			Sn:          warehouse.Sn,
-			StartTime:   model2.Time(time.Now()),
+			StartTime:   c.StartTime,
 			ControlBy: model2.ControlBy{
 				CreateBy: p.UserId,
 			},
@@ -491,17 +611,24 @@ func (e *Waybill) WarehouseOut(c *dto.WaybillInOutReq, p *actions.DataPermission
 		if waybillModel.WarehouseId == warehouse.Id && waybillModel.Status == model.WaybillStatusStorageOut {
 			continue
 		}
+		if waybillModel.Status != model.WaybillStatusStorage {
+			err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法出库!", waybillNo, model.WaybillStatusMap[waybillModel.Status]))
+			return err
+		}
 		waybillModel.Status = model.WaybillStatusStorageOut
 		waybillModel.WarehouseId = warehouse.Id
-		err = tx.Save(waybillModel).Error
+		err = tx.Save(&waybillModel).Error
 		if err != nil {
 			e.Log.Errorf("db error: %s", err)
 			return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
 		}
 		// 获取传感器信息
-		deviceSensorList, count, err := nats_server.Cold_CompanyDeviceSensor_List_ByKey(warehouse.Sn)
+		var deviceSensorList = []nats_server.DeviceSensor_R{}
+		var count int64
+		deviceSensorList, count, err = nats_server.Cold_CompanyDeviceSensor_List_ByKey(warehouse.Sn)
 		if err != nil || count == 0 {
-			return errors.New(fmt.Sprintf("查询设备定位信息失败:%s", err.Error()))
+			err = errors.New("查询设备定位信息失败")
+			return err
 		}
 		var lng, Lat string
 		if len(deviceSensorList[0].T_DeviceSensorData.T_site) > 0 {
@@ -526,6 +653,9 @@ func (e *Waybill) WarehouseOut(c *dto.WaybillInOutReq, p *actions.DataPermission
 			DeptBy: model2.DeptBy{
 				DeptId: p.DeptId,
 			},
+			ModelTime: model2.ModelTime{
+				CreatedAt: c.StartTime,
+			},
 		}
 		err = tx.Create(&Logistics).Error
 		if err != nil {
@@ -541,7 +671,7 @@ func (e *Waybill) WarehouseOut(c *dto.WaybillInOutReq, p *actions.DataPermission
 			e.Log.Errorf("db error: %s", err)
 			return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
 		}
-		task.EndTime = model2.Time(time.Now())
+		task.EndTime = c.StartTime
 		task.UpdateBy = p.UserId
 		err = tx.Save(&task).Error
 		if err != nil {
@@ -590,17 +720,24 @@ func (e *Waybill) CarIn(c *dto.WaybillInOutReq, p *actions.DataPermission) error
 		if waybillModel.CarId == car.Id && waybillModel.Status == model.WaybillStatusTruck {
 			continue
 		}
+		if waybillModel.Status == model.WaybillStatusStorage || (waybillModel.CarId != car.Id && waybillModel.Status == model.WaybillStatusTruck) {
+			err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法装车!", waybillNo, model.WaybillStatusMap[waybillModel.Status]))
+			return err
+		}
 		waybillModel.Status = model.WaybillStatusTruck
 		waybillModel.CarId = car.Id
-		err = tx.Save(waybillModel).Error
+		err = tx.Save(&waybillModel).Error
 		if err != nil {
 			e.Log.Errorf("db error: %s", err)
 			return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
 		}
 		// 获取传感器信息
-		deviceSensorList, count, err := nats_server.Cold_CompanyDeviceSensor_List_ByKey(car.Sn)
+		var deviceSensorList = []nats_server.DeviceSensor_R{}
+		var count int64
+		deviceSensorList, count, err = nats_server.Cold_CompanyDeviceSensor_List_ByKey(car.Sn)
 		if err != nil || count == 0 {
-			return errors.New(fmt.Sprintf("查询设备定位信息失败:%s", err.Error()))
+			err = errors.New("查询设备定位信息失败")
+			return err
 		}
 		var lng, Lat string
 		if len(deviceSensorList[0].T_DeviceSensorData.T_site) > 0 {
@@ -625,6 +762,9 @@ func (e *Waybill) CarIn(c *dto.WaybillInOutReq, p *actions.DataPermission) error
 			DeptBy: model2.DeptBy{
 				DeptId: p.DeptId,
 			},
+			ModelTime: model2.ModelTime{
+				CreatedAt: c.StartTime,
+			},
 		}
 		err = tx.Create(&Logistics).Error
 		if err != nil {
@@ -638,7 +778,7 @@ func (e *Waybill) CarIn(c *dto.WaybillInOutReq, p *actions.DataPermission) error
 			CarId:     car.Id,
 			UserId:    p.UserId,
 			Sn:        car.Sn,
-			StartTime: model2.Time(time.Now()),
+			StartTime: c.StartTime,
 			ControlBy: model2.ControlBy{
 				CreateBy: p.UserId,
 			},
@@ -698,17 +838,24 @@ func (e *Waybill) CarOut(c *dto.WaybillInOutReq, p *actions.DataPermission) erro
 		if waybillModel.CarId == car.Id && waybillModel.Status == model.WaybillStatusTruckOut {
 			continue
 		}
+		if waybillModel.Status != model.WaybillStatusTruck {
+			err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法下车!", waybillNo, model.WaybillStatusMap[waybillModel.Status]))
+			return err
+		}
 		waybillModel.Status = model.WaybillStatusTruckOut
 		waybillModel.CarId = car.Id
-		err = tx.Save(waybillModel).Error
+		err = tx.Save(&waybillModel).Error
 		if err != nil {
 			e.Log.Errorf("db error: %s", err)
 			return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
 		}
 		// 获取传感器信息
-		deviceSensorList, count, err := nats_server.Cold_CompanyDeviceSensor_List_ByKey(car.Sn)
+		var deviceSensorList = []nats_server.DeviceSensor_R{}
+		var count int64
+		deviceSensorList, count, err = nats_server.Cold_CompanyDeviceSensor_List_ByKey(car.Sn)
 		if err != nil || count == 0 {
-			return errors.New(fmt.Sprintf("查询设备定位信息失败:%s", err.Error()))
+			err = errors.New("查询设备定位信息失败")
+			return err
 		}
 		var lng, Lat string
 		if len(deviceSensorList[0].T_DeviceSensorData.T_site) > 0 {
@@ -733,6 +880,9 @@ func (e *Waybill) CarOut(c *dto.WaybillInOutReq, p *actions.DataPermission) erro
 			DeptBy: model2.DeptBy{
 				DeptId: p.DeptId,
 			},
+			ModelTime: model2.ModelTime{
+				CreatedAt: c.StartTime,
+			},
 		}
 		err = tx.Create(&Logistics).Error
 		if err != nil {
@@ -748,7 +898,7 @@ func (e *Waybill) CarOut(c *dto.WaybillInOutReq, p *actions.DataPermission) erro
 			e.Log.Errorf("db error: %s", err)
 			return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
 		}
-		task.EndTime = model2.Time(time.Now())
+		task.EndTime = c.StartTime
 		task.UpdateBy = p.UserId
 		err = tx.Save(&task).Error
 		if err != nil {
@@ -801,15 +951,19 @@ func (e *Waybill) Receipt(c *dto.WaybillInOutReq, p *actions.DataPermission) err
 		waybillModel.Status = model.WaybillStatusReceipt
 		waybillModel.CarId = car.Id
 		waybillModel.ReceiptTime = model2.Time(time.Now())
-		err = tx.Save(waybillModel).Error
+		err = tx.Save(&waybillModel).Error
 		if err != nil {
 			e.Log.Errorf("db error: %s", err)
 			return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
 		}
 		// 获取传感器信息
-		deviceSensorList, count, err := nats_server.Cold_CompanyDeviceSensor_List_ByKey(car.Sn)
+		// 获取传感器信息
+		var deviceSensorList = []nats_server.DeviceSensor_R{}
+		var count int64
+		deviceSensorList, count, err = nats_server.Cold_CompanyDeviceSensor_List_ByKey(car.Sn)
 		if err != nil || count == 0 {
-			return errors.New(fmt.Sprintf("查询设备定位信息失败:%s", err.Error()))
+			err = errors.New("查询设备定位信息失败")
+			return err
 		}
 		var lng, Lat string
 		if len(deviceSensorList[0].T_DeviceSensorData.T_site) > 0 {
@@ -851,6 +1005,9 @@ func (e *Waybill) Receipt(c *dto.WaybillInOutReq, p *actions.DataPermission) err
 				DeptBy: model2.DeptBy{
 					DeptId: p.DeptId,
 				},
+				ModelTime: model2.ModelTime{
+					CreatedAt: c.StartTime,
+				},
 			}
 			err = tx.Create(&Logistics).Error
 			if err != nil {
@@ -859,7 +1016,7 @@ func (e *Waybill) Receipt(c *dto.WaybillInOutReq, p *actions.DataPermission) err
 			}
 		}
 
-		// 添加物流
+		// 添加签收记录
 		Logistics := model.WaybillLogistics{
 			WaybillNo: waybillNo,
 			Status:    model.WaybillStatusReceipt,
@@ -873,6 +1030,9 @@ func (e *Waybill) Receipt(c *dto.WaybillInOutReq, p *actions.DataPermission) err
 			DeptBy: model2.DeptBy{
 				DeptId: p.DeptId,
 			},
+			ModelTime: model2.ModelTime{
+				CreatedAt: c.StartTime,
+			},
 		}
 		err = tx.Create(&Logistics).Error
 		if err != nil {

+ 8 - 0
app/admin/service/waybill_logistics.go

@@ -17,10 +17,18 @@ func (e *WaybillLogistics) GetPage(c *dto.WaybillLogisticsGetPageReq, list *[]mo
 	var err error
 	var data model.WaybillLogistics
 
+	status := []int{
+		model.WaybillStatusTruck,
+		model.WaybillStatusStorage,
+		model.WaybillStatusTruckOut,
+		model.WaybillStatusStorageOut,
+		model.WaybillStatusReceipt,
+	}
 	err = e.Orm.Model(&data).
 		Scopes(
 			cDto.MakeCondition(c.GetNeedSearch()),
 		).
+		Where("status in (?)", status).
 		Preload("Warehouse").Preload("Car").
 		Find(list).Limit(-1).Offset(-1).
 		Count(count).Error