/var/www/myprog.com.ua/server/node/db
Edit: /var/www/myprog.com.ua/server/node/db/db_context_users.js (7944B)
const User = require('../objects/user');
const log = require('../helpers/log');
module.exports = (DbContext) => {
DbContext.prototype.get_user = async function (id) {
let t1 = new Date().microtime(true);
let user = null;
let sql = `SELECT u.* FROM users u
WHERE u.id=${id} LIMIT 1`;
//log.debug('DbContext', 'get_user()', `SQL: ${sql}`);
user = await get_user(sql, this);
//log.debug('DbContext', 'get_user()', `complete ${new Date().microtime(true) - t1}`);
return user;
}
DbContext.prototype.login = async function (login,pass) {
let t1 = new Date().microtime(true);
let user = null;
let sql = `SELECT u.*
FROM users u
WHERE u.login='${login}' AND u.pass='${pass}' LIMIT 1`
//log.debug('DbContext', 'login()', `SQL: ${sql}`);
user = await get_user(sql, this);
//log.debug('DbContext', 'login()', `complete ${new Date().microtime(true) - t1}`);
return user;
}
DbContext.prototype.get_user_by_key = async function (key) {
let t1 = new Date().microtime(true);
let client = null;
let sql = `SELECT u.*
FROM users u
WHERE u.key='${key}' LIMIT 1`
//log.debug('DbContext', 'get_user_by_key()', `SQL: ${sql}`);
client = await get_user(sql, this);
//log.debug('DbContext', 'get_user_by_key()', `complete ${new Date().microtime(true) - t1}`);
return client;
}
DbContext.prototype.update_client = async function (client) {
let t1 = new Date().microtime(true);
let _self = this;
//await this.query('START TRANSACTION');
try {
// добавить или обновить клиента
let sql = _self.sql_insert_or_update(client, 'clients');
//log.debug('DbContext', 'update_client()', `SQL: ${sql}`);
let result = await _self.query(sql);
// обновить ид клиента если создан
if (result.insertId > 0) {
client.id = result.insertId;
}
let t2 = new Date().microtime(true);
//log.debug('DbContext', 'update_client()', `complete ${t2 - t1}`);
return client;
} catch (e) {
// ошибка откат
log.error('DbContext', 'update_client()', e);
//await _self.query('ROLLBACK');
return null;
}
}
DbContext.prototype.get_clients = async function (skip, take, order_by) {
let _self = this;
let t1 = new Date().microtime(true);
let clients = [];
if (!take || take == 0) {
take = 10;
}
if (!skip) {
skip = 0;
}
if (!order_by) {
order_by = 'asc'
}
// get clients
let sql = `SELECT c.*, sa.activated, sa.client_key FROM clients c
LEFT JOIN sms_activation sa ON c.id=sa.client_id ORDER BY c.id ${order_by} LIMIT ${skip},${take}`;
//log.debug('DbContext', 'get_clients()', `SQL: ${sql}`);
let rows_clients = await _self.query(sql);
rows_clients.forEach(async (row_client) => { // add clients
let client = new Client(row_client);
clients.push(client);
// get client phones
client.set_phones(await _self.get_client_phones(client.id));
});
let t2 = new Date().microtime(true);
//log.debug('DbContext', 'get_clients()', `complete ${t2 - t1}`);
return clients;
}
async function get_user(sql, instance) {
let t1 = new Date().microtime(true);
let user;
//log.debug('DbContext', 'get_user()', `SQL: ${sql}`);
let rows = await instance.query(sql);
user = new User(rows[0]);
if (user != null) {
// get user phones
//user.set_phones(await instance.get_user(client.id));
}
let t2 = new Date().microtime(true);
//log.debug('DbContext', 'get_user()', `complete ${t2 - t1}`);
return user;
}
DbContext.prototype.update_client_balance = async function (options) {
let t1 = new Date().microtime(true);
let _self = this;
let client = await _self.get_client(options.client_id);
if (options) {
if (!options.who_id) {
options.who_id = 0;
}
if (!options.who) {
options.who = WHO.SYSTEM.KEY;
}
if (!options.comment) {
options.comment = '';
}
if (!options.order_id) {
options.order_id = 0;
}
if (!options.car_id) {
options.car_id = 0;
}
if (!options.driver_id) {
options.driver_id = 0;
}
if (!options.b_order_id) {
options.b_order_id = 0;
}
if (!options.plus) {
options.plus = 0;
}
if (!options.minus) {
options.minus = 0;
}
if (client && (options.plus != 0 || options.minus != 0)) {
let sql = '';
if (options.driver_id > 0) {
sql = `SELECT * FROM \`clients_balance_history\`
WHERE \`order_id\`='${options.order_id}' AND \`driver_id\`='${options.driver_id}' LIMIT 1`;
} else {
sql = `SELECT * FROM \`clients_balance_history\`
WHERE \`order_id\`='${options.order_id}'
AND \`client_id\`='${options.client_id}'
AND \`driver_id\`='0' LIMIT 1`;
}
log.debug('DbContext', 'update_client_balance_history()', `SQL: ${sql}`);
let rows = await _self.query(sql);
if (rows.length === 0) {
let before = client.get_balance();
let after = before;
if (options.plus > 0) {
after = client.balance_plus(options.plus);
}
if (options.minus > 0) {
after = client.balance_minus(options.minus);
}
sql = `INSERT INTO \`clients_balance_history\`
SET \`client_id\`='${options.client_id}',\`order_id\`='${options.order_id}',\`car_id\`='${options.car_id}',\`driver_id\`='${options.driver_id}',
\`plus\`='${options.plus}',\`minus\`='${options.minus}',\`before\`='${before}',\`after\`='${after}',\`comment\`='${options.comment}',
\`who\`='${options.who}',\`who_id\`='${options.who_id}', \`b_order_id\`='${options.b_order_id}'`;
log.debug('DbContext', 'update_client_balance_history()', `SQL: ${sql}`);
let result = await _self.query(sql);
if (result.insertId > 0) {
if (options.driver_id > 0) {
sql = `UPDATE \`clients\` SET \`balance\`='${after}' WHERE \`id\`='${options.driver_id}';`;
} else {
sql = `UPDATE \`clients\` SET \`balance\`='${after}' WHERE \`id\`='${options.client_id}';`;
}
log.debug('DbContext', 'update_client_balance_history()', `SQL: ${sql}`);
await _self.query(sql)
}
} else {
log.debug('DbContext', 'update_client_balance_history()', 'уже полачено');
}
}
}
let t2 = new Date().microtime(true);
log.debug('DbContext', 'update_client_balance_history()', `complete ${t2 - t1}`);
return client;
}
};