// 判断子表某个字段值相同则提示不然保存前端公式
export default {
    execute: function () {
        // console.log("进入： 判断子表某个字段值是否相同前端公式");
        // 表名
        // 有一个则直接执行判断;三个则判断是否属于要判断的情况 ，属于则判断，否则false
        if (arguments[0][0] && arguments[0][1] && arguments[0][2]) {
            let subTable = arguments[0][0].FieldTableCode
            // 字段名
            let subFieldCode = arguments[0][0].FieldCode

            // 主表
            let targetField = arguments[0][1]
            let masterTable = arguments[0][1].FieldTableCode
            // 字段
            let masterFieldCode = arguments[0][1].FieldCode

            // bill
            let context = arguments[1]

            // 字段值
            let fieldValue = null
            if (targetField instanceof BillDataNode) {
                let masterObj = context.getTableType(masterTable)
                if (masterObj.type == 'master') {
                    let masterData = context.getMasterData(masterObj.name)
                    fieldValue = masterData.getValue(masterFieldCode).code
                }
            }

            let str = arguments[0][2].StaticValue
            let strNum = str.split(",")

            // 要判断是否重复的字段所在的表的所有字段
            let tableObj = context.getTableType(subTable)
            let dataFun = context.getSubData(tableObj.title)
            let flag = 1  // 判断是否有相等字段
            let num = 0  // 判断是否属于要判断的
            // 一条以上数据进行判断
            if (dataFun.length > 1) {
                for (let i = 0; i < strNum.length; i++) {
                    if (fieldValue == strNum[i]) {
                        for (let j = 0; j < dataFun.length; j++) {
                            let _data1 = dataFun[j].getData()
                            for (let k = j + 1; k < dataFun.length; k++) {
                                let _data2 = dataFun[k].getData()
                                if (_data1[subFieldCode].value === _data2[subFieldCode].value) {
                                    flag = 0
                                    return false;
                                }
                            }
                        }
                        num = num + 1
                    }
                }
            } else if (dataFun.length <= 1) {
                return true
            }
            // 不用判断
            if (num == 0) {
                return true
            } else{
                if (flag == 1) {
                    return true
                }
            }
        } else {
            let subTable = arguments[0][0].FieldTableCode
            // 字段名
            let subFieldCode = arguments[0][0].FieldCode

            // bill
            let context = arguments[1]

            // 表所有字段
            let tableObj = context.getTableType(subTable)
            let dataFun = context.getSubData(tableObj.title)
            let flag = 1
            if (dataFun.length > 1) {
                for (let i = 0; i < dataFun.length; i++) {
                    let _data1 = dataFun[i].getData()
                    for (let j = i + 1; j < dataFun.length; j++) {
                        let _data2 = dataFun[j].getData()
                        if (_data1[subFieldCode].value === _data2[subFieldCode].value) {
                            flag = 0
                            return false;
                        }
                    }
                }
            } else if (dataFun.length <= 1) {
                return true
            }
            if (flag === 1) {
                return true;
            }
        }
    },
    getResultType:function(){
        return FMR.ConstDataTypes.Bool;
    }
}