safe_api.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * @description: rpc - 安全巡检
  3. * @Author: LanJianRong
  4. * @Date: 2020-11-18
  5. * @FilePath: \construction_management\web\api\safe_rpc_api.go
  6. */
  7. package api
  8. import (
  9. "fmt"
  10. "time"
  11. "github.com/kataras/iris/v12"
  12. "go.mod/conf"
  13. "go.mod/models"
  14. "go.mod/services"
  15. "go.mod/web/utils"
  16. )
  17. type SafeApi struct {
  18. //框架-web应用上下文环境
  19. Ctx iris.Context
  20. // 需要用的service
  21. ServiceSafe services.SafeService
  22. }
  23. // @Summary 安全巡检列表
  24. // @Tags 安全巡检
  25. // @Description 获得列表数据
  26. // @Accept json
  27. // @Produce json
  28. // @Security ApiKeyAuth
  29. // @Param bidsectionId path string true "标段ID"
  30. // @Param pageNo path int true "页码" eg:1
  31. // @Param pageSize path int true "页数" eg:15
  32. // @Success 200 {object} viewmodels.Safe "{code:0成功,data:viewmodels.Safe,msg:}"
  33. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  34. // @Router /api/safe [get]
  35. func (c *SafeApi) Get() {
  36. // 1.规则验证
  37. safeData, err := c.ServiceSafe.ValidRule(c.Ctx)
  38. if err != nil {
  39. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  40. return
  41. }
  42. pid, err := utils.GetProjectId(c.Ctx)
  43. if err != nil {
  44. c.Ctx.JSON(iris.Map{"code": -1, "msg": "项目id不存在, 请重新登录"})
  45. return
  46. }
  47. bidsectionId, err := utils.GetDecryptId(safeData.BidsectionId)
  48. if err != nil {
  49. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  50. return
  51. }
  52. // c.ServiceSafe.Get(bidsectionId, RpcConnect)
  53. SafeData, total := c.ServiceSafe.Get(bidsectionId, pid, safeData.PageNo, safeData.PageSize)
  54. c.Ctx.JSON(iris.Map{
  55. "code": 0,
  56. "msg": "请求成功",
  57. "data": SafeData,
  58. "total": total,
  59. })
  60. }
  61. // @Summary 创建新的安全巡检记录
  62. // @Tags 安全巡检
  63. // @Description 创建新的安全巡检记录
  64. // @Accept json
  65. // @Produce json
  66. // @Security ApiKeyAuth
  67. // @Param bidsectionId body string true "标段ID"
  68. // @Param code body string true "编号"
  69. // @Param createTime body string true "日期"
  70. // @Param inspection body string true "检查部位"
  71. // @Param position body string true "部位"
  72. // @Success 200 {object} viewmodels.Safe "{code:0成功,msg:}"
  73. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  74. // @Router /api/safe [post]
  75. func (c *SafeApi) Post() {
  76. // 1.规则验证
  77. safeData, err := c.ServiceSafe.ValidRule(c.Ctx)
  78. // fmt.Println("-------------------------------", err)
  79. if err != nil {
  80. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  81. return
  82. }
  83. bidsectionId, err := utils.GetDecryptId(safeData.BidsectionId)
  84. if err != nil {
  85. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  86. return
  87. }
  88. data := models.CmSafe{}
  89. data.BidsectionId = bidsectionId
  90. data.Code = safeData.Code
  91. data.Inspection = safeData.Inspection
  92. uid, err := utils.GetProjectAccountId(c.Ctx)
  93. if err != nil {
  94. c.Ctx.JSON(iris.Map{"code": -1, "msg": "未登录或账号失效,请重新登录"})
  95. return
  96. }
  97. data.Uid = uid
  98. createTime, err := time.Parse(conf.SysTimeform, safeData.CreateTime)
  99. if err != nil {
  100. c.Ctx.JSON(iris.Map{"code": -1, "msg": "日期转换异常,请检查参数"})
  101. return
  102. }
  103. // fmt.Println(createTime)
  104. data.CreateTime = createTime
  105. err = c.ServiceSafe.Post(data)
  106. if err != nil {
  107. c.Ctx.JSON(iris.Map{
  108. "code": -1,
  109. "msg": fmt.Sprintf("%s", err),
  110. })
  111. return
  112. }
  113. c.Ctx.JSON(iris.Map{
  114. "code": 0,
  115. "msg": "插入成功",
  116. })
  117. }
  118. // @Summary 删除记录
  119. // @Tags 安全巡检
  120. // @Description 删除安全巡检记录
  121. // @Accept json
  122. // @Produce json
  123. // @Security ApiKeyAuth
  124. // @Param id body string true "安全巡检ID"
  125. // @Success 200 {object} viewmodels.Safe "{code:0成功,msg:}"
  126. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  127. // @Router /api/safe [delete]
  128. func (c *SafeApi) Delete() {
  129. queryId := c.Ctx.URLParam("id")
  130. if queryId == "" {
  131. c.Ctx.JSON(iris.Map{"code": -1, "msg": "id不存在"})
  132. return
  133. }
  134. id, err := utils.GetDecryptId(queryId)
  135. if err != nil {
  136. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  137. return
  138. }
  139. err = c.ServiceSafe.Del(id)
  140. if err != nil {
  141. c.Ctx.JSON(iris.Map{
  142. "code": -1,
  143. "msg": fmt.Sprintf("%s", err),
  144. })
  145. return
  146. }
  147. c.Ctx.JSON(iris.Map{
  148. "code": 0,
  149. "msg": "删除成功",
  150. })
  151. }
  152. // @Summary 获取安全巡检详情
  153. // @Tags 安全巡检
  154. // @Description 获得安全巡检详情页面数据
  155. // @Accept json
  156. // @Produce json
  157. // @Security ApiKeyAuth
  158. // @Param id path string true "巡检ID"
  159. // @Success 200 {object} viewmodels.SafeDetail "{code:0成功,data:viewmodels.SafeDetail,msg:}"
  160. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  161. // @Router /api/safe/detail [get]
  162. func (c *SafeApi) GetDetail() {
  163. // 1.规则验证
  164. safeData, err := c.ServiceSafe.ValidRule(c.Ctx)
  165. if err != nil {
  166. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  167. return
  168. }
  169. id, err := utils.GetDecryptId(safeData.Id)
  170. if err != nil {
  171. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadForm转换异常,请检查参数"})
  172. return
  173. }
  174. pid, _ := utils.GetProjectId(c.Ctx)
  175. SafeData := c.ServiceSafe.GetDetail(id, pid)
  176. c.Ctx.JSON(iris.Map{
  177. "code": 0,
  178. "msg": "请求成功",
  179. "data": SafeData,
  180. })
  181. }