|
@@ -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 {
|