safe_api.go 6.8 KB

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