package controller import ( "errors" "gas-cylinder-api/app/admin/model" "gas-cylinder-api/app/admin/service" "gas-cylinder-api/app/admin/service/dto" "gas-cylinder-api/common/actions" "gas-cylinder-api/common/global" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "github.com/xuri/excelize/v2" "gogs.baozhida.cn/zoie/OAuth-core/api" "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user" _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response" "net/url" "os" "path" "strings" ) type GasCylinderController struct { api.Api } // GetPage 获取钢瓶档案列表 // @Summary 获取钢瓶档案列表 // @Description 获取钢瓶档案列表 // @Tags 钢瓶档案 // @Param innerCode query string false "单位内编码" // @Param pageSize query int false "页条数" // @Param page query int false "页码" // @Success 200 {object} response.Response{data=response.Page{list=[]model.GasCylinder}} "{"code": 200, "data": [...]}" // @Router /api/gas-cylinder [get] // @Security Bearer func (e GasCylinderController) GetPage(c *gin.Context) { s := service.GasCylinder{} req := dto.GasCylinderGetPageReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.Query). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //数据权限检查 p := actions.GetPermissionFromContext(c) list := make([]model.GasCylinder, 0) var count int64 err = s.GetPage(&req, &list, &count, p) if err != nil { e.Error(500, err, err.Error()) return } e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功") } // Get 通过id获取钢瓶档案 // @Summary 通过id获取钢瓶档案 // @Description 通过id获取钢瓶档案 // @Tags 钢瓶档案 // @Param id path path true "钢瓶档案id" // @Success 200 {object} response.Response{data=model.GasCylinder} "{"code": 200, "data": [...]}" // @Router /api/gas-cylinder/{id} [get] // @Security Bearer func (e GasCylinderController) Get(c *gin.Context) { s := service.GasCylinder{} req := dto.GasCylinderGetReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, nil). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } var object model.GasCylinder p := actions.GetPermissionFromContext(c) //数据权限检查 err = s.Get(&req, &object, p) if err != nil { if errors.Is(err, global.GetNotFoundOrNoPermissionErr) { e.Error(400, err, "钢瓶不存在,请在钢瓶档案添加钢瓶信息!") return } e.Error(500, err, err.Error()) return } e.OK(object, "查询成功") } // GetByUid 通过高频编码获取钢瓶信息 // @Summary 通过高频编码获取钢瓶信息 // @Description 通过高频编码获取钢瓶信息 // @Tags 钢瓶档案 // @Param chipUid path string true "高频编码" // @Success 200 {object} response.Response{data=model.GasCylinder} "{"code": 200, "data": [...]}" // @Router /api/gas-cylinder/uid/{chipUid} [get] // @Security Bearer func (e GasCylinderController) GetByUid(c *gin.Context) { s := service.GasCylinder{} operationLogSvc := service.OperationLog{} req := dto.GasCylinderGetByUidReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, nil). MakeService(&s.Service). MakeService(&operationLogSvc.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } var gasCylinder model.GasCylinder //数据权限检查 err = s.GetByUid(&req, &gasCylinder, nil) if err != nil { e.Error(500, err, err.Error()) return } operationLogList := make([]model.OperationLog, 0) err = operationLogSvc.ListByInnerCode(gasCylinder.InnerCode, &operationLogList) if err != nil { e.Error(500, err, err.Error()) return } e.OK(map[string]interface{}{ "gasCylinder": gasCylinder, "operationLog": operationLogList, }, "查询成功") } // Mock 模拟气瓶数据 // @Summary 模拟气瓶数据 // @Description 模拟气瓶数据 // @Tags 钢瓶档案 // @Param data body dto.GasCylinderMockReq true "data" // @Success 200 {object} response.Response{data=model.GasCylinder} "{"code": 200, "data": [...]}" // @Router /api/gas-cylinder/mock [get] // @Security Bearer func (e GasCylinderController) Mock(c *gin.Context) { s := service.GasCylinder{} req := dto.GasCylinderMockReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.JSON). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //数据权限检查 err = s.Mock(&req) if err != nil { e.Error(500, err, err.Error()) return } e.OK(nil, "生成成功") } // Insert 添加钢瓶 // @Summary 添加钢瓶 // @Description 添加钢瓶 // @Tags 钢瓶 // @Accept application/json // @Product application/json // @Param data body dto.GasCylinderInsertReq true "data" // @Param pageSize query int false "页条数" // @Param page query int false "页码" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/gas-cylinder [post] // @Security Bearer func (e GasCylinderController) Insert(c *gin.Context) { s := service.GasCylinder{} req := dto.GasCylinderInsertReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.JSON). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } p := actions.GetPermissionFromContext(c) // 设置创建人 req.SetCreateBy(user.GetUserId(c)) req.SetDeptId(p.DeptId) err = s.Insert(&req) if err != nil { e.Error(500, err, err.Error()) return } e.OK(req.GetId(), "创建成功") } // Update 修改钢瓶 // @Summary 修改钢瓶 // @Description 修改钢瓶 // @Tags 钢瓶 // @Accept application/json // @Product application/json // @Param data body dto.GasCylinderUpdateReq true "body" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/dispatch-cost [put] // @Security Bearer func (e GasCylinderController) Update(c *gin.Context) { s := service.GasCylinder{} req := dto.GasCylinderUpdateReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } p := actions.GetPermissionFromContext(c) req.SetUpdateBy(user.GetUserId(c)) err = s.Update(&req, p) if err != nil { e.Error(500, err, err.Error()) return } e.OK(req.GetId(), "更新成功") } // Delete 删除钢瓶 // @Summary 删除钢瓶 // @Description 删除钢瓶 // @Tags 钢瓶 // @Accept application/json // @Product application/json // @Param data body dto.GasCylinderDeleteReq true "body" // @Success 200 {string} string "{"code": 200, "message": "删除成功"}" // @Success 200 {string} string "{"code": -1, "message": "删除失败"}" // @Router /api/dispatch-cost [delete] // @Security Bearer func (e GasCylinderController) Delete(c *gin.Context) { s := service.GasCylinder{} req := dto.GasCylinderDeleteReq{} userSvc := service.SysUser{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.JSON, nil). MakeService(&s.Service). MakeService(&userSvc.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //数据权限检查 p := actions.GetPermissionFromContext(c) err = s.Remove(&req, p) if err != nil { e.Error(500, err, err.Error()) return } e.OK(req.GetId(), "删除成功") } // Import 添加钢瓶 // @Summary 添加钢瓶 // @Description 添加钢瓶 // @Tags 钢瓶 // @Accept application/json // @Product application/json // @Param data body dto.GasCylinderInsertReq true "data" // @Param pageSize query int false "页条数" // @Param page query int false "页码" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/gas-cylinder [post] // @Security Bearer func (e GasCylinderController) Import(c *gin.Context) { s := service.GasCylinder{} err := e.MakeContext(c). MakeOrm(). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } p := actions.GetPermissionFromContext(c) // 获取上传的文件 file, err := c.FormFile("file") if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } // 打开上传的文件 f, err := file.Open() if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } defer f.Close() // 解析Excel文件 excelFile, err := excelize.OpenReader(f) if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } // 读取第一个Sheet rows, err := excelFile.GetRows(excelFile.GetSheetName(0)) if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } // 遍历行数据并插入数据库 for i, row := range rows { if i == 0 { continue } if len(row) < 13 { continue // 跳过不完整的行 } r := dto.GasCylinderInsertReq{ InnerCode: strings.TrimSpace(row[0]), // 单位内编码 Uid: strings.TrimSpace(row[1]), // 高频编码 Status: strings.TrimSpace(row[2]), // 高频编码 ProVariety: strings.TrimSpace(row[3]), // 设备品种 ProName: strings.TrimSpace(row[4]), // 产品名称 ProNo: strings.TrimSpace(row[5]), // 气瓶生产编号 FillMedia: strings.TrimSpace(row[6]), // 充装介质 MakeUnit: strings.TrimSpace(row[7]), // 制造单位 MakeTime: strings.TrimSpace(row[8]), // 生产日期 WorkPressure: strings.TrimSpace(row[9]), // 公称工作压口(MPa) Volume: strings.TrimSpace(row[10]), // 容积(L) CheckTime: strings.TrimSpace(row[11]), // 最近一次检验日期 NextCheckTime: strings.TrimSpace(row[12]), // 下次检验日期 ProUuid: strings.TrimSpace(row[13]), // 产品唯一性编码 } // 设置创建人 r.SetCreateBy(user.GetUserId(c)) r.SetDeptId(p.DeptId) err = s.Insert(&r) if err != nil { e.Error(500, err, err.Error()) return } } e.OK(nil, "导入成功") } // ExportTemplate 导出钢瓶档案模板 // @Summary 导出钢瓶档案模板 // @Description 导出钢瓶档案模板 // @Tags 运单 // @Accept application/json // @Product application/json // @Param data body dto.WaybillInsertReq true "data" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/waybill/export-template [post] // @Security Bearer func (e GasCylinderController) ExportTemplate(c *gin.Context) { s := service.GasCylinder{} err := e.MakeContext(c). MakeOrm(). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } filePath := "./ofile/钢瓶档案导入模板.xlsx" //打开文件 fileTmp, errByOpenFile := os.Open(filePath) defer fileTmp.Close() //获取文件的名称 fileName := path.Base(filePath) if errByOpenFile != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } c.Header("Content-Type", "application/vnd.ms-excel;charset=utf8") // PathEscape 函数对中文做处理 c.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fileName)) c.Header("Content-Transfer-Encoding", "binary") c.File(filePath) } func (e GasCylinderController) Status(c *gin.Context) { s := service.GasCylinder{} err := e.MakeContext(c). MakeOrm(). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } var object []string p := actions.GetPermissionFromContext(c) //数据权限检查 err = s.GetStatus(&object, p) if err != nil { if errors.Is(err, global.GetNotFoundOrNoPermissionErr) { e.Error(400, err, "钢瓶不存在,请在钢瓶档案添加钢瓶信息!") return } e.Error(500, err, err.Error()) return } e.OK(object, "查询成功") }