check_response.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package utils
  4. import (
  5. "encoding/json"
  6. "fmt"
  7. "gd_vehicle/apis"
  8. "gd_vehicle/errors"
  9. "reflect"
  10. "strings"
  11. )
  12. func CheckResponseParamNew(ResponseParamConf []apis.ManagementBaseApiParam, param interface{}, filter string) error {
  13. bytes, err := json.Marshal(param)
  14. if err != nil {
  15. return err
  16. }
  17. var m = map[string]interface{}{}
  18. json.Unmarshal(bytes, &m)
  19. for _, v := range ResponseParamConf {
  20. if v.Selected == false { // 没有勾选不返回该字段
  21. continue
  22. } else {
  23. if _, ok := m[v.Name]; ok {
  24. // 核定载客和核定载质量不做判断
  25. if v.Name == "approved_number" || v.Name == "approved_load" {
  26. continue
  27. }
  28. // 不检查过滤字段
  29. if strings.Contains(filter, v.Name) {
  30. continue
  31. }
  32. if v.Required == true { // 必选项必须不为空,为空返回查无
  33. val := reflect.ValueOf(m[v.Name])
  34. if reflect.DeepEqual(m[v.Name], reflect.Zero(val.Type()).Interface()) {
  35. // 判断是否是初登,初登日期和续保都为空返回查无
  36. if v.Name == "initial_registration_date" || v.Name == "first_insurance_date" {
  37. if _, ok := m["insurance_first_date"]; ok {
  38. val := reflect.ValueOf(m["insurance_first_date"])
  39. if reflect.DeepEqual(m["insurance_first_date"], reflect.Zero(val.Type()).Interface()) {
  40. return errors.NoRecordMustSelect
  41. } else {
  42. continue
  43. }
  44. }
  45. }
  46. return errors.NoRecordMustSelect
  47. }
  48. if v.Name == "is_owner" {
  49. if m[v.Name] == "0" {
  50. return errors.NoRecordMustSelect
  51. }
  52. }
  53. }
  54. if v.Type == "struct" && len(v.Child) > 0 {
  55. err = getStructResponseParam(v.Child, m[v.Name])
  56. if err != nil {
  57. return err
  58. }
  59. } else if v.Type == "list-struct" && len(v.Child) > 0 {
  60. err = getListResponseParam(v.Child, m[v.Name])
  61. if err != nil {
  62. return err
  63. }
  64. }
  65. }
  66. }
  67. }
  68. return nil
  69. }
  70. func getListResponseParam(checkResult []apis.ManagementBaseApiParam, result interface{}) (err error) {
  71. rtype := fmt.Sprintf("%v", reflect.TypeOf(result))
  72. if rtype == "<nil>" {
  73. return nil
  74. }
  75. resultParamList := result.([]interface{})
  76. for _, resultParam := range resultParamList {
  77. resultParamMap, _ := resultParam.(map[string]interface{})
  78. for _, v := range checkResult {
  79. if v.Selected == false { // 没有勾选不返回该字段
  80. continue
  81. } else {
  82. if _, ok := resultParamMap[v.Name]; ok {
  83. if v.Required == true { // 必选项必须不为空,为空返回查无
  84. val := reflect.ValueOf(resultParamMap[v.Name])
  85. if val.Kind() != reflect.Bool && reflect.DeepEqual(resultParamMap[v.Name], reflect.Zero(val.Type()).Interface()) {
  86. return errors.NoRecordMustSelect
  87. }
  88. }
  89. if v.Type == "struct" {
  90. err = getStructResponseParam(v.Child, resultParamMap[v.Name])
  91. if err != nil {
  92. return err
  93. }
  94. } else if v.Type == "list-struct" {
  95. err = getListResponseParam(v.Child, resultParamMap[v.Name])
  96. if err != nil {
  97. return err
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. return nil
  105. }
  106. func getStructResponseParam(checkResult []apis.ManagementBaseApiParam, result interface{}) (err error) {
  107. resultParamMap := result.(map[string]interface{})
  108. for _, v := range checkResult {
  109. if v.Selected == false { // 没有勾选不返回该字段
  110. continue
  111. } else {
  112. if _, ok := resultParamMap[v.Name]; ok {
  113. if v.Required == true { // 必选项必须不为空,为空返回查无
  114. val := reflect.ValueOf(resultParamMap[v.Name])
  115. if val.Kind() != reflect.Bool && reflect.DeepEqual(resultParamMap[v.Name], reflect.Zero(val.Type()).Interface()) {
  116. return errors.NoRecordMustSelect
  117. }
  118. }
  119. if v.Type == "struct" {
  120. err = getStructResponseParam(v.Child, resultParamMap[v.Name])
  121. if err != nil {
  122. return err
  123. }
  124. } else if v.Type == "list-struct" {
  125. err = getListResponseParam(v.Child, resultParamMap[v.Name])
  126. if err != nil {
  127. return err
  128. }
  129. }
  130. }
  131. }
  132. }
  133. return nil
  134. }