import _ from 'lodash'
import util from './FormulaConstants'

/**
 * 金额校验 校验接待活动登记单主子表金额是否相等
 * 公式说明 本公式仅适用接待活动单据主子表金额校验
 * @author fengjiansheng
 * @date 2021-09-04
 */
export default {
    execute: function () {
        console.log("s1 CheckJdhdAmount")
        if (arguments && arguments[0] && arguments[0].length > 1 && arguments[1]) {
            let context = arguments[1]
            let masterField = arguments[0][0]
            let masterTableObject = context.getTableType(masterField.FieldTableCode)
            let controlField = arguments[0][arguments[0].length - 1]
            if (!_.isEqual(masterTableObject.type, "master")) {
                console.error("CheckJdhdAmount公式配置错误,第一个参数需为主表金额字段")
                return false;
            }
            let masterData = context.getMasterData().getValue(masterField.FieldCode)
            let masterMoney = _.toNumber(masterData)
            let subMoneyTotal = 0
            for (let [index, data] of arguments[0].entries()) {
                if (index === 0) {
                    continue
                }
                if (index === arguments[0].length - 1) {
                    continue
                }
                let tempSubTableObject = context.getTableType(data.FieldTableCode)
                if (!_.isEqual(tempSubTableObject.type, "sub")) {
                    console.error("CheckJdhdAmount公式配置错误,第", (index + 1), "个参数需为子表金额字段")
                    return false;
                }
                let allSubData = context.getAllSubData()
                if (!allSubData || _.isEmpty(allSubData)) {
                    break
                }
                let tempAllSubData = context.getAllSubData()[tempSubTableObject.title]
                if (!tempAllSubData || _.isEmpty(tempAllSubData)) {
                    continue
                }
                if (tempAllSubData instanceof Array) {
                    for (const tempSubData of tempAllSubData) {
                        let tempData = tempSubData.getValue(data.FieldCode)
                        if (_.isEqual(data.FieldTableCode, controlField.FieldTableCode)) {
                            let isAddFlag = "0"
                            if (!_.isEmpty(tempSubData.getValue(controlField.FieldCode))) {
                                isAddFlag = tempSubData.getValue(controlField.FieldCode).code
                            }
                            if (_.isNumber(_.toNumber(tempData))) {
                                subMoneyTotal += _.isEqual("1", isAddFlag) ? 0 : _.toNumber(tempData)
                            } else {
                                console.error("CheckJdhdAmount公式配置错误,配置字段为非金额数据")
                                return false
                            }
                        } else {
                            if (_.isNumber(_.toNumber(tempData))) {
                                subMoneyTotal += _.toNumber(tempData)
                            } else {
                                console.error("CheckJdhdAmount公式配置错误,配置字段为非金额数据")
                                return false
                            }
                        }
                    }
                }
            }
            return Math.abs(masterMoney - subMoneyTotal) <= 0.0000001
        }
    },
    getResultType: function () {
        return FMR.ConstDataTypes.Bool
    }
}
