gas_cylinder.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package controller
  2. import (
  3. "errors"
  4. "gas-cylinder-api/app/admin/model"
  5. "gas-cylinder-api/app/admin/service"
  6. "gas-cylinder-api/app/admin/service/dto"
  7. "gas-cylinder-api/common/actions"
  8. "gas-cylinder-api/common/global"
  9. "github.com/gin-gonic/gin"
  10. "github.com/gin-gonic/gin/binding"
  11. "github.com/xuri/excelize/v2"
  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. "net/url"
  16. "os"
  17. "path"
  18. "strings"
  19. )
  20. type GasCylinderController struct {
  21. api.Api
  22. }
  23. // GetPage 获取钢瓶档案列表
  24. // @Summary 获取钢瓶档案列表
  25. // @Description 获取钢瓶档案列表
  26. // @Tags 钢瓶档案
  27. // @Param innerCode query string false "单位内编码"
  28. // @Param pageSize query int false "页条数"
  29. // @Param page query int false "页码"
  30. // @Success 200 {object} response.Response{data=response.Page{list=[]model.GasCylinder}} "{"code": 200, "data": [...]}"
  31. // @Router /api/gas-cylinder [get]
  32. // @Security Bearer
  33. func (e GasCylinderController) GetPage(c *gin.Context) {
  34. s := service.GasCylinder{}
  35. req := dto.GasCylinderGetPageReq{}
  36. err := e.MakeContext(c).
  37. MakeOrm().
  38. Bind(&req, binding.Query).
  39. MakeService(&s.Service).
  40. Errors
  41. if err != nil {
  42. e.Logger.Error(err)
  43. e.Error(500, err, err.Error())
  44. return
  45. }
  46. //数据权限检查
  47. p := actions.GetPermissionFromContext(c)
  48. list := make([]model.GasCylinder, 0)
  49. var count int64
  50. err = s.GetPage(&req, &list, &count, p)
  51. if err != nil {
  52. e.Error(500, err, err.Error())
  53. return
  54. }
  55. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  56. }
  57. // Get 通过id获取钢瓶档案
  58. // @Summary 通过id获取钢瓶档案
  59. // @Description 通过id获取钢瓶档案
  60. // @Tags 钢瓶档案
  61. // @Param id path path true "钢瓶档案id"
  62. // @Success 200 {object} response.Response{data=model.GasCylinder} "{"code": 200, "data": [...]}"
  63. // @Router /api/gas-cylinder/{id} [get]
  64. // @Security Bearer
  65. func (e GasCylinderController) Get(c *gin.Context) {
  66. s := service.GasCylinder{}
  67. req := dto.GasCylinderGetReq{}
  68. err := e.MakeContext(c).
  69. MakeOrm().
  70. Bind(&req, nil).
  71. MakeService(&s.Service).
  72. Errors
  73. if err != nil {
  74. e.Logger.Error(err)
  75. e.Error(500, err, err.Error())
  76. return
  77. }
  78. var object model.GasCylinder
  79. p := actions.GetPermissionFromContext(c)
  80. //数据权限检查
  81. err = s.Get(&req, &object, p)
  82. if err != nil {
  83. if errors.Is(err, global.GetNotFoundOrNoPermissionErr) {
  84. e.Error(400, err, "钢瓶不存在,请在钢瓶档案添加钢瓶信息!")
  85. return
  86. }
  87. e.Error(500, err, err.Error())
  88. return
  89. }
  90. e.OK(object, "查询成功")
  91. }
  92. // GetByUid 通过高频编码获取钢瓶信息
  93. // @Summary 通过高频编码获取钢瓶信息
  94. // @Description 通过高频编码获取钢瓶信息
  95. // @Tags 钢瓶档案
  96. // @Param chipUid path string true "高频编码"
  97. // @Success 200 {object} response.Response{data=model.GasCylinder} "{"code": 200, "data": [...]}"
  98. // @Router /api/gas-cylinder/uid/{chipUid} [get]
  99. // @Security Bearer
  100. func (e GasCylinderController) GetByUid(c *gin.Context) {
  101. s := service.GasCylinder{}
  102. operationLogSvc := service.OperationLog{}
  103. req := dto.GasCylinderGetByUidReq{}
  104. err := e.MakeContext(c).
  105. MakeOrm().
  106. Bind(&req, nil).
  107. MakeService(&s.Service).
  108. MakeService(&operationLogSvc.Service).
  109. Errors
  110. if err != nil {
  111. e.Logger.Error(err)
  112. e.Error(500, err, err.Error())
  113. return
  114. }
  115. var gasCylinder model.GasCylinder
  116. //数据权限检查
  117. err = s.GetByUid(&req, &gasCylinder, nil)
  118. if err != nil {
  119. e.Error(500, err, err.Error())
  120. return
  121. }
  122. operationLogList := make([]model.OperationLog, 0)
  123. err = operationLogSvc.ListByInnerCode(gasCylinder.InnerCode, &operationLogList)
  124. if err != nil {
  125. e.Error(500, err, err.Error())
  126. return
  127. }
  128. e.OK(map[string]interface{}{
  129. "gasCylinder": gasCylinder,
  130. "operationLog": operationLogList,
  131. }, "查询成功")
  132. }
  133. // Mock 模拟气瓶数据
  134. // @Summary 模拟气瓶数据
  135. // @Description 模拟气瓶数据
  136. // @Tags 钢瓶档案
  137. // @Param data body dto.GasCylinderMockReq true "data"
  138. // @Success 200 {object} response.Response{data=model.GasCylinder} "{"code": 200, "data": [...]}"
  139. // @Router /api/gas-cylinder/mock [get]
  140. // @Security Bearer
  141. func (e GasCylinderController) Mock(c *gin.Context) {
  142. s := service.GasCylinder{}
  143. req := dto.GasCylinderMockReq{}
  144. err := e.MakeContext(c).
  145. MakeOrm().
  146. Bind(&req, binding.JSON).
  147. MakeService(&s.Service).
  148. Errors
  149. if err != nil {
  150. e.Logger.Error(err)
  151. e.Error(500, err, err.Error())
  152. return
  153. }
  154. //数据权限检查
  155. err = s.Mock(&req)
  156. if err != nil {
  157. e.Error(500, err, err.Error())
  158. return
  159. }
  160. e.OK(nil, "生成成功")
  161. }
  162. // Insert 添加钢瓶
  163. // @Summary 添加钢瓶
  164. // @Description 添加钢瓶
  165. // @Tags 钢瓶
  166. // @Accept application/json
  167. // @Product application/json
  168. // @Param data body dto.GasCylinderInsertReq true "data"
  169. // @Param pageSize query int false "页条数"
  170. // @Param page query int false "页码"
  171. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  172. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  173. // @Router /api/gas-cylinder [post]
  174. // @Security Bearer
  175. func (e GasCylinderController) Insert(c *gin.Context) {
  176. s := service.GasCylinder{}
  177. req := dto.GasCylinderInsertReq{}
  178. err := e.MakeContext(c).
  179. MakeOrm().
  180. Bind(&req, binding.JSON).
  181. MakeService(&s.Service).
  182. Errors
  183. if err != nil {
  184. e.Logger.Error(err)
  185. e.Error(500, err, err.Error())
  186. return
  187. }
  188. p := actions.GetPermissionFromContext(c)
  189. // 设置创建人
  190. req.SetCreateBy(user.GetUserId(c))
  191. req.SetDeptId(p.DeptId)
  192. err = s.Insert(&req)
  193. if err != nil {
  194. e.Error(500, err, err.Error())
  195. return
  196. }
  197. e.OK(req.GetId(), "创建成功")
  198. }
  199. // Update 修改钢瓶
  200. // @Summary 修改钢瓶
  201. // @Description 修改钢瓶
  202. // @Tags 钢瓶
  203. // @Accept application/json
  204. // @Product application/json
  205. // @Param data body dto.GasCylinderUpdateReq true "body"
  206. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  207. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  208. // @Router /api/dispatch-cost [put]
  209. // @Security Bearer
  210. func (e GasCylinderController) Update(c *gin.Context) {
  211. s := service.GasCylinder{}
  212. req := dto.GasCylinderUpdateReq{}
  213. err := e.MakeContext(c).
  214. MakeOrm().
  215. Bind(&req).
  216. MakeService(&s.Service).
  217. Errors
  218. if err != nil {
  219. e.Logger.Error(err)
  220. e.Error(500, err, err.Error())
  221. return
  222. }
  223. p := actions.GetPermissionFromContext(c)
  224. req.SetUpdateBy(user.GetUserId(c))
  225. err = s.Update(&req, p)
  226. if err != nil {
  227. e.Error(500, err, err.Error())
  228. return
  229. }
  230. e.OK(req.GetId(), "更新成功")
  231. }
  232. // Delete 删除钢瓶
  233. // @Summary 删除钢瓶
  234. // @Description 删除钢瓶
  235. // @Tags 钢瓶
  236. // @Accept application/json
  237. // @Product application/json
  238. // @Param data body dto.GasCylinderDeleteReq true "body"
  239. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  240. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  241. // @Router /api/dispatch-cost [delete]
  242. // @Security Bearer
  243. func (e GasCylinderController) Delete(c *gin.Context) {
  244. s := service.GasCylinder{}
  245. req := dto.GasCylinderDeleteReq{}
  246. userSvc := service.SysUser{}
  247. err := e.MakeContext(c).
  248. MakeOrm().
  249. Bind(&req, binding.JSON, nil).
  250. MakeService(&s.Service).
  251. MakeService(&userSvc.Service).
  252. Errors
  253. if err != nil {
  254. e.Logger.Error(err)
  255. e.Error(500, err, err.Error())
  256. return
  257. }
  258. //数据权限检查
  259. p := actions.GetPermissionFromContext(c)
  260. err = s.Remove(&req, p)
  261. if err != nil {
  262. e.Error(500, err, err.Error())
  263. return
  264. }
  265. e.OK(req.GetId(), "删除成功")
  266. }
  267. // Import 添加钢瓶
  268. // @Summary 添加钢瓶
  269. // @Description 添加钢瓶
  270. // @Tags 钢瓶
  271. // @Accept application/json
  272. // @Product application/json
  273. // @Param data body dto.GasCylinderInsertReq true "data"
  274. // @Param pageSize query int false "页条数"
  275. // @Param page query int false "页码"
  276. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  277. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  278. // @Router /api/gas-cylinder [post]
  279. // @Security Bearer
  280. func (e GasCylinderController) Import(c *gin.Context) {
  281. s := service.GasCylinder{}
  282. err := e.MakeContext(c).
  283. MakeOrm().
  284. MakeService(&s.Service).
  285. Errors
  286. if err != nil {
  287. e.Logger.Error(err)
  288. e.Error(500, err, err.Error())
  289. return
  290. }
  291. p := actions.GetPermissionFromContext(c)
  292. // 获取上传的文件
  293. file, err := c.FormFile("file")
  294. if err != nil {
  295. e.Logger.Error(err)
  296. e.Error(500, err, err.Error())
  297. return
  298. }
  299. // 打开上传的文件
  300. f, err := file.Open()
  301. if err != nil {
  302. e.Logger.Error(err)
  303. e.Error(500, err, err.Error())
  304. return
  305. }
  306. defer f.Close()
  307. // 解析Excel文件
  308. excelFile, err := excelize.OpenReader(f)
  309. if err != nil {
  310. e.Logger.Error(err)
  311. e.Error(500, err, err.Error())
  312. return
  313. }
  314. // 读取第一个Sheet
  315. rows, err := excelFile.GetRows(excelFile.GetSheetName(0))
  316. if err != nil {
  317. e.Logger.Error(err)
  318. e.Error(500, err, err.Error())
  319. return
  320. }
  321. // 遍历行数据并插入数据库
  322. for i, row := range rows {
  323. if i == 0 {
  324. continue
  325. }
  326. if len(row) < 13 {
  327. continue // 跳过不完整的行
  328. }
  329. r := dto.GasCylinderInsertReq{
  330. InnerCode: strings.TrimSpace(row[0]), // 单位内编码
  331. Uid: strings.TrimSpace(row[1]), // 高频编码
  332. Status: strings.TrimSpace(row[2]), // 高频编码
  333. ProVariety: strings.TrimSpace(row[3]), // 设备品种
  334. ProName: strings.TrimSpace(row[4]), // 产品名称
  335. ProNo: strings.TrimSpace(row[5]), // 气瓶生产编号
  336. FillMedia: strings.TrimSpace(row[6]), // 充装介质
  337. MakeUnit: strings.TrimSpace(row[7]), // 制造单位
  338. MakeTime: strings.TrimSpace(row[8]), // 生产日期
  339. WorkPressure: strings.TrimSpace(row[9]), // 公称工作压口(MPa)
  340. Volume: strings.TrimSpace(row[10]), // 容积(L)
  341. CheckTime: strings.TrimSpace(row[11]), // 最近一次检验日期
  342. NextCheckTime: strings.TrimSpace(row[12]), // 下次检验日期
  343. ProUuid: strings.TrimSpace(row[13]), // 产品唯一性编码
  344. }
  345. // 设置创建人
  346. r.SetCreateBy(user.GetUserId(c))
  347. r.SetDeptId(p.DeptId)
  348. err = s.Insert(&r)
  349. if err != nil {
  350. e.Error(500, err, err.Error())
  351. return
  352. }
  353. }
  354. e.OK(nil, "导入成功")
  355. }
  356. // ExportTemplate 导出钢瓶档案模板
  357. // @Summary 导出钢瓶档案模板
  358. // @Description 导出钢瓶档案模板
  359. // @Tags 运单
  360. // @Accept application/json
  361. // @Product application/json
  362. // @Param data body dto.WaybillInsertReq true "data"
  363. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  364. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  365. // @Router /api/waybill/export-template [post]
  366. // @Security Bearer
  367. func (e GasCylinderController) ExportTemplate(c *gin.Context) {
  368. s := service.GasCylinder{}
  369. err := e.MakeContext(c).
  370. MakeOrm().
  371. MakeService(&s.Service).
  372. Errors
  373. if err != nil {
  374. e.Logger.Error(err)
  375. e.Error(500, err, err.Error())
  376. return
  377. }
  378. filePath := "./ofile/钢瓶档案导入模板.xlsx"
  379. //打开文件
  380. fileTmp, errByOpenFile := os.Open(filePath)
  381. defer fileTmp.Close()
  382. //获取文件的名称
  383. fileName := path.Base(filePath)
  384. if errByOpenFile != nil {
  385. e.Logger.Error(err)
  386. e.Error(500, err, err.Error())
  387. return
  388. }
  389. c.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
  390. // PathEscape 函数对中文做处理
  391. c.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fileName))
  392. c.Header("Content-Transfer-Encoding", "binary")
  393. c.File(filePath)
  394. }
  395. func (e GasCylinderController) Status(c *gin.Context) {
  396. s := service.GasCylinder{}
  397. err := e.MakeContext(c).
  398. MakeOrm().
  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. var object []string
  407. p := actions.GetPermissionFromContext(c)
  408. //数据权限检查
  409. err = s.GetStatus(&object, p)
  410. if err != nil {
  411. if errors.Is(err, global.GetNotFoundOrNoPermissionErr) {
  412. e.Error(400, err, "钢瓶不存在,请在钢瓶档案添加钢瓶信息!")
  413. return
  414. }
  415. e.Error(500, err, err.Error())
  416. return
  417. }
  418. e.OK(object, "查询成功")
  419. }