SetValueOfSubColumn.js 4.84 KB
Newer Older
wangcong committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
// 将主表字段值付给子表字段
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;
    }
}