|
@@ -68,6 +68,15 @@ func (e *WaybillTask) GetPage(c *dto.WaybillTaskGetPageReq, list *[]model.Waybil
|
|
|
|
|
|
// GetPage 获取WaybillTask列表
|
|
|
func (e *WaybillTask) GetDataPage(c *dto.WaybillTaskGetDataPageReq) (list []nats_server.DeviceData_R, count int64, err error) {
|
|
|
+
|
|
|
+ var waybill model.Waybill
|
|
|
+ err = e.Orm.Where("waybill_no = ?", c.WaybillNo).First(&waybill).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ err = errors.New("获取运单信息错误!")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
var task model.WaybillTask
|
|
|
err = e.Orm.Where("id = ? and waybill_no = ?", c.TaskId, c.WaybillNo).First(&task).Error
|
|
|
if err != nil {
|
|
@@ -76,6 +85,14 @@ func (e *WaybillTask) GetDataPage(c *dto.WaybillTaskGetDataPageReq) (list []nats
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ // 获取最后一个任务id
|
|
|
+ var lastWaybillTask model.WaybillTask
|
|
|
+ err = e.Orm.Model(&lastWaybillTask).Where("waybill_no = ?", c.WaybillNo).Last(&lastWaybillTask).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ err = errors.New("获取运单信息错误!")
|
|
|
+ return
|
|
|
+ }
|
|
|
if len(c.StartTime) > 0 {
|
|
|
var st time.Time
|
|
|
st, err = time.Parse("2006-01-02 15:04:05", c.StartTime)
|
|
@@ -107,14 +124,74 @@ func (e *WaybillTask) GetDataPage(c *dto.WaybillTaskGetDataPageReq) (list []nats
|
|
|
for _, id := range c.T_ids {
|
|
|
T_snid += fmt.Sprintf("%s,%d|", task.Sn, id)
|
|
|
}
|
|
|
- if c.PageSize == 9999 {
|
|
|
- // 获取传感器信息
|
|
|
- list, count, err = nats_server.Cold_ReadDeviceDataListBy_T_snid(T_snid, c.StartTime, c.EndTime, 0, 9999)
|
|
|
- return
|
|
|
+ list, count, err = nats_server.Cold_ReadDeviceDataListBy_T_snid(T_snid, c.StartTime, c.EndTime, 0, 9999)
|
|
|
+ firstMap := map[int]nats_server.DeviceData_R{}
|
|
|
+ lastMap := map[int]nats_server.DeviceData_R{}
|
|
|
+ if count > 0 {
|
|
|
+ for _, v := range c.T_ids {
|
|
|
+
|
|
|
+ for i := 0; i < len(list); i++ {
|
|
|
+ if v == list[i].T_id {
|
|
|
+ if list[i].T_time != task.StartTime.String() {
|
|
|
+ data := list[i]
|
|
|
+ data.T_time = task.StartTime.String()
|
|
|
+ firstMap[v] = data
|
|
|
+ count += 1
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if waybill.Status == model.WaybillStatusReceipt {
|
|
|
+ for i := len(list) - 1; i >= 0; i-- {
|
|
|
+ if v == list[i].T_id {
|
|
|
+ if c.TaskId == lastWaybillTask.Id && list[i].T_time != task.EndTime.String() && !time.Time(task.EndTime).IsZero() {
|
|
|
+ data := list[i]
|
|
|
+ data.T_time = task.EndTime.String()
|
|
|
+ lastMap[v] = data
|
|
|
+ count += 1
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- // 获取传感器信息
|
|
|
- list, count, err = nats_server.Cold_ReadDeviceDataListBy_T_snid(T_snid, c.StartTime, c.EndTime, c.Page, c.PageSize)
|
|
|
- return
|
|
|
+
|
|
|
+ for _, data := range firstMap {
|
|
|
+ list = append(list, data)
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, data := range lastMap {
|
|
|
+ list = append(list, data)
|
|
|
+ }
|
|
|
+
|
|
|
+ sort.Slice(list, func(i, j int) bool {
|
|
|
+ // 先按 T_time 字段排序,如果 T_time 相同则按 T_name 字段排序
|
|
|
+ if list[i].T_time == list[j].T_time {
|
|
|
+ return list[i].T_name < list[j].T_name
|
|
|
+ }
|
|
|
+ return list[i].T_time > list[j].T_time
|
|
|
+ })
|
|
|
+
|
|
|
+ // 计算总页数
|
|
|
+ totalPages := (int(count) + c.PageSize - 1) / c.PageSize
|
|
|
+
|
|
|
+ // 如果n超出范围,返回错误
|
|
|
+ if c.Page < 1 {
|
|
|
+ c.Page = 1
|
|
|
+ }
|
|
|
+ if c.Page > totalPages {
|
|
|
+ c.Page = totalPages
|
|
|
+ }
|
|
|
+ // 计算当前页的数据
|
|
|
+ start := (c.Page - 1) * c.PageSize
|
|
|
+ end := start + c.PageSize
|
|
|
+ if end > int(count) {
|
|
|
+ end = int(count)
|
|
|
+ }
|
|
|
+ currentPage := list[start:end]
|
|
|
+
|
|
|
+ return currentPage, count, err
|
|
|
}
|
|
|
func (e *WaybillTask) GetNewestDataPage(c *dto.WaybillTaskGetNewestDataPageReq) (data nats_server.DeviceData_R, err error) {
|
|
|
var waybill model.Waybill
|