我从客户端发送一个像这样的对象
var condition={"bedrooms":"1,2,3,4","Inhibition":"1,6","possession":"3","id":"8",toilets:"1,2",...,}
所以它必须是相同的键,只能在单个对象中,但长度可能是1/2/3/4 / .. 像这个例子:
var condition={"bedrooms":"1,2,3,4","Inhibition":"1,6"};
var condition={"bedrooms":"1,2,3,4"};
var condition={"Inhibition":"1,6"};
var condition={"id":"1,6"};
var condition={"toilets":"1,6","possession":"3"};
var condition={"possession":"3"};
像这样,我有10把钥匙
所以基于我,我得到了近100个不同场景的场景,我必须返回不同的对象。如何通过使用switch来处理所有这些场景,如果不正确的话。
我试过这个
function comparison(condition,requestId,dbId){
var obj={};
var andCondition=[];
var subCondition={};
switch(true) {
case (condition.hasOwnProperty("Inhibition") && !condition.hasOwnProperty("bedrooms") && !condition.hasOwnProperty("budgetPrice") && !condition.hasOwnProperty("possession")): {
subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_property`.`hp_property_inhibition_status_id` IN (" + condition.Inhibition + ")"));
obj['$and']=andCondition;
return obj;
};
break;
case (condition.hasOwnProperty("Inhibition") && condition.hasOwnProperty("bedrooms") && !condition.hasOwnProperty("budgetPrice") && !condition.hasOwnProperty("possession")): {
subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_property`.`hp_property_inhibition_status_id` IN (" + condition.Inhibition + ")"),Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"));
obj['$and']=andCondition;
return obj;
}
break;
case (condition.hasOwnProperty("Inhibition") && condition.hasOwnProperty("bedrooms") && condition.hasOwnProperty("budgetPrice") && !condition.hasOwnProperty("possession")): {
var budgetPrice = condition.budgetPrice.split(",");
var minRange = budgetPrice[0];
var maxRange = budgetPrice[1];
subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_property`.`hp_property_inhibition_status_id` IN (" + condition.Inhibition + ")"),Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"),Sequelize.literal("`hp_unit_details.hp_unit_prices`.`price_per_sqft` * `hp_unit_details`.`property_size` BETWEEN " + minRange + " AND " + maxRange));
obj['$and']=andCondition;
return obj;
}
break;
case (condition.hasOwnProperty("Inhibition") && condition.hasOwnProperty("bedrooms") && condition.hasOwnProperty("budgetPrice") && condition.hasOwnProperty("possession")): {
var budgetPrice = condition.budgetPrice.split(",");
var minRange = budgetPrice[0];
var maxRange = budgetPrice[1];
subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_property`.`hp_property_inhibition_status_id` IN (" + condition.Inhibition + ")"),Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"),Sequelize.literal("`hp_unit_details.hp_unit_prices`.`price_per_sqft` * `hp_unit_details`.`property_size` BETWEEN " + minRange + " AND " + maxRange),Sequelize.literal("`hp_property`.`property_completion_date` between now() and DATE_ADD(now(), INTERVAL "+condition.possession+" MONTH)"));
obj['$and']=andCondition;
return obj;
}
break;
case (condition.hasOwnProperty("bedrooms") && condition.hasOwnProperty("budgetPrice")): {
var budgetPrice = condition.budgetPrice.split(",");
var minRange = budgetPrice[0];
var maxRange = budgetPrice[1];
subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"),Sequelize.literal("`hp_unit_details.hp_unit_prices`.`price_per_sqft` * `hp_unit_details`.`property_size` BETWEEN " + minRange + " AND " + maxRange));
obj['$and']=andCondition;
return obj;
}
break;
case (condition.hasOwnProperty("bedrooms") && !condition.hasOwnProperty("budgetPrice") && !condition.hasOwnProperty("possession")): {
subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"))
obj['$and']=andCondition;
return obj;
}
break;
case (condition.hasOwnProperty("budgetPrice") && !condition.hasOwnProperty("bedrooms") && !condition.hasOwnProperty("possession")): {
var budgetPrice = condition.budgetPrice.split(",");
var minRange = budgetPrice[0];
var maxRange = budgetPrice[1];
subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_unit_details.hp_unit_prices`.`price_per_sqft` * `hp_unit_details`.`property_size` BETWEEN " + minRange + " AND " + maxRange))
obj['$and']=andCondition;
return obj;
}
break;
case (condition.hasOwnProperty("possession") && !condition.hasOwnProperty("Inhibition") && !condition.hasOwnProperty("bedrooms") && !condition.hasOwnProperty("budgetPrice")): {
subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_property`.`property_completion_date` between now() and DATE_ADD(now(), INTERVAL "+condition.possession+" MONTH)"))
obj['$and']=andCondition;
return obj;
}
break;
case (condition.hasOwnProperty("possession") && condition.hasOwnProperty("bedrooms") && !condition.hasOwnProperty("Inhibition") && !condition.hasOwnProperty("budgetPrice")): {
subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_property`.`property_completion_date` between now() and DATE_ADD(now(), INTERVAL "+condition.possession+" MONTH)"),Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"))
obj['$and']=andCondition;
return obj;
}
break;
case (condition.hasOwnProperty("possession") && condition.hasOwnProperty("bedrooms") && condition.hasOwnProperty("Inhibition") && !condition.hasOwnProperty("budgetPrice")): {
subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_property`.`property_completion_date` between now() and DATE_ADD(now(), INTERVAL "+condition.possession+" MONTH)"),Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"),Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"),Sequelize.literal("`hp_property`.`hp_property_inhibition_status_id` IN (" + condition.Inhibition + ")"))
obj['$and']=andCondition;
return obj;
}
break;
default:{
obj[dbId] =requestId
return obj;
}
}
}
答案 0 :(得分:1)
看起来您可以轻松地分别处理每个条件
类似
var andCondition = [];
subCondition[dbId]=requestId;
andCondition.push(subCondition)
if(condition.hasOwnProperty("Inhibition")) {
andCondition.push(Sequelize.literal("`hp_property`.`hp_property_ inhibition_status_id` IN (" + condition.Inhibition + ")"));
};
if(condition.hasOwnProperty("bedrooms")) {
andCondition.push(Sequelize.literal("`hp_unit_details`.`hp_unit_ type_id` IN (" + condition.bedrooms + ")");
}
if(condition.hasOwnProperty("budgetPrice")) {
var budgetPrice = condition.budgetPrice.split(",");
var minRange = budgetPrice[0];
var maxRange = budgetPrice[1];
andCondition.push(Sequelize.literal("`hp_unit_details.hp_unit_prices`.`price_ per_sqft` * `hp_unit_details`.`property_size` BETWEEN " + minRange + " AND " + maxRange));
}
if(condition.hasOwnProperty("possession")): {
andCondition.push(Sequelize.literal("`hp_property`.`property_completion_date` between now() and DATE_ADD(now(), INTERVAL "+condition.possession+" MONTH)"));
}
obj['$and']=andCondition;
return obj;