// 获取当前用户关联职员基础数据---后台公式
export default {
    execute: async function () {
        console.log("进入给设置默认基础数据值前端公式");
        // 初始化汇率
        let result = '',
        // 获取指定字段值
        propName = arguments[0][2].StaticValue
        if (arguments && arguments[0] && arguments[0][0] && arguments[0][1] && arguments[1]) {
            let bill = arguments[1], sourceValueField = arguments[0][0], isValueFromSub = false
            if (sourceValueField instanceof FunctionNode) {
                let funcResult = sourceValueField.getResult()
                if(funcResult instanceof AbstractData) {
                    await funcResult.Value.then(res => {
                        result = res
                    })
                }
            } else if (sourceValueField instanceof BillDataNode) {
                let tableObj = bill.getTableType(sourceValueField.FieldTableCode);
                if (tableObj.type == "sub") {
                    isValueFromSub = true
                } else {
                    result = sourceValueField.dynamicNodeFactory.methodJson.GetFieldData('', sourceValueField.FieldTableCode + "." + sourceValueField.FieldCode, '')
                }
            } else if (sourceValueField instanceof StaticDataNode) {
                result = sourceValueField.StaticValue
            }
            // 获取目标字段
            let targetField = arguments[0][1], targetFieldLink = targetField.FieldTableCode + "." + targetField.FieldCode,
            // 目标字段原始值
            oldVal = '',
            // 是否覆盖:0不覆盖；1覆盖
            recoverFlag = arguments[0][3].StaticValue
            // 当是单据字段时进行赋值
            if (targetField instanceof BillDataNode) {
                let tableObj = bill.getTableType(targetField.FieldTableCode);
                if (tableObj.type == "master") {
                    oldVal = targetField.dynamicNodeFactory.methodJson.GetFieldData('',targetFieldLink, '')
                    if (recoverFlag == 1 || !oldVal) {
                        targetField.dynamicNodeFactory.methodJson.SetFieldData('', targetFieldLink, propName ? result[propName] : result, '')
                    }
                } else {
                    let _data = bill.getSubData(tableObj.title);
                    if(_data instanceof Array){
                        for (const iterator of _data) {
                            oldVal = iterator.getValue(targetField.FieldCode)
                            if (recoverFlag == 1 || !oldVal) {
                                if (isValueFromSub) {
                                    result = iterator.getValue(sourceValueField.FieldCode)
                                }
                                iterator.setValue(targetField.FieldCode, result && propName ? result[propName] : result)
                            }
                        }
                    }
                    // 刷新表格
                    if (!bill.isRefresh) {
                        bill.refreshSubDataRow(tableObj.title)
                        bill.isRefresh = true
                    }
                }
            }
        }
        return result && propName ? result[propName] : result
    },
    getResultType: function () {
        return FMR.ConstDataTypes.General;
    }
}