nodejs中的条件插入语句

时间:2018-06-18 11:31:32

标签: node.js

我需要根据用户的输入有条件地插入数据库

这是我的功能到目前为止的样子:

ManageBillsRepo.prototype.addBill=function (billObj,billServices, memberships, prepaidCards, customPackages) {
    var qryToInsertSalonBills="INSERT INTO salon_client_billing SET ?"
    var qryToInsertBillServices="INSERT INTO salon_appointment_billing_services (bill_id, service_id, service_quantity,service_beautician_id, type) VALUES ?";
    var qryToInsertMembership="INSERT INTO salon_client_memberships SET ?";
    var qryToInsertCustomPackge="INSERT INTO salon_client_customPackage SET ?";
    var qryToInsertPrepaidCard="INSERT INTO salon_client_prepaidCard SET ? ";
    return  fn.defer(webocitySalonPOSConnection.query , webocitySalonPOSConnection)(qryToInsertSalonBills, billObj).to(function (resAddBill) {
        console.log("bill services", billServices)
        let toSaveObjArr = billServices.reduce(function (acc, val) {
            let temp = [];
            temp.push(resAddBill.insertId);
            temp.push(val.service_id);
            temp.push(val.service_quantity);
            temp.push(val.service_beautician_id);
            temp.push(val.type)
            acc.push(temp);
            return acc;
        }, []);

        if(memberships && memberships.length > 0){
            let toSaveMembershipsArr = memberships.reduce(function (acc, val) {
                console.log("val", val)
                let temp = [];
                temp.push(resAddBill.insertId);
                temp.push(billObj.client_id);
                temp.push(billObj.salon_id);
                temp.push(val.membership_id);
                temp.push(val.redeemable_amount)
                acc.push(temp);
                return acc;
            }, []);

            console.log("memberships are", toSaveMembershipsArr)
             return fn.defer(webocitySalonPOSConnection.query , webocitySalonPOSConnection)(qryToInsertMembership,[toSaveMembershipsArr] ).to(function (resAddMems) {
              console.log("resAddMems", resAddMems)
                return resAddMems
            });

         }

        if(customPackages && customPackages.length > 0){
            let toSaveCustomPackagesArr = customPackages.reduce(function (acc, val) {
                console.log("val", val)
                let temp = [];
                temp.push(resAddBill.insertId);
                temp.push(billObj.client_id);
                temp.push(billObj.salon_id);
                temp.push(val.services);
                acc.push(temp);
                return acc;
            }, []);
            console.log("customPackages are", toSaveCustomPackagesArr)

             return fn.defer(webocitySalonPOSConnection.query , webocitySalonPOSConnection)(qryToInsertCustomPackge,[toSaveCustomPackagesArr] ).to(function (resAddBillServices) {

            });

        }

        if(prepaidCards && prepaidCards.length > 0){
            let toSaveprepaidCardsArr = prepaidCards.reduce(function (acc, val) {
                console.log("val", val)
                let temp = [];
                temp.push(billObj.invoice_number);
                temp.push(billObj.client_id);
                temp.push(billObj.salon_id);
                temp.push(val.prepaidcard_id);
                temp.push(val.redeemable_amount)
                acc.push(temp);
                return acc;
            }, []);
            console.log("prepaidCards are", toSaveprepaidCardsArr)

              return fn.defer(webocitySalonPOSConnection.query , webocitySalonPOSConnection)(qryToInsertPrepaidCard,[toSaveprepaidCardsArr] ).to(function (resAddBillServices) {

            });

        }

        return  fn.defer(webocitySalonPOSConnection.query , webocitySalonPOSConnection)(qryToInsertBillServices,[toSaveObjArr] ).to(function (resAddBillServices) {
            return resAddBill;
        });
    });
};

但是,在这种情况下,我不打算在第一次插入后return,并跳过返回时发生的情况。实现这一目标的最佳途径是什么?

defer函数供参考:

function defer(fn, parent) {
    return function () {
        var args = Array.prototype.slice.call(arguments, 0);
        return deferred.defer(function (callbacks) {
            function callback(err, result) {
                if (err) {
                    callbacks.failure(err);
                } else {
                    callbacks.success(result);
                }
            }

            args.push(callback);
            fn.apply(parent, args);
        });
    };
}

0 个答案:

没有答案