cooler_box.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. package controller
  2. import (
  3. "cold-delivery/app/admin/model"
  4. "cold-delivery/app/admin/service"
  5. "cold-delivery/app/admin/service/dto"
  6. "cold-delivery/common/actions"
  7. "cold-delivery/common/nats/nats_server"
  8. "encoding/json"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "github.com/gin-gonic/gin/binding"
  12. "gogs.baozhida.cn/zoie/OAuth-core/api"
  13. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  14. _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  15. "sort"
  16. "time"
  17. )
  18. type CoolerBoxController struct {
  19. api.Api
  20. }
  21. // GetPage 获取保温箱列表
  22. // @Summary 获取保温箱列表
  23. // @Description 获取保温箱列表
  24. // @Tags 保温箱
  25. // @Param name query string false "保温箱名称"
  26. // @Param sn query string false "保温箱关联sn"
  27. // @Param status query string false "状态:1-停用 2-启用"
  28. // @Param showTemp query bool false "是否展示最新温度"
  29. // @Param pageSize query int false "页条数"
  30. // @Param page query int false "页码"
  31. // @Success 200 {object} response.Response{data=response.Page{list=[]model.CoolerBox}} "{"code": 200, "data": [...]}"
  32. // @Router /api/cooler-box [get]
  33. // @Security Bearer
  34. func (e CoolerBoxController) GetPage(c *gin.Context) {
  35. s := service.CoolerBox{}
  36. req := dto.CoolerBoxGetPageReq{}
  37. err := e.MakeContext(c).
  38. MakeOrm().
  39. Bind(&req, binding.Query).
  40. MakeService(&s.Service).
  41. Errors
  42. if err != nil {
  43. e.Logger.Error(err)
  44. e.Error(500, err, err.Error())
  45. return
  46. }
  47. //数据权限检查
  48. p := actions.GetPermissionFromContext(c)
  49. list := make([]model.CoolerBox, 0)
  50. var count int64
  51. err = s.GetPage(&req, &list, &count, p)
  52. if err != nil {
  53. e.Error(500, err, err.Error())
  54. return
  55. }
  56. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  57. }
  58. // Get 通过id获取保温箱
  59. // @Summary 通过id获取保温箱
  60. // @Description 通过id获取保温箱
  61. // @Tags 保温箱
  62. // @Param id path string true "保温箱id"
  63. // @Success 200 {object} response.Response{data=model.CoolerBox} "{"code": 200, "data": [...]}"
  64. // @Router /api/cooler-box/{id} [get]
  65. // @Security Bearer
  66. func (e CoolerBoxController) Get(c *gin.Context) {
  67. s := service.CoolerBox{}
  68. req := dto.CoolerBoxGetReq{}
  69. err := e.MakeContext(c).
  70. MakeOrm().
  71. Bind(&req, nil).
  72. MakeService(&s.Service).
  73. Errors
  74. if err != nil {
  75. e.Logger.Error(err)
  76. e.Error(500, err, err.Error())
  77. return
  78. }
  79. var object model.CoolerBox
  80. p := actions.GetPermissionFromContext(c)
  81. //数据权限检查
  82. err = s.Get(&req, &object, p)
  83. if err != nil {
  84. e.Error(500, err, err.Error())
  85. return
  86. }
  87. e.OK(object, "查询成功")
  88. }
  89. // Insert 添加保温箱
  90. // @Summary 添加保温箱
  91. // @Description 添加保温箱
  92. // @Tags 保温箱
  93. // @Accept application/json
  94. // @Product application/json
  95. // @Param data body dto.CoolerBoxInsertReq true "data"
  96. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  97. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  98. // @Router /api/cooler-box [post]
  99. // @Security Bearer
  100. func (e CoolerBoxController) Insert(c *gin.Context) {
  101. s := service.CoolerBox{}
  102. req := dto.CoolerBoxInsertReq{}
  103. err := e.MakeContext(c).
  104. MakeOrm().
  105. Bind(&req, binding.JSON).
  106. MakeService(&s.Service).
  107. Errors
  108. if err != nil {
  109. e.Logger.Error(err)
  110. e.Error(500, err, err.Error())
  111. return
  112. }
  113. p := actions.GetPermissionFromContext(c)
  114. // 设置创建人
  115. req.SetCreateBy(user.GetUserId(c))
  116. req.SetDeptId(p.DeptId)
  117. err = s.Insert(&req, p)
  118. if err != nil {
  119. e.Error(500, err, err.Error())
  120. return
  121. }
  122. e.OK(req.GetId(), "添加成功")
  123. }
  124. // Update 修改保温箱
  125. // @Summary 修改保温箱
  126. // @Description 修改保温箱
  127. // @Tags 保温箱
  128. // @Accept application/json
  129. // @Product application/json
  130. // @Param data body dto.CoolerBoxUpdateReq true "body"
  131. // @Success 200 {string} string "{"code": 200, "message": "修改成功"}"
  132. // @Success 200 {string} string "{"code": -1, "message": "修改失败"}"
  133. // @Router /api/cooler-box [put]
  134. // @Security Bearer
  135. func (e CoolerBoxController) Update(c *gin.Context) {
  136. s := service.CoolerBox{}
  137. req := dto.CoolerBoxUpdateReq{}
  138. err := e.MakeContext(c).
  139. MakeOrm().
  140. Bind(&req).
  141. MakeService(&s.Service).
  142. Errors
  143. if err != nil {
  144. e.Logger.Error(err)
  145. e.Error(500, err, err.Error())
  146. return
  147. }
  148. p := actions.GetPermissionFromContext(c)
  149. req.SetUpdateBy(user.GetUserId(c))
  150. err = s.Update(&req, p)
  151. if err != nil {
  152. e.Error(500, err, err.Error())
  153. return
  154. }
  155. e.OK(req.GetId(), "修改成功")
  156. }
  157. // Delete 删除保温箱
  158. // @Summary 删除保温箱
  159. // @Description 删除保温箱
  160. // @Tags 保温箱
  161. // @Accept application/json
  162. // @Product application/json
  163. // @Param data body dto.CoolerBoxDeleteReq true "body"
  164. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  165. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  166. // @Router /api/cooler-box [delete]
  167. // @Security Bearer
  168. func (e CoolerBoxController) Delete(c *gin.Context) {
  169. s := service.CoolerBox{}
  170. req := dto.CoolerBoxDeleteReq{}
  171. err := e.MakeContext(c).
  172. MakeOrm().
  173. Bind(&req, binding.JSON, nil).
  174. MakeService(&s.Service).
  175. Errors
  176. if err != nil {
  177. e.Logger.Error(err)
  178. e.Error(500, err, err.Error())
  179. return
  180. }
  181. //数据权限检查
  182. p := actions.GetPermissionFromContext(c)
  183. err = s.Remove(&req, p)
  184. if err != nil {
  185. e.Error(500, err, err.Error())
  186. return
  187. }
  188. e.OK(req.GetId(), "删除成功")
  189. }
  190. // BatchInsert 批量添加保温箱
  191. // @Summary 批量添加保温箱
  192. // @Description 批量添加保温箱
  193. // @Tags 保温箱
  194. // @Accept application/json
  195. // @Product application/json
  196. // @Param data body dto.CoolerBoxBatchInsertReq true "data"
  197. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  198. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  199. // @Router /api/cooler-box/import [post]
  200. // @Security Bearer
  201. func (e CoolerBoxController) BatchInsert(c *gin.Context) {
  202. s := service.CoolerBox{}
  203. req := dto.CoolerBoxBatchInsertReq{}
  204. err := e.MakeContext(c).
  205. MakeOrm().
  206. Bind(&req, binding.JSON).
  207. MakeService(&s.Service).
  208. Errors
  209. if err != nil {
  210. e.Logger.Error(err)
  211. e.Error(500, err, err.Error())
  212. return
  213. }
  214. p := actions.GetPermissionFromContext(c)
  215. // 设置创建人
  216. req.SetCreateBy(user.GetUserId(c))
  217. req.SetDeptId(p.DeptId)
  218. err = s.BatchInsert(&req)
  219. if err != nil {
  220. e.Error(500, err, err.Error())
  221. return
  222. }
  223. e.OK(nil, "添加成功")
  224. }
  225. // GetCollerBoxLocus 获取保温箱历史
  226. // @Summary 获取保温箱历史轨迹
  227. // @Description 获取保温箱历史
  228. // @Tags 保温箱
  229. // @Accept application/json
  230. // @Product application/json
  231. // @Param data body dto.CoolerBoxBatchInsertReq true "data"
  232. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  233. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  234. // @Router /api/cooler-box/locus [get]
  235. // @Security Bearer
  236. func (e CoolerBoxController) GetCollerBoxLocus(c *gin.Context) {
  237. s := service.CoolerBox{}
  238. req := dto.CoolerBoxBatchReqSN{}
  239. err := e.MakeContext(c).
  240. MakeOrm().
  241. Bind(&req, binding.JSON).
  242. MakeService(&s.Service).
  243. Errors
  244. if err != nil {
  245. e.Logger.Error(err)
  246. e.Error(500, err, err.Error())
  247. return
  248. }
  249. p := actions.GetPermissionFromContext(c)
  250. history, err := s.GetHistory(&req, p)
  251. if err != nil {
  252. e.Error(500, err, err.Error())
  253. return
  254. }
  255. count := req.PageZ / req.Page
  256. e.PageOK(history, int(count), req.Page, req.PageZ, "查询成功")
  257. }
  258. // GetCoolerBoxLocus 获取保温箱历史轨迹
  259. // @Summary 获取保温箱历史轨迹
  260. // @Description 获取保温箱历史轨迹
  261. // @Tags 保温箱
  262. // @Accept application/json
  263. // @Product application/json
  264. // @Param data body dto.CoolerBoxBatchInsertReq true "data"
  265. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  266. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  267. // @Router /api/cooler-box/historylocus [post]
  268. // @Security Bearer
  269. func (e CoolerBoxController) GetCoolerBoxLocus(c *gin.Context) {
  270. s := service.CoolerBox{}
  271. req := dto.CoolerBoxBatchReq{}
  272. err := e.MakeContext(c).
  273. MakeOrm().
  274. Bind(&req, binding.JSON).
  275. MakeService(&s.Service).
  276. Errors
  277. if err != nil {
  278. e.Logger.Error(err)
  279. e.Error(500, err, err.Error())
  280. return
  281. }
  282. data, _, err := nats_server.Cold_ReadDeviceDataListBy_T_snidForLocus(req.SnId+","+req.T_id, req.StartTime, req.EndTime, req.Page, req.PageZ)
  283. if err != nil {
  284. e.Logger.Error("nats 获取轨迹信息失败:", err)
  285. fmt.Println("nats 获取轨迹信息失败: %s", err)
  286. e.Error(500, err, err.Error())
  287. return
  288. }
  289. // 倒序排序
  290. sort.Slice(data, func(i, j int) bool {
  291. if data[i].T_time < data[j].T_time {
  292. return true
  293. }
  294. return false
  295. })
  296. e.OK(data, "查询成功")
  297. }
  298. // GetCoolerBoxNewestLocus 获取保温箱最新轨迹
  299. // @Summary 获取保温箱最新轨迹
  300. // @Description 获取保温箱最新轨迹
  301. // @Tags 保温箱
  302. // @Accept application/json
  303. // @Product application/json
  304. // @Param data body dto.CoolerBoxBatchInsertReq true "data"
  305. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  306. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  307. // @Router /api/cooler-box/newlocus [post]
  308. // @Security Bearer
  309. func (e CoolerBoxController) GetCoolerBoxNewestLocus(c *gin.Context) {
  310. s := service.CoolerBox{}
  311. req := dto.CoolerBoxReq{}
  312. err := e.MakeContext(c).
  313. MakeOrm().
  314. Bind(&req, binding.Query).
  315. MakeService(&s.Service).
  316. Errors
  317. if err != nil {
  318. e.Logger.Error(err)
  319. e.Error(500, err, err.Error())
  320. return
  321. }
  322. if len(req.T_sn) == 0 || req.T_sn == "" {
  323. e.Error(500, err, err.Error())
  324. return
  325. }
  326. conn := true
  327. // 数据权限检查
  328. // p := actions.GetPermissionFromContext(c)
  329. list := make([]nats_server.DeviceData_, 0)
  330. // 设置响应头
  331. c.Header("Content-Type", "text/event-stream")
  332. c.Header("Cache-Control", "no-cache")
  333. c.Header("Connection", "keep-alive")
  334. // 监听客户端断开连接
  335. notify := c.Writer.CloseNotify()
  336. type Response struct {
  337. RequestId string `protobuf:"bytes,1,opt,name=requestId,proto3" json:"requestId,omitempty"`
  338. Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
  339. Msg string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
  340. SN string `protobuf:"bytes,3,opt,name=sn,proto3" json:"sn,omitempty"`
  341. Data []nats_server.DeviceData_ `json:"data"`
  342. }
  343. var response Response
  344. for conn {
  345. select {
  346. case <-notify:
  347. fmt.Println("断开连接")
  348. conn = false
  349. return
  350. default:
  351. err = s.GetNewLocus(&req, &list)
  352. if err != nil {
  353. e.Logger.Error(err)
  354. response.Code = 500
  355. response.Msg = err.Error()
  356. response.Data = list
  357. response.SN = req.T_sn
  358. res, _ := json.Marshal(&response)
  359. fmt.Fprintf(c.Writer, "data: %s\n\n", string(res))
  360. c.Writer.Flush()
  361. time.Sleep(20 * time.Second)
  362. } else {
  363. response.Code = 200
  364. response.Msg = "查询成功"
  365. response.Data = list
  366. response.SN = req.T_sn
  367. res, _ := json.Marshal(&response)
  368. fmt.Fprintf(c.Writer, "data: %s\n\n", string(res))
  369. c.Writer.Flush()
  370. time.Sleep(10 * time.Second)
  371. }
  372. }
  373. }
  374. }
  375. // GetCoolerBoxIce 获取指定保温箱下的所有冰排信息
  376. // @Summary 获取指定保温箱下的所有冰排信息
  377. // @Description 获取指定保温箱下的所有冰排信息
  378. // @Tags 保温箱
  379. // @Accept application/json
  380. // @Product application/json
  381. // @Param data body dto.CoolerBoxBatchInsertReq true "data"
  382. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  383. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  384. // @Router /api/cooler-box/getcoolerboxice [post]
  385. // @Security Bearer
  386. func (e CoolerBoxController) GetCoolerBoxIce(c *gin.Context) {
  387. s := service.CoolerBox{}
  388. req := dto.GetCoolerBoxIce{}
  389. err := e.MakeContext(c).
  390. MakeOrm().
  391. Bind(&req, binding.Query).
  392. MakeService(&s.Service).
  393. Errors
  394. if err != nil {
  395. e.Logger.Error(err)
  396. e.Error(500, err, err.Error())
  397. return
  398. }
  399. list := make([]model.IceRaftRecord, 0)
  400. var count int64
  401. //数据权限检查
  402. p := actions.GetPermissionFromContext(c)
  403. err = s.GetCoolerBoxIce(&req, &list, p, &count)
  404. if err != nil {
  405. e.Error(500, err, err.Error())
  406. return
  407. }
  408. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  409. }
  410. // GetCoolerBoxAll 获取所有保温箱使用记录
  411. // @Summary 获取所有保温箱使用记录
  412. // @Description 获取所有保温箱使用记录
  413. // @Tags 保温箱
  414. // @Accept application/json
  415. // @Product application/json
  416. // @Param data body dto.CoolerBoxBatchInsertReq true "data"
  417. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  418. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  419. // @Router /api/cooler-box/getcoolerboxall [get]
  420. // @Security Bearer
  421. func (e CoolerBoxController) GetCoolerBoxAll(c *gin.Context) {
  422. s := service.CoolerBox{}
  423. req := dto.GetCoolerBoxIceAll{}
  424. err := e.MakeContext(c).
  425. MakeOrm().
  426. Bind(&req, binding.Query).
  427. MakeService(&s.Service).
  428. Errors
  429. if err != nil {
  430. e.Logger.Error(err)
  431. e.Error(500, err, err.Error())
  432. return
  433. }
  434. list := make([]model.IceRaftRecord, 0)
  435. var count int64
  436. //数据权限检查
  437. p := actions.GetPermissionFromContext(c)
  438. err = s.GetCoolerBoxIceAll(&req, &list, p, &count)
  439. if err != nil {
  440. e.Error(500, err, err.Error())
  441. return
  442. }
  443. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  444. }