123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package utils
- import (
- "encoding/json"
- "fmt"
- "gd_vehicle/apis"
- "gd_vehicle/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 == "<nil>" {
- 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
- }
|