// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package utils import ( "encoding/json" "fmt" "gd_service/apis" "gd_service/errors" "reflect" "strings" ) func CheckResponseParamNew(ResponseParamConf []apis.ManagementBaseApiParam, param interface{}, filter string) error { bytes, err := json.Marshal(param) if err != nil { return err } var m = map[string]interface{}{} json.Unmarshal(bytes, &m) for _, v := range ResponseParamConf { if v.Selected == false { // 没有勾选不返回该字段 continue } else { if _, ok := m[v.Name]; ok { // 核定载客和核定载质量不做判断 if v.Name == "approved_number" || v.Name == "approved_load" { continue } // 不检查过滤字段 if strings.Contains(filter, v.Name) { continue } if v.Required == true { // 必选项必须不为空,为空返回查无 val := reflect.ValueOf(m[v.Name]) if reflect.DeepEqual(m[v.Name], reflect.Zero(val.Type()).Interface()) { // 判断是否是初登,初登日期和续保都为空返回查无 if v.Name == "initial_registration_date" || v.Name == "first_insurance_date" { if _, ok := m["insurance_first_date"]; ok { val := reflect.ValueOf(m["insurance_first_date"]) if reflect.DeepEqual(m["insurance_first_date"], reflect.Zero(val.Type()).Interface()) { return errors.NoRecordMustSelect } else { continue } } } return errors.NoRecordMustSelect } if v.Name == "is_owner" { if m[v.Name] == "0" { return errors.NoRecordMustSelect } } } if v.Type == "struct" && len(v.Child) > 0 { err = getStructResponseParam(v.Child, m[v.Name]) if err != nil { return err } } else if v.Type == "list-struct" && len(v.Child) > 0 { err = getListResponseParam(v.Child, m[v.Name]) if err != nil { return err } } } } } return nil } func getListResponseParam(checkResult []apis.ManagementBaseApiParam, result interface{}) (err error) { rtype := fmt.Sprintf("%v", reflect.TypeOf(result)) if rtype == "" { return nil } resultParamList := result.([]interface{}) for _, resultParam := range resultParamList { resultParamMap, _ := resultParam.(map[string]interface{}) for _, v := range checkResult { if v.Selected == false { // 没有勾选不返回该字段 continue } else { if _, ok := resultParamMap[v.Name]; ok { if v.Required == true { // 必选项必须不为空,为空返回查无 val := reflect.ValueOf(resultParamMap[v.Name]) if val.Kind() != reflect.Bool && reflect.DeepEqual(resultParamMap[v.Name], reflect.Zero(val.Type()).Interface()) { return errors.NoRecordMustSelect } } if v.Type == "struct" { err = getStructResponseParam(v.Child, resultParamMap[v.Name]) if err != nil { return err } } else if v.Type == "list-struct" { err = getListResponseParam(v.Child, resultParamMap[v.Name]) if err != nil { return err } } } } } } return nil } func getStructResponseParam(checkResult []apis.ManagementBaseApiParam, result interface{}) (err error) { resultParamMap := result.(map[string]interface{}) for _, v := range checkResult { if v.Selected == false { // 没有勾选不返回该字段 continue } else { if _, ok := resultParamMap[v.Name]; ok { if v.Required == true { // 必选项必须不为空,为空返回查无 val := reflect.ValueOf(resultParamMap[v.Name]) if val.Kind() != reflect.Bool && reflect.DeepEqual(resultParamMap[v.Name], reflect.Zero(val.Type()).Interface()) { return errors.NoRecordMustSelect } } if v.Type == "struct" { err = getStructResponseParam(v.Child, resultParamMap[v.Name]) if err != nil { return err } } else if v.Type == "list-struct" { err = getListResponseParam(v.Child, resultParamMap[v.Name]) if err != nil { return err } } } } } return nil }