Bladeren bron

航班查询相关

kindring 3 jaren geleden
bovenliggende
commit
63555b1dc7
4 gewijzigde bestanden met toevoegingen van 70 en 97 verwijderingen
  1. 38 6
      controller/c_flight.js
  2. 11 74
      database/d_air.js
  3. 11 11
      routes/flight_api.js
  4. 10 6
      until/handle.js

+ 38 - 6
controller/c_flight.js

@@ -111,21 +111,53 @@ async function addFlight(
  */
 async function updateFlight(flightId,updateOption){
     let err,result,departCityType,targetCityType,routerType;
+    let updateOptions = {}
+
+    // 如果修改了城市,那么直接修改对应的航班类型
     if(updateOption.departureCity){
-        // 获取城市类型
-        [err,departCityType] = await handle(db_area.cityType(departureCity));
+        [err,departCityType] = await handle(db_area.cityType(updateOption.departureCity));
+        if(err) throw err;
+        departCityType = departCityType[0].cityType;
+        if(departCityType){
+            // 判断是否为国内城市
+            if(departCityType==field.cityType_international){
+                routerType = field.routeType_international;
+            }
+        }
     }
-    if(updateOption.departureCity){
-        // 获取城市类型
-        [err,departCityType] = await handle(db_area.cityType(departureCity));
+    if(updateOption.targetCity){
+        [err,targetCityType] = await handle(db_area.cityType(updateOption.departureCity));
+        if(err) throw err;
+        targetCityType = targetCityType[0].cityType;
+        if(targetCityType) {
+            // 判断是否为国内城市
+            if (targetCityType == field.cityType_international) {
+                routerType = field.routeType_international;
+            }
+        }
     }
 
-    [err,targetCityType] = await handle(db_area.cityType(targetCity));
+    if(routerType){
+        updateOptions.routeType = routerType;
+    }
+    updateOptions.flightName = updateOption.flightName;
+    updateOptions.airCode = updateOption.airCode;
+    updateOptions.originalPrice = updateOption.originalPrice;
+    updateOptions.currentPrice = updateOption.currentPrice;
+    updateOptions.seilingTime = updateOption.seilingTime;
+    updateOptions.langdinTime = updateOption.langdinTime;
+    updateOptions.totalVotes = updateOption.totalVotes;
+    updateOptions.departureCity = updateOption.departureCity;
+    updateOptions.targetCity = updateOption.targetCity;
+    [err,result] = await handle(db_air.updateFlight(flightId,updateOptions));
+    if(err) throw err;
+    return result
 }
 
 module.exports = {
     searchFlight,
     flightList,
     addFlight,
+    updateFlight,
     flightInfo
 }

+ 11 - 74
database/d_air.js

@@ -138,87 +138,24 @@ function addFlight(flightName,
 /**
  * 修改航班信息
  * @param flightId
- * @param flightName
- * @param airplaneCode
- * @param originalPrice
- * @param currentPrice
- * @param sailingTime
- * @param langdinTime
- * @param totalVotes
- * @param routeType
- * @param departureCity
- * @param targetCity
+ * @param updateOptions
  * @returns {Promise<unknown>}
  */
-function updateFlight(
-                      flightId,
-                      flightName,
-                      airplaneCode,
-                      originalPrice,
-                      currentPrice,
-                      sailingTime,
-                      langdinTime,
-                      totalVotes,
-                      routeType,
-                      departureCity,
-                      targetCity){
+function updateFlight(flightId,updateOptions){
     let sql=``,values=[];
     let _arguments = Array.from(arguments);
 
     // 判断如果所有参数都为空时抛出异常
-    if(checkArgumentsIsEmpty(Array.from(arguments))){
-        throw {rcode:code.notParam}
-    }
-    sql+=`update area set`
-    //航班名
-    if(flightName){
-        sql+=' flightName = ?'
-        values.push(flightName)
-    }
-    //机票代码
-    if(airplaneCode){
-        sql+=' airplaneCode = ?'
-        values.push(airplaneCode)
-    }
-    //原始价格
-    if(originalPrice){
-        sql+=' originalPrice = ?'
-        values.push(originalPrice)
-    }
-    // 当前价格
-    if(currentPrice){
-        sql+=' currentPrice = ?'
-        values.push(currentPrice)
-    }
-    //起飞时间
-    if(sailingTime){
-        sql+=' sailingTime = ?'
-        values.push(sailingTime)
-    }
-    // 登录时间
-    if(langdinTime){
-        sql+=' langdinTime = ?'
-        values.push(langdinTime)
-    }
-    //票数
-    if(totalVotes){
-        sql+=' totalVotes = ?'
-        values.push(totalVotes)
-    }
-    //航线类型
-    if(routeType){
-        sql+=' routeType = ?'
-        values.push(routeType)
-    }
-    //出发城市
-    if(departureCity){
-        sql+=' departureCity = ?'
-        values.push(departureCity)
+    sql+=`update from flight set`
+    let keys = Object.keys(updateOptions);
+    if(keys.length < 1){
+        throw {rcode:code.notParam,msg:'没有修改项'}
     }
-    // 目标城市
-    if(targetCity){
-        sql+=' targetCity = ?'
-        values.push(targetCity)
+    for(let i = 0;i<keys.length;i++){
+        let field = keys[i];
+        if(i>0){sql+=','}
+        sql+=`${field} = ?`
+        values.push(updateOptions[field])
     }
     sql += ` where id = ?;`
     values.push(flightId);

+ 11 - 11
routes/flight_api.js

@@ -137,17 +137,17 @@ router.post('/update',
     }),
     async (req,res)=>{
         try{
-            let results = await c_flight.(
-                req.body.flightName,
-                req.body.airCode,
-                req.body.originalPrice,
-                req.body.currentPrice,
-                req.body.seilingTime,
-                req.body.langdinTime,
-                req.body.totalVotes,
-                req.body.departureCity,
-                req.body.targetCity,
-            );
+            let results = await c_flight.updateFlight({
+                flightName:req.body.flightName,
+                airCode:req.body.airCode,
+                originalPrice:req.body.originalPrice,
+                currentPrice:req.body.currentPrice,
+                seilingTime:req.body.seilingTime,
+                langdinTime:req.body.langdinTime,
+                totalVotes:req.body.totalVotes,
+                departureCity:req.body.departureCity,
+                targetCity:req.body.targetCity
+            });
             res.json({
                 rcode: code.ok,
                 data: results

+ 10 - 6
until/handle.js

@@ -8,12 +8,16 @@
  */
 function handle(promise) {
     return new Promise(resolve => {
-        promise.then(val => {
-            resolve([null, val])
-        }).catch(err => {
-            resolve([err])
-        })
+        try{
+            promise.then(val => {
+                resolve([null, val])
+            }).catch(err => {
+                resolve([err])
+            })
+        }catch (e) {
+            resolve([e]);
+        }
     })
 }
 
-module.exports = handle;
+module.exports = handle;