Bladeren bron

航班与城市相关数据库操作

kindring 3 jaren geleden
bovenliggende
commit
a81a3bad8b
5 gewijzigde bestanden met toevoegingen van 218 en 8 verwijderingen
  1. 147 6
      database/d_air.js
  2. 58 0
      database/d_area.js
  3. 2 2
      db.md
  4. 4 0
      maps/field.js
  5. 7 0
      until/checkArgumentsIsEmpty.js

+ 147 - 6
database/d_air.js

@@ -2,6 +2,8 @@ const field = require('../maps/field');
 const mysql = require('./mysql');
 const until_time = require('../until/time');
 const {getUnixTimeStamp} = require("../until/time");
+const code = require("../maps/rcodeMap");
+const checkArgumentsIsEmpty = require("../until/checkArgumentsIsEmpty");
 // 查询指定城市航班
 
 
@@ -14,7 +16,7 @@ const {getUnixTimeStamp} = require("../until/time");
  * @param [endUnixTime] 出发时间截止,某个时间段内的
  * @returns {Promise | Promise<unknown>}
  */
-function fightSearch(departureCity,targetCity,routeType,startUnixTime,endUnixTime){
+function flightSearch(departureCity,targetCity,routeType,startUnixTime,endUnixTime){
     let sql=``,values=[];
     sql = `select f.* ,dep.cityname as departureCityName,tar.cityname as targetCityName from
             flight as f
@@ -43,14 +45,153 @@ function fightSearch(departureCity,targetCity,routeType,startUnixTime,endUnixTim
 
 /**
  * 获取航班票数
- * @param fightId
+ * @param flightId
  * @returns {Promise<unknown>}
  */
-function fightTicks(fightId){
+function flightTicks(flightId){
     let sql=``,values=[];
-    sql=`select ff.*,count(t.flightId = 1 or null) as pay
+    sql=`select ff.*,count(t.flightId = ? or null) as pay
             from 
-            (select totalVotes from flight where id = 1) as ff,
-            airTickets as t`
+            (select totalVotes from flight where id = ?) as ff,
+            airTickets as t`;
+    values.push(flightId,flightId);
     return mysql.pq(sql,values);
 }
+
+/**
+ * 添加航班
+ * @param flightName 航班名
+ * @param airplaneCode 飞机代码
+ * @param originalPrice 原始价格
+ * @param currentPrice 当前价格
+ * @param sailingTime 起飞时间
+ * @param langdinTime 到站时间
+ * @param totalVotes 机票数量
+ * @param routeType 线路类型,国际or国内
+ * @param departureCity 出发城市
+ * @param targetCity 目标城市
+ * @returns {Promise<unknown>}
+ */
+function addFlight(flightName,
+                   airplaneCode,
+                   originalPrice,
+                   currentPrice,
+                   sailingTime,
+                   langdinTime,
+                   totalVotes,
+                   routeType,
+                   departureCity,
+                   targetCity){
+    let sql = ``,values = [];
+    sql = `insert into flight 
+    (
+        flightName,airplaneCode,
+        originalPrice,currentPrice,
+        sailingTime,langdinTime,
+        totalVotes,routeType,
+        departureCity,targetCity
+    ) values(
+        ?,?,
+        ?,?,
+        ?,?,
+        ?,?
+        )`;
+    values.push(...arguments);
+    sql += ';'
+    return mysql.pq(sql,values);
+}
+
+/**
+ * 修改航班信息
+ * @param flightId
+ * @param flightName
+ * @param airplaneCode
+ * @param originalPrice
+ * @param currentPrice
+ * @param sailingTime
+ * @param langdinTime
+ * @param totalVotes
+ * @param routeType
+ * @param departureCity
+ * @param targetCity
+ * @returns {Promise<unknown>}
+ */
+function updateFlight(
+                      flightId,
+                      flightName,
+                      airplaneCode,
+                      originalPrice,
+                      currentPrice,
+                      sailingTime,
+                      langdinTime,
+                      totalVotes,
+                      routeType,
+                      departureCity,
+                      targetCity){
+    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)
+    }
+    // 目标城市
+    if(targetCity){
+        sql+=' targetCity = ?'
+        values.push(targetCity)
+    }
+    sql += ` where id = ?;`
+    values.push(flightId);
+    return mysql.pq(sql,values);
+}
+
+
+function recommendFlight(){
+
+}

+ 58 - 0
database/d_area.js

@@ -0,0 +1,58 @@
+const mysql = require('./mysql')
+const field = require('../maps/field')
+const code = require('../maps/rcodeMap')
+const checkArgumentsIsEmpty = require('../until/checkArgumentsIsEmpty')
+// 地区管理
+
+/**
+ * 新增地区
+ * @param cityType 地区类型国际或者国内
+ * @param cityName 城市名称
+ * @returns {Promise<unknown>}
+ */
+function addArea(cityType = field.cityType_domestic,cityName){
+    let sql=``,values=[];
+    sql+=`insert into (cityname,cityType) values(?,?);`
+    values.push(cityName,cityType)
+    return mysql.pq(sql,values);
+}
+
+/**
+ * 搜索地区
+ * @param [cityType] 城市类型
+ * @returns {Promise<unknown>}
+ */
+function searchAreas(cityType){
+    let sql=``,values=[];
+    sql+=`select * from area`
+    if(cityType){
+        sql+=' where cityType = ?'
+        values.push(cityType)
+    }
+    sql+=`;`
+    return mysql.pq(sql,values);
+}
+
+/**
+ * 更新指定城市
+ * @param cityId 城市id
+ * @param cityType 新的城市类型
+ * @param cityName 新的城市名
+ * @returns {Promise<unknown>}
+ */
+function updateCity(cityId,cityType,cityName){
+    let sql=``,values=[];
+    if(checkArgumentsIsEmpty(Array.from(arguments))){throw {rcode:code.notParam}}
+    sql+=`update area set`
+    if(cityType){
+        sql+=' cityType = ?'
+        values.push(cityType)
+    }
+    if(cityName){
+        sql+=' cityname = ?'
+        values.push(cityName)
+    }
+    sql += ` where id = ?;`
+    values.push(cityId);
+    return mysql.pq(sql,values);
+}

+ 2 - 2
db.md

@@ -31,7 +31,7 @@
 | cityState | int     | 1    | 1启用2封禁 | 封禁 |
 
 
-## 航班表fight(管理员通过航线创建航班),需要输入价格和指定飞机和起飞时间
+## 航班表flight(管理员通过航线创建航班),需要输入价格和指定飞机和起飞时间
 | 字段            | 类型      | 默认值 | 可选值                      | 备注     |
 |---------------|---------|-----|--------------------------|--------|
 | id            | int     | pk  | n                        | id     |
@@ -47,7 +47,7 @@
 | routeType     | char | 1 | 1(国内),2(国际) | 航线类型,是否为跨国航线,程序自动生成 | 
 | departureCity | int  | null | n           | 出发城市          |
 | targetCity    | int  | null | n           | 目标城市          |
-| fightName | varchar | '' | '' | 航班名称 | 
+| flightName    | varchar | '' | '' | 航班名称 | 
 
 ## 机票表 airTickets
 | 字段       | 类型      | 默认值 | 可选值 | 备注                  |

+ 4 - 0
maps/field.js

@@ -12,4 +12,8 @@ module.exports = {
     routeType_domestic:1,
     // 国际航班
     routeType_international:2,
+    // 国内城市
+    cityType_domestic:1,
+    // 国际城市
+    cityType_international:2,
 }

+ 7 - 0
until/checkArgumentsIsEmpty.js

@@ -0,0 +1,7 @@
+function checkArgumentsIsEmpty(array){
+    return (-1 == array.findIndex((val,ind)=>{
+        return val && ind !=0
+    }))
+}
+
+module.exports = checkArgumentsIsEmpty;