ice_raft.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. "encoding/json"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "github.com/gin-gonic/gin/binding"
  11. "gogs.baozhida.cn/zoie/OAuth-core/api"
  12. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  13. _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  14. "time"
  15. )
  16. type IceRaftController struct {
  17. api.Api
  18. }
  19. // GetPage 获取冰排列表
  20. // @Summary 获取冰排列表
  21. // @Description 获取冰排列表
  22. // @Tags 冰排
  23. // @Param name query string false "冰排名称"
  24. // @Param pageSize query int false "页条数"
  25. // @Param page query int false "页码"
  26. // @Success 200 {object} response.Response{data=response.Page{list=[]model.IceRaft}} "{"code": 200, "data": [...]}"
  27. // @Router /api/ice-raft [get]
  28. // @Security Bearer
  29. func (e IceRaftController) GetPage(c *gin.Context) {
  30. s := service.IceRaft{}
  31. req := dto.IceRaftGetPageReq{}
  32. err := e.MakeContext(c).
  33. MakeOrm().
  34. Bind(&req, binding.Query).
  35. MakeService(&s.Service).
  36. Errors
  37. if err != nil {
  38. e.Logger.Error(err)
  39. e.Error(500, err, err.Error())
  40. return
  41. }
  42. //数据权限检查
  43. p := actions.GetPermissionFromContext(c)
  44. list := make([]model.IceRaft, 0)
  45. var count int64
  46. err = s.GetPage(&req, &list, &count, p)
  47. if err != nil {
  48. e.Error(500, err, err.Error())
  49. return
  50. }
  51. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  52. }
  53. func (e IceRaftController) GetPageByCoolerBoxId(c *gin.Context) {
  54. s := service.IceRaft{}
  55. req := dto.GetPageByCoolerBoxIdReq{}
  56. err := e.MakeContext(c).
  57. MakeOrm().
  58. Bind(&req, binding.Query).
  59. MakeService(&s.Service).
  60. Errors
  61. if err != nil {
  62. e.Logger.Error(err)
  63. e.Error(500, err, err.Error())
  64. return
  65. }
  66. //数据权限检查
  67. p := actions.GetPermissionFromContext(c)
  68. list := make([]string, 0)
  69. err = s.GetPageByCoolerBoxId(req.CoolerBoxId, &list, p)
  70. if err != nil {
  71. e.Error(500, err, err.Error())
  72. return
  73. }
  74. e.OK(list, "查询成功")
  75. }
  76. // GetNewestRecordPage 获取冰排记录列表
  77. // @Summary 获取冰排列表
  78. // @Description 获取冰排列表
  79. // @Tags 冰排
  80. // @Param name query string false "冰排名称"
  81. // @Param pageSize query int false "页条数"
  82. // @Param page query int false "页码"
  83. // @Success 200 {object} response.Response{data=response.Page{list=[]model.IceRaft}} "{"code": 200, "data": [...]}"
  84. // @Router /api/ice-raft [get]
  85. // @Security Bearer
  86. func (e IceRaftController) GetNewestRecordPage(c *gin.Context) {
  87. s := service.IceRaft{}
  88. req := dto.IceRaftGetNewestRecordPageReq{}
  89. err := e.MakeContext(c).
  90. MakeOrm().
  91. Bind(&req, binding.Query).
  92. MakeService(&s.Service).
  93. Errors
  94. if err != nil {
  95. e.Logger.Error(err)
  96. e.Error(500, err, err.Error())
  97. return
  98. }
  99. //数据权限检查
  100. p := actions.GetPermissionFromContext(c)
  101. list := make([]model.IceRaft, 0)
  102. var count int64
  103. err = s.GetRecordPage(&req, &list, &count, p)
  104. if err != nil {
  105. e.Error(500, err, err.Error())
  106. return
  107. }
  108. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  109. }
  110. func (e IceRaftController) GetNewestRecordPageSSE(c *gin.Context) {
  111. s := service.IceRaft{}
  112. req := dto.IceRaftGetNewestRecordPageReq{}
  113. err := e.MakeContext(c).
  114. MakeOrm().
  115. Bind(&req, binding.Query).
  116. MakeService(&s.Service).
  117. Errors
  118. if err != nil {
  119. e.Logger.Error(err)
  120. e.Error(500, err, err.Error())
  121. return
  122. }
  123. //数据权限检查
  124. p := actions.GetPermissionFromContext(c)
  125. list := make([]model.IceRaft, 0)
  126. var count int64
  127. // Set the response header to indicate SSE content type
  128. c.Header("Content-Type", "text/event-stream")
  129. c.Header("Cache-Control", "no-cache")
  130. c.Header("Connection", "keep-alive")
  131. // Create a channel to send events to the client
  132. println("Client connected")
  133. // Listen for client close and remove the client from the list
  134. notify := c.Writer.CloseNotify()
  135. go func() {
  136. <-notify
  137. fmt.Println("Client disconnected")
  138. }()
  139. type Page struct {
  140. Count int `json:"count"` //总数
  141. Page int `json:"page"` //页码
  142. PageSize int `json:"pageSize"` //页条数
  143. List interface{} `json:"list"`
  144. }
  145. type Response struct {
  146. RequestId string `protobuf:"bytes,1,opt,name=requestId,proto3" json:"requestId,omitempty"`
  147. Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
  148. Msg string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
  149. Status string `protobuf:"bytes,4,opt,name=status,proto3" json:"status,omitempty"`
  150. Data Page `json:"data"`
  151. }
  152. // Continuously send data to the client
  153. for {
  154. err = s.GetRecordPage(&req, &list, &count, p)
  155. if err != nil {
  156. e.Error(500, err, err.Error())
  157. return
  158. }
  159. var response Response
  160. var page Page
  161. response.Code = 200
  162. response.Msg = "查询成功"
  163. response.Msg = "查询成功"
  164. page.Count = int(count)
  165. page.Page = req.GetPageIndex()
  166. page.PageSize = req.GetPageSize()
  167. page.List = list
  168. response.Data = page
  169. //data := <-eventChan
  170. res, _ := json.Marshal(&response)
  171. fmt.Fprintf(c.Writer, "data: %s\n\n", string(res))
  172. c.Writer.Flush()
  173. time.Sleep(10 * time.Second)
  174. }
  175. }
  176. // Get 通过id获取冰排
  177. // @Summary 通过id获取冰排
  178. // @Description 通过id获取冰排
  179. // @Tags 冰排
  180. // @Param id path string true "冰排id"
  181. // @Success 200 {object} response.Response{data=model.IceRaft} "{"code": 200, "data": [...]}"
  182. // @Router /api/ice-raft/{id} [get]
  183. // @Security Bearer
  184. func (e IceRaftController) Get(c *gin.Context) {
  185. s := service.IceRaft{}
  186. req := dto.IceRaftGetReq{}
  187. err := e.MakeContext(c).
  188. MakeOrm().
  189. Bind(&req, nil).
  190. MakeService(&s.Service).
  191. Errors
  192. if err != nil {
  193. e.Logger.Error(err)
  194. e.Error(500, err, err.Error())
  195. return
  196. }
  197. var object model.IceRaft
  198. p := actions.GetPermissionFromContext(c)
  199. //数据权限检查
  200. err = s.Get(&req, &object, p)
  201. if err != nil {
  202. e.Error(500, err, err.Error())
  203. return
  204. }
  205. e.OK(object, "查询成功")
  206. }
  207. // GetByCode 通过code获取冰排
  208. // @Summary 通过code获取冰排
  209. // @Description 通过code获取冰排
  210. // @Tags 冰排
  211. // @Param id path string true "冰排id"
  212. // @Success 200 {object} response.Response{data=model.IceRaft} "{"code": 200, "data": [...]}"
  213. // @Router /api/ice-raft/code/{code} [get]
  214. // @Security Bearer
  215. func (e IceRaftController) GetByCode(c *gin.Context) {
  216. s := service.IceRaft{}
  217. req := dto.IceRaftGetByCodeReq{}
  218. err := e.MakeContext(c).
  219. MakeOrm().
  220. Bind(&req, nil).
  221. MakeService(&s.Service).
  222. Errors
  223. if err != nil {
  224. e.Logger.Error(err)
  225. e.Error(500, err, err.Error())
  226. return
  227. }
  228. var object model.IceRaft
  229. p := actions.GetPermissionFromContext(c)
  230. //数据权限检查
  231. err = s.GetByCode(&req, &object, p)
  232. if err != nil {
  233. e.Error(500, err, err.Error())
  234. return
  235. }
  236. e.OK(object, "查询成功")
  237. }
  238. // Insert 添加冰排
  239. // @Summary 添加冰排
  240. // @Description 添加冰排
  241. // @Tags 冰排
  242. // @Accept application/json
  243. // @Product application/json
  244. // @Param data body dto.IceRaftInsertReq true "data"
  245. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  246. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  247. // @Router /api/ice-raft [post]
  248. // @Security Bearer
  249. func (e IceRaftController) Insert(c *gin.Context) {
  250. s := service.IceRaft{}
  251. req := dto.IceRaftInsertReq{}
  252. err := e.MakeContext(c).
  253. MakeOrm().
  254. Bind(&req, binding.JSON).
  255. MakeService(&s.Service).
  256. Errors
  257. if err != nil {
  258. e.Logger.Error(err)
  259. e.Error(500, err, err.Error())
  260. return
  261. }
  262. p := actions.GetPermissionFromContext(c)
  263. // 设置创建人
  264. req.SetCreateBy(user.GetUserId(c))
  265. req.SetDeptId(p.DeptId)
  266. err = s.Insert(&req)
  267. if err != nil {
  268. e.Error(500, err, err.Error())
  269. return
  270. }
  271. e.OK(req.GetId(), "添加成功")
  272. }
  273. // Update 修改冰排
  274. // @Summary 修改冰排
  275. // @Description 修改冰排
  276. // @Tags 冰排
  277. // @Accept application/json
  278. // @Product application/json
  279. // @Param data body dto.IceRaftUpdateReq true "body"
  280. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  281. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  282. // @Router /api/ice-raft [put]
  283. // @Security Bearer
  284. func (e IceRaftController) Update(c *gin.Context) {
  285. s := service.IceRaft{}
  286. req := dto.IceRaftUpdateReq{}
  287. err := e.MakeContext(c).
  288. MakeOrm().
  289. Bind(&req).
  290. MakeService(&s.Service).
  291. Errors
  292. if err != nil {
  293. e.Logger.Error(err)
  294. e.Error(500, err, err.Error())
  295. return
  296. }
  297. p := actions.GetPermissionFromContext(c)
  298. req.SetUpdateBy(user.GetUserId(c))
  299. err = s.Update(&req, p)
  300. if err != nil {
  301. e.Error(500, err, err.Error())
  302. return
  303. }
  304. e.OK(req.GetId(), "修改成功")
  305. }
  306. // Delete 删除冰排
  307. // @Summary 删除冰排
  308. // @Description 删除冰排
  309. // @Tags 冰排
  310. // @Accept application/json
  311. // @Product application/json
  312. // @Param data body dto.IceRaftDeleteReq true "body"
  313. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  314. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  315. // @Router /api/ice-raft [delete]
  316. // @Security Bearer
  317. func (e IceRaftController) Delete(c *gin.Context) {
  318. s := service.IceRaft{}
  319. req := dto.IceRaftDeleteReq{}
  320. err := e.MakeContext(c).
  321. MakeOrm().
  322. Bind(&req, binding.JSON, nil).
  323. MakeService(&s.Service).
  324. Errors
  325. if err != nil {
  326. e.Logger.Error(err)
  327. e.Error(500, err, err.Error())
  328. return
  329. }
  330. //数据权限检查
  331. p := actions.GetPermissionFromContext(c)
  332. err = s.Remove(&req, p)
  333. if err != nil {
  334. e.Error(500, err, err.Error())
  335. return
  336. }
  337. e.OK(req.GetId(), "删除成功")
  338. }
  339. // InStorage 冰排入库
  340. // @Summary 冰排入库
  341. // @Description 冰排入库
  342. // @Tags 冰排
  343. // @Accept application/json
  344. // @Product application/json
  345. // @Param data body dto.IceRaftInStorageReq true "body"
  346. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  347. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  348. // @Router /api/ice-raft [delete]
  349. // @Security Bearer
  350. func (e IceRaftController) InStorage(c *gin.Context) {
  351. s := service.IceRaft{}
  352. req := dto.IceRaftInStorageReq{}
  353. err := e.MakeContext(c).
  354. MakeOrm().
  355. Bind(&req, binding.JSON, nil).
  356. MakeService(&s.Service).
  357. Errors
  358. if err != nil {
  359. e.Logger.Error(err)
  360. e.Error(500, err, err.Error())
  361. return
  362. }
  363. //数据权限检查
  364. p := actions.GetPermissionFromContext(c)
  365. err = s.InStorage(&req, p)
  366. if err != nil {
  367. e.Error(500, err, err.Error())
  368. return
  369. }
  370. e.OK(nil, "入库成功")
  371. }
  372. // OutStorage 冰排出库
  373. // @Summary 冰排出库
  374. // @Description 冰排出库
  375. // @Tags 冰排
  376. // @Accept application/json
  377. // @Product application/json
  378. // @Param data body dto.IceRaftOutStorageReq true "body"
  379. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  380. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  381. // @Router /api/ice-raft [delete]
  382. // @Security Bearer
  383. func (e IceRaftController) OutStorage(c *gin.Context) {
  384. s := service.IceRaft{}
  385. req := dto.IceRaftOutStorageReq{}
  386. err := e.MakeContext(c).
  387. MakeOrm().
  388. Bind(&req, binding.JSON, nil).
  389. MakeService(&s.Service).
  390. Errors
  391. if err != nil {
  392. e.Logger.Error(err)
  393. e.Error(500, err, err.Error())
  394. return
  395. }
  396. //数据权限检查
  397. p := actions.GetPermissionFromContext(c)
  398. err = s.OutStorage(&req, p)
  399. if err != nil {
  400. e.Error(500, err, err.Error())
  401. return
  402. }
  403. e.OK(nil, "出库成功")
  404. }
  405. type IceRaftRecordController struct {
  406. api.Api
  407. }
  408. // GetPage 获取冰排历史记记录列表
  409. // @Summary 获取冰排列表
  410. // @Description 获取冰排列表
  411. // @Tags 冰排
  412. // @Param name query string false "冰排名称"
  413. // @Param pageSize query int false "页条数"
  414. // @Param page query int false "页码"
  415. // @Success 200 {object} response.Response{data=response.Page{list=[]model.IceRaft}} "{"code": 200, "data": [...]}"
  416. // @Router /api/ice-raft [get]
  417. // @Security Bearer
  418. func (e IceRaftRecordController) GetPage(c *gin.Context) {
  419. s := service.IceRaftRecord{}
  420. req := dto.IceRaftRecordGetPageReq{}
  421. err := e.MakeContext(c).
  422. MakeOrm().
  423. Bind(&req, binding.Query).
  424. MakeService(&s.Service).
  425. Errors
  426. if err != nil {
  427. e.Logger.Error(err)
  428. e.Error(500, err, err.Error())
  429. return
  430. }
  431. //数据权限检查
  432. p := actions.GetPermissionFromContext(c)
  433. list := make([]model.IceRaftRecord, 0)
  434. var count int64
  435. err = s.GetPage(&req, &list, &count, p)
  436. if err != nil {
  437. e.Error(500, err, err.Error())
  438. return
  439. }
  440. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  441. }
  442. // Update 修改冰排出入库历史记录
  443. // @Summary 修改冰排出入库历史记录
  444. // @Description 修改冰排出入库历史记录
  445. // @Tags 冰排
  446. // @Accept application/json
  447. // @Product application/json
  448. // @Param data body dto.IceRaftUpdateReq true "body"
  449. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  450. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  451. // @Router /api/ice-raft [put]
  452. // @Security Bearer
  453. func (e IceRaftRecordController) Update(c *gin.Context) {
  454. s := service.IceRaftRecord{}
  455. req := dto.IceRaftRecordUpdateReq{}
  456. err := e.MakeContext(c).
  457. MakeOrm().
  458. Bind(&req).
  459. MakeService(&s.Service).
  460. Errors
  461. if err != nil {
  462. e.Logger.Error(err)
  463. e.Error(500, err, err.Error())
  464. return
  465. }
  466. p := actions.GetPermissionFromContext(c)
  467. req.SetUpdateBy(user.GetUserId(c))
  468. err = s.Update(&req, p)
  469. if err != nil {
  470. e.Error(500, err, err.Error())
  471. return
  472. }
  473. e.OK(req.GetId(), "修改成功")
  474. }
  475. // Delete 删除冰排出入库历史记录
  476. // @Summary 删除冰排出入库历史记录
  477. // @Description 删除冰排出入库历史记录
  478. // @Tags 冰排
  479. // @Accept application/json
  480. // @Product application/json
  481. // @Param data body dto.IceRaftDeleteReq true "body"
  482. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  483. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  484. // @Router /api/ice-raft [delete]
  485. // @Security Bearer
  486. func (e IceRaftRecordController) Delete(c *gin.Context) {
  487. s := service.IceRaftRecord{}
  488. req := dto.IceRaftRecordDeleteReq{}
  489. err := e.MakeContext(c).
  490. MakeOrm().
  491. Bind(&req, binding.JSON, nil).
  492. MakeService(&s.Service).
  493. Errors
  494. if err != nil {
  495. e.Logger.Error(err)
  496. e.Error(500, err, err.Error())
  497. return
  498. }
  499. //数据权限检查
  500. p := actions.GetPermissionFromContext(c)
  501. err = s.Remove(&req, p)
  502. if err != nil {
  503. e.Error(500, err, err.Error())
  504. return
  505. }
  506. e.OK(req.GetId(), "删除成功")
  507. }