123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589 |
- const db_user = require('../database/d_user')
- const d_air = require('../database/d_air')
- const c_flight = require('./c_flight');
- const handle = require('../until/handle')
- const field = require('../maps/field')
- const codeMap = require('../maps/rcodeMap')
- const {userType, orderType_all} = require("../maps/field");
- const {getUnixTimeStamp} = require("../until/time");
- const {reloadOrder, payOrderItem} = require("./TimeUpdate");
- async function checkAccount(account,userType= field.userType){
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err){throw err}
-
- if (result.length >= 1 ) {throw {rcode:codeMap.notFound,msg:'该账户已经被注册'}}
- return true;
- }
- async function login(userType = field.userType,account,passwd){
- let [err,result] = await handle(db_user.login(userType,account,passwd));
- if(err){throw err}
-
- if (result.length < 1 ) {throw {rcode:codeMap.notFound,msg:'账号或者密码错误'}}
-
- if (result[0].state == field.userFreezeState ){throw {rcode:codeMap.permissionDenied,msg:'账号被冻结'}}
- return result[0];
- }
- async function changePasswd(type,account,oldPasswd,newPasswd){
-
- let [err,result] = await handle(db_user.login(type,account,oldPasswd));
- if(err)throw err;
- if (result.length < 1 ) {throw {rcode:codeMap.notFound,msg:'密码错误'}}
- let id = result[0].id;
- [err,result] = await handle(db_user.changePasswd(id,newPasswd));
- if(err)throw err;
- return;
- }
- async function changePhone(type,account,passwd,newPhone){
-
- let [err,result] = await handle(db_user.login(type,account,passwd));
- if(err)throw err;
- if (result.length < 1 ) {throw {rcode:codeMap.notFound,msg:'密码错误'}}
-
- let id = result[0].id;
- [err,result] = await handle(db_user.findPhoneUser(type,newPhone));
- if(err)throw err;
- if(result[0].total){throw {rcode:codeMap.dataRepeat,msg:'手机号已经使用'}}
-
- [err,result] = await handle(db_user.changePhone(id,newPhone));
- if(err)throw err;
- return result;
- }
- async function register(type,account,passwd,nickName){
-
- let [err,result] = await handle(db_user.findAccountUser(type,account));
- if(err)throw err;
- if(result.length&&result[0].total){throw {rcode:codeMap.dataRepeat,msg:'账号重复'}}
-
- [err,result] = await handle(db_user.register(type,nickName,account,passwd));
- if(err)throw err;
-
- return result;
- }
- async function info(type,account){
-
- let [err,result] = await handle(db_user.info(type,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
-
- return result[0];
- }
- async function cars(account){
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- [err,result] = await handle(db_user.cars(result[0].id));
- if(err)throw err;
-
- return result;
- }
- async function removeCar(carId){
- let [err,result] = await handle(db_user.removeCar(carId));
- if(err)throw err;
- return result;
- }
- async function removeTravel(travelId){
- let [err,result] = await handle(db_user.removeTravel(travelId));
- if(err)throw err;
- return result;
- }
- async function addTravel(name,card,phone,account,isDefault = field.travelState_isDefault){
- let userId;
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- userId = result[0].id;
-
- [err,result] = await handle(db_user.addTravel(userId,name,card,phone,isDefault));
- if(err)throw err;
- return result;
- }
- async function updateTravel(account,travelId,params){
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
-
- [err,result] = await handle(db_user.findUserTravel(result[0].id,travelId));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法匹配用户的乘车人'}
- }
- [err,result] = await handle(db_user.changeTravel(travelId,{
- name:params.name,
- card:params.card,
- phone:params.phone,
- default:params.default
- }));
- if(err)throw err;
-
- return result;
- }
- async function travels(account){
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- [err,result] = await handle(db_user.travels(result[0].id));
- if(err)throw err;
-
- return result.map(val=>{
- let card = val.card.replace(val.card.substr(4,10),'**********')
- return {
- ...val,
- card:card
- }
- });
- }
- async function travelInfo(account,passwd,travelId){
-
- let [err,result] = await handle(db_user.login(userType,account,passwd));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- [err,result] = await handle(db_user.travelInfo(travelId));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到乘车人'}
- }
- return result[0];
- }
- async function addCar(flightId,account){
- let userId;
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- userId = result[0].id;
-
- [err,result] = await handle(d_air.flightInfo(flightId));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到航班,航班号错误'}
- }
- console.log(result[0]);
- if(result[0].flightState !== field.flightState_sail+''){
- throw {rcode:codeMap.customError,msg:'航班非销售状态'}
- }
- [err,result] = await handle(db_user.findCar(userId,flightId));
- if(err)throw err;
- if(result.length >= 1){
- throw {rcode:codeMap.customError,msg:'该航班已经在购物车中'}
- }
-
- [err,result] = await handle(db_user.addCar(flightId,userId));
- if(err)throw err;
- return result;
- }
- async function orders(account,orderType){
-
- let err,user,result;
- [err,user] = await handle(info(userType,account));
- if(err)throw err;
- console.log(orderType);
-
- [err,result] = await handle(db_user.userOrder(user.id,orderType));
- if(err)throw err;
- return result;
- }
- async function addOrder(account,flightId,travelIds){
- let flight,err,result;
- if(travelIds.length < 1){throw {rcode:codeMap.notParam,msg:'缺少乘车人'}}
-
- for(let i = 0;i<travelIds.length;i++){
- [err,result] = await handle(db_user.travelInfo(travelIds[0]));
- if(err)throw err;
- if(result.length<1){throw {rcode:codeMap.customError,msg:'不在数据表的乘机人'}}
- }
- let userId,unixTime = getUnixTimeStamp();
-
- [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- userId = result[0].id;
-
- [err,flight] = await handle(c_flight.flightInfo(flightId));
- if(err)throw err;
- if((parseInt(flight.pay) + travelIds.length) > parseInt(flight.totalVotes)){
- throw {rcode:codeMap.customError,msg:'航班机票不足'}
- }
- if(flight.flightState != field.flightState_sail){
- throw {rcode:codeMap.customError,msg:'该航班已经结束售卖'}
- }
-
- [err,result] = await handle(db_user.addOrder(userId,flightId,travelIds,unixTime));
- if(err)throw err;
- await reloadOrder();
-
- [err,result] = await handle(db_user.findOrder(userId,flightId,travelIds,unixTime));
- if(err)throw err;
- return result[0]||{};
- }
- async function refundOrder(account,orderId){
- let userId,order,ticks,isRefund = false;
-
-
- let [err,result] = await handle(info(userType,account));
- if(err)throw err;
- userId = result.id;
- [err,result] = await handle(db_user.userOrderInfo(userId,orderId));
- if(err)throw err;
- if(result.length < 1){ throw {rcode:codeMap.notFound,msg:'无法找到相关订单'} }
- order = result[0];
-
- [err,ticks] = await handle(db_user.orderTicks(order.id));
- if(err)throw err;
-
- for (const tick of ticks) {
- if(tick.tickState == field.tickState_seat){
- throw {rcode:codeMap.customError,msg:'该订单部分机票已经值机不允许退票'}
- }
- }
-
- [err,result] = await handle(db_user.changeOrder(orderId, {payState:field.payState_refund}));
- if(err)throw err;
-
- [err,result] = await handle(db_user.refundOrderTick(orderId));
- if(err)throw err;
- return result;
- }
- async function payOrder(account,passwd,orderId){
- let userId,order,flight,travels;
-
-
- let [err,result] = await handle(login(userType,account,passwd));
- if(err)throw err;
- userId = result.id;
- [err,result] = await handle(db_user.userOrderInfo(userId,orderId));
- if(err)throw err;
- if(result.length < 1){ throw {rcode:codeMap.notFound,msg:'无法找到相关订单'} }
- order = result[0];
- if(order.payState!=field.payState_create){throw {rcode:codeMap.notFound,msg:'无法支付非未支付订单'}}
- [err,flight] = await handle(c_flight.flightInfo(order.flightId));
- if(err)throw err;
- if(flight.flightState!=field.flightState_sail){throw {rcode:codeMap.notFound,msg:'航班已经开始检票或者起飞,无法支付'}}
-
- travels=order.travelIds.split(',');
- for(let i = 0;i<travels.length;i++){
-
- [err,result] = await handle(db_user.addTick(order.id,travels[i]));
- if(err){
-
- [err,result] = await handle(db_user.clearTick(order.id));
- }
- }
- let unitPrice = parseFloat(flight.currentPrice);
- let payPrice = parseFloat(flight.currentPrice) * travels.length;
- [err,result] = await handle(db_user.payOrder(orderId,unitPrice,payPrice,getUnixTimeStamp()));
- if(err){console.log(err);throw err}
- await reloadOrder();
- return result;
- }
- async function chooseSit(account,tickId,row,col){
- let tick,order,travels;
-
- let [err,result] = await handle(db_user.tickInfo(tickId))
- if(err)throw err;
- if(result.length < 1){throw {rcode:codeMap.notFound,msg:'无法找到机票'}}
- tick = result[0];
- [err,order] = await handle(orderInfo(account,tick.orderId))
- if(err)throw err;
-
- [err,result] = await handle(db_user.findTickRowCol(order.flightId,row,col))
- if(err)throw err;
- if(result.length > 0){throw {rcode:codeMap.dataRepeat,msg:'该位置已经被占用了呢'}}
-
- [err,result] = await handle(db_user.tickChooseToSel(tickId,row,col,getUnixTimeStamp()))
- if(err)throw err;
-
- travels=order.travelIds.split(',');
- [err,result] = await handle(db_user.tickSearch({
- orderId: order.id,
- tickState: field.tickState_seat
- }));
- if(err) {throw err}
-
- if(result.length === travels.length){
- [err,result] = await handle(db_user.changeOrder(order.id,{payState:field.payState_choose,chooseNum:result.length}));
- if(err) {throw err}
- }else{
-
- [err,result] = await handle(db_user.changeOrder(order.id,{chooseNum:result.length}));
- if(err) {throw err}
- }
- return result;
- }
- async function refundTick(account,tickId){
- let tick,order,travels;
-
- let [err,result] = await handle(db_user.tickInfo(tickId))
- if(err)throw err;
- if(result.length < 1){throw {rcode:codeMap.notFound,msg:'无法找到机票'}}
- tick = result[0];
- if(tick.tickState != field.tickState_create){throw {rcode:codeMap.customError,msg:'该机票不允许退款'}}
-
- [err,result] = await handle(db_user.refundTick(tickId))
- if(err)throw err;
-
- [err,order] = await handle(orderInfo(account,tick.orderId))
- if(err)throw err;
-
- travels=order.travelIds.split(',');
- [err,result] = await handle(db_user.tickSearch({
- orderId: order.id,
- tickState: field.tickState_refund
- }));
- if(err) {throw err}
-
- if(result.length === travels.length){
- [err,result] = await handle(db_user.changeOrder(order.id,{payState:field.payState_refund}));
- if(err) {throw err}
- }else{
- [err,result] = await handle(db_user.changeOrder(order.id,{payState:field.payState_rebates}));
- if(err) {throw err}
- }
- return result;
- }
- async function orderInfo(account,orderId){
- let userId,order,travels,ticks,tmpTravels=[];
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- userId = result[0].id;
- [err,result] = await handle(db_user.userOrderInfo(userId,orderId));
- if(err)throw err;
- if(result.length < 1){ throw {rcode:codeMap.notFound,msg:'无法找到相关订单'} }
- order = result[0];
-
- [err,ticks] = await handle(db_user.orderTicks(orderId));
- order.ticks = ticks
-
- travels=order.travelIds.split(',');
- for (let i = 0; i < travels.length ; i++) {
- [err,result] = await handle(db_user.travelInfo(travels[i]));
- if(err) {console.log('获取乘车人信息失败'); throw err}
- if(result.length < 1){ throw {rcode:codeMap.notFound,msg:'无法找到乘车人'} }
- console.log(result);
- tmpTravels.push({
- id:result[0].id,
- name:result[0].name,
- })
- }
- order.travels = tmpTravels;
- return order;
- }
- async function changeOrderTravel(account,orderId,travelIds){
- let userId,order,travels;
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- [err,result] = await handle(db_user.userOrderInfo(userId,orderId));
- if(err)throw err;
- if(result.length < 1){ throw {rcode:codeMap.notFound,msg:'无法找到相关订单'} }
- order = result[0];
-
- if(order.payState == field.payState_create || order.payState == field.payState_pay){
- }else{
- throw {rcode:codeMap.notFound,msg:'当前订单不允许修改乘车人'}
- }
- }
- module.exports = {
- register,
- changePhone,
- changePasswd,
- checkAccount,
- login,
- info,
- removeCar,
- addCar,
- cars,
- removeTravel,
- addTravel,
- travels,
- travelInfo,
- updateTravel,
- orders,
- addOrder,
- chooseSit,
- payOrder,
- orderInfo,
- refundTick,
- refundOrder,
- changeOrderTravel
- }
|