/* * @description: rpc - 安全巡检 * @Author: LanJianRong * @Date: 2020-11-18 * @FilePath: \construction_management\web\api\safe_api.go */ package api import ( "fmt" "time" "github.com/kataras/iris/v12" "go.mod/conf" "go.mod/models" "go.mod/services" "go.mod/web/utils" "go.mod/web/viewmodels" ) type SafeApi struct { //框架-web应用上下文环境 Ctx iris.Context // 需要用的service ServiceSafe services.SafeService } // @Summary 安全巡检列表 // @Tags 安全巡检 // @Description 获得列表数据 // @Accept json // @Produce json // @Security ApiKeyAuth // @Param bidsectionId path string true "标段ID" // @Param pageNo path int true "页码" eg:1 // @Param pageSize path int true "页数" eg:15 // @Success 200 {object} viewmodels.Safe "{code:0成功,data:viewmodels.Safe,msg:}" // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}" // @Router /api/safe [get] func (c *SafeApi) Get() { // 1.规则验证 safeData, err := c.ServiceSafe.ValidRule(c.Ctx) if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)}) return } pid, err := utils.GetProjectId(c.Ctx) if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": "项目id不存在, 请重新登录"}) return } bidsectionId, err := utils.GetDecryptId(safeData.BidsectionId) if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"}) return } // c.ServiceSafe.Get(bidsectionId, RpcConnect) SafeData, total := c.ServiceSafe.Get(bidsectionId, pid, safeData.PageNo, safeData.PageSize) c.Ctx.JSON(iris.Map{ "code": 0, "msg": "请求成功", "data": SafeData, "total": total, }) } // @Summary 创建新的安全巡检记录 // @Tags 安全巡检 // @Description 创建新的安全巡检记录 // @Accept json // @Produce json // @Security ApiKeyAuth // @Param bidsectionId body string true "标段ID" // @Param code body string true "编号" // @Param createTime body string true "日期" // @Param inspection body string true "检查部位" // @Param position body string true "部位" // @Success 200 {object} viewmodels.Safe "{code:0成功,msg:}" // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}" // @Router /api/safe [post] func (c *SafeApi) Post() { // 1.规则验证 safeData, err := c.ServiceSafe.ValidRule(c.Ctx) // fmt.Println("-------------------------------", err) if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)}) return } bidsectionId, err := utils.GetDecryptId(safeData.BidsectionId) if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"}) return } data := models.CmSafe{} data.BidsectionId = bidsectionId data.Code = safeData.Code data.Inspection = safeData.Inspection data.Times = 1 uid, err := utils.GetProjectAccountId(c.Ctx) if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": "未登录或账号失效,请重新登录"}) return } data.Uid = uid createTime, err := time.Parse(conf.SysTimeform, safeData.CreateTime) if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": "日期转换异常,请检查参数"}) return } // fmt.Println(createTime) data.CreateTime = createTime err = c.ServiceSafe.Post(data) if err != nil { c.Ctx.JSON(iris.Map{ "code": -1, "msg": fmt.Sprintf("%s", err), }) return } c.Ctx.JSON(iris.Map{ "code": 0, "msg": "插入成功", }) } // @Summary 删除记录 // @Tags 安全巡检 // @Description 删除安全巡检记录 // @Accept json // @Produce json // @Security ApiKeyAuth // @Param id body string true "安全巡检ID" // @Success 200 {object} viewmodels.Safe "{code:0成功,msg:}" // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}" // @Router /api/safe [delete] func (c *SafeApi) Delete() { queryId := c.Ctx.URLParam("id") if queryId == "" { c.Ctx.JSON(iris.Map{"code": -1, "msg": "id不存在"}) return } id, err := utils.GetDecryptId(queryId) if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"}) return } err = c.ServiceSafe.Del(id) if err != nil { c.Ctx.JSON(iris.Map{ "code": -1, "msg": fmt.Sprintf("%s", err), }) return } c.Ctx.JSON(iris.Map{ "code": 0, "msg": "删除成功", }) } // @Summary 获取安全巡检详情 // @Tags 安全巡检 // @Description 获得安全巡检详情页面数据 // @Accept json // @Produce json // @Security ApiKeyAuth // @Param id path string true "巡检ID" // @Success 200 {object} viewmodels.SafeDetail "{code:0成功,data:viewmodels.SafeDetail,msg:}" // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}" // @Router /api/safe/detail [get] func (c *SafeApi) GetDetail() { // 1.规则验证 safeData, err := c.ServiceSafe.ValidRule(c.Ctx) if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)}) return } id, err := utils.GetDecryptId(safeData.Id) if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadForm转换异常,请检查参数"}) return } pid, _ := utils.GetProjectId(c.Ctx) SafeData := c.ServiceSafe.GetDetail(id, pid) c.Ctx.JSON(iris.Map{ "code": 0, "msg": "请求成功", "data": SafeData, }) } // @Summary 安全概述 // @Tags 安全巡检 // @Description 安全概述 // @Accept json // @Produce json // @Security ApiKeyAuth // @Param bidsectionId path string true "标段ID" // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,msg:错误信息}" // @Router /api/safe/survey [get] func (c *SafeApi) GetSurvey() { sectionData := viewmodels.Safe{} err := c.Ctx.ReadForm(§ionData) if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"}) return } err = sectionData.ValidateBidsectionId() if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)}) return } // 标段ID bidsectionId, err := utils.GetDecryptId(sectionData.BidsectionId) if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"}) return } // 项目ID projectId, err := utils.GetProjectId(c.Ctx) if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": err}) return } SafeData := c.ServiceSafe.GetSurvey(projectId, bidsectionId) c.Ctx.JSON(iris.Map{ "code": 0, "msg": "", "data": SafeData, }) }