cooler_box.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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/beego/beego/v2/core/logs"
  11. "github.com/gin-gonic/gin"
  12. "github.com/gin-gonic/gin/binding"
  13. "github.com/xuri/excelize/v2"
  14. "gogs.baozhida.cn/zoie/OAuth-core/api"
  15. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  16. _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  17. "net/url"
  18. "os"
  19. "sort"
  20. "time"
  21. )
  22. type CoolerBoxController struct {
  23. api.Api
  24. }
  25. // GetPage 获取保温箱列表
  26. // @Summary 获取保温箱列表
  27. // @Description 获取保温箱列表
  28. // @Tags 保温箱
  29. // @Param name query string false "保温箱名称"
  30. // @Param sn query string false "保温箱关联sn"
  31. // @Param status query string false "状态:1-停用 2-启用"
  32. // @Param showTemp query bool false "是否展示最新温度"
  33. // @Param pageSize query int false "页条数"
  34. // @Param page query int false "页码"
  35. // @Success 200 {object} response.Response{data=response.Page{list=[]model.CoolerBox}} "{"code": 200, "data": [...]}"
  36. // @Router /api/cooler-box [get]
  37. // @Security Bearer
  38. func (e CoolerBoxController) GetPage(c *gin.Context) {
  39. s := service.CoolerBox{}
  40. req := dto.CoolerBoxGetPageReq{}
  41. err := e.MakeContext(c).
  42. MakeOrm().
  43. Bind(&req, binding.Query).
  44. MakeService(&s.Service).
  45. Errors
  46. if err != nil {
  47. e.Logger.Error(err)
  48. e.Error(500, err, err.Error())
  49. return
  50. }
  51. //数据权限检查
  52. p := actions.GetPermissionFromContext(c)
  53. list := make([]model.CoolerBox, 0)
  54. var count int64
  55. err = s.GetPage(&req, &list, &count, p)
  56. if err != nil {
  57. e.Error(500, err, err.Error())
  58. return
  59. }
  60. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  61. }
  62. // Get 通过id获取保温箱
  63. // @Summary 通过id获取保温箱
  64. // @Description 通过id获取保温箱
  65. // @Tags 保温箱
  66. // @Param id path string true "保温箱id"
  67. // @Success 200 {object} response.Response{data=model.CoolerBox} "{"code": 200, "data": [...]}"
  68. // @Router /api/cooler-box/{id} [get]
  69. // @Security Bearer
  70. func (e CoolerBoxController) Get(c *gin.Context) {
  71. s := service.CoolerBox{}
  72. req := dto.CoolerBoxGetReq{}
  73. err := e.MakeContext(c).
  74. MakeOrm().
  75. Bind(&req, nil).
  76. MakeService(&s.Service).
  77. Errors
  78. if err != nil {
  79. e.Logger.Error(err)
  80. e.Error(500, err, err.Error())
  81. return
  82. }
  83. var object model.CoolerBox
  84. p := actions.GetPermissionFromContext(c)
  85. //数据权限检查
  86. err = s.Get(&req, &object, p)
  87. if err != nil {
  88. e.Error(500, err, err.Error())
  89. return
  90. }
  91. e.OK(object, "查询成功")
  92. }
  93. // Insert 添加保温箱
  94. // @Summary 添加保温箱
  95. // @Description 添加保温箱
  96. // @Tags 保温箱
  97. // @Accept application/json
  98. // @Product application/json
  99. // @Param data body dto.CoolerBoxInsertReq true "data"
  100. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  101. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  102. // @Router /api/cooler-box [post]
  103. // @Security Bearer
  104. func (e CoolerBoxController) Insert(c *gin.Context) {
  105. s := service.CoolerBox{}
  106. req := dto.CoolerBoxInsertReq{}
  107. err := e.MakeContext(c).
  108. MakeOrm().
  109. Bind(&req, binding.JSON).
  110. MakeService(&s.Service).
  111. Errors
  112. if err != nil {
  113. e.Logger.Error(err)
  114. e.Error(500, err, err.Error())
  115. return
  116. }
  117. p := actions.GetPermissionFromContext(c)
  118. // 设置创建人
  119. req.SetCreateBy(user.GetUserId(c))
  120. req.SetDeptId(p.DeptId)
  121. err = s.Insert(&req, p)
  122. if err != nil {
  123. e.Error(500, err, err.Error())
  124. return
  125. }
  126. e.OK(req.GetId(), "添加成功")
  127. }
  128. // Update 修改保温箱
  129. // @Summary 修改保温箱
  130. // @Description 修改保温箱
  131. // @Tags 保温箱
  132. // @Accept application/json
  133. // @Product application/json
  134. // @Param data body dto.CoolerBoxUpdateReq true "body"
  135. // @Success 200 {string} string "{"code": 200, "message": "修改成功"}"
  136. // @Success 200 {string} string "{"code": -1, "message": "修改失败"}"
  137. // @Router /api/cooler-box [put]
  138. // @Security Bearer
  139. func (e CoolerBoxController) Update(c *gin.Context) {
  140. s := service.CoolerBox{}
  141. req := dto.CoolerBoxUpdateReq{}
  142. err := e.MakeContext(c).
  143. MakeOrm().
  144. Bind(&req).
  145. MakeService(&s.Service).
  146. Errors
  147. if err != nil {
  148. e.Logger.Error(err)
  149. e.Error(500, err, err.Error())
  150. return
  151. }
  152. p := actions.GetPermissionFromContext(c)
  153. req.SetUpdateBy(user.GetUserId(c))
  154. err = s.Update(&req, p)
  155. if err != nil {
  156. e.Error(500, err, err.Error())
  157. return
  158. }
  159. e.OK(req.GetId(), "修改成功")
  160. }
  161. // Delete 删除保温箱
  162. // @Summary 删除保温箱
  163. // @Description 删除保温箱
  164. // @Tags 保温箱
  165. // @Accept application/json
  166. // @Product application/json
  167. // @Param data body dto.CoolerBoxDeleteReq true "body"
  168. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  169. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  170. // @Router /api/cooler-box [delete]
  171. // @Security Bearer
  172. func (e CoolerBoxController) Delete(c *gin.Context) {
  173. s := service.CoolerBox{}
  174. req := dto.CoolerBoxDeleteReq{}
  175. err := e.MakeContext(c).
  176. MakeOrm().
  177. Bind(&req, binding.JSON, nil).
  178. MakeService(&s.Service).
  179. Errors
  180. if err != nil {
  181. e.Logger.Error(err)
  182. e.Error(500, err, err.Error())
  183. return
  184. }
  185. //数据权限检查
  186. p := actions.GetPermissionFromContext(c)
  187. err = s.Remove(&req, p)
  188. if err != nil {
  189. e.Error(500, err, err.Error())
  190. return
  191. }
  192. e.OK(req.GetId(), "删除成功")
  193. }
  194. // BatchInsert 批量添加保温箱
  195. // @Summary 批量添加保温箱
  196. // @Description 批量添加保温箱
  197. // @Tags 保温箱
  198. // @Accept application/json
  199. // @Product application/json
  200. // @Param data body dto.CoolerBoxBatchInsertReq true "data"
  201. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  202. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  203. // @Router /api/cooler-box/import [post]
  204. // @Security Bearer
  205. func (e CoolerBoxController) BatchInsert(c *gin.Context) {
  206. s := service.CoolerBox{}
  207. req := dto.CoolerBoxBatchInsertReq{}
  208. err := e.MakeContext(c).
  209. MakeOrm().
  210. Bind(&req, binding.JSON).
  211. MakeService(&s.Service).
  212. Errors
  213. if err != nil {
  214. e.Logger.Error(err)
  215. e.Error(500, err, err.Error())
  216. return
  217. }
  218. p := actions.GetPermissionFromContext(c)
  219. // 设置创建人
  220. req.SetCreateBy(user.GetUserId(c))
  221. req.SetDeptId(p.DeptId)
  222. err = s.BatchInsert(&req)
  223. if err != nil {
  224. e.Error(500, err, err.Error())
  225. return
  226. }
  227. e.OK(nil, "添加成功")
  228. }
  229. // GetCollerBoxLocus 获取保温箱历史
  230. // @Summary 获取保温箱历史轨迹
  231. // @Description 获取保温箱历史
  232. // @Tags 保温箱
  233. // @Accept application/json
  234. // @Product application/json
  235. // @Param data body dto.CoolerBoxBatchInsertReq true "data"
  236. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  237. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  238. // @Router /api/cooler-box/locus [get]
  239. // @Security Bearer
  240. func (e CoolerBoxController) GetCollerBoxLocus(c *gin.Context) {
  241. s := service.CoolerBox{}
  242. req := dto.CoolerBoxBatchReqSN{}
  243. err := e.MakeContext(c).
  244. MakeOrm().
  245. Bind(&req, binding.JSON).
  246. MakeService(&s.Service).
  247. Errors
  248. if err != nil {
  249. e.Logger.Error(err)
  250. e.Error(500, err, err.Error())
  251. return
  252. }
  253. p := actions.GetPermissionFromContext(c)
  254. history, err := s.GetHistory(&req, p)
  255. if err != nil {
  256. e.Error(500, err, err.Error())
  257. return
  258. }
  259. count := req.PageZ / req.Page
  260. e.PageOK(history, int(count), req.Page, req.PageZ, "查询成功")
  261. }
  262. // GetCoolerBoxLocus 获取保温箱历史轨迹
  263. // @Summary 获取保温箱历史轨迹
  264. // @Description 获取保温箱历史轨迹
  265. // @Tags 保温箱
  266. // @Accept application/json
  267. // @Product application/json
  268. // @Param data body dto.CoolerBoxBatchInsertReq true "data"
  269. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  270. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  271. // @Router /api/cooler-box/historylocus [post]
  272. // @Security Bearer
  273. func (e CoolerBoxController) GetCoolerBoxLocus(c *gin.Context) {
  274. s := service.CoolerBox{}
  275. req := dto.CoolerBoxBatchReq{}
  276. err := e.MakeContext(c).
  277. MakeOrm().
  278. Bind(&req, binding.JSON).
  279. MakeService(&s.Service).
  280. Errors
  281. if err != nil {
  282. e.Logger.Error(err)
  283. e.Error(500, err, err.Error())
  284. return
  285. }
  286. data, _, err := nats_server.Cold_ReadDeviceDataListBy_T_snidForLocus(req.SnId+","+req.T_id, req.StartTime, req.EndTime, req.Page, req.PageZ)
  287. if err != nil {
  288. e.Logger.Error("nats 获取轨迹信息失败:", err)
  289. fmt.Println("nats 获取轨迹信息失败: %s", err)
  290. e.Error(500, err, err.Error())
  291. return
  292. }
  293. // 倒序排序
  294. sort.Slice(data, func(i, j int) bool {
  295. if data[i].T_time < data[j].T_time {
  296. return true
  297. }
  298. return false
  299. })
  300. e.OK(data, "查询成功")
  301. }
  302. // GetCoolerBoxNewestLocus 获取保温箱最新轨迹
  303. // @Summary 获取保温箱最新轨迹
  304. // @Description 获取保温箱最新轨迹
  305. // @Tags 保温箱
  306. // @Accept application/json
  307. // @Product application/json
  308. // @Param data body dto.CoolerBoxBatchInsertReq true "data"
  309. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  310. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  311. // @Router /api/cooler-box/newlocus [post]
  312. // @Security Bearer
  313. func (e CoolerBoxController) GetCoolerBoxNewestLocus(c *gin.Context) {
  314. s := service.CoolerBox{}
  315. req := dto.CoolerBoxReq{}
  316. err := e.MakeContext(c).
  317. MakeOrm().
  318. Bind(&req, binding.Query).
  319. MakeService(&s.Service).
  320. Errors
  321. if err != nil {
  322. e.Logger.Error(err)
  323. e.Error(500, err, err.Error())
  324. return
  325. }
  326. if len(req.T_sn) == 0 || req.T_sn == "" {
  327. e.Error(500, err, err.Error())
  328. return
  329. }
  330. conn := true
  331. // 数据权限检查
  332. // p := actions.GetPermissionFromContext(c)
  333. list := make([]nats_server.DeviceData_, 0)
  334. // 设置响应头
  335. c.Header("Content-Type", "text/event-stream")
  336. c.Header("Cache-Control", "no-cache")
  337. c.Header("Connection", "keep-alive")
  338. // 监听客户端断开连接
  339. notify := c.Writer.CloseNotify()
  340. type Response struct {
  341. RequestId string `protobuf:"bytes,1,opt,name=requestId,proto3" json:"requestId,omitempty"`
  342. Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
  343. Msg string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
  344. SN string `protobuf:"bytes,3,opt,name=sn,proto3" json:"sn,omitempty"`
  345. CoolerName string `protobuf:"bytes,4,opt,name=CoolerName,proto3" json:"CoolerName,omitempty"`
  346. Data []nats_server.DeviceData_ `json:"data"`
  347. }
  348. var response Response
  349. for conn {
  350. select {
  351. case <-notify:
  352. fmt.Println("断开连接")
  353. conn = false
  354. return
  355. default:
  356. err = s.GetNewLocus(&req, &list)
  357. if err != nil {
  358. e.Logger.Error(err)
  359. response.Code = 500
  360. response.Msg = err.Error()
  361. response.Data = list
  362. response.SN = req.T_sn
  363. response.CoolerName = req.CoolerName
  364. res, _ := json.Marshal(&response)
  365. fmt.Fprintf(c.Writer, "data: %s\n\n", string(res))
  366. c.Writer.Flush()
  367. time.Sleep(20 * time.Second)
  368. } else {
  369. response.Code = 200
  370. response.Msg = "查询成功"
  371. response.Data = list
  372. response.SN = req.T_sn
  373. response.CoolerName = req.CoolerName
  374. res, _ := json.Marshal(&response)
  375. fmt.Fprintf(c.Writer, "data: %s\n\n", string(res))
  376. c.Writer.Flush()
  377. time.Sleep(10 * time.Second)
  378. }
  379. }
  380. }
  381. }
  382. // GetCoolerBoxIce 获取指定保温箱下的所有冰排信息
  383. // @Summary 获取指定保温箱下的所有冰排信息
  384. // @Description 获取指定保温箱下的所有冰排信息
  385. // @Tags 保温箱
  386. // @Accept application/json
  387. // @Product application/json
  388. // @Param data body dto.CoolerBoxBatchInsertReq true "data"
  389. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  390. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  391. // @Router /api/cooler-box/getcoolerboxice [post]
  392. // @Security Bearer
  393. func (e CoolerBoxController) GetCoolerBoxIce(c *gin.Context) {
  394. s := service.CoolerBox{}
  395. req := dto.GetCoolerBoxIce{}
  396. err := e.MakeContext(c).
  397. MakeOrm().
  398. Bind(&req, binding.Query).
  399. MakeService(&s.Service).
  400. Errors
  401. if err != nil {
  402. e.Logger.Error(err)
  403. e.Error(500, err, err.Error())
  404. return
  405. }
  406. list := make([]model.IceRaftRecord, 0)
  407. var count int64
  408. //数据权限检查
  409. p := actions.GetPermissionFromContext(c)
  410. err = s.GetCoolerBoxIce(&req, &list, p, &count)
  411. if err != nil {
  412. e.Error(500, err, err.Error())
  413. return
  414. }
  415. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  416. }
  417. // GetCoolerBoxAll 获取所有保温箱使用记录
  418. // @Summary 获取所有保温箱使用记录
  419. // @Description 获取所有保温箱使用记录
  420. // @Tags 保温箱
  421. // @Accept application/json
  422. // @Product application/json
  423. // @Param data body dto.CoolerBoxBatchInsertReq true "data"
  424. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  425. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  426. // @Router /api/cooler-box/getcoolerboxall [get]
  427. // @Security Bearer
  428. func (e CoolerBoxController) GetCoolerBoxAll(c *gin.Context) {
  429. s := service.CoolerBox{}
  430. req := dto.GetCoolerBoxIceAll{}
  431. err := e.MakeContext(c).
  432. MakeOrm().
  433. Bind(&req, binding.Query).
  434. MakeService(&s.Service).
  435. Errors
  436. if err != nil {
  437. e.Logger.Error(err)
  438. e.Error(500, err, err.Error())
  439. return
  440. }
  441. list := make([]model.CoolerBoxRecord, 0)
  442. var count int64
  443. //数据权限检查
  444. p := actions.GetPermissionFromContext(c)
  445. err = s.GetCoolerBoxIceAll(&req, &list, p, &count)
  446. if err != nil {
  447. e.Error(500, err, err.Error())
  448. return
  449. }
  450. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  451. }
  452. // ExportExecl 导出保温箱使用记录
  453. // @Summary 导出保温箱使用记录
  454. // @Description 导出保温箱使用记录
  455. // @Tags 保温箱
  456. // @Accept application/json
  457. // @Product application/json
  458. // @Param data body dto.IceRaftRecordEedReq true "body"
  459. // @Success 200 {string} string "{"code": 200, "message": "提交成功"}"
  460. // @Success 200 {string} string "{"code": -1, "message": "提交成功"}"
  461. // @Router /api/export-execl [get]
  462. // @Security Bearer
  463. func (e CoolerBoxController) ExportExecl(c *gin.Context) {
  464. s := service.CoolerBox{}
  465. req := dto.GetCoolerBoxIceAll{}
  466. err := e.MakeContext(c).
  467. MakeOrm().
  468. Bind(&req, binding.Query).
  469. MakeService(&s.Service).
  470. Errors
  471. if err != nil {
  472. e.Logger.Error(err)
  473. e.Error(500, err, err.Error())
  474. return
  475. }
  476. list := make([]model.CoolerBoxRecord, 0)
  477. var count int64
  478. //数据权限检查
  479. p := actions.GetPermissionFromContext(c)
  480. req.PageSize = 9999
  481. req.Page = 1
  482. err = s.GetCoolerBoxIceAll(&req, &list, p, &count)
  483. if err != nil {
  484. e.Error(500, err, err.Error())
  485. return
  486. }
  487. f := excelize.NewFile() // 设置单元格的值
  488. //获取当前登录用户信息,查询是否有权限
  489. err, sysUser := service.GetUserProfile(e.Orm, p.UserId)
  490. if err != nil {
  491. e.Error(500, err, "获取用户信息失败")
  492. return
  493. }
  494. fmt.Println(sysUser)
  495. if sysUser.Dept.IsCoolerReleaseCold {
  496. // 这里设置表头ÒÒ
  497. f.SetCellValue("Sheet1", "A1", "序号")
  498. f.SetCellValue("Sheet1", "B1", "SN")
  499. f.SetCellValue("Sheet1", "C1", "预冷放入时间")
  500. f.SetCellValue("Sheet1", "D1", "预冷温度")
  501. f.SetCellValue("Sheet1", "E1", "预冷地点")
  502. f.SetCellValue("Sheet1", "F1", "预冷操作人员")
  503. f.SetCellValue("Sheet1", "G1", "使用日期")
  504. f.SetCellValue("Sheet1", "H1", "放置冰排编号")
  505. f.SetCellValue("Sheet1", "I1", "放置冰排数量")
  506. f.SetCellValue("Sheet1", "J1", "操作人员")
  507. f.SetCellValue("Sheet1", "K1", "备注")
  508. // 设置列宽
  509. f.SetColWidth("Sheet1", "A", "A", 6)
  510. f.SetColWidth("Sheet1", "B", "B", 8)
  511. f.SetColWidth("Sheet1", "C", "C", 14)
  512. f.SetColWidth("Sheet1", "D", "D", 14)
  513. f.SetColWidth("Sheet1", "E", "E", 30)
  514. f.SetColWidth("Sheet1", "F", "F", 14)
  515. f.SetColWidth("Sheet1", "G", "G", 14)
  516. f.SetColWidth("Sheet1", "H", "H", 30)
  517. f.SetColWidth("Sheet1", "I", "J", 15)
  518. f.SetColWidth("Sheet1", "K", "K", 30)
  519. line := 1
  520. // 循环写入数据
  521. for i, v := range list {
  522. line++
  523. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  524. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.Sn)
  525. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.CoolerBoxStarTime)
  526. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.ColdTemperatures)
  527. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.CoolerColdAddress)
  528. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.CoolerColdUsers)
  529. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.CoolerUserTime)
  530. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.HistoryCode)
  531. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), len(v.HistoryCode))
  532. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), v.CoolerUseUsers)
  533. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), "")
  534. }
  535. } else {
  536. // 这里设置表头ÒÒ
  537. f.SetCellValue("Sheet1", "A1", "序号")
  538. f.SetCellValue("Sheet1", "B1", "SN")
  539. f.SetCellValue("Sheet1", "C1", "使用日期")
  540. f.SetCellValue("Sheet1", "D1", "放置冰排编号")
  541. f.SetCellValue("Sheet1", "E1", "放置冰排数量")
  542. f.SetCellValue("Sheet1", "F1", "操作人员")
  543. f.SetCellValue("Sheet1", "G1", "备注")
  544. // 设置列宽
  545. f.SetColWidth("Sheet1", "A", "A", 6)
  546. f.SetColWidth("Sheet1", "B", "B", 8)
  547. f.SetColWidth("Sheet1", "C", "C", 14)
  548. f.SetColWidth("Sheet1", "D", "D", 14)
  549. f.SetColWidth("Sheet1", "E", "E", 30)
  550. f.SetColWidth("Sheet1", "F", "F", 14)
  551. f.SetColWidth("Sheet1", "G", "G", 14)
  552. line := 1
  553. // 循环写入数据
  554. for i, v := range list {
  555. line++
  556. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  557. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.Sn)
  558. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.CoolerUserTime)
  559. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.HistoryCode)
  560. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), len(v.HistoryCode))
  561. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.CoolerUseUsers)
  562. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), "")
  563. }
  564. }
  565. timeStr := time.Now().Format("20060102150405")
  566. filePath := "ofile/" + "保温箱使用记录表" + timeStr + ".xlsx"
  567. // 保存文件
  568. if err = f.SaveAs(filePath); err != nil {
  569. logs.Error("文件失败:", err)
  570. }
  571. defer func() {
  572. os.Remove(filePath)
  573. }()
  574. c.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
  575. // PathEscape 函数对中文做处理
  576. c.Header("Content-Disposition", "attachment; filename="+url.PathEscape("保温箱使用记录表"+timeStr+".xlsx"))
  577. c.Header("Content-Transfer-Encoding", "binary")
  578. c.File(filePath)
  579. }
  580. // CoolerBoxStarTime 保温箱开始遇冷
  581. // @Summary 保温箱开始遇冷
  582. // @Description 保温箱开始遇冷
  583. // @Tags 冰排
  584. // @Accept application/json
  585. // @Product application/json
  586. // @Param data body dto.IceRaftRecordEedReq true "body"
  587. // @Success 200 {string} string "{"code": 200, "message": "提交成功"}"
  588. // @Success 200 {string} string "{"code": -1, "message": "提交成功"}"
  589. // @Router /api/coolerboxstarttime [post]
  590. // @Security Bearer
  591. func (e CoolerBoxController) CoolerBoxStarTime(c *gin.Context) {
  592. s := service.CoolerBox{}
  593. req := dto.IceStartbleForColfTimReq{}
  594. err := e.MakeContext(c).
  595. MakeOrm().
  596. Bind(&req, binding.JSON, nil).
  597. MakeService(&s.Service).
  598. Errors
  599. if err != nil {
  600. e.Logger.Error(err)
  601. e.Error(500, err, err.Error())
  602. return
  603. }
  604. //数据权限检查
  605. p := actions.GetPermissionFromContext(c)
  606. err = s.CoolerBoxStarTime(&req, p)
  607. if err != nil {
  608. e.Error(500, err, err.Error())
  609. return
  610. }
  611. e.OK(req, "提交成功")
  612. }