cooler_box.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. package service
  2. import (
  3. "cold-delivery/app/admin/model"
  4. "cold-delivery/app/admin/service/dto"
  5. "cold-delivery/common/actions"
  6. cDto "cold-delivery/common/dto"
  7. "cold-delivery/common/global"
  8. "cold-delivery/common/lib"
  9. model2 "cold-delivery/common/model"
  10. "cold-delivery/common/nats/nats_server"
  11. "errors"
  12. "fmt"
  13. "gogs.baozhida.cn/zoie/OAuth-core/service"
  14. "gorm.io/gorm"
  15. "sort"
  16. "strconv"
  17. "strings"
  18. "sync"
  19. "time"
  20. )
  21. type CoolerBox struct {
  22. service.Service
  23. }
  24. // GetPage 获取CoolerBox列表
  25. func (e *CoolerBox) GetPage(c *dto.CoolerBoxGetPageReq, list *[]model.CoolerBox, count *int64, p *actions.DataPermission) error {
  26. var err error
  27. var data model.CoolerBox
  28. if c.PageSize == 9999 {
  29. err = e.Orm.Model(&data).
  30. Scopes(
  31. actions.Permission(data.TableName(), p),
  32. ).
  33. Find(list).Limit(-1).Offset(-1).
  34. Count(count).Error
  35. } else {
  36. err = e.Orm.Model(&data).
  37. Scopes(
  38. cDto.MakeCondition(c.GetNeedSearch()),
  39. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  40. actions.Permission(data.TableName(), p),
  41. ).
  42. Find(list).Limit(-1).Offset(-1).
  43. Count(count).Error
  44. }
  45. if err != nil {
  46. e.Log.Errorf("db error: %s", err)
  47. return global.GetFailedErr
  48. }
  49. if c.ShowTemp {
  50. // 获取公司秘钥
  51. var company model.SysDept
  52. company, err = model.GetCompanyById(p.DeptId)
  53. if err != nil {
  54. e.Log.Errorf("db error: %s", err)
  55. return model.GetCompanyKeyErr
  56. }
  57. for i := 0; i < len(*list); i++ {
  58. deviceData, _, _ := nats_server.Cold_CompanyDeviceSensor_List_ByKey((*list)[i].Sn, company.ColdKey)
  59. if len(deviceData) > 0 {
  60. (*list)[i].DeviceData = deviceData[0].T_DeviceSensorData
  61. }
  62. }
  63. }
  64. for i, _ := range *list {
  65. var dataIce model.IceRaft
  66. var dataIces []model.IceRaft
  67. err := e.Orm.Model(&dataIce).
  68. Scopes(
  69. actions.Permission(dataIce.TableName(), p),
  70. IceRaftNameRecordStatusScopes(model.IceRaftRecordStatusUsing),
  71. IceRaftNameRecordCoolerBoxIdScopes((*list)[i].Id),
  72. ).
  73. Joins("left join ice_raft_record on ice_raft.ice_raft_record_id = ice_raft_record.id").
  74. Preload("IceRaftRecord").
  75. Find(&dataIces).Error
  76. if err != nil {
  77. e.Log.Errorf("db error: %s", err)
  78. return global.GetFailedErr
  79. }
  80. (*list)[i].IceRaft = dataIces
  81. }
  82. return nil
  83. }
  84. // Get 获取CoolerBox对象
  85. func (e *CoolerBox) Get(d *dto.CoolerBoxGetReq, CoolerBoxModel *model.CoolerBox, p *actions.DataPermission) error {
  86. err := e.Orm.
  87. Scopes(actions.Permission(CoolerBoxModel.TableName(), p)).
  88. First(CoolerBoxModel, d.GetId()).Error
  89. if err != nil {
  90. e.Log.Errorf("db error: %s", err)
  91. if errors.Is(err, gorm.ErrRecordNotFound) {
  92. return global.GetNotFoundOrNoPermissionErr
  93. }
  94. return global.GetFailedErr
  95. }
  96. return nil
  97. }
  98. // Insert 创建CoolerBox对象
  99. func (e *CoolerBox) Insert(c *dto.CoolerBoxInsertReq, p *actions.DataPermission) error {
  100. var err error
  101. var data model.CoolerBox
  102. tx := e.Orm.Begin()
  103. defer func() {
  104. if err != nil {
  105. tx.Rollback()
  106. } else {
  107. tx.Commit()
  108. }
  109. }()
  110. var k int64
  111. err = tx.Model(&data).Where("sn = ?", c.Sn).Count(&k).Error
  112. if err != nil {
  113. e.Log.Errorf("db error: %s", err)
  114. return global.CreateFailedErr
  115. }
  116. if k > 0 {
  117. err = errors.New("该Sn已绑定!")
  118. e.Log.Errorf("db error: %s", err)
  119. return err
  120. }
  121. //var device nats_server.Device
  122. //var company model.SysDept
  123. //device, err = nats_server.Cold_ReadDeviceByT_sn(c.Sn)
  124. //if err != nil {
  125. // err = errors.New("获取SN信息失败,请检查SN是否正确!")
  126. // e.Log.Errorf("db error: %s", err)
  127. // return err
  128. //}
  129. //company, err = model.GetCompanyById(p.DeptId)
  130. //if err != nil {
  131. // e.Log.Errorf("db error: %s", err)
  132. // return err
  133. //}
  134. //if device.T_pid != company.Id {
  135. // err = errors.New("获取SN信息失败,请检查SN是否正确!")
  136. // e.Log.Errorf("获取SN信息失败,device.T_pid != company.Id")
  137. // return err
  138. //}
  139. // 添加保温箱
  140. c.Generate(&data)
  141. data.HistorySn = []string{data.Sn}
  142. err = tx.Create(&data).Error
  143. if err != nil {
  144. e.Log.Errorf("db error: %s", err)
  145. return global.CreateFailedErr
  146. }
  147. c.Id = data.Id
  148. return nil
  149. }
  150. func (e *CoolerBox) BatchInsert(c *dto.CoolerBoxBatchInsertReq) error {
  151. var err error
  152. tx := e.Orm.Begin()
  153. defer func() {
  154. if err != nil {
  155. tx.Rollback()
  156. } else {
  157. tx.Commit()
  158. }
  159. }()
  160. for _, coolerBox := range c.List {
  161. var data model.CoolerBox
  162. var k int64
  163. err = tx.Model(&data).Where("sn = ?", coolerBox.Sn).Count(&k).Error
  164. if err != nil {
  165. e.Log.Errorf("db error: %s", err)
  166. return global.CreateFailedErr
  167. }
  168. if k > 0 {
  169. //err = errors.New("该Sn已绑定!")
  170. continue
  171. }
  172. // 添加保温箱
  173. coolerBox.CreateBy = c.CreateBy
  174. coolerBox.DeptId = c.DeptId
  175. coolerBox.Status = c.Status
  176. coolerBox.Generate(&data)
  177. data.HistorySn = []string{data.Sn}
  178. err = tx.Create(&data).Error
  179. if err != nil {
  180. e.Log.Errorf("db error: %s", err)
  181. return global.CreateFailedErr
  182. }
  183. }
  184. return nil
  185. }
  186. // Update 修改CoolerBox对象
  187. func (e *CoolerBox) Update(c *dto.CoolerBoxUpdateReq, p *actions.DataPermission) error {
  188. var err error
  189. tx := e.Orm.Begin()
  190. defer func() {
  191. if err != nil {
  192. tx.Rollback()
  193. } else {
  194. tx.Commit()
  195. }
  196. }()
  197. var CoolerBoxModel = model.CoolerBox{}
  198. // 查询保温箱是否存在
  199. err = e.Orm.Scopes(actions.Permission(CoolerBoxModel.TableName(), p)).
  200. First(&CoolerBoxModel, c.GetId()).Error
  201. if err != nil {
  202. e.Log.Errorf("db error: %s", err)
  203. if errors.Is(err, gorm.ErrRecordNotFound) {
  204. return global.UpdateNotFoundOrNoPermissionErr
  205. }
  206. return global.UpdateFailedErr
  207. }
  208. if CoolerBoxModel.Sn != c.Sn && len(c.Sn) > 0 {
  209. var k int64
  210. var data = model.CoolerBox{}
  211. err = tx.Model(&data).Where("sn = ?", c.Sn).Count(&k).Error
  212. if err != nil {
  213. e.Log.Errorf("db error: %s", err)
  214. return global.CreateFailedErr
  215. }
  216. if k > 0 {
  217. err = errors.New("该Sn已绑定!")
  218. e.Log.Errorf("db error: %s", err)
  219. return err
  220. }
  221. //var device nats_server.Device
  222. //var company model.SysDept
  223. //device, err = nats_server.Cold_ReadDeviceByT_sn(c.Sn)
  224. //if err != nil {
  225. // err = errors.New("获取SN信息失败,请检查SN是否正确!")
  226. // e.Log.Errorf("db error: %s", err)
  227. // return err
  228. //}
  229. //company, err = model.GetCompanyById(p.DeptId)
  230. //if err != nil {
  231. // e.Log.Errorf("db error: %s", err)
  232. // return err
  233. //}
  234. //if device.T_pid != company.Id {
  235. // err = errors.New("获取SN信息失败,请检查SN是否正确!")
  236. // e.Log.Errorf("获取SN信息失败,device.T_pid != company.Id")
  237. // return err
  238. //}
  239. CoolerBoxModel.HistorySn = append(CoolerBoxModel.HistorySn, c.Sn)
  240. }
  241. c.Generate(&CoolerBoxModel)
  242. err = tx.Save(&CoolerBoxModel).Error
  243. if err != nil {
  244. e.Log.Errorf("db error: %s", err)
  245. return global.UpdateFailedErr
  246. }
  247. c.Id = CoolerBoxModel.Id
  248. return nil
  249. }
  250. // Remove 删除CoolerBox
  251. func (e *CoolerBox) Remove(c *dto.CoolerBoxDeleteReq, p *actions.DataPermission) error {
  252. var err error
  253. tx := e.Orm.Begin()
  254. defer func() {
  255. if err != nil {
  256. tx.Rollback()
  257. } else {
  258. tx.Commit()
  259. }
  260. }()
  261. var iceRaftModel model.IceRaft
  262. var count int64
  263. err = e.Orm.Model(&iceRaftModel).
  264. Scopes(
  265. actions.Permission(iceRaftModel.TableName(), p),
  266. IceRaftNameRecordStatusScopes(model.IceRaftRecordStatusUsing),
  267. IceRaftNameRecordCoolerBoxIdScopes(c.Id),
  268. ).
  269. Where("ice_raft.status = '2'").
  270. Joins("left join ice_raft_record on ice_raft.ice_raft_record_id = ice_raft_record.id").
  271. Count(&count).Error
  272. if err != nil {
  273. e.Log.Errorf("db error: %s", err)
  274. return global.DeleteFailedErr
  275. }
  276. if count > 0 {
  277. err = errors.New("该保温箱下有冰排正在使用中,无法删除!")
  278. return err
  279. }
  280. var CoolerBoxModel model.CoolerBox
  281. // 查询保温箱是否存在
  282. err = e.Orm.Scopes(actions.Permission(CoolerBoxModel.TableName(), p)).
  283. First(&CoolerBoxModel, c.GetId()).Error
  284. if err != nil {
  285. e.Log.Errorf("db error: %s", err)
  286. if errors.Is(err, gorm.ErrRecordNotFound) {
  287. return global.DeleteNotFoundOrNoPermissionErr
  288. }
  289. return global.DeleteFailedErr
  290. }
  291. db := tx.Delete(&CoolerBoxModel)
  292. if err = db.Error; err != nil {
  293. e.Log.Errorf("db error: %s", err)
  294. return global.DeleteFailedErr
  295. }
  296. if db.RowsAffected == 0 {
  297. return global.DeleteNotFoundOrNoPermissionErr
  298. }
  299. return nil
  300. }
  301. // GetHistory 获取保温箱历史信息
  302. func (e *CoolerBox) GetHistory(c *dto.CoolerBoxBatchReqSN, p *actions.DataPermission) ([]dto.HistoricalData, error) {
  303. var err error
  304. var taskList []model.WaybillTask
  305. var historical []dto.HistoricalData
  306. if c.Page == 0 {
  307. c.Page = 1
  308. }
  309. if c.PageZ == 0 {
  310. c.PageZ = 10
  311. }
  312. // 查询运单任务信息
  313. err = e.Orm.Model(&model.WaybillTask{}).
  314. Where("cooler_box_id = ?", c.Id).
  315. Find(&taskList).Error
  316. if err != nil {
  317. e.Log.Errorf("db error: %s", err)
  318. return nil, errors.New("获取运单任务信息失败")
  319. }
  320. // 获取公司秘钥
  321. var dataDept model.SysDept
  322. var Depts []model.SysDept
  323. err = e.Orm.Model(&dataDept).
  324. Scopes(
  325. CompanyIdScopesUser(p.DeptId),
  326. ).
  327. Find(&Depts).Limit(-1).Offset(-1).Error
  328. if err != nil {
  329. e.Log.Errorf("db error: %s", err)
  330. return nil, model.GetCompanyKeyErr
  331. }
  332. //company, err := model.GetCompanyById(p.DeptId)
  333. //if err != nil {
  334. // e.Log.Errorf("db error: %s", err)
  335. // return nil, model.GetCompanyKeyErr
  336. //}
  337. // 限制并发请求数量
  338. const maxConcurrentRequests = 10
  339. semaphore := make(chan struct{}, maxConcurrentRequests)
  340. var deviceSensorList []nats_server.DeviceSensor_R
  341. var wg sync.WaitGroup
  342. for _, task := range taskList {
  343. wg.Add(1)
  344. go func(task model.WaybillTask) {
  345. defer wg.Done()
  346. semaphore <- struct{}{}
  347. defer func() { <-semaphore }()
  348. for _, v := range Depts {
  349. deviceSensorList, _, _ = nats_server.Cold_CompanyDeviceSensor_List_ByKey(task.Sn, v.ColdKey)
  350. if len(deviceSensorList) > 0 {
  351. historical = append(historical, dto.HistoricalData{
  352. Sn: task.Sn,
  353. T_id: strconv.Itoa(deviceSensorList[0].T_id) + "|",
  354. StartTime: task.StartTime.String(),
  355. EndTime: task.EndTime.String(),
  356. CompanyName: v.Name,
  357. })
  358. break
  359. }
  360. }
  361. if len(deviceSensorList) == 0 {
  362. return
  363. }
  364. if err != nil {
  365. e.Log.Errorf("nats 获取轨迹信息失败: %s", err)
  366. return
  367. }
  368. }(task)
  369. }
  370. wg.Wait()
  371. sort.Slice(historical, func(i, j int) bool {
  372. if historical[i].StartTime < historical[j].StartTime {
  373. return true
  374. }
  375. return false
  376. })
  377. startIndex := (c.Page - 1) * c.PageZ
  378. endIndex := startIndex + c.PageZ
  379. if endIndex > len(historical) {
  380. endIndex = len(historical)
  381. }
  382. pageHistory := historical[startIndex:endIndex]
  383. return pageHistory, nil
  384. }
  385. // GetNewLocus 获取最新轨迹
  386. func (e *CoolerBox) GetNewLocus(c *dto.CoolerBoxReq, list *[]nats_server.DeviceData_) error {
  387. // 获取公司秘钥
  388. var dataDept model.SysDept
  389. var Depts []model.SysDept
  390. atoi, _ := strconv.Atoi(c.Id)
  391. err := e.Orm.Model(&dataDept).
  392. Scopes(
  393. CompanyIdScopesUser(atoi),
  394. ).
  395. Find(&Depts).Limit(-1).Offset(-1).Error
  396. if err != nil {
  397. e.Log.Errorf("db error: %s", err)
  398. return global.GetFailedErr
  399. }
  400. var deviceSensorList []nats_server.DeviceSensor_R
  401. for _, v := range Depts {
  402. deviceSensorList, _, _ = nats_server.Cold_CompanyDeviceSensor_List_ByKey(c.T_sn, v.ColdKey)
  403. if len(deviceSensorList) > 0 {
  404. break
  405. }
  406. }
  407. if len(deviceSensorList) == 0 {
  408. return errors.New("该设备不属于该公司")
  409. }
  410. data, err := nats_server.Read_DeviceTask_List_By_Condition(c.T_sn)
  411. if err != nil {
  412. e.Log.Errorf("设备未开启监控: %s", err)
  413. return errors.New("设备未开启监控")
  414. }
  415. // 当最新设备状态为1时,证明设备正在启动中,查询大于开始时间的所有轨迹,设备状态为2时,查询开始时间到结束时间的轨迹
  416. if data.T_State == 1 {
  417. 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"), "")
  418. if err != nil {
  419. e.Log.Errorf("获取设备轨迹失败: %s", err)
  420. return errors.New("获取设备轨迹失败")
  421. }
  422. *list = appendUniqueDeviceData(*list, deviceData)
  423. } else if data.T_State == 2 {
  424. 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"))
  425. if err != nil {
  426. e.Log.Errorf("获取设备轨迹失败: %s", err)
  427. return errors.New("获取设备轨迹失败")
  428. }
  429. *list = appendUniqueDeviceData(*list, deviceData)
  430. }
  431. return nil
  432. }
  433. // 去重并追加设备数据
  434. func appendUniqueDeviceData(list []nats_server.DeviceData_, newData []nats_server.DeviceData_) []nats_server.DeviceData_ {
  435. uniqueMap := make(map[time.Time]bool)
  436. for _, d := range list {
  437. //split := strings.Split(list[i].T_site, ",")
  438. //defer func() {
  439. // if r := recover(); r != nil {
  440. // fmt.Println("数组下标越界:", r)
  441. // }
  442. //}()
  443. //Lng := split[0]
  444. //Lat := split[1]
  445. //Lngs, _ := strconv.ParseFloat(Lng, 64)
  446. //Lats, _ := strconv.ParseFloat(Lat, 64)
  447. //mLng, mLat := lib.Wgs84ToGcj02(Lngs, Lats)
  448. //list[i].T_site = fmt.Sprintf("%v,%v", mLng, mLat)
  449. uniqueMap[d.T_time] = true
  450. }
  451. for _, d := range newData {
  452. if !uniqueMap[d.T_time] {
  453. split := strings.Split(d.T_site, ",")
  454. defer func() {
  455. if r := recover(); r != nil {
  456. fmt.Println("数组下标越界:", r)
  457. }
  458. }()
  459. Lng := split[0]
  460. Lat := split[1]
  461. Lngs, _ := strconv.ParseFloat(Lng, 64)
  462. Lats, _ := strconv.ParseFloat(Lat, 64)
  463. mLng, mLat := lib.Wgs84ToGcj02(Lngs, Lats)
  464. d.T_site = fmt.Sprintf("%v,%v", mLng, mLat)
  465. list = append(list, d)
  466. }
  467. }
  468. //排序
  469. sort.Slice(list, func(i, j int) bool {
  470. return list[i].T_time.After(list[j].T_time)
  471. })
  472. return list
  473. }
  474. // GetCoolerBoxIce 获取保温箱下的所有历史冰排
  475. func (e *CoolerBox) GetCoolerBoxIce(c *dto.GetCoolerBoxIce, list *[]model.IceRaftRecord, p *actions.DataPermission, count *int64) error {
  476. var err error
  477. tx := e.Orm.Begin()
  478. defer func() {
  479. if err != nil {
  480. tx.Rollback()
  481. } else {
  482. tx.Commit()
  483. }
  484. }()
  485. //查询保温箱是否存在
  486. var icerecord model.IceRaftRecord
  487. err = e.Orm.Scopes(
  488. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  489. actions.Permission(icerecord.TableName(), p)).
  490. Where("cooler_box_id=?", c.CoolerBoxId).
  491. Preload("IceLocker").
  492. Preload("CoolerBox").
  493. Find(&list).Count(count).Error
  494. if err != nil {
  495. e.Log.Errorf("db error: %s", err)
  496. if errors.Is(err, gorm.ErrRecordNotFound) {
  497. return global.GetFailedErr
  498. }
  499. return global.GetFailedErr
  500. }
  501. return nil
  502. }
  503. // GetCoolerBoxIceAll 获取保温箱所有历史记录
  504. func (e *CoolerBox) GetCoolerBoxIceAll(c *dto.GetCoolerBoxIceAll, list *[]model.CoolerBoxRecord, p *actions.DataPermission, count *int64) error {
  505. var err error
  506. tx := e.Orm.Begin()
  507. defer func() {
  508. if err != nil {
  509. tx.Rollback()
  510. } else {
  511. tx.Commit()
  512. }
  513. }()
  514. //查询保温箱是否存在
  515. var cooler model.CoolerBoxRecord
  516. if c.PageSize == 9999 {
  517. err = e.Orm.Scopes(
  518. cDto.MakeCondition(c.GetNeedSearch()),
  519. actions.Permission(cooler.TableName(), p),
  520. CollerStartTimeAndEndTimeScopes(c.StartTime, c.EndTime),
  521. ).
  522. Where("status = ?", 2).
  523. Find(&list).
  524. Count(count).Error
  525. } else {
  526. err = e.Orm.Scopes(
  527. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  528. cDto.MakeCondition(c.GetNeedSearch()),
  529. actions.Permission(cooler.TableName(), p),
  530. CollerStartTimeAndEndTimeScopes(c.StartTime, c.EndTime),
  531. ).
  532. Where("status = ?", 2).
  533. Find(&list).
  534. Count(count).Error
  535. }
  536. if err != nil {
  537. e.Log.Errorf("db error: %s", err)
  538. if errors.Is(err, gorm.ErrRecordNotFound) {
  539. return global.GetFailedErr
  540. }
  541. return global.GetFailedErr
  542. }
  543. return nil
  544. }
  545. // CoolerBoxStarTime 保温箱开始释冷
  546. //
  547. // func (e *CoolerBox) CoolerBoxStarTime(c *dto.IceStartbleForColfTimReq, p *actions.DataPermission) error {
  548. // var err error
  549. //
  550. // tx := e.Orm.Begin()
  551. // defer func() {
  552. // if err != nil {
  553. // tx.Rollback()
  554. // } else {
  555. // tx.Commit()
  556. // }
  557. // }()
  558. // //查询冰排是否存在
  559. // for _, v := range c.Id {
  560. // var iceRaftRecordModel model.IceRaftRecord
  561. // err = e.Orm.Scopes(actions.Permission(iceRaftRecordModel.TableName(), p)).
  562. // First(&iceRaftRecordModel, v).Error
  563. // if err != nil {
  564. // e.Log.Errorf("db error: %s", err)
  565. // if errors.Is(err, gorm.ErrRecordNotFound) {
  566. // return global.UpdateNotFoundOrNoPermissionErr
  567. // }
  568. // return global.UpdateFailedErr
  569. // }
  570. // iceRaftRecordModel.CoolerBoxStarTime = model2.Time(time.Now())
  571. // updates := tx.Where("id = ?", iceRaftRecordModel.Id).Updates(&iceRaftRecordModel)
  572. // if err = updates.Error; err != nil {
  573. // e.Log.Errorf("db error: %s", err)
  574. // return global.UpdateFailedErr
  575. // }
  576. // }
  577. // return nil
  578. // }
  579. func (e *CoolerBox) CoolerBoxStarTime(c *dto.IceStartbleForColfTimReq, p *actions.DataPermission) error {
  580. var err error
  581. tx := e.Orm.Begin()
  582. defer func() {
  583. if err != nil {
  584. tx.Rollback()
  585. } else {
  586. tx.Commit()
  587. }
  588. }()
  589. var user model.SysUser
  590. first := e.Orm.Model(&model.SysUser{}).Where("id = ?", p.UserId).First(&user)
  591. if first.Error != nil {
  592. return global.GetFailedErr
  593. }
  594. //添加保温箱记录
  595. for _, v := range c.Id {
  596. var cooler model.CoolerBoxRecord
  597. var coolerBox model.CoolerBox
  598. err = e.Orm.Model(&model.CoolerBox{}).Where("id = ?", v).First(&coolerBox).Error
  599. if err != nil {
  600. e.Log.Errorf("获取保温箱信息失败: %s", err)
  601. return errors.New("获取保温箱信息失败")
  602. }
  603. cooler.CoolerBoxStarTime = model2.Time(time.Now())
  604. cooler.CoolerColdUsers = user.NickName
  605. cooler.CoolerColdAddress = coolerBox.ColdSpots
  606. cooler.ColdTemperatures = coolerBox.ColdTemperatures
  607. cooler.Status = "1" //保温箱使用状态变为使用中
  608. CoolerBoxId, _ := strconv.Atoi(v)
  609. cooler.CoolerBoxId = CoolerBoxId
  610. cooler.DeptId = p.DeptId
  611. cooler.Sn = coolerBox.Sn
  612. cooler.Name = coolerBox.Name
  613. err = e.Orm.Create(&cooler).Error
  614. if err != nil {
  615. e.Log.Errorf("db error: %s", err)
  616. return global.CreateFailedErr
  617. }
  618. }
  619. return nil
  620. }