vue2-重复校验

53 阅读2分钟

索引(a,b,c)已创建的情况下不能再创建索引(a)和(a,b)

image.png

checkDuplicate(rule, value, callback) {
    if (!value) {
        callback(new Error('必填项'))
    } else {
        const allValues = this.form.tableData.map((item) => item.inputValue)
        const count = allValues?.filter((v) => v === value).length
        if (count > 1) {
            callback(new Error('该内容已存在'))
        } else {
            callback()
        }
    }
},
checkFieldEnName(rule, value, callback) {
    if (!value || value.length < 1) {
        callback(new Error('必填项'))
    } else {
        // 判断索引是否一致
        const listTableIndexInfo = this.form.tableData
        if (listTableIndexInfo.length < 2) {
            return callback()
        }
        const key = rule.field.split('.')
        const current_index = Number(key[1])
        const newIndex = value.map((v) => v + '|')
        //fix2025.5.21 每一个fieldEnName后新增一个|,方便后续判断是否重复;
        //如果不加|,例如xin_zeng_xiu_fu_pn_add 和 xin_zeng_xiu_fu_pn 字段会判断为重复
        for (let j = 0; j < listTableIndexInfo.length; j++) {
            if (j === current_index) {
                continue
            }
            const existingIndices = listTableIndexInfo[j].selectedList.map((v) => v + '|')
            if (existingIndices?.length > 0 && newIndex?.length) {
                const is_equal = this.shouldCreateIndex([existingIndices], newIndex)
                console.log(j, current_index, is_equal ? '重复' : '不重复', existingIndices, newIndex)
                if (is_equal) {
                    return callback(new Error('索引字段重复'))
                }
            }
        }
    }
    callback()
},
checkIndexColumn(rule, value, callback) {
    if (!value || value.length < 1) {
        callback(new Error('必填项'))
    } else {
        // 判断索引是否一致
        const formList = this.form.tableData
        if (formList?.length < 2) {
            return callback()
        }
        const key = rule.field.split('.')
        const current_index = Number(key[1])
        for (let i = 0; i < formList.length; i++) {
            const elementList = formList[i]
            if (i === current_index || elementList?.securityOperateList?.length === 0) {
                continue
            }
            const sortDataValue = value
                .map((ele) => ele)
                .slice()
                .sort()
            const sortData = elementList.securityOperateList
                .map((ele) => ele)
                .slice()
                .sort()
            const is_equal = JSON.stringify(sortDataValue) === JSON.stringify(sortData)
            if (is_equal) {
                return callback(new Error('字段重复'))
            }
        }
    }
    callback()
},
shouldCreateIndex(existingIndices, newIndex) {
    //索引(a,b,c)已创建的情况下不要再创建索引(a)和(a,b)
    //existingIndices已存在的索引数组,newIndex新创建的索引数组
    for (const existingIndex of existingIndices) {
        const existingIndexArr = existingIndex.join('|')
        const newIndexArr = newIndex.join('|')
        const hasSubIndex = existingIndexArr.startsWith(newIndexArr) || newIndexArr.startsWith(existingIndexArr)
        if (hasSubIndex) return true
    }
    return false
},

当主键为一个字段时,索引不允许出现该字段;当主键为多个字段时如abc,则不允许出现的情况a,ab,abc,abc*,*abc,abc

checkFieldEnName(rule, value, callback) {
    if (!value || value.length < 1) {
        callback(new Error('必填项'))
    } else {
        const valueFieldArr = value.map(v=>v.fieldEnName + '|')
        if(this.primaryList.length === 1){
            const primaryFieldEnName = this.primaryList[0].fieldEnName + '|'
            if(valueFieldArr.includes(primaryFieldEnName)){
                return callback(new Error('索引字段与主键冲突'))
            }
        }else{
            //if 拼接索引.includes(拼接主键) 禁用
            //if 拼接主键.startsWith(拼接索引) 禁用
            const primaryFieldEnName = this.primaryList.map(v=>v.fieldEnName + '|')
            const primaryFieldStr = primaryFieldEnName.join('|')
            const valueFieldStr = valueFieldArr.join('|')
            
            if(valueFieldStr.includes(primaryFieldStr) || primaryFieldStr.startsWith(valueFieldStr)){
                return callback(new Error('索引字段与主键冲突'))
            }  
        }
    
    }
    callback()
}