rule_api.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * @description: 编号规则
  3. * @Author: LanJianRong
  4. * @Date: 2020-11-27
  5. * @FilePath: \construction_management\web\api\rule_api.go
  6. */
  7. package api
  8. import (
  9. "fmt"
  10. "github.com/kataras/iris/v12"
  11. "go.mod/services"
  12. "go.mod/web/utils"
  13. )
  14. type RuleApi struct {
  15. //框架-web应用上下文环境
  16. Ctx iris.Context
  17. // 需要用的service
  18. RuleService services.RuleService
  19. }
  20. // @Summary 获取编号规则
  21. // @Tags 编号规则
  22. // @Description 获得制定pid、bid的编号规则
  23. // @Accept json
  24. // @Produce json
  25. // @Security ApiKeyAuth
  26. // @Param bidsectionId path string true "标段ID"
  27. // @Param projectId path string true "项目ID"
  28. // @Success 200 {object} viewmodels.ViewRule "{code:0成功,data:viewmodels.Safe,msg:请求成功}"
  29. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  30. // @Router /api/rule [get]
  31. func (c *RuleApi) Get() {
  32. // 1.规则验证
  33. safeData, err := c.RuleService.ValidRule(c.Ctx)
  34. if err != nil {
  35. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  36. return
  37. }
  38. pid, err := utils.GetProjectId(c.Ctx)
  39. if err != nil {
  40. c.Ctx.JSON(iris.Map{"code": -1, "msg": "项目id不存在, 请重新登录"})
  41. return
  42. }
  43. bidsectionId, err := utils.GetDecryptId(safeData.BidsectionId)
  44. if err != nil {
  45. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  46. return
  47. }
  48. rule := c.RuleService.Get(pid, bidsectionId)
  49. c.Ctx.JSON(iris.Map{
  50. "code": 0,
  51. "msg": "请求成功",
  52. "data": rule,
  53. })
  54. }
  55. // @Summary 提交编号规则
  56. // @Tags 编号规则
  57. // @Description 提交规则
  58. // @Accept json
  59. // @Produce json
  60. // @Security ApiKeyAuth
  61. // @Param bidsectionId body string true "标段ID"
  62. // @Param type body string true "规则类型" eg:"safeRule、qualityRule、contractRule"
  63. // @Param value body string true "编号规则" eg:"'['202011', 'cc3']'"
  64. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  65. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  66. // @Router /api/rule [post]
  67. func (c *RuleApi) Post() {
  68. // 1.规则验证
  69. safeData, err := c.RuleService.ValidRule(c.Ctx)
  70. if err != nil {
  71. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  72. return
  73. }
  74. pid, err := utils.GetProjectId(c.Ctx)
  75. if err != nil {
  76. c.Ctx.JSON(iris.Map{"code": -1, "msg": "项目id不存在, 请重新登录"})
  77. return
  78. }
  79. bidsectionId, err := utils.GetDecryptId(safeData.BidsectionId)
  80. if err != nil {
  81. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  82. return
  83. }
  84. err = c.RuleService.Post(pid, bidsectionId, safeData.Type, safeData.Rule)
  85. if err == nil {
  86. c.Ctx.JSON(iris.Map{
  87. "code": 0,
  88. "msg": "请求成功",
  89. })
  90. } else {
  91. c.Ctx.JSON(iris.Map{
  92. "code": -1,
  93. "msg": fmt.Sprintf("%s", err),
  94. })
  95. }
  96. }
  97. // @Summary 生成编号
  98. // @Tags 编号规则
  99. // @Description 提交规则
  100. // @Accept json
  101. // @Produce json
  102. // @Security ApiKeyAuth
  103. // @Param bidsectionId body string true "标段ID"
  104. // @Param type body string true "规则类型" eg:"safeRule、qualityRule、contractRule"
  105. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  106. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  107. // @Router /api/rule/auto [post]
  108. func (c *RuleApi) PostAuto() {
  109. // 1.规则验证
  110. safeData, err := c.RuleService.ValidRule(c.Ctx)
  111. if err != nil {
  112. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  113. return
  114. }
  115. pid, err := utils.GetProjectId(c.Ctx)
  116. if err != nil {
  117. c.Ctx.JSON(iris.Map{"code": -1, "msg": "项目id不存在, 请重新登录"})
  118. return
  119. }
  120. bidsectionId, err := utils.GetDecryptId(safeData.BidsectionId)
  121. if err != nil {
  122. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  123. return
  124. }
  125. rule, newCode, err := c.RuleService.AutoCode(bidsectionId, pid, safeData.Type)
  126. data := map[string]interface{}{
  127. "rule": rule,
  128. "newCode": newCode,
  129. }
  130. if err != nil {
  131. c.Ctx.JSON(iris.Map{
  132. "code": -1,
  133. "msg": fmt.Sprintf("%s", err),
  134. })
  135. } else {
  136. c.Ctx.JSON(iris.Map{
  137. "code": 0,
  138. "msg": "请求成功",
  139. "data": data,
  140. })
  141. }
  142. }