safe_api.go 7.4 KB

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