safe_api.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. data.Times = 1
  93. uid, err := utils.GetProjectAccountId(c.Ctx)
  94. if err != nil {
  95. c.Ctx.JSON(iris.Map{"code": -1, "msg": "未登录或账号失效,请重新登录"})
  96. return
  97. }
  98. data.Uid = uid
  99. createTime, err := time.Parse(conf.SysTimeform, safeData.CreateTime)
  100. if err != nil {
  101. c.Ctx.JSON(iris.Map{"code": -1, "msg": "日期转换异常,请检查参数"})
  102. return
  103. }
  104. // fmt.Println(createTime)
  105. data.CreateTime = createTime
  106. err = c.ServiceSafe.Post(data)
  107. if err != nil {
  108. c.Ctx.JSON(iris.Map{
  109. "code": -1,
  110. "msg": fmt.Sprintf("%s", err),
  111. })
  112. return
  113. }
  114. c.Ctx.JSON(iris.Map{
  115. "code": 0,
  116. "msg": "插入成功",
  117. })
  118. }
  119. // @Summary 删除记录
  120. // @Tags 安全巡检
  121. // @Description 删除安全巡检记录
  122. // @Accept json
  123. // @Produce json
  124. // @Security ApiKeyAuth
  125. // @Param id body string true "安全巡检ID"
  126. // @Success 200 {object} viewmodels.Safe "{code:0成功,msg:}"
  127. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  128. // @Router /api/safe [delete]
  129. func (c *SafeApi) Delete() {
  130. queryId := c.Ctx.URLParam("id")
  131. if queryId == "" {
  132. c.Ctx.JSON(iris.Map{"code": -1, "msg": "id不存在"})
  133. return
  134. }
  135. id, err := utils.GetDecryptId(queryId)
  136. if err != nil {
  137. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  138. return
  139. }
  140. err = c.ServiceSafe.Del(id)
  141. if err != nil {
  142. c.Ctx.JSON(iris.Map{
  143. "code": -1,
  144. "msg": fmt.Sprintf("%s", err),
  145. })
  146. return
  147. }
  148. c.Ctx.JSON(iris.Map{
  149. "code": 0,
  150. "msg": "删除成功",
  151. })
  152. }
  153. // @Summary 获取安全巡检详情
  154. // @Tags 安全巡检
  155. // @Description 获得安全巡检详情页面数据
  156. // @Accept json
  157. // @Produce json
  158. // @Security ApiKeyAuth
  159. // @Param id path string true "巡检ID"
  160. // @Success 200 {object} viewmodels.SafeDetail "{code:0成功,data:viewmodels.SafeDetail,msg:}"
  161. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  162. // @Router /api/safe/detail [get]
  163. func (c *SafeApi) GetDetail() {
  164. // 1.规则验证
  165. safeData, err := c.ServiceSafe.ValidRule(c.Ctx)
  166. if err != nil {
  167. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  168. return
  169. }
  170. id, err := utils.GetDecryptId(safeData.Id)
  171. if err != nil {
  172. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadForm转换异常,请检查参数"})
  173. return
  174. }
  175. pid, _ := utils.GetProjectId(c.Ctx)
  176. SafeData := c.ServiceSafe.GetDetail(id, pid)
  177. c.Ctx.JSON(iris.Map{
  178. "code": 0,
  179. "msg": "请求成功",
  180. "data": SafeData,
  181. })
  182. }