// 将主表字段值付给子表字段
export default {
    execute:async function(){
        console.log("进入到按条件给子表字段赋值前端公式");
        if(arguments&&arguments[0]&&arguments[0][0]&&arguments[0][1]&&arguments[1]){
            // 子表字段信息
            let subTable = arguments[0][0].FieldTableCode, subField = arguments[0][0].FieldCode, trueValueInfo = arguments[0][1], falseValueInfo = arguments[0][2],
            // 条件信息
            conditionField = arguments[0][3], compareSymbol = arguments[0][4].StaticValue, conditionValue = arguments[0][5],
            // 是否有值覆盖(0表示不覆盖，1表示覆盖)
            recoverFlag = arguments[0][6].StaticValue  
            // 获取待赋值，若待赋值来源于子表字段，
            let trueValue = await this.getNewValue(trueValueInfo, arguments[1]) 
            let falseValue = await this.getNewValue(falseValueInfo, arguments[1]) 
            // 获取条件判断结果
            // let judgeResult = await this.getCompareResult(conditionField, compareSymbol, conditionValue, arguments[1])
            // 获取子表对象
            let tableObj = arguments[1].getTableType(subTable)
            if (tableObj.type == "sub") {
                let _data = arguments[1].getSubData(tableObj.title);
                if(_data instanceof Array){
                    let judgeValue = await this.getNewValue(conditionValue, arguments[1])
                    let conditionFieldValue = "", oldVal = "", conditionProps = ""
                    let judgeResult = true
                    for (const iterator of _data) {
                        conditionProps = conditionField.FieldCode.split(".")
                        if (conditionProps.length > 1) {
                            conditionFieldValue = iterator.getValue(conditionProps[0]) ? iterator.getValue(conditionProps[0])[conditionProps[1]] : iterator.getValue(conditionProps[0])
                        } else {
                            conditionFieldValue = iterator.getValue(conditionField.FieldCode)
                        }
                        oldVal = iterator.getValue(subField)
                        judgeResult = await this.getCompareResult(conditionFieldValue, compareSymbol, judgeValue, arguments[1])
                        if (recoverFlag == 1 || !oldVal) {
                            // 当条件成立时赋trueValue，反之赋falseValue
                            iterator.setValue(subField, judgeResult ? trueValue : falseValue)
                        }
                    }
                }
            }
        }else{
            return '';
        }
    },

    /**
     * 获取判断结果
     * @param {*} conditionField 
     * @param {*} compareSymbol 
     * @param {*} conditionValue 
     * @param {*} context 
     * @returns 
     */
    async getCompareResult(conditionFieldValue, compareSymbol, judgeValue , context) {
        // let conditionFieldValue = await this.getNewValue(conditionField, context) 
        // let judgeValue = await this.getNewValue(conditionValue, context)
        let judgeFlag = true
        if ("=" == compareSymbol) {
            judgeFlag = (conditionFieldValue == judgeValue)
        } else if (">" == compareSymbol) {
            judgeFlag = (conditionFieldValue > judgeValue)
        } else if ("<" == compareSymbol) {
            judgeFlag = (conditionFieldValue < judgeValue)
        } else if (">=" == compareSymbol) {
            judgeFlag = (conditionFieldValue >= judgeValue)
        } else if ("<=" == compareSymbol) {
            judgeFlag = (conditionFieldValue <= judgeValue)
        }  else if ("<>" == compareSymbol) {
            judgeFlag = (conditionFieldValue != judgeValue)
        }
        return judgeFlag
    },

    /**
     * 获取待赋的值，若为子表不解析
     * @param {*} newValueInfo 
     * @param {*} context 
     * @returns 
     */
    async getNewValue(newValueInfo, context) {
        if (newValueInfo instanceof StaticDataNode) {
            return newValueInfo.StaticValue
        } else if (newValueInfo instanceof BillDataNode) {
            let tableObj = context.getTableType(newValueInfo.FieldTableCode)
            if (tableObj.type == "master") {
                let fieldLink = newValueInfo.FieldTableCode + "." + newValueInfo.FieldCode
                return newValueInfo.dynamicNodeFactory.methodJson.GetFieldData('',fieldLink, '')
            }
        } else if (newValueInfo instanceof FunctionNode) {
            let funcResult = newValueInfo.getResult()
            if(funcResult instanceof AbstractData) {
                let newValue = ''
                await funcResult.Value.then(res => {
                    newValue = res
                })
                return newValue
            }
        } else {
            return ''
        }
    },
    getResultType:function(){
        return FMR.ConstDataTypes.General;
    }
}