safe_api.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/add [post]
  76. func (c *SafeApi) PostAdd() {
  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. pid, err := utils.GetProjectId(c.Ctx)
  101. if err != nil {
  102. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  103. return
  104. }
  105. data.ProjectId = pid
  106. createTime, err := time.Parse(conf.SysTimeform, safeData.CreateTime)
  107. if err != nil {
  108. c.Ctx.JSON(iris.Map{"code": -1, "msg": "日期转换异常,请检查参数"})
  109. return
  110. }
  111. // fmt.Println(createTime)
  112. data.CreateTime = createTime
  113. err = c.ServiceSafe.Post(data)
  114. if err != nil {
  115. c.Ctx.JSON(iris.Map{
  116. "code": -1,
  117. "msg": fmt.Sprintf("%s", err),
  118. })
  119. return
  120. }
  121. c.Ctx.JSON(iris.Map{
  122. "code": 0,
  123. "msg": "插入成功",
  124. })
  125. }
  126. // @Summary 删除记录
  127. // @Tags 安全巡检
  128. // @Description 删除安全巡检记录
  129. // @Accept json
  130. // @Produce json
  131. // @Security ApiKeyAuth
  132. // @Param id body string true "安全巡检ID"
  133. // @Success 200 {object} viewmodels.Safe "{code:0成功,msg:}"
  134. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  135. // @Router /api/safe/del [delete]
  136. func (c *SafeApi) DeleteDel() {
  137. queryId := c.Ctx.URLParam("id")
  138. if queryId == "" {
  139. c.Ctx.JSON(iris.Map{"code": -1, "msg": "id不存在"})
  140. return
  141. }
  142. id, err := utils.GetDecryptId(queryId)
  143. if err != nil {
  144. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  145. return
  146. }
  147. err = c.ServiceSafe.Del(id)
  148. if err != nil {
  149. c.Ctx.JSON(iris.Map{
  150. "code": -1,
  151. "msg": fmt.Sprintf("%s", err),
  152. })
  153. return
  154. }
  155. c.Ctx.JSON(iris.Map{
  156. "code": 0,
  157. "msg": "删除成功",
  158. })
  159. }
  160. // @Summary 获取安全巡检详情
  161. // @Tags 安全巡检
  162. // @Description 获得安全巡检详情页面数据
  163. // @Accept json
  164. // @Produce json
  165. // @Security ApiKeyAuth
  166. // @Param id path string true "巡检ID"
  167. // @Success 200 {object} viewmodels.SafeDetail "{code:0成功,data:viewmodels.SafeDetail,msg:}"
  168. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  169. // @Router /api/safe/detail [get]
  170. func (c *SafeApi) GetDetail() {
  171. // 1.规则验证
  172. safeData, err := c.ServiceSafe.ValidRule(c.Ctx)
  173. if err != nil {
  174. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  175. return
  176. }
  177. id, err := utils.GetDecryptId(safeData.Id)
  178. if err != nil {
  179. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadForm转换异常,请检查参数"})
  180. return
  181. }
  182. pid, _ := utils.GetProjectId(c.Ctx)
  183. SafeData := c.ServiceSafe.GetDetail(id, pid)
  184. c.Ctx.JSON(iris.Map{
  185. "code": 0,
  186. "msg": "请求成功",
  187. "data": SafeData,
  188. })
  189. }
  190. // @Summary 安全概述
  191. // @Tags 安全巡检
  192. // @Description 安全概述
  193. // @Accept json
  194. // @Produce json
  195. // @Security ApiKeyAuth
  196. // @Param bidsectionId path string true "标段ID"
  197. // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,msg:错误信息}"
  198. // @Router /api/safe/survey [get]
  199. func (c *SafeApi) GetSurvey() {
  200. sectionData := viewmodels.Safe{}
  201. err := c.Ctx.ReadForm(&sectionData)
  202. if err != nil {
  203. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  204. return
  205. }
  206. err = sectionData.ValidateBidsectionId()
  207. if err != nil {
  208. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  209. return
  210. }
  211. // 标段ID
  212. bidsectionId, err := utils.GetDecryptId(sectionData.BidsectionId)
  213. if err != nil {
  214. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  215. return
  216. }
  217. // 项目ID
  218. projectId, err := utils.GetProjectId(c.Ctx)
  219. if err != nil {
  220. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  221. return
  222. }
  223. // 账号ID
  224. // accountId, err := utils.GetProjectAccountId(c.Ctx)
  225. // if err != nil {
  226. // c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  227. // return
  228. // }
  229. // // 缓存标段ID-用于权限
  230. // key := fmt.Sprintf("pm_%d_%d", projectId, accountId)
  231. // lib.NewRedis().SetBidsectionIdByCache(key, bidsectionId)
  232. SafeData := c.ServiceSafe.GetSurvey(projectId, bidsectionId)
  233. c.Ctx.JSON(iris.Map{
  234. "code": 0,
  235. "msg": "",
  236. "bidsectionId": sectionData.BidsectionId,
  237. "data": SafeData,
  238. })
  239. }