rule_api.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.Safe "{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 {object} viewmodels.Safe "{code:0成功,data:viewmodels.Safe,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. isOk, err := c.RuleService.Post(pid, bidsectionId, safeData.Type, safeData.Rule)
  85. fmt.Println(err)
  86. if isOk && err == nil {
  87. c.Ctx.JSON(iris.Map{
  88. "code": 0,
  89. "msg": "请求成功",
  90. })
  91. } else {
  92. c.Ctx.JSON(iris.Map{
  93. "code": -1,
  94. "msg": err,
  95. })
  96. }
  97. }