// 获取当前用户关联职员基础数据---后台公式
export default {
    execute: async function () {
        console.log("进入获取汇率前端公式");
        // 初始化汇率
        let rate = {convertype: "01", defaultrate: 1},
        // 获取指定字段值
        propName = arguments[0][4].StaticValue
        if (arguments && arguments[0] && arguments[0][0] && arguments[0][1] && arguments[1]) {
            // 获取存放原币的表及字段
            let sourceFieldLink = arguments[0][0].FieldTableCode + "." + arguments[0][0].FieldCode,
            // 获取原币code
            sourceCurrency = arguments[0][0].dynamicNodeFactory.methodJson.GetFieldData('', sourceFieldLink + ".objectcode", ''),
            targetCurrency = '',
            funcResult = ''
            // 获取目标币code
            if (arguments[0][1] instanceof FunctionNode) {
                funcResult = arguments[0][1].getResult()
                if(funcResult instanceof AbstractData) {
                    await funcResult.Value.then(res => {
                        targetCurrency = res.objectcode
                    })
                }
            } else {
                targetCurrency = arguments[0][1].StaticValue
            }
            // 当原币不为空时，获取汇率
            if (sourceCurrency && targetCurrency) {
                rate = await this.getMDObject(sourceCurrency, targetCurrency)
            }
            // 获取目标字段
            let targetField = arguments[0][2], targetFieldLink = targetField.FieldTableCode + "." + targetField.FieldCode,
            // 目标字段原始值
            oldVal = '',
            // 是否覆盖:0不覆盖；1覆盖
            recoverFlag = arguments[0][5].StaticValue
            // 当是单据字段时进行赋值
            if (targetField instanceof BillDataNode) {
                let tableObj = arguments[1].getTableType(targetField.FieldTableCode);
                if (tableObj.type == "master") {
                    oldVal = targetField.dynamicNodeFactory.methodJson.GetFieldData('',targetFieldLink, '')
                    if (recoverFlag == 1 || !oldVal) {
                        targetField.dynamicNodeFactory.methodJson.SetFieldData('', targetFieldLink, propName ? rate[propName] : rate, '')
                    }
                } else {
                    let _data = arguments[1].getSubData(tableObj.title);
                    if(_data instanceof Array){
                        for (const iterator of _data) {
                            oldVal = iterator.getValue(targetField.FieldCode)
                            if (recoverFlag == 1 || !oldVal) {
                                iterator.setValue(targetField.FieldCode, propName ? rate[propName] : rate)
                            }
                        }
                    }
                }
            }
            let masterField = arguments[0][3]
            if (masterField && masterField instanceof BillDataNode) {
                let masterFieldLink = masterField.FieldTableCode + "." + masterField.FieldCode
                if (recoverFlag == 1 || !masterField.dynamicNodeFactory.methodJson.GetFieldData('',masterFieldLink, '')) {
                    masterField.dynamicNodeFactory.methodJson.SetFieldData('', masterFieldLink, rate.convertype, '')
                }
            }
        }
        return propName ? rate[propName] : rate
    },
    getResultType: function () {
        return FMR.ConstDataTypes.General;
    },
    getMDObject(sourceCurrency, targetCurrency) {
        let param = {
            "limit": 1,
            "offset": 0,
            "orgncurrencyid": sourceCurrency,
            "targetcurrencyid": targetCurrency,
            "stopflag": 0,
            "tableName": 'MD_EXCHRATE',
            "queryDataStructure": "ALL"//返回的接口类型
        }
        return new Promise((resolve, reject) => {
            let promise = GMS.$http.post("/baseData/data/list", param);
            promise
                .then((data) => {
                    if (data.data && data.data.rows && data.data.rows.length > 0) {
                        resolve(data.data.rows[0]);
                    } else {
                        resolve({convertype: "01", defaultrate: 1});
                    }
                })
                .catch((error) => {
                    resolve({convertype: "01", defaultrate: 1});
                });
        });
    }
}