package controller import ( "cold-delivery/app/admin/model" "cold-delivery/app/admin/service" "cold-delivery/app/admin/service/dto" "cold-delivery/common/actions" "cold-delivery/common/nats/nats_server" "encoding/json" "fmt" "github.com/beego/beego/v2/core/logs" "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" "sort" "time" ) type CoolerBoxController struct { api.Api } // GetPage 获取保温箱列表 // @Summary 获取保温箱列表 // @Description 获取保温箱列表 // @Tags 保温箱 // @Param name query string false "保温箱名称" // @Param sn query string false "保温箱关联sn" // @Param status query string false "状态:1-停用 2-启用" // @Param showTemp query bool false "是否展示最新温度" // @Param pageSize query int false "页条数" // @Param page query int false "页码" // @Success 200 {object} response.Response{data=response.Page{list=[]model.CoolerBox}} "{"code": 200, "data": [...]}" // @Router /api/cooler-box [get] // @Security Bearer func (e CoolerBoxController) GetPage(c *gin.Context) { s := service.CoolerBox{} req := dto.CoolerBoxGetPageReq{} 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.CoolerBox, 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 string true "保温箱id" // @Success 200 {object} response.Response{data=model.CoolerBox} "{"code": 200, "data": [...]}" // @Router /api/cooler-box/{id} [get] // @Security Bearer func (e CoolerBoxController) Get(c *gin.Context) { s := service.CoolerBox{} req := dto.CoolerBoxGetReq{} 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.CoolerBox p := actions.GetPermissionFromContext(c) //数据权限检查 err = s.Get(&req, &object, p) if err != nil { e.Error(500, err, err.Error()) return } e.OK(object, "查询成功") } // Insert 添加保温箱 // @Summary 添加保温箱 // @Description 添加保温箱 // @Tags 保温箱 // @Accept application/json // @Product application/json // @Param data body dto.CoolerBoxInsertReq true "data" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/cooler-box [post] // @Security Bearer func (e CoolerBoxController) Insert(c *gin.Context) { s := service.CoolerBox{} req := dto.CoolerBoxInsertReq{} 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, p) 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.CoolerBoxUpdateReq true "body" // @Success 200 {string} string "{"code": 200, "message": "修改成功"}" // @Success 200 {string} string "{"code": -1, "message": "修改失败"}" // @Router /api/cooler-box [put] // @Security Bearer func (e CoolerBoxController) Update(c *gin.Context) { s := service.CoolerBox{} req := dto.CoolerBoxUpdateReq{} 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.CoolerBoxDeleteReq true "body" // @Success 200 {string} string "{"code": 200, "message": "删除成功"}" // @Success 200 {string} string "{"code": -1, "message": "删除失败"}" // @Router /api/cooler-box [delete] // @Security Bearer func (e CoolerBoxController) Delete(c *gin.Context) { s := service.CoolerBox{} req := dto.CoolerBoxDeleteReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.JSON, nil). MakeService(&s.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(), "删除成功") } // BatchInsert 批量添加保温箱 // @Summary 批量添加保温箱 // @Description 批量添加保温箱 // @Tags 保温箱 // @Accept application/json // @Product application/json // @Param data body dto.CoolerBoxBatchInsertReq true "data" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/cooler-box/import [post] // @Security Bearer func (e CoolerBoxController) BatchInsert(c *gin.Context) { s := service.CoolerBox{} req := dto.CoolerBoxBatchInsertReq{} 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.BatchInsert(&req) if err != nil { e.Error(500, err, err.Error()) return } e.OK(nil, "添加成功") } // GetCollerBoxLocus 获取保温箱历史 // @Summary 获取保温箱历史轨迹 // @Description 获取保温箱历史 // @Tags 保温箱 // @Accept application/json // @Product application/json // @Param data body dto.CoolerBoxBatchInsertReq true "data" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/cooler-box/locus [get] // @Security Bearer func (e CoolerBoxController) GetCollerBoxLocus(c *gin.Context) { s := service.CoolerBox{} req := dto.CoolerBoxBatchReqSN{} 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) history, err := s.GetHistory(&req, p) if err != nil { e.Error(500, err, err.Error()) return } count := req.PageZ / req.Page e.PageOK(history, int(count), req.Page, req.PageZ, "查询成功") } // GetCoolerBoxLocus 获取保温箱历史轨迹 // @Summary 获取保温箱历史轨迹 // @Description 获取保温箱历史轨迹 // @Tags 保温箱 // @Accept application/json // @Product application/json // @Param data body dto.CoolerBoxBatchInsertReq true "data" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/cooler-box/historylocus [post] // @Security Bearer func (e CoolerBoxController) GetCoolerBoxLocus(c *gin.Context) { s := service.CoolerBox{} req := dto.CoolerBoxBatchReq{} 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 } data, _, err := nats_server.Cold_ReadDeviceDataListBy_T_snidForLocus(req.SnId+","+req.T_id, req.StartTime, req.EndTime, req.Page, req.PageZ) if err != nil { e.Logger.Error("nats 获取轨迹信息失败:", err) fmt.Println("nats 获取轨迹信息失败: %s", err) e.Error(500, err, err.Error()) return } // 倒序排序 sort.Slice(data, func(i, j int) bool { if data[i].T_time < data[j].T_time { return true } return false }) e.OK(data, "查询成功") } // GetCoolerBoxNewestLocus 获取保温箱最新轨迹 // @Summary 获取保温箱最新轨迹 // @Description 获取保温箱最新轨迹 // @Tags 保温箱 // @Accept application/json // @Product application/json // @Param data body dto.CoolerBoxBatchInsertReq true "data" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/cooler-box/newlocus [post] // @Security Bearer func (e CoolerBoxController) GetCoolerBoxNewestLocus(c *gin.Context) { s := service.CoolerBox{} req := dto.CoolerBoxReq{} 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 } if len(req.T_sn) == 0 || req.T_sn == "" { e.Error(500, err, err.Error()) return } conn := true // 数据权限检查 // p := actions.GetPermissionFromContext(c) list := make([]nats_server.DeviceData_, 0) // 设置响应头 c.Header("Content-Type", "text/event-stream") c.Header("Cache-Control", "no-cache") c.Header("Connection", "keep-alive") // 监听客户端断开连接 notify := c.Writer.CloseNotify() type Response struct { RequestId string `protobuf:"bytes,1,opt,name=requestId,proto3" json:"requestId,omitempty"` Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"` Msg string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"` SN string `protobuf:"bytes,3,opt,name=sn,proto3" json:"sn,omitempty"` CoolerName string `protobuf:"bytes,4,opt,name=CoolerName,proto3" json:"CoolerName,omitempty"` Data []nats_server.DeviceData_ `json:"data"` } var response Response for conn { select { case <-notify: fmt.Println("断开连接") conn = false return default: err = s.GetNewLocus(&req, &list) if err != nil { e.Logger.Error(err) response.Code = 500 response.Msg = err.Error() response.Data = list response.SN = req.T_sn response.CoolerName = req.CoolerName res, _ := json.Marshal(&response) fmt.Fprintf(c.Writer, "data: %s\n\n", string(res)) c.Writer.Flush() time.Sleep(20 * time.Second) } else { response.Code = 200 response.Msg = "查询成功" response.Data = list response.SN = req.T_sn response.CoolerName = req.CoolerName res, _ := json.Marshal(&response) fmt.Fprintf(c.Writer, "data: %s\n\n", string(res)) c.Writer.Flush() time.Sleep(10 * time.Second) } } } } // GetCoolerBoxIce 获取指定保温箱下的所有冰排信息 // @Summary 获取指定保温箱下的所有冰排信息 // @Description 获取指定保温箱下的所有冰排信息 // @Tags 保温箱 // @Accept application/json // @Product application/json // @Param data body dto.CoolerBoxBatchInsertReq true "data" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/cooler-box/getcoolerboxice [post] // @Security Bearer func (e CoolerBoxController) GetCoolerBoxIce(c *gin.Context) { s := service.CoolerBox{} req := dto.GetCoolerBoxIce{} 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 } list := make([]model.IceRaftRecord, 0) var count int64 //数据权限检查 p := actions.GetPermissionFromContext(c) err = s.GetCoolerBoxIce(&req, &list, p, &count) if err != nil { e.Error(500, err, err.Error()) return } e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功") } // GetCoolerBoxAll 获取所有保温箱使用记录 // @Summary 获取所有保温箱使用记录 // @Description 获取所有保温箱使用记录 // @Tags 保温箱 // @Accept application/json // @Product application/json // @Param data body dto.CoolerBoxBatchInsertReq true "data" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/cooler-box/getcoolerboxall [get] // @Security Bearer func (e CoolerBoxController) GetCoolerBoxAll(c *gin.Context) { s := service.CoolerBox{} req := dto.GetCoolerBoxIceAll{} 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 } list := make([]model.CoolerBoxRecord, 0) var count int64 //数据权限检查 p := actions.GetPermissionFromContext(c) err = s.GetCoolerBoxIceAll(&req, &list, p, &count) if err != nil { e.Error(500, err, err.Error()) return } e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功") } // ExportExecl 导出保温箱使用记录 // @Summary 导出保温箱使用记录 // @Description 导出保温箱使用记录 // @Tags 保温箱 // @Accept application/json // @Product application/json // @Param data body dto.IceRaftRecordEedReq true "body" // @Success 200 {string} string "{"code": 200, "message": "提交成功"}" // @Success 200 {string} string "{"code": -1, "message": "提交成功"}" // @Router /api/export-execl [get] // @Security Bearer func (e CoolerBoxController) ExportExecl(c *gin.Context) { s := service.CoolerBox{} req := dto.GetCoolerBoxIceAll{} 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 } list := make([]model.CoolerBoxRecord, 0) var count int64 //数据权限检查 p := actions.GetPermissionFromContext(c) req.PageSize = 9999 req.Page = 1 err = s.GetCoolerBoxIceAll(&req, &list, p, &count) if err != nil { e.Error(500, err, err.Error()) return } f := excelize.NewFile() // 设置单元格的值 //获取当前登录用户信息,查询是否有权限 err, sysUser := service.GetUserProfile(e.Orm, p.UserId) if err != nil { e.Error(500, err, "获取用户信息失败") return } fmt.Println(sysUser) if sysUser.Dept.IsCoolerReleaseCold { // 这里设置表头ÒÒ f.SetCellValue("Sheet1", "A1", "序号") f.SetCellValue("Sheet1", "B1", "SN") f.SetCellValue("Sheet1", "C1", "预冷放入时间") f.SetCellValue("Sheet1", "D1", "预冷温度") f.SetCellValue("Sheet1", "E1", "预冷地点") f.SetCellValue("Sheet1", "F1", "预冷操作人员") f.SetCellValue("Sheet1", "G1", "使用日期") f.SetCellValue("Sheet1", "H1", "放置冰排编号") f.SetCellValue("Sheet1", "I1", "放置冰排数量") f.SetCellValue("Sheet1", "J1", "操作人员") f.SetCellValue("Sheet1", "K1", "备注") // 设置列宽 f.SetColWidth("Sheet1", "A", "A", 6) f.SetColWidth("Sheet1", "B", "B", 8) f.SetColWidth("Sheet1", "C", "C", 14) f.SetColWidth("Sheet1", "D", "D", 14) f.SetColWidth("Sheet1", "E", "E", 30) f.SetColWidth("Sheet1", "F", "F", 14) f.SetColWidth("Sheet1", "G", "G", 14) f.SetColWidth("Sheet1", "H", "H", 30) f.SetColWidth("Sheet1", "I", "J", 15) f.SetColWidth("Sheet1", "K", "K", 30) line := 1 // 循环写入数据 for i, v := range list { line++ f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1) f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.Sn) f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.CoolerBoxStarTime) f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.ColdTemperatures) f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.CoolerColdAddress) f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.CoolerColdUsers) f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.CoolerUserTime) f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.HistoryCode) f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), len(v.HistoryCode)) f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), v.CoolerUseUsers) f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), "") } } else { // 这里设置表头ÒÒ f.SetCellValue("Sheet1", "A1", "序号") f.SetCellValue("Sheet1", "B1", "SN") f.SetCellValue("Sheet1", "C1", "使用日期") f.SetCellValue("Sheet1", "D1", "放置冰排编号") f.SetCellValue("Sheet1", "E1", "放置冰排数量") f.SetCellValue("Sheet1", "F1", "操作人员") f.SetCellValue("Sheet1", "G1", "备注") // 设置列宽 f.SetColWidth("Sheet1", "A", "A", 6) f.SetColWidth("Sheet1", "B", "B", 8) f.SetColWidth("Sheet1", "C", "C", 14) f.SetColWidth("Sheet1", "D", "D", 14) f.SetColWidth("Sheet1", "E", "E", 30) f.SetColWidth("Sheet1", "F", "F", 14) f.SetColWidth("Sheet1", "G", "G", 14) line := 1 // 循环写入数据 for i, v := range list { line++ f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1) f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.Sn) f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.CoolerUserTime) f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.HistoryCode) f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), len(v.HistoryCode)) f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.CoolerUseUsers) f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), "") } } timeStr := time.Now().Format("20060102150405") filePath := "ofile/" + "保温箱使用记录表" + timeStr + ".xlsx" // 保存文件 if err = f.SaveAs(filePath); err != nil { logs.Error("文件失败:", err) } defer func() { os.Remove(filePath) }() c.Header("Content-Type", "application/vnd.ms-excel;charset=utf8") // PathEscape 函数对中文做处理 c.Header("Content-Disposition", "attachment; filename="+url.PathEscape("保温箱使用记录表"+timeStr+".xlsx")) c.Header("Content-Transfer-Encoding", "binary") c.File(filePath) } // CoolerBoxStarTime 保温箱开始遇冷 // @Summary 保温箱开始遇冷 // @Description 保温箱开始遇冷 // @Tags 冰排 // @Accept application/json // @Product application/json // @Param data body dto.IceRaftRecordEedReq true "body" // @Success 200 {string} string "{"code": 200, "message": "提交成功"}" // @Success 200 {string} string "{"code": -1, "message": "提交成功"}" // @Router /api/coolerboxstarttime [post] // @Security Bearer func (e CoolerBoxController) CoolerBoxStarTime(c *gin.Context) { s := service.CoolerBox{} req := dto.IceStartbleForColfTimReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.JSON, nil). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //数据权限检查 p := actions.GetPermissionFromContext(c) err = s.CoolerBoxStarTime(&req, p) if err != nil { e.Error(500, err, err.Error()) return } e.OK(req, "提交成功") }