package ValidationUtil import ( "dsDataex/MyService/DataEX" "dsDataex/MyService/DataEX/DataexDAO" "dsDataex/MyService/MySwagger" "dsDataex/Utils/CommonUtil" "encoding/json" "strings" ) /** * @title ValidESDataContent * @Description 校验ESDataContent * @Author wangshuai * @Date 2020-09-16 * @Param datasourceCode string 数据源CODE * @Param datas []MySwagger.Data Data * @Return bool 校验是否完全通过 * @Return string 提示信息 * @Return []MySwagger.Data 校验通过数据集合 * @Return []string 校验失败ID集合 */ func ValidESDataContent(datasourceCode string, datas []MySwagger.Data) (bool, string, []MySwagger.Data, []string) { var dataContent map[string]interface{} var fails string var failIDs []string var successDatas []MySwagger.Data var conditions string var esData DataEX.ESData var r bool conditions = " AND datasource_id='" + datasourceCode + "'" // 获取该数据源对应的元数据集合 result, _, _, metadatas, _ := DataexDAO.GetMetadataResults(conditions) if result == true { for no := 0; no < len(datas) && no < 1000; no++ { r = true var jsonData map[string]interface{} json.Unmarshal([]byte(datas[no].Data), &jsonData) esData.DataContent = jsonData dataContent = esData.DataContent for _, v := range metadatas { // 判断集合中是否含有该项数据 if _, ok := dataContent[v["item_name"].(string)]; ok { // 校验数据 res, mes, _ := ValidESDataContentItem(v, v["item_name"].(string), dataContent[v["item_name"].(string)]) if res == false { fails += v["item_name"].(string) + mes + " " r = false // 将校验失败的ID集合写入到failIDs failIDs = append(failIDs, datas[no].DataID) continue } } } if r == true { // 将校验通过的数据集合写入到successDatas successDatas = append(successDatas, datas[no]) } } } // 首尾去空格 fails = strings.TrimRight(fails, " ") return r, fails, successDatas, failIDs } /** * @title ValidESDataContentItem * @Description 校验ESDataContent每项元数据 * @Author wangshuai * @Date 2020-09-16 * @Param dataContentItem map[string]interface{} 数据源CODE * @Param itemName string 校验项名称 * @Param itemValue interface{} 校验项值 * @Return bool 校验是否通过 * @Return string 校验提示信息 * @Return error 错误信息 */ func ValidESDataContentItem(dataContentItem map[string]interface{}, itemName string, itemValue interface{}) (bool, string, error) { if dataContentItem != nil { dicId := dataContentItem["dic_id"] itemLength := dataContentItem["item_length"].(int64) checkName := dataContentItem["check_name"].(int64) checkDic := dataContentItem["check_dic"].(int64) checkType := dataContentItem["check_type"].(int64) checkPattern := dataContentItem["check_pattern"].(int64) checkExist := dataContentItem["check_exist"].(int64) if itemLength > 0 { // 校验长度 if CommonUtil.ConvertIntToInt64(len(itemValue.(string))) > itemLength { return false, "最大长度超出", nil } else { return true, "ok", nil } } else if checkName > 0 { // 校验名称 return true, "ok", nil } else if checkDic > 0 { // 校验字典 if CheckDic(dicId.(string), itemValue) == false { return false, "字典检测不通过", nil } else { return true, "ok", nil } } else if checkType > 0 { // 校验类型 return true, "ok", nil } else if checkPattern > 0 { // 校验规则 return true, "ok", nil } else if checkExist > 0 { // 校验必填项 if itemValue == nil { return false, itemName + "是必填项", nil } else { return true, "ok", nil } } else { return true, "ok", nil } } else { return false, "", nil } } /** * @title CheckDic * @Description 字典校验 * @Author wangshuai * @Date 2020-09-16 * @Param dicId string 字典ID * @Param itemValue interface{} 字典项值 * @Return bool 校验是否通过 */ func CheckDic(dicId string, itemValue interface{}) bool { total := DataexDAO.GetJyt2012CountByParentIdAndDicValue(dicId, itemValue.(string)) if total > 0 { return true } else { return false } } // 暂时作废 2020-09-16 func ValidESDataContentBak(datasourceCode string, dataContent map[string]interface{}) (bool, string) { var fails string var conditions string r := true conditions = " AND datasource_id=" + datasourceCode result, _, _, metadatas, _ := DataexDAO.GetMetadataResults(conditions) if result == true { for _, v := range metadatas { for _, vv := range v { if _, ok := dataContent[vv.(string)]; ok { res, mes, _ := ValidESDataContentItem(v, vv.(string), dataContent[vv.(string)]) if res == false { fails += vv.(string) + mes + " " r = false } } } } } fails = strings.TrimRight(fails, " ") return r, fails }