|
@@ -6,10 +6,17 @@ import (
|
|
|
"cold-delivery/common/actions"
|
|
|
cDto "cold-delivery/common/dto"
|
|
|
"cold-delivery/common/global"
|
|
|
+ "cold-delivery/common/lib"
|
|
|
"cold-delivery/common/nats/nats_server"
|
|
|
"errors"
|
|
|
+ "fmt"
|
|
|
"gogs.baozhida.cn/zoie/OAuth-core/service"
|
|
|
"gorm.io/gorm"
|
|
|
+ "sort"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "sync"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type CoolerBox struct {
|
|
@@ -20,7 +27,6 @@ type CoolerBox struct {
|
|
|
func (e *CoolerBox) GetPage(c *dto.CoolerBoxGetPageReq, list *[]model.CoolerBox, count *int64, p *actions.DataPermission) error {
|
|
|
var err error
|
|
|
var data model.CoolerBox
|
|
|
-
|
|
|
if c.PageSize == 9999 {
|
|
|
err = e.Orm.Model(&data).
|
|
|
Scopes(
|
|
@@ -57,6 +63,24 @@ func (e *CoolerBox) GetPage(c *dto.CoolerBoxGetPageReq, list *[]model.CoolerBox,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ for i, _ := range *list {
|
|
|
+ var dataIce model.IceRaft
|
|
|
+ var dataIces []model.IceRaft
|
|
|
+ err := e.Orm.Model(&dataIce).
|
|
|
+ Scopes(
|
|
|
+ actions.Permission(dataIce.TableName(), p),
|
|
|
+ IceRaftNameRecordStatusScopes(model.IceRaftRecordStatusUsing),
|
|
|
+ IceRaftNameRecordCoolerBoxIdScopes((*list)[i].Id),
|
|
|
+ ).
|
|
|
+ Joins("left join ice_raft_record on ice_raft.ice_raft_record_id = ice_raft_record.id").
|
|
|
+ Preload("IceRaftRecord").
|
|
|
+ Find(&dataIces).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return global.GetFailedErr
|
|
|
+ }
|
|
|
+ (*list)[i].IceRaft = dataIces
|
|
|
+ }
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -307,3 +331,243 @@ func (e *CoolerBox) Remove(c *dto.CoolerBoxDeleteReq, p *actions.DataPermission)
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+// GetHistory 获取保温箱历史信息
|
|
|
+func (e *CoolerBox) GetHistory(c *dto.CoolerBoxBatchReqSN, p *actions.DataPermission) ([]dto.HistoricalData, error) {
|
|
|
+ var err error
|
|
|
+ var taskList []model.WaybillTask
|
|
|
+ var historical []dto.HistoricalData
|
|
|
+ if c.Page == 0 {
|
|
|
+ c.Page = 1
|
|
|
+ }
|
|
|
+ if c.PageZ == 0 {
|
|
|
+ c.PageZ = 10
|
|
|
+ }
|
|
|
+ // 查询运单任务信息
|
|
|
+ err = e.Orm.Model(&model.WaybillTask{}).
|
|
|
+ Where("cooler_box_id = ?", c.Id).
|
|
|
+ Find(&taskList).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return nil, errors.New("获取运单任务信息失败")
|
|
|
+ }
|
|
|
+ // 获取公司秘钥
|
|
|
+ var dataDept model.SysDept
|
|
|
+ var Depts []model.SysDept
|
|
|
+ err = e.Orm.Model(&dataDept).
|
|
|
+ Scopes(
|
|
|
+ CompanyIdScopesUser(p.DeptId),
|
|
|
+ ).
|
|
|
+ Find(&Depts).Limit(-1).Offset(-1).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return nil, model.GetCompanyKeyErr
|
|
|
+ }
|
|
|
+
|
|
|
+ //company, err := model.GetCompanyById(p.DeptId)
|
|
|
+ //if err != nil {
|
|
|
+ // e.Log.Errorf("db error: %s", err)
|
|
|
+ // return nil, model.GetCompanyKeyErr
|
|
|
+ //}
|
|
|
+ // 限制并发请求数量
|
|
|
+ const maxConcurrentRequests = 10
|
|
|
+ semaphore := make(chan struct{}, maxConcurrentRequests)
|
|
|
+ var deviceSensorList []nats_server.DeviceSensor_R
|
|
|
+ var wg sync.WaitGroup
|
|
|
+ for _, task := range taskList {
|
|
|
+ wg.Add(1)
|
|
|
+ go func(task model.WaybillTask) {
|
|
|
+ defer wg.Done()
|
|
|
+ semaphore <- struct{}{}
|
|
|
+ defer func() { <-semaphore }()
|
|
|
+ for _, v := range Depts {
|
|
|
+ deviceSensorList, _, _ = nats_server.Cold_CompanyDeviceSensor_List_ByKey(task.Sn, v.ColdKey)
|
|
|
+ if len(deviceSensorList) > 0 {
|
|
|
+ historical = append(historical, dto.HistoricalData{
|
|
|
+ Sn: task.Sn,
|
|
|
+ T_id: strconv.Itoa(deviceSensorList[0].T_id) + "|",
|
|
|
+ StartTime: task.StartTime.String(),
|
|
|
+ EndTime: task.EndTime.String(),
|
|
|
+ CompanyName: v.Name,
|
|
|
+ })
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(deviceSensorList) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("nats 获取轨迹信息失败: %s", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ }(task)
|
|
|
+ }
|
|
|
+ wg.Wait()
|
|
|
+ sort.Slice(historical, func(i, j int) bool {
|
|
|
+ if historical[i].StartTime < historical[j].StartTime {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return false
|
|
|
+ })
|
|
|
+ startIndex := (c.Page - 1) * c.PageZ
|
|
|
+ endIndex := startIndex + c.PageZ
|
|
|
+ if endIndex > len(historical) {
|
|
|
+ endIndex = len(historical)
|
|
|
+ }
|
|
|
+ pageHistory := historical[startIndex:endIndex]
|
|
|
+ return pageHistory, nil
|
|
|
+}
|
|
|
+
|
|
|
+// GetNewLocus 获取最新轨迹
|
|
|
+func (e *CoolerBox) GetNewLocus(c *dto.CoolerBoxReq, list *[]nats_server.DeviceData_) error {
|
|
|
+ // 获取公司秘钥
|
|
|
+ var dataDept model.SysDept
|
|
|
+ var Depts []model.SysDept
|
|
|
+ atoi, _ := strconv.Atoi(c.Id)
|
|
|
+ err := e.Orm.Model(&dataDept).
|
|
|
+ Scopes(
|
|
|
+ CompanyIdScopesUser(atoi),
|
|
|
+ ).
|
|
|
+ Find(&Depts).Limit(-1).Offset(-1).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return global.GetFailedErr
|
|
|
+ }
|
|
|
+ var deviceSensorList []nats_server.DeviceSensor_R
|
|
|
+ for _, v := range Depts {
|
|
|
+ deviceSensorList, _, _ = nats_server.Cold_CompanyDeviceSensor_List_ByKey(c.T_sn, v.ColdKey)
|
|
|
+ if len(deviceSensorList) > 0 {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(deviceSensorList) == 0 {
|
|
|
+ return errors.New("该设备不属于该公司")
|
|
|
+ }
|
|
|
+ data, err := nats_server.Read_DeviceTask_List_By_Condition(c.T_sn)
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("设备未开启监控: %s", err)
|
|
|
+ return errors.New("设备未开启监控")
|
|
|
+ }
|
|
|
+ // 当最新设备状态为1时,证明设备正在启动中,查询大于开始时间的所有轨迹,设备状态为2时,查询开始时间到结束时间的轨迹
|
|
|
+ if data.T_State == 1 {
|
|
|
+ deviceData, err := nats_server.Read_Start_Time_DeviceData(c.T_sn, deviceSensorList[0].T_id, data.T_Ut_start.Format("2006-01-02 15:04:05"), "")
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("获取设备轨迹失败: %s", err)
|
|
|
+ return errors.New("获取设备轨迹失败")
|
|
|
+ }
|
|
|
+ *list = appendUniqueDeviceData(*list, deviceData)
|
|
|
+ } else if data.T_State == 2 {
|
|
|
+ deviceData, err := nats_server.Read_Start_Time_DeviceData(c.T_sn, deviceSensorList[0].T_id, data.T_Ut_start.Format("2006-01-02 15:04:05"), data.T_Ut_end.Format("2006-01-02 15:04:05"))
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("获取设备轨迹失败: %s", err)
|
|
|
+ return errors.New("获取设备轨迹失败")
|
|
|
+ }
|
|
|
+ *list = appendUniqueDeviceData(*list, deviceData)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// 去重并追加设备数据
|
|
|
+func appendUniqueDeviceData(list []nats_server.DeviceData_, newData []nats_server.DeviceData_) []nats_server.DeviceData_ {
|
|
|
+ uniqueMap := make(map[time.Time]bool)
|
|
|
+ for _, d := range list {
|
|
|
+ //split := strings.Split(list[i].T_site, ",")
|
|
|
+ //defer func() {
|
|
|
+ // if r := recover(); r != nil {
|
|
|
+ // fmt.Println("数组下标越界:", r)
|
|
|
+ // }
|
|
|
+ //}()
|
|
|
+ //Lng := split[0]
|
|
|
+ //Lat := split[1]
|
|
|
+ //Lngs, _ := strconv.ParseFloat(Lng, 64)
|
|
|
+ //Lats, _ := strconv.ParseFloat(Lat, 64)
|
|
|
+ //mLng, mLat := lib.Wgs84ToGcj02(Lngs, Lats)
|
|
|
+ //list[i].T_site = fmt.Sprintf("%v,%v", mLng, mLat)
|
|
|
+ uniqueMap[d.T_time] = true
|
|
|
+ }
|
|
|
+ for _, d := range newData {
|
|
|
+ if !uniqueMap[d.T_time] {
|
|
|
+ split := strings.Split(d.T_site, ",")
|
|
|
+ defer func() {
|
|
|
+ if r := recover(); r != nil {
|
|
|
+ fmt.Println("数组下标越界:", r)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ Lng := split[0]
|
|
|
+ Lat := split[1]
|
|
|
+ Lngs, _ := strconv.ParseFloat(Lng, 64)
|
|
|
+ Lats, _ := strconv.ParseFloat(Lat, 64)
|
|
|
+ mLng, mLat := lib.Wgs84ToGcj02(Lngs, Lats)
|
|
|
+ d.T_site = fmt.Sprintf("%v,%v", mLng, mLat)
|
|
|
+ list = append(list, d)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //排序
|
|
|
+ sort.Slice(list, func(i, j int) bool {
|
|
|
+ return list[i].T_time.After(list[j].T_time)
|
|
|
+ })
|
|
|
+ return list
|
|
|
+}
|
|
|
+
|
|
|
+// GetCoolerBoxIce 获取保温箱下的所有历史冰排
|
|
|
+func (e *CoolerBox) GetCoolerBoxIce(c *dto.GetCoolerBoxIce, list *[]model.IceRaftRecord, p *actions.DataPermission, count *int64) error {
|
|
|
+ var err error
|
|
|
+
|
|
|
+ tx := e.Orm.Begin()
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ } else {
|
|
|
+ tx.Commit()
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ //查询保温箱是否存在
|
|
|
+ var icerecord model.IceRaftRecord
|
|
|
+ err = e.Orm.Scopes(
|
|
|
+ cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
|
|
|
+ actions.Permission(icerecord.TableName(), p)).
|
|
|
+ Where("cooler_box_id=?", c.CoolerBoxId).
|
|
|
+ Preload("IceLocker").
|
|
|
+ Preload("CoolerBox").
|
|
|
+ Find(&list).Count(count).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return global.GetFailedErr
|
|
|
+ }
|
|
|
+ return global.GetFailedErr
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// GetCoolerBoxIceAll 获取保温箱所有历史记录
|
|
|
+func (e *CoolerBox) GetCoolerBoxIceAll(c *dto.GetCoolerBoxIceAll, list *[]model.IceRaftRecord, p *actions.DataPermission, count *int64) error {
|
|
|
+ var err error
|
|
|
+
|
|
|
+ tx := e.Orm.Begin()
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ } else {
|
|
|
+ tx.Commit()
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ //查询保温箱是否存在
|
|
|
+ var icerecord model.IceRaftRecord
|
|
|
+ err = e.Orm.Scopes(
|
|
|
+ cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
|
|
|
+ actions.Permission(icerecord.TableName(), p)).
|
|
|
+ Not("cooler_box_id", 0).
|
|
|
+ Preload("IceLocker").
|
|
|
+ Preload("CoolerBox").
|
|
|
+ Find(&list).Count(count).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return global.GetFailedErr
|
|
|
+ }
|
|
|
+ return global.GetFailedErr
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|