waybill_task.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package service
  2. import (
  3. "cold-delivery/app/admin/model"
  4. "cold-delivery/app/admin/service/dto"
  5. cDto "cold-delivery/common/dto"
  6. "cold-delivery/common/global"
  7. model2 "cold-delivery/common/model"
  8. "cold-delivery/common/nats/nats_server"
  9. "errors"
  10. "fmt"
  11. "gogs.baozhida.cn/zoie/OAuth-core/service"
  12. "gorm.io/gorm"
  13. "sort"
  14. "time"
  15. )
  16. type WaybillTask struct {
  17. service.Service
  18. }
  19. func WaybillTaskTimeScopes(startTime, endTime string) func(db *gorm.DB) *gorm.DB {
  20. return func(db *gorm.DB) *gorm.DB {
  21. if len(startTime) == 0 && len(endTime) == 0 {
  22. return db
  23. }
  24. return db.Where("(start_time between ? and ?) or (end_time between ? and ?)",
  25. startTime, endTime, startTime, endTime)
  26. }
  27. }
  28. // GetPage 获取WaybillTask列表
  29. func (e *WaybillTask) GetPage(c *dto.WaybillTaskGetPageReq, list *[]model.WaybillTask, count *int64) error {
  30. var err error
  31. var data model.WaybillTask
  32. var waybill model.Waybill
  33. err = e.Orm.Model(&waybill).Where("waybill_no = ?", c.WaybillNo).First(&waybill).Error
  34. if err != nil {
  35. e.Log.Errorf("db error: %s", err)
  36. return errors.New("获取运单信息失败")
  37. }
  38. err = e.Orm.Model(&data).
  39. Scopes(
  40. cDto.MakeCondition(c.GetNeedSearch()),
  41. ).
  42. Preload("CoolerBox").
  43. Find(list).Limit(-1).Offset(-1).
  44. Count(count).Error
  45. if err != nil {
  46. e.Log.Errorf("db error: %s", err)
  47. return global.GetFailedErr
  48. }
  49. // 获取公司秘钥
  50. var company model.SysDept
  51. company, err = model.GetCompanyById(waybill.DeptId)
  52. if err != nil {
  53. e.Log.Errorf("db error: %s", err)
  54. return model.GetCompanyKeyErr
  55. }
  56. for i := 0; i < len(*list); i++ {
  57. // 获取传感器信息
  58. deviceSensorList, _, _ := nats_server.Cold_CompanyDeviceSensor_List_ByKey((*list)[i].Sn, company.ColdKey)
  59. (*list)[i].DeviceSensorList = deviceSensorList
  60. }
  61. return nil
  62. }
  63. func (e *WaybillTask) Get(c *dto.WeianGetWaybillDataReq, task *model.WaybillTask) error {
  64. var err error
  65. var data model.WaybillTask
  66. var waybill model.Waybill
  67. err = e.Orm.Model(&waybill).Where("waybill_no = ? or order_no = ?", c.WaybillNo, c.WaybillNo).First(&waybill).Error
  68. if err != nil {
  69. e.Log.Errorf("db error: %s", err)
  70. return errors.New("获取运单信息失败")
  71. }
  72. err = e.Orm.Model(&data).
  73. Where("waybill_no = ?", waybill.WaybillNo).
  74. Preload("CoolerBox").
  75. First(task).Error
  76. if err != nil {
  77. e.Log.Errorf("db error: %s", err)
  78. return errors.New("获取运单任务信息失败")
  79. }
  80. // 获取公司秘钥
  81. var company model.SysDept
  82. company, err = model.GetCompanyById(waybill.DeptId)
  83. if err != nil {
  84. e.Log.Errorf("db error: %s", err)
  85. return model.GetCompanyKeyErr
  86. }
  87. // 获取传感器信息
  88. deviceSensorList, _, _ := nats_server.Cold_CompanyDeviceSensor_List_ByKey(task.Sn, company.ColdKey)
  89. task.DeviceSensorList = deviceSensorList
  90. return nil
  91. }
  92. // GetPage 获取WaybillTask列表
  93. func (e *WaybillTask) GetDataPage(c *dto.WaybillTaskGetDataPageReq) (list []nats_server.DeviceData_R, count int64, err error) {
  94. var waybill model.Waybill
  95. err = e.Orm.Where("waybill_no = ?", c.WaybillNo).First(&waybill).Error
  96. if err != nil {
  97. e.Log.Errorf("db error: %s", err)
  98. err = errors.New("获取运单信息错误!")
  99. return
  100. }
  101. var task model.WaybillTask
  102. err = e.Orm.Where("id = ? and waybill_no = ?", c.TaskId, c.WaybillNo).First(&task).Error
  103. if err != nil {
  104. e.Log.Errorf("db error: %s", err)
  105. err = errors.New("获取运单信息错误!")
  106. return
  107. }
  108. if len(c.StartTime) > 0 {
  109. var st time.Time
  110. st, err = time.ParseInLocation("2006-01-02 15:04:05", c.StartTime, time.Local)
  111. if err != nil {
  112. err = errors.New("解析时间字符串出错!")
  113. return
  114. }
  115. if st.Before(time.Time(task.StartTime)) {
  116. c.StartTime = task.StartTime.String()
  117. }
  118. } else {
  119. c.StartTime = task.StartTime.String()
  120. }
  121. if len(c.EndTime) > 0 {
  122. var et time.Time
  123. et, err = time.ParseInLocation("2006-01-02 15:04:05", c.EndTime, time.Local)
  124. if err != nil {
  125. err = errors.New("解析时间字符串出错!")
  126. return
  127. }
  128. if et.After(time.Time(task.EndTime)) {
  129. c.EndTime = task.EndTime.String()
  130. }
  131. } else {
  132. c.EndTime = task.EndTime.String()
  133. }
  134. T_snid := ""
  135. for _, id := range c.T_ids {
  136. T_snid += fmt.Sprintf("%s,%d|", task.Sn, id)
  137. }
  138. list, count, err = nats_server.Cold_ReadDeviceDataListBy_T_snid(T_snid, c.StartTime, c.EndTime, c.Page, c.PageSize)
  139. return list, count, err
  140. }
  141. func (e *WaybillTask) GetNewestDataPage(c *dto.WaybillTaskGetNewestDataPageReq) (data nats_server.DeviceData_R, err error) {
  142. var waybill model.Waybill
  143. err = e.Orm.Model(&waybill).Where("waybill_no = ?", c.WaybillNo).First(&waybill).Error
  144. if err != nil {
  145. e.Log.Errorf("db error: %s", err)
  146. err = errors.New("获取运单信息错误!")
  147. return
  148. }
  149. // 获取公司秘钥
  150. var company model.SysDept
  151. company, err = model.GetCompanyById(waybill.DeptId)
  152. if err != nil {
  153. e.Log.Errorf("db error: %s", err)
  154. err = model.GetCompanyKeyErr
  155. return
  156. }
  157. var taskList []model.WaybillTask
  158. err = e.Orm.Where("waybill_no = ?", c.WaybillNo).Order("id desc").Find(&taskList).Error
  159. if err != nil {
  160. e.Log.Errorf("db error: %s", err)
  161. err = errors.New("获取运单信息任务失败!")
  162. return
  163. }
  164. if len(taskList) == 0 {
  165. err = errors.New("暂无温湿度记录信息!")
  166. return
  167. }
  168. var listAll []nats_server.DeviceData_R
  169. for _, task := range taskList {
  170. deviceSensorList, _, _ := nats_server.Cold_CompanyDeviceSensor_List_ByKey(task.Sn, company.ColdKey)
  171. if len(deviceSensorList) == 0 {
  172. continue
  173. }
  174. T_snid := fmt.Sprintf("%s,%d|", deviceSensorList[0].T_sn, deviceSensorList[0].T_id)
  175. if len(task.EndTime.String()) == 0 {
  176. task.EndTime = model2.Time(time.Now())
  177. }
  178. // 获取传感器信息
  179. list, _, _ := nats_server.Cold_ReadDeviceDataListBy_T_snid(T_snid, task.StartTime.String(), task.EndTime.String(), 0, 9999)
  180. if len(list) > 0 {
  181. listAll = append(listAll, list...)
  182. break
  183. }
  184. }
  185. if len(listAll) > 0 {
  186. data = listAll[0]
  187. return
  188. }
  189. err = errors.New("暂无温湿度记录信息!")
  190. return
  191. }
  192. func (e *WaybillTask) GetLocus(c *dto.WaybillGetLocusReq) ([]nats_server.DeviceData_R2, error) {
  193. var err error
  194. var data model.WaybillTask
  195. var waybill model.Waybill
  196. var taskList []model.WaybillTask
  197. locusList := make([]nats_server.DeviceData_R2, 0)
  198. err = e.Orm.Model(&waybill).Where("waybill_no = ? or order_no = ?", c.WaybillNo, c.WaybillNo).First(&waybill).Error
  199. if err != nil {
  200. e.Log.Errorf("db error: %s", err)
  201. return locusList, errors.New("获取运单信息失败")
  202. }
  203. // 获取公司秘钥
  204. var company model.SysDept
  205. company, err = model.GetCompanyById(waybill.DeptId)
  206. if err != nil {
  207. e.Log.Errorf("db error: %s", err)
  208. return locusList, model.GetCompanyKeyErr
  209. }
  210. err = e.Orm.Model(&data).
  211. Where("waybill_no = ?", waybill.WaybillNo).
  212. Find(&taskList).Error
  213. if err != nil {
  214. e.Log.Errorf("db error: %s", err)
  215. return locusList, errors.New("获取运单任务信息失败")
  216. }
  217. for i := 0; i < len(taskList); i++ {
  218. // 获取传感器信息
  219. deviceSensorList, _, _ := nats_server.Cold_CompanyDeviceSensor_List_ByKey(taskList[i].Sn, company.ColdKey)
  220. if len(deviceSensorList) > 0 {
  221. T_snid := fmt.Sprintf("%s,%d|", taskList[i].Sn, deviceSensorList[0].T_id)
  222. dataList, _, err := nats_server.Cold_ReadDeviceDataListBy_T_snidForLocus(T_snid, taskList[i].StartTime.String(), taskList[i].EndTime.String(), 0, 9999)
  223. if err != nil {
  224. e.Log.Errorf("nats 获取轨迹信息失败: %s", err)
  225. return locusList, errors.New("获取运单轨迹信息失败")
  226. }
  227. // 倒序
  228. sort.Slice(dataList, func(i, j int) bool {
  229. if dataList[i].T_time < dataList[j].T_time {
  230. return true
  231. }
  232. return false
  233. })
  234. locusList = append(locusList, dataList...)
  235. }
  236. }
  237. return locusList, nil
  238. }