SetSubFieldValue.js 5.78 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
import _ from 'lodash'
import FormulaConstants from "@/formula/FormulaConstants";

/**
 * 子表赋值公式
 * 目前只拓展了一个部门经济分类子表赋值,后续可进行自由拓展。
 * @author fengjiansheng
 * @data 2021-09-13
 */
export default {
    execute: function () {
        console.log("s1 SetSubFieldValue")
        if (arguments && arguments[0] && arguments[0].length > 1 && arguments[1]) {
            // TODO 参数校验
            let context = arguments[1]
            let targetField = arguments[0][0]
            let targetObject = context.getTableType(targetField.FieldTableCode)
            let valueType = arguments[0][1]
            // TODO 入参校验
            if (valueType instanceof StaticDataNode && _.isNumber(valueType.StaticValue)) {
                valueType = valueType.StaticValue
            } else {
                console.error("SetSubFieldValue公式配置错误,第二个入参类型有误")
                return null
            }
            if (targetObject.type === "sub") {
                let subTableFocusOldIndex = context.getSubFocus()
qiaoyanqi committed
28
                let subTableFocusData = subTableFocusOldIndex && context.getAllSubData()[targetObject.title][subTableFocusOldIndex].getData()
wangcong committed
29
                let oldIndex = context.getSubFocus()
qiaoyanqi committed
30
                try {
qiaoyanqi committed
31 32 33 34 35 36 37 38 39 40
                    switch (valueType) {
                        case  FormulaConstants.billFiled.BMJJFL: {
                            let comp1 = arguments[0][2].getResult().Value;
                            let comp2 = arguments[0][3].getResult().Value;
                            let comp3 = arguments[0][4].getResult().Value;
                            let comp4 = arguments[0][5].getResult().Value;
                            if ((!_.isEmpty(comp1) || _.isNumber(comp1)) && (!_.isEmpty(comp2) || _.isNumber(comp2)) && (!_.isEmpty(comp3) || (_.isNumber(comp3)) && (!_.isEmpty(comp4) || (_.isNumber(comp4))))) {
                                if (!_.isEmpty(comp3) &&  !(_.isEqual(subTableFocusData[arguments[0][4].FieldCode.split(".")[0]]._field_old_value_, subTableFocusData[arguments[0][4].FieldCode.split(".")[0]].value))) {
                                    console.log("s2 SetSubField api")
                                    this.getMDObject({
wangcong committed
41 42 43
                                        "limit": 1,
                                        "offset": 0,
                                        "pagination": true,
qiaoyanqi committed
44 45
                                        "tableName": comp2,
                                        "code": comp3,
wangcong committed
46
                                        "queryDataStructure": "ALL"
qiaoyanqi committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
                                    }, data => {
                                        let param = {
                                            "limit": 1,
                                            "offset": 0,
                                            "pagination": true,
                                            "tableName": comp1,
                                            "code": data.rows[0][comp4].split("||")[0],
                                            "queryDataStructure": "ALL"
                                        };
                                        this.getMDObject(param, data => {
                                            let newIndex = context.getSubFocus();
                                            if (oldIndex !== newIndex) {
                                                context.setSubFocus(oldIndex)
                                            }
                                            console.log("s3 setValue")
                                            subTableFocusData[targetField.FieldCode]._field_old_value_ = subTableFocusData[targetField.FieldCode].value = data.rows[0]
                                            if (oldIndex !== newIndex) {
                                                context.setSubFocus(newIndex)
                                            }
                                            context.refreshSubDataRow(targetObject.title)
                                        }, resp => {
                                            console.error("子表赋值:SetSubFieldValue执行失败 resp:" + resp)
                                        });
wangcong committed
70 71 72
                                    }, resp => {
                                        console.error("子表赋值:SetSubFieldValue执行失败 resp:" + resp)
                                    });
qiaoyanqi committed
73 74 75 76
                                }
                            } else {
                                subTableFocusData[targetField.FieldCode]._field_old_value_ = subTableFocusData[targetField.FieldCode].value = null;
                                context.refreshSubDataRow(targetObject.title);
wangcong committed
77
                            }
qiaoyanqi committed
78 79 80
                            break
                        }
                        case FormulaConstants.billFiled.NULL: {
wangcong committed
81 82
                            subTableFocusData[targetField.FieldCode]._field_old_value_ = subTableFocusData[targetField.FieldCode].value = null;
                            context.refreshSubDataRow(targetObject.title);
qiaoyanqi committed
83 84 85 86
                            break
                        }
                        default : {
                            console.log("")
wangcong committed
87
                        }
qiaoyanqi committed
88 89 90
                    } 
                } catch (error) {
                    console.log(error)
wangcong committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
                }
            } else {
                console.error("SetSubFieldValue公式配置错误,暂不支持主表字段赋值")
                return null
            }
        }
    },
    getMDObject(param, callBack, errorCallBack) {
        GMS.$http.post("/baseData/data/list", param).then(data => {
            callBack(data.data);
        }).catch((resp) => {
            errorCallBack(resp);
        });
    },
    getResultType: function () {
        return FMR.ConstDataTypes.Void;
    }
}