|
@@ -28,9 +28,7 @@ func WaybillCustomerStatusScopes(status int) func(db *gorm.DB) *gorm.DB {
|
|
|
// 未发货
|
|
|
if status == 1 {
|
|
|
statusList := []int{
|
|
|
- model.WaybillStatusWaitDelivery,
|
|
|
model.WaybillStatusTruck,
|
|
|
- model.WaybillStatusWaitStorage,
|
|
|
}
|
|
|
return db.Where("status in (?)", statusList)
|
|
|
}
|
|
@@ -41,6 +39,8 @@ func WaybillCustomerStatusScopes(status int) func(db *gorm.DB) *gorm.DB {
|
|
|
model.WaybillStatusStorage,
|
|
|
model.WaybillStatusTruckOut,
|
|
|
model.WaybillStatusStorageOut,
|
|
|
+ model.WaybillStatusVanning,
|
|
|
+ model.WaybillStatusVanningOut,
|
|
|
}
|
|
|
return db.Where("status in (?)", statusList)
|
|
|
}
|
|
@@ -51,6 +51,15 @@ func WaybillCustomerStatusScopes(status int) func(db *gorm.DB) *gorm.DB {
|
|
|
}
|
|
|
return db.Where("status in (?)", statusList)
|
|
|
}
|
|
|
+ // 已处理
|
|
|
+ if status == 4 {
|
|
|
+ statusList := []int{
|
|
|
+ model.WaybillStatusWaitDelivery,
|
|
|
+ model.WaybillStatusWaitStorage,
|
|
|
+ model.WaybillStatusWaitVanning,
|
|
|
+ }
|
|
|
+ return db.Where("status in (?)", statusList)
|
|
|
+ }
|
|
|
return db
|
|
|
}
|
|
|
|
|
@@ -182,6 +191,7 @@ func (e *Waybill) Get(d *dto.WaybillGetReq, waybillModel *model.Waybill, p *acti
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
func (e *Waybill) GetByWaybillNo(d *dto.WaybillGetByWaybillPdfReq, waybillModel *model.Waybill, p *actions.DataPermission) error {
|
|
|
err := e.Orm.
|
|
|
Scopes(actions.Permission(waybillModel.TableName(), p)).
|
|
@@ -265,7 +275,7 @@ func (e *Waybill) AppletInsert(c *dto.WaybillInsertReq, p *actions.DataPermissio
|
|
|
}
|
|
|
return global.CreateFailedErr
|
|
|
}
|
|
|
- if (userModel.Type != model.SysUserTypeDriver && userModel.Type != model.SysUserTypeWarehouse) && userModel.UserType != "customer" {
|
|
|
+ if c.CoolerBoxId == 0 && (userModel.Type != model.SysUserTypeDriver && userModel.Type != model.SysUserTypeWarehouse) && userModel.UserType != "customer" {
|
|
|
err = errors.New("无权添加!")
|
|
|
return err
|
|
|
}
|
|
@@ -299,8 +309,8 @@ func (e *Waybill) AppletInsert(c *dto.WaybillInsertReq, p *actions.DataPermissio
|
|
|
var coolerBox = model.CoolerBox{}
|
|
|
if c.DeliveryCondition == "保温箱" {
|
|
|
status = model.WaybillStatusWaitVanning
|
|
|
- // 查询仓库信息
|
|
|
- err = tx.Scopes(actions.Permission(warehouse.TableName(), p)).
|
|
|
+ // 查询保温箱信息
|
|
|
+ err = tx.Scopes(actions.Permission(coolerBox.TableName(), p)).
|
|
|
Where("id = ?", c.CoolerBoxId).
|
|
|
First(&coolerBox).Error
|
|
|
if err != nil {
|
|
@@ -486,6 +496,7 @@ func (e *Waybill) Delivery(c *dto.WaybillDeliveryReq, p *actions.DataPermission)
|
|
|
|
|
|
if c.Type == model.TypeCoolerBox {
|
|
|
waybillModel.Status = model.WaybillStatusWaitVanning
|
|
|
+ waybillModel.CoolerBoxId = coolerBox.Id
|
|
|
}
|
|
|
|
|
|
waybillModel.PrintUserId = c.PrintUserId
|
|
@@ -1121,7 +1132,7 @@ func (e *Waybill) CarOut(c *dto.WaybillInOutReq, p *actions.DataPermission) erro
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (e *Waybill) CoolerBoxIn(c *dto.WaybillInOutReq, p *actions.DataPermission) error {
|
|
|
+func (e *Waybill) CoolerBoxIn(c *dto.WaybillCoolerBoxInReq, p *actions.DataPermission) error {
|
|
|
var err error
|
|
|
|
|
|
tx := e.Orm.Begin()
|
|
@@ -1146,7 +1157,9 @@ func (e *Waybill) CoolerBoxIn(c *dto.WaybillInOutReq, p *actions.DataPermission)
|
|
|
}
|
|
|
return errors.New(fmt.Sprintf("运单号%s查询失败", waybillNo))
|
|
|
}
|
|
|
- if waybillModel.Status != model.WaybillStatusWaitVanning {
|
|
|
+ if waybillModel.Status != model.WaybillStatusWaitVanning &&
|
|
|
+ waybillModel.Status != model.WaybillStatusStorage &&
|
|
|
+ waybillModel.Status != model.WaybillStatusTruck {
|
|
|
err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法装箱!", waybillNo, model.WaybillStatusMap[waybillModel.Status]))
|
|
|
return err
|
|
|
}
|
|
@@ -1155,11 +1168,11 @@ func (e *Waybill) CoolerBoxIn(c *dto.WaybillInOutReq, p *actions.DataPermission)
|
|
|
var coolerBox = model.CoolerBox{}
|
|
|
// 查询运单是否存在
|
|
|
err = tx.Scopes(actions.Permission(coolerBox.TableName(), p)).
|
|
|
- Where("user_id = ?", p.UserId).
|
|
|
+ Where("id = ?", c.CoolerBoxId).
|
|
|
First(&coolerBox).Error
|
|
|
if err != nil {
|
|
|
e.Log.Errorf("db error: %s", err)
|
|
|
- return errors.New("获取车辆绑定信息失败")
|
|
|
+ return errors.New("获取保温箱信息失败")
|
|
|
}
|
|
|
|
|
|
// 验证时间
|
|
@@ -1177,6 +1190,105 @@ func (e *Waybill) CoolerBoxIn(c *dto.WaybillInOutReq, p *actions.DataPermission)
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
+ // 如果上一个状态为已装车,则自动下车
|
|
|
+ if waybillModel.Status == model.WaybillStatusTruck {
|
|
|
+ // 已装车
|
|
|
+ var car model.Car
|
|
|
+ err = e.Orm.First(&car, waybillModel.CarId).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return errors.New(fmt.Sprintf("查询仓库信息失败:%s", err))
|
|
|
+ }
|
|
|
+ // 添加物流
|
|
|
+ Logistics := model.WaybillLogistics{
|
|
|
+ WaybillNo: waybillNo,
|
|
|
+ Status: model.WaybillStatusTruckOut,
|
|
|
+ CarId: waybillModel.CarId,
|
|
|
+ UserId: car.UserId,
|
|
|
+ Lng: lng,
|
|
|
+ Lat: lat,
|
|
|
+ ControlBy: model2.ControlBy{
|
|
|
+ CreateBy: p.UserId,
|
|
|
+ },
|
|
|
+ DeptBy: model2.DeptBy{
|
|
|
+ DeptId: car.DeptId,
|
|
|
+ },
|
|
|
+ ModelTime: model2.ModelTime{
|
|
|
+ CreatedAt: c.StartTime,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ err = tx.Create(&Logistics).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询任务
|
|
|
+ var task model.WaybillTask
|
|
|
+ err = tx.Model(&task).Where("waybill_no = ? and car_id = ?", waybillNo, waybillModel.CarId).
|
|
|
+ Last(&task).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
|
|
|
+ }
|
|
|
+ task.EndTime = c.StartTime
|
|
|
+ task.UpdateBy = p.UserId
|
|
|
+ err = tx.Save(&task).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果上一个状态为入库,则自动出库
|
|
|
+ if waybillModel.Status == model.WaybillStatusStorage {
|
|
|
+ var warehouse model.Warehouse
|
|
|
+ err = e.Orm.First(&warehouse, waybillModel.WarehouseId).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return errors.New(fmt.Sprintf("查询仓库信息失败:%s", err))
|
|
|
+ }
|
|
|
+ // 添加物流
|
|
|
+ Logistics := model.WaybillLogistics{
|
|
|
+ WaybillNo: waybillNo,
|
|
|
+ Status: model.WaybillStatusStorageOut,
|
|
|
+ WarehouseId: waybillModel.WarehouseId,
|
|
|
+ UserId: warehouse.UserId,
|
|
|
+ Lng: lng,
|
|
|
+ Lat: lat,
|
|
|
+ ControlBy: model2.ControlBy{
|
|
|
+ CreateBy: p.UserId,
|
|
|
+ },
|
|
|
+ DeptBy: model2.DeptBy{
|
|
|
+ DeptId: warehouse.DeptId,
|
|
|
+ },
|
|
|
+ ModelTime: model2.ModelTime{
|
|
|
+ CreatedAt: c.StartTime,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ err = tx.Create(&Logistics).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询任务
|
|
|
+ var task model.WaybillTask
|
|
|
+ err = tx.Model(&task).Where("waybill_no = ? and warehouse_id = ?", waybillNo, waybillModel.WarehouseId).
|
|
|
+ Last(&task).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
|
|
|
+ }
|
|
|
+ task.EndTime = c.StartTime
|
|
|
+ task.UpdateBy = p.UserId
|
|
|
+ err = tx.Save(&task).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
waybillModel.Status = model.WaybillStatusVanning
|
|
|
waybillModel.CoolerBoxId = coolerBox.Id
|
|
|
waybillModel.DeliveryTime = c.StartTime
|
|
@@ -1189,7 +1301,7 @@ func (e *Waybill) CoolerBoxIn(c *dto.WaybillInOutReq, p *actions.DataPermission)
|
|
|
// 添加物流
|
|
|
Logistics := model.WaybillLogistics{
|
|
|
WaybillNo: waybillNo,
|
|
|
- Status: model.WaybillStatusTruck,
|
|
|
+ Status: model.WaybillStatusVanning,
|
|
|
CoolerBoxId: coolerBox.Id,
|
|
|
UserId: p.UserId,
|
|
|
Lng: lng,
|
|
@@ -1302,7 +1414,7 @@ func (e *Waybill) Receipt(c *dto.WaybillReceiptReq, p *actions.DataPermission) e
|
|
|
}
|
|
|
// 未出箱 直接点签收
|
|
|
if time.Time(task.EndTime).IsZero() {
|
|
|
- task.EndTime = model2.Time(time.Now())
|
|
|
+ task.EndTime = c.StartTime
|
|
|
task.UpdateBy = p.UserId
|
|
|
err = tx.Save(&task).Error
|
|
|
if err != nil {
|
|
@@ -1399,7 +1511,7 @@ func (e *Waybill) Receipt(c *dto.WaybillReceiptReq, p *actions.DataPermission) e
|
|
|
}
|
|
|
// 未下车 直接点签收
|
|
|
if time.Time(task.EndTime).IsZero() {
|
|
|
- task.EndTime = model2.Time(time.Now())
|
|
|
+ task.EndTime = c.StartTime
|
|
|
task.UpdateBy = p.UserId
|
|
|
err = tx.Save(&task).Error
|
|
|
if err != nil {
|
|
@@ -1522,8 +1634,14 @@ func (e *Waybill) GetBasicsStats(c *dto.WaybillStatsReq, p *actions.DataPermissi
|
|
|
e.Orm.Model(&data).Scopes(actions.Permission(data.TableName(), p)).Where("status = ?", model.WaybillStatusWaitTruck).Count(&res.WaitTruckNum)
|
|
|
// 未入库
|
|
|
e.Orm.Model(&data).Scopes(actions.Permission(data.TableName(), p)).Where("status = ?", model.WaybillStatusWaitStorage).Count(&res.WaitStorageNum)
|
|
|
+ // 未装箱
|
|
|
+ e.Orm.Model(&data).Scopes(actions.Permission(data.TableName(), p)).Where("status = ?", model.WaybillStatusWaitVanning).Count(&res.WaitVanningNum)
|
|
|
// 配送中
|
|
|
- e.Orm.Model(&data).Scopes(actions.Permission(data.TableName(), p)).Where("status in (?)", []int{model.WaybillStatusTruck, model.WaybillStatusStorage, model.WaybillStatusTruckOut, model.WaybillStatusStorageOut}).Count(&res.InDeliveryNum)
|
|
|
+ e.Orm.Model(&data).Scopes(actions.Permission(data.TableName(), p)).Where("status in (?)",
|
|
|
+ []int{model.WaybillStatusTruck, model.WaybillStatusStorage,
|
|
|
+ model.WaybillStatusTruckOut, model.WaybillStatusStorageOut,
|
|
|
+ model.WaybillStatusVanning, model.WaybillStatusVanningOut,
|
|
|
+ }).Count(&res.InDeliveryNum)
|
|
|
// 获取本月,上月数据
|
|
|
e.Orm.Model(&data).Select("date_format(order_time,'%Y%m') date,count(1) as count ").Scopes(actions.Permission(data.TableName(), p)).
|
|
|
Where("order_time between ? and ?", monthStartTime, now).Group("date").Find(&monthCount)
|
|
@@ -1563,7 +1681,7 @@ func (e *Waybill) GetBasicsStats(c *dto.WaybillStatsReq, p *actions.DataPermissi
|
|
|
|
|
|
if c.Type == "month" {
|
|
|
// 获取上个月第一天
|
|
|
- month, _ := time.Parse("2006-01", c.Date)
|
|
|
+ month, _ := time.ParseInLocation("2006-01", c.Date, time.Local)
|
|
|
firstDayOfMonth := time.Date(month.Year(), month.Month(), 1, 0, 0, 0, 0, now.Location())
|
|
|
lastDayOfMonth := time.Date(month.Year(), month.Month()+1, 1, 23, 59, 59, 0, now.Location()).Add(-time.Hour * 24)
|
|
|
e.Orm.Model(&data).Select("date_format(order_time,'%Y-%m-%d') date,count(1) as num ").Scopes(actions.Permission(data.TableName(), p)).
|
|
@@ -1573,7 +1691,7 @@ func (e *Waybill) GetBasicsStats(c *dto.WaybillStatsReq, p *actions.DataPermissi
|
|
|
return res
|
|
|
}
|
|
|
|
|
|
-// 获取运单所有温湿度素具
|
|
|
+// 获取运单所有温湿度数据
|
|
|
func (e *Waybill) GetAllData(c *dto.WaybillGetByWaybillNoReq) ([]nats_server.DeviceData_R, []WaybillPDF, error) {
|
|
|
var err error
|
|
|
var data model.WaybillTask
|
|
@@ -1885,3 +2003,82 @@ func DeviceSensorDataListToDeviceDataPdfList(list []nats_server.DeviceData_R, T_
|
|
|
return finalList
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+// 获取运单所有温湿度数据
|
|
|
+func (e *Waybill) VerifyDataQualified(waybillNo string) (qualified bool, err error) {
|
|
|
+ var data model.WaybillTask
|
|
|
+ var waybill model.Waybill
|
|
|
+ var taskList []model.WaybillTask
|
|
|
+
|
|
|
+ err = e.Orm.Model(&waybill).Where("waybill_no = ?", waybillNo).First(&waybill).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return qualified, errors.New("获取运单信息失败")
|
|
|
+ }
|
|
|
+
|
|
|
+ // 未签收,不返回数据
|
|
|
+ if waybill.Status != model.WaybillStatusReceipt {
|
|
|
+ return qualified, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取公司秘钥
|
|
|
+ var company model.SysDept
|
|
|
+ company, err = model.GetCompanyById(waybill.DeptId)
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return qualified, model.GetCompanyKeyErr
|
|
|
+ }
|
|
|
+
|
|
|
+ err = e.Orm.Model(&data).
|
|
|
+ Where("waybill_no = ?", waybillNo).
|
|
|
+ Order("id desc").
|
|
|
+ Find(&taskList).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return qualified, global.GetFailedErr
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取最后一个任务id
|
|
|
+ var lastWaybillTask model.WaybillTask
|
|
|
+ err = e.Orm.Model(&lastWaybillTask).Where("waybill_no = ?", waybillNo).Last(&lastWaybillTask).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ err = errors.New("获取运单信息错误!")
|
|
|
+ return qualified, err
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建名称到权重的映射
|
|
|
+ orderMap := make(map[string]int)
|
|
|
+ for i, v := range taskList {
|
|
|
+ orderMap[v.Sn] = i
|
|
|
+ }
|
|
|
+
|
|
|
+ for i := 0; i < len(taskList); i++ {
|
|
|
+ // 获取传感器信息
|
|
|
+ deviceSensorList, _, _ := nats_server.Cold_CompanyDeviceSensor_List_ByKey(taskList[i].Sn, company.ColdKey)
|
|
|
+ var T_snid string
|
|
|
+ deviceSensorParameterMap := map[string]nats_server.DeviceSensorParameter_R{}
|
|
|
+ for _, r := range deviceSensorList {
|
|
|
+ T_snid += fmt.Sprintf("%s,%d|", r.T_sn, r.T_id)
|
|
|
+ deviceSensorParameterMap[fmt.Sprintf("%s-%d", r.T_sn, r.T_id)] = r.T_DeviceSensorParameter
|
|
|
+ }
|
|
|
+ var list []nats_server.DeviceData_R
|
|
|
+ list, _, err = nats_server.Cold_ReadDeviceDataListBy_T_snid(T_snid, taskList[i].StartTime.String(), taskList[i].EndTime.String(), 0, 9999)
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("nats 获取温湿度信息失败: %s", err)
|
|
|
+ return qualified, global.GetFailedErr
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断数据是否合格
|
|
|
+ qualified = true
|
|
|
+ for _, v := range list {
|
|
|
+ dsp := deviceSensorParameterMap[fmt.Sprintf("%s-%d", v.T_sn, v.T_id)]
|
|
|
+ if v.T_t < dsp.T_Tlower || v.T_t > dsp.T_Tupper || v.T_rh < dsp.T_RHlower || v.T_t > dsp.T_RHupper {
|
|
|
+ qualified = false
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return qualified, nil
|
|
|
+}
|