123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- 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, "查询成功")
- }
|