|
@@ -13,6 +13,7 @@ import (
|
|
|
"gogs.baozhida.cn/zoie/OAuth-core/pkg/utils"
|
|
|
"gogs.baozhida.cn/zoie/OAuth-core/service"
|
|
|
"gorm.io/gorm"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
@@ -503,24 +504,13 @@ func (e *Waybill) WarehouseIn(c *dto.WaybillInOutReq, p *actions.DataPermission)
|
|
|
e.Log.Errorf("db error: %s", err)
|
|
|
return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
|
|
|
}
|
|
|
- // 获取传感器信息
|
|
|
- // 获取传感器信息
|
|
|
- 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 {
|
|
|
- err = errors.New("查询设备定位信息失败")
|
|
|
+
|
|
|
+ var lng, lat string
|
|
|
+ lng, lat, err = e.GetSite(p.DeptId, warehouse.Sn, c.StartTime.String())
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("获取定位信息失败: %s", err)
|
|
|
return err
|
|
|
}
|
|
|
- var lng, Lat string
|
|
|
- if len(deviceSensorList[0].T_DeviceSensorData.T_site) > 0 {
|
|
|
- site := strings.Split(deviceSensorList[0].T_DeviceSensorData.T_site, ",")
|
|
|
- if len(site) == 2 {
|
|
|
- lng = site[0]
|
|
|
- Lat = site[1]
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
// 添加物流
|
|
|
Logistics := model.WaybillLogistics{
|
|
|
WaybillNo: waybillNo,
|
|
@@ -528,7 +518,7 @@ func (e *Waybill) WarehouseIn(c *dto.WaybillInOutReq, p *actions.DataPermission)
|
|
|
WarehouseId: warehouse.Id,
|
|
|
UserId: p.UserId,
|
|
|
Lng: lng,
|
|
|
- Lat: Lat,
|
|
|
+ Lat: lat,
|
|
|
ControlBy: model2.ControlBy{
|
|
|
CreateBy: p.UserId,
|
|
|
},
|
|
@@ -615,6 +605,9 @@ func (e *Waybill) WarehouseOut(c *dto.WaybillInOutReq, p *actions.DataPermission
|
|
|
err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法出库!", waybillNo, model.WaybillStatusMap[waybillModel.Status]))
|
|
|
return err
|
|
|
}
|
|
|
+ if waybillModel.Status == model.WaybillStatusWaitStorage {
|
|
|
+ waybillModel.DeliveryTime = c.StartTime
|
|
|
+ }
|
|
|
waybillModel.Status = model.WaybillStatusStorageOut
|
|
|
waybillModel.WarehouseId = warehouse.Id
|
|
|
err = tx.Save(&waybillModel).Error
|
|
@@ -622,22 +615,13 @@ func (e *Waybill) WarehouseOut(c *dto.WaybillInOutReq, p *actions.DataPermission
|
|
|
e.Log.Errorf("db error: %s", err)
|
|
|
return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
|
|
|
}
|
|
|
- // 获取传感器信息
|
|
|
- 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 {
|
|
|
- err = errors.New("查询设备定位信息失败")
|
|
|
+
|
|
|
+ var lng, lat string
|
|
|
+ lng, lat, err = e.GetSite(p.DeptId, warehouse.Sn, c.StartTime.String())
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("获取定位信息失败: %s", err)
|
|
|
return err
|
|
|
}
|
|
|
- var lng, Lat string
|
|
|
- if len(deviceSensorList[0].T_DeviceSensorData.T_site) > 0 {
|
|
|
- site := strings.Split(deviceSensorList[0].T_DeviceSensorData.T_site, ",")
|
|
|
- if len(site) == 2 {
|
|
|
- lng = site[0]
|
|
|
- Lat = site[1]
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
// 添加物流
|
|
|
Logistics := model.WaybillLogistics{
|
|
@@ -646,7 +630,7 @@ func (e *Waybill) WarehouseOut(c *dto.WaybillInOutReq, p *actions.DataPermission
|
|
|
WarehouseId: warehouse.Id,
|
|
|
UserId: p.UserId,
|
|
|
Lng: lng,
|
|
|
- Lat: Lat,
|
|
|
+ Lat: lat,
|
|
|
ControlBy: model2.ControlBy{
|
|
|
CreateBy: p.UserId,
|
|
|
},
|
|
@@ -701,7 +685,7 @@ func (e *Waybill) CarIn(c *dto.WaybillInOutReq, p *actions.DataPermission) error
|
|
|
First(&car).Error
|
|
|
if err != nil {
|
|
|
e.Log.Errorf("db error: %s", err)
|
|
|
- return errors.New("获取车辆信息失败")
|
|
|
+ return errors.New("获取车辆绑定信息失败")
|
|
|
}
|
|
|
|
|
|
for _, waybillNo := range c.WaybillNoList {
|
|
@@ -724,29 +708,24 @@ func (e *Waybill) CarIn(c *dto.WaybillInOutReq, p *actions.DataPermission) error
|
|
|
err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法装车!", waybillNo, model.WaybillStatusMap[waybillModel.Status]))
|
|
|
return err
|
|
|
}
|
|
|
+ if waybillModel.Status == model.WaybillStatusWaitTruck {
|
|
|
+ waybillModel.DeliveryTime = c.StartTime
|
|
|
+ }
|
|
|
waybillModel.Status = model.WaybillStatusTruck
|
|
|
waybillModel.CarId = car.Id
|
|
|
+
|
|
|
err = tx.Save(&waybillModel).Error
|
|
|
if err != nil {
|
|
|
e.Log.Errorf("db error: %s", err)
|
|
|
return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
|
|
|
}
|
|
|
- // 获取传感器信息
|
|
|
- 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 {
|
|
|
- err = errors.New("查询设备定位信息失败")
|
|
|
+
|
|
|
+ var lng, lat string
|
|
|
+ lng, lat, err = e.GetSite(p.DeptId, car.Sn, c.StartTime.String())
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("获取定位信息失败: %s", err)
|
|
|
return err
|
|
|
}
|
|
|
- var lng, Lat string
|
|
|
- if len(deviceSensorList[0].T_DeviceSensorData.T_site) > 0 {
|
|
|
- site := strings.Split(deviceSensorList[0].T_DeviceSensorData.T_site, ",")
|
|
|
- if len(site) == 2 {
|
|
|
- lng = site[0]
|
|
|
- Lat = site[1]
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
// 添加物流
|
|
|
Logistics := model.WaybillLogistics{
|
|
@@ -755,7 +734,7 @@ func (e *Waybill) CarIn(c *dto.WaybillInOutReq, p *actions.DataPermission) error
|
|
|
CarId: car.Id,
|
|
|
UserId: p.UserId,
|
|
|
Lng: lng,
|
|
|
- Lat: Lat,
|
|
|
+ Lat: lat,
|
|
|
ControlBy: model2.ControlBy{
|
|
|
CreateBy: p.UserId,
|
|
|
},
|
|
@@ -814,7 +793,7 @@ func (e *Waybill) CarOut(c *dto.WaybillInOutReq, p *actions.DataPermission) erro
|
|
|
First(&car).Error
|
|
|
if err != nil {
|
|
|
e.Log.Errorf("db error: %s", err)
|
|
|
- return errors.New("获取车辆信息失败")
|
|
|
+ return errors.New("获取车辆绑定信息失败")
|
|
|
}
|
|
|
|
|
|
for _, waybillNo := range c.WaybillNoList {
|
|
@@ -849,22 +828,13 @@ 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))
|
|
|
}
|
|
|
- // 获取传感器信息
|
|
|
- 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 {
|
|
|
- err = errors.New("查询设备定位信息失败")
|
|
|
+
|
|
|
+ var lng, lat string
|
|
|
+ lng, lat, err = e.GetSite(p.DeptId, car.Sn, c.StartTime.String())
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("获取定位信息失败: %s", err)
|
|
|
return err
|
|
|
}
|
|
|
- var lng, Lat string
|
|
|
- if len(deviceSensorList[0].T_DeviceSensorData.T_site) > 0 {
|
|
|
- site := strings.Split(deviceSensorList[0].T_DeviceSensorData.T_site, ",")
|
|
|
- if len(site) == 2 {
|
|
|
- lng = site[0]
|
|
|
- Lat = site[1]
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
// 添加物流
|
|
|
Logistics := model.WaybillLogistics{
|
|
@@ -873,7 +843,7 @@ func (e *Waybill) CarOut(c *dto.WaybillInOutReq, p *actions.DataPermission) erro
|
|
|
CarId: car.Id,
|
|
|
UserId: p.UserId,
|
|
|
Lng: lng,
|
|
|
- Lat: Lat,
|
|
|
+ Lat: lat,
|
|
|
ControlBy: model2.ControlBy{
|
|
|
CreateBy: p.UserId,
|
|
|
},
|
|
@@ -956,23 +926,13 @@ func (e *Waybill) Receipt(c *dto.WaybillReceiptReq, p *actions.DataPermission) e
|
|
|
e.Log.Errorf("db error: %s", err)
|
|
|
return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
|
|
|
}
|
|
|
- // 获取传感器信息
|
|
|
- // 获取传感器信息
|
|
|
- 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 {
|
|
|
- err = errors.New("查询设备定位信息失败")
|
|
|
+
|
|
|
+ var lng, lat string
|
|
|
+ lng, lat, err = e.GetSite(p.DeptId, car.Sn, c.StartTime.String())
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("获取定位信息失败: %s", err)
|
|
|
return err
|
|
|
}
|
|
|
- var lng, Lat string
|
|
|
- if len(deviceSensorList[0].T_DeviceSensorData.T_site) > 0 {
|
|
|
- site := strings.Split(deviceSensorList[0].T_DeviceSensorData.T_site, ",")
|
|
|
- if len(site) == 2 {
|
|
|
- lng = site[0]
|
|
|
- Lat = site[1]
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
// 查询任务
|
|
|
var task model.WaybillTask
|
|
@@ -998,7 +958,7 @@ func (e *Waybill) Receipt(c *dto.WaybillReceiptReq, p *actions.DataPermission) e
|
|
|
CarId: car.Id,
|
|
|
UserId: p.UserId,
|
|
|
Lng: lng,
|
|
|
- Lat: Lat,
|
|
|
+ Lat: lat,
|
|
|
ControlBy: model2.ControlBy{
|
|
|
CreateBy: p.UserId,
|
|
|
},
|
|
@@ -1023,7 +983,7 @@ func (e *Waybill) Receipt(c *dto.WaybillReceiptReq, p *actions.DataPermission) e
|
|
|
CarId: car.Id,
|
|
|
UserId: p.UserId,
|
|
|
Lng: lng,
|
|
|
- Lat: Lat,
|
|
|
+ Lat: lat,
|
|
|
ControlBy: model2.ControlBy{
|
|
|
CreateBy: p.UserId,
|
|
|
},
|
|
@@ -1042,3 +1002,114 @@ func (e *Waybill) Receipt(c *dto.WaybillReceiptReq, p *actions.DataPermission) e
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+// 获取入库 出库 上车 下车 定位信息
|
|
|
+func (e *Waybill) GetSite(companyId int, sn string, time string) (lng, lat string, err error) {
|
|
|
+ // 获取公司秘钥
|
|
|
+ var company model.SysDept
|
|
|
+ company, err = model.GetCompanyById(companyId)
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return lng, lat, model.GetCompanyKeyErr
|
|
|
+ }
|
|
|
+ // 获取传感器信息
|
|
|
+ var deviceSensorList = []nats_server.DeviceSensor_R{}
|
|
|
+ var count int64
|
|
|
+ deviceSensorList, count, err = nats_server.Cold_CompanyDeviceSensor_List_ByKey(sn, company.ColdKey)
|
|
|
+ if err != nil || count == 0 {
|
|
|
+ err = errors.New("查询设备定位信息失败")
|
|
|
+ return lng, lat, err
|
|
|
+ }
|
|
|
+ var deviceData nats_server.DeviceData_
|
|
|
+ deviceData, err = nats_server.Cold_ReadDeviceDataBy_T_snid_T_time(deviceSensorList[0].T_sn, deviceSensorList[0].T_id, time)
|
|
|
+ if err != nil {
|
|
|
+ err = errors.New("查询设备定位信息失败")
|
|
|
+ return lng, lat, err
|
|
|
+ }
|
|
|
+ if len(deviceData.T_site) > 0 {
|
|
|
+ site := strings.Split(deviceSensorList[0].T_DeviceSensorData.T_site, ",")
|
|
|
+ if len(site) == 2 {
|
|
|
+ lng = site[0]
|
|
|
+ lat = site[1]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return lng, lat, nil
|
|
|
+}
|
|
|
+
|
|
|
+// 获取今日运单数 未派单 未装车 未入库 运送中
|
|
|
+// 本月运单总数 上月运单总数 本年运单总数 上年运单总数
|
|
|
+func (e *Waybill) GetBasicsStats(c *dto.WaybillStatsReq, p *actions.DataPermission) dto.WaybillStatsRes {
|
|
|
+ var res dto.WaybillStatsRes
|
|
|
+ var data model.Waybill
|
|
|
+ type DateCount struct {
|
|
|
+ Date string
|
|
|
+ Count int64
|
|
|
+ }
|
|
|
+ yearCount := make([]DateCount, 0)
|
|
|
+ monthCount := make([]DateCount, 0)
|
|
|
+ now := time.Now()
|
|
|
+ todayStartTime := now.Format("2006-01-02") + " 00:00:00"
|
|
|
+ todayEndTime := now.Format("2006-01-02") + " 23:59:59"
|
|
|
+
|
|
|
+ // 获取上个月第一天
|
|
|
+ firstDayOfLastMonth := time.Date(now.Year(), now.Month()-1, 1, 0, 0, 0, 0, now.Location())
|
|
|
+
|
|
|
+ // 获取去年的第一天
|
|
|
+ firstDayOfLastYear := time.Date(now.Year()-1, time.January, 1, 0, 0, 0, 0, now.Location())
|
|
|
+ monthStartTime := firstDayOfLastMonth.Format("2006-01-02") + " 00:00:00"
|
|
|
+ yearStartTime := firstDayOfLastYear.Format("2006-01-02") + " 00:00:00"
|
|
|
+
|
|
|
+ // 今日总运单数
|
|
|
+ e.Orm.Model(&data).Scopes(actions.Permission(data.TableName(), p)).Where("order_time between ? and ?", todayStartTime, todayEndTime).Count(&res.TodayNum)
|
|
|
+ // 未装车
|
|
|
+ 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).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)
|
|
|
+
|
|
|
+ for _, month := range monthCount {
|
|
|
+ if month.Date == now.Format("200601") {
|
|
|
+ res.ThisMonthNum = month.Count
|
|
|
+ }
|
|
|
+ if month.Date == firstDayOfLastMonth.Format("200601") {
|
|
|
+ res.LastMonthNum = month.Count
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取本年,上年数据
|
|
|
+ e.Orm.Model(&data).Select("date_format(order_time,'%Y') date,count(1) as count ").Scopes(actions.Permission(data.TableName(), p)).
|
|
|
+ Where("order_time between ? and ?", yearStartTime, now).Group("date").Find(&yearCount)
|
|
|
+
|
|
|
+ for _, month := range yearCount {
|
|
|
+ if month.Date == now.Format("2006") {
|
|
|
+ res.ThisYearNum = month.Count
|
|
|
+ }
|
|
|
+ if month.Date == firstDayOfLastYear.Format("2006") {
|
|
|
+ res.LastYearNum = month.Count
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if c.Type == "year" {
|
|
|
+ // 获取上个月第一天
|
|
|
+ year, _ := strconv.Atoi(c.Date)
|
|
|
+ firstDayOfyear := time.Date(year, 1, 1, 0, 0, 0, 0, now.Location())
|
|
|
+ lastDayOfyear := time.Date(year, 12, 31, 23, 59, 59, 0, now.Location())
|
|
|
+
|
|
|
+ // 年度数据统计
|
|
|
+ e.Orm.Model(&data).Select("date_format(order_time,'%Y-%m') date,count(1) as num ").Scopes(actions.Permission(data.TableName(), p)).
|
|
|
+ Where("order_time between ? and ?", firstDayOfyear, lastDayOfyear).Group("date").Find(&res.Stats)
|
|
|
+ }
|
|
|
+
|
|
|
+ if c.Type == "month" {
|
|
|
+ // 获取上个月第一天
|
|
|
+ month, _ := time.Parse("2006-01", c.Date)
|
|
|
+ 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)).
|
|
|
+ Where("order_time between ? and ?", firstDayOfMonth, lastDayOfMonth).Group("date").Find(&res.Stats)
|
|
|
+ }
|
|
|
+
|
|
|
+ return res
|
|
|
+}
|