update
This commit is contained in:
32
app.js
32
app.js
@ -106,6 +106,27 @@ async function commitMessage(now, logDevice) {
|
||||
}
|
||||
}
|
||||
|
||||
// ================= UPDATE SUM MILEAGE DARI t_vehicle ====================
|
||||
// if (logDevice.latitude != null && logDevice.longitude != null && lastTrack.length > 0) {
|
||||
// const distance = LibHelper.haversineGreatCircleDistance(lastTrack[0].latitude, lastTrack[0].longitude, logDevice.latitude, logDevice.longitude, LibHelper.EARTH_RADIUS_KM);
|
||||
// const distance_km = LibHelper.kmToKm(distance, 100000000);
|
||||
|
||||
// if (distance_km >= 3) {
|
||||
// GpsTracksModels.bundleCreate2(logDevice, logDevice);
|
||||
// return false;
|
||||
// }
|
||||
// logDevice.pre_milleage = distance_km;
|
||||
// logDevice.sum_milleage = (lastTrack[0].sum_milleage + logDevice.pre_milleage).toFixed(8);
|
||||
// if (vhc.length > 0) {
|
||||
// logDevice.vhc_id = vhc[0] ? vhc[0].vid : 0;
|
||||
// if (lastTrack[0].vhc_id == logDevice.vhc_id) {
|
||||
// logDevice.vhc_milleage = (lastTrack[0].vhc_milleage + logDevice.pre_milleage).toFixed(8);
|
||||
// } else {
|
||||
// logDevice.vhc_milleage = (vhc[0] ? vhc[0].sum_milleage : 0 + logDevice.pre_milleage).toFixed(8);
|
||||
// }
|
||||
// VhcModels.update(vhc[0] ? vhc[0].vid : 0, { sum_milleage: logDevice.vhc_milleage });
|
||||
// }
|
||||
// }
|
||||
if (logDevice.latitude != null && logDevice.longitude != null && lastTrack.length > 0) {
|
||||
const distance = LibHelper.haversineGreatCircleDistance(lastTrack[0].latitude, lastTrack[0].longitude, logDevice.latitude, logDevice.longitude, LibHelper.EARTH_RADIUS_KM);
|
||||
const distance_km = LibHelper.kmToKm(distance, 100000000);
|
||||
@ -117,13 +138,10 @@ async function commitMessage(now, logDevice) {
|
||||
logDevice.pre_milleage = distance_km;
|
||||
logDevice.sum_milleage = (lastTrack[0].sum_milleage + logDevice.pre_milleage).toFixed(8);
|
||||
if (vhc.length > 0) {
|
||||
logDevice.vhc_id = vhc[0] ? vhc[0].vid : 0;
|
||||
if (lastTrack[0].vhc_id == logDevice.vhc_id) {
|
||||
logDevice.vhc_milleage = (lastTrack[0].vhc_milleage + logDevice.pre_milleage).toFixed(8);
|
||||
} else {
|
||||
logDevice.vhc_milleage = (vhc[0] ? vhc[0].sum_milleage : 0 + logDevice.pre_milleage).toFixed(8);
|
||||
}
|
||||
VhcModels.update(vhc[0] ? vhc[0].vid : 0, { sum_milleage: logDevice.vhc_milleage });
|
||||
logDevice.vhc_id = vhc[0].vid;
|
||||
const increment = parseFloat(logDevice.pre_milleage || 0);
|
||||
|
||||
await VhcModels.incrementMileage(vhc[0].vid, increment);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -2,7 +2,6 @@ const db = require(`../config/dbMysqlConn`);
|
||||
const MysqlHelpers = require(`../library/LibMysqlHelper`);
|
||||
|
||||
class GpsTracksModels {
|
||||
|
||||
static DEFAULT_COUNTRY_ID = 1;
|
||||
|
||||
static TYPE_ADDR_OSM = 1;
|
||||
@ -84,12 +83,12 @@ class GpsTracksModels {
|
||||
static SOURCE_GPS_TRACKER = 1;
|
||||
static SOURCE_SMARTPHONE = 2;
|
||||
|
||||
static PROTOCOL_SMARTPHONE = 'smartphone';
|
||||
static PROTOCOL_SMARTPHONE = "smartphone";
|
||||
|
||||
static ACT_LOGIN = 'login';
|
||||
static ACT_HEARTBEAT = 'heartbeat';
|
||||
static ACT_LOCATION = 'location';
|
||||
static ACT_OTHER = 'other';
|
||||
static ACT_LOGIN = "login";
|
||||
static ACT_HEARTBEAT = "heartbeat";
|
||||
static ACT_LOCATION = "location";
|
||||
static ACT_OTHER = "other";
|
||||
|
||||
static async create2(data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
@ -128,12 +127,11 @@ class GpsTracksModels {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
static bundleCreate2 (logs = {}, rltm = {}) {
|
||||
static bundleCreate2(logs = {}, rltm = {}) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
const conn = await MysqlHelpers.createConnection();
|
||||
@ -144,7 +142,7 @@ class GpsTracksModels {
|
||||
|
||||
if (Object.keys(logs).length > 0) resLogs = await MysqlHelpers.queryTrx(conn, `INSERT INTO t_gps_tracks SET ?;`, [logs]);
|
||||
|
||||
if (rltmLength > 0 && typeof resLogs !== 'undefined') rltm.master_id = resLogs.insertId;
|
||||
if (rltmLength > 0 && typeof resLogs !== "undefined") rltm.master_id = resLogs.insertId;
|
||||
if (rltmLength > 0 && rltm.latitude !== null && rltm.longitude !== null) {
|
||||
let devices = await MysqlHelpers.queryTrx(conn, `SELECT id FROM t_gps_tracks_rltm as rltm WHERE rltm.device_id = ?`, [rltm.device_id]);
|
||||
if (devices.length > 1) await MysqlHelpers.queryTrx(conn, `DELETE from t_gps_tracks_rltm WHERE device_id = ?;`, [rltm.device_id]);
|
||||
@ -164,20 +162,20 @@ class GpsTracksModels {
|
||||
|
||||
await MysqlHelpers.commit(conn);
|
||||
resolve({
|
||||
type: 'success',
|
||||
type: "success",
|
||||
result: resLogs,
|
||||
})
|
||||
});
|
||||
} catch (err) {
|
||||
console.log('err');
|
||||
console.log("err");
|
||||
reject(err);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
static async get2() {
|
||||
return new Promise((resolve, reject) => {
|
||||
let params = [];
|
||||
let query = 'SELECT * FROM t_gps_tracks ORDER BY id DESC LIMIT 1000';
|
||||
let query = "SELECT * FROM t_gps_tracks ORDER BY id DESC LIMIT 1000";
|
||||
db.query(query, params, function (err, resp) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
@ -192,7 +190,7 @@ class GpsTracksModels {
|
||||
static async get2ForReverseGeo(limit) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let params = [];
|
||||
let query = 'SELECT * FROM t_gps_tracks WHERE latitude is not null AND longitude is not null AND stts_reverse_geo = ? ORDER BY id ASC LIMIT ?';
|
||||
let query = "SELECT * FROM t_gps_tracks WHERE latitude is not null AND longitude is not null AND stts_reverse_geo = ? ORDER BY id ASC LIMIT ?";
|
||||
params.push(GpsTracksModels.STTS_REVERSE_GEO_NOT, limit);
|
||||
db.query(query, params, function (err, resp) {
|
||||
if (err) {
|
||||
@ -208,7 +206,7 @@ class GpsTracksModels {
|
||||
static async get2LastDevice(device_id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let params = [];
|
||||
let query = 'SELECT * FROM t_gps_tracks WHERE device_id = ? ORDER BY id DESC LIMIT 1';
|
||||
let query = "SELECT * FROM t_gps_tracks WHERE device_id = ? ORDER BY id DESC LIMIT 1";
|
||||
params.push(device_id);
|
||||
db.query(query, params, function (err, resp) {
|
||||
if (err) {
|
||||
@ -238,21 +236,44 @@ class GpsTracksModels {
|
||||
});
|
||||
}
|
||||
|
||||
static async getLastSumMileage(device_id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let params = [];
|
||||
let query = `
|
||||
select
|
||||
*
|
||||
from
|
||||
t_vehicles tv
|
||||
where
|
||||
tv.device_id = "0866833070208837"
|
||||
and tv.dlt is null
|
||||
limit 1
|
||||
`;
|
||||
params.push(device_id);
|
||||
db.query(query, params, function (err, resp) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return false;
|
||||
}
|
||||
resolve(resp);
|
||||
return true;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
static async get2LastLoc(filter = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let params = [], where = '';
|
||||
if (filter['device_id']) {
|
||||
where += ' AND device_id = ?';
|
||||
params.push(filter['device_id']);
|
||||
|
||||
} else if (filter['vhc_id']) {
|
||||
where += ' AND vhc_id = ?';
|
||||
params.push(filter['vhc_id']);
|
||||
|
||||
} else if (filter['drv_id']) {
|
||||
where += ' AND drv_id = ?';
|
||||
params.push(filter['drv_id']);
|
||||
|
||||
let params = [],
|
||||
where = "";
|
||||
if (filter["device_id"]) {
|
||||
where += " AND device_id = ?";
|
||||
params.push(filter["device_id"]);
|
||||
} else if (filter["vhc_id"]) {
|
||||
where += " AND vhc_id = ?";
|
||||
params.push(filter["vhc_id"]);
|
||||
} else if (filter["drv_id"]) {
|
||||
where += " AND drv_id = ?";
|
||||
params.push(filter["drv_id"]);
|
||||
} else {
|
||||
reject(false);
|
||||
return false;
|
||||
@ -271,16 +292,18 @@ class GpsTracksModels {
|
||||
|
||||
static async getLastLocAndAddrByDid(filter = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let params = [], where = '', limit = '';
|
||||
let params = [],
|
||||
where = "",
|
||||
limit = "";
|
||||
|
||||
if (typeof filter['did'] !== 'undefined') {
|
||||
where += ' AND tr.drv_id = ?';
|
||||
params.push(filter['did']);
|
||||
if (typeof filter["did"] !== "undefined") {
|
||||
where += " AND tr.drv_id = ?";
|
||||
params.push(filter["did"]);
|
||||
}
|
||||
|
||||
if (typeof filter['limit'] !== 'undefined') {
|
||||
limit += ' LIMIT ?';
|
||||
params.push(filter['limit']);
|
||||
if (typeof filter["limit"] !== "undefined") {
|
||||
limit += " LIMIT ?";
|
||||
params.push(filter["limit"]);
|
||||
}
|
||||
|
||||
let query = `SELECT tr.*,tra.state_text,tra.city_text,tra.district_text,tra.village_text,tra.postcode,tra.streets,tra.fulladdress FROM t_gps_tracks as tr LEFT JOIN t_gps_tracks_address as tra ON tr.id = tra.master_id WHERE latitude is not null AND longitude != 0 ${where} ORDER BY id DESC ${limit}`;
|
||||
@ -298,7 +321,7 @@ class GpsTracksModels {
|
||||
static async checkSttsEngineBetweenDate(device_id, stts_engine, start, end) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let params = [];
|
||||
let query = 'SELECT * FROM t_gps_tracks WHERE device_id = ? AND stts_engine = ? AND crt BETWEEN ? AND ? ORDER BY id ASC LIMIT 1';
|
||||
let query = "SELECT * FROM t_gps_tracks WHERE device_id = ? AND stts_engine = ? AND crt BETWEEN ? AND ? ORDER BY id ASC LIMIT 1";
|
||||
params.push(device_id, stts_engine, start, end);
|
||||
db.query(query, params, function (err, resp) {
|
||||
if (err) {
|
||||
@ -330,7 +353,7 @@ class GpsTracksModels {
|
||||
static async getLastHeartbeatToDeterminIdling(device_id, lst_pos_crt, now) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let params = [];
|
||||
let query = 'SELECT * FROM t_gps_tracks WHERE device_id = ? AND action IN ("heartbeat") AND stts_gps != 0 AND crt BETWEEN '+lst_pos_crt+' AND '+now+' ORDER BY id DESC;';
|
||||
let query = 'SELECT * FROM t_gps_tracks WHERE device_id = ? AND action IN ("heartbeat") AND stts_gps != 0 AND crt BETWEEN ' + lst_pos_crt + " AND " + now + " ORDER BY id DESC;";
|
||||
params.push(device_id);
|
||||
db.query(query, params, function (err, resp) {
|
||||
if (err) {
|
||||
@ -379,15 +402,14 @@ class GpsTracksModels {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
static async select2Address(lat = '', lng = '') {
|
||||
static async select2Address(lat = "", lng = "") {
|
||||
return new Promise((resolve, reject) => {
|
||||
let params = [];
|
||||
let query = 'SELECT * FROM t_gps_tracks_address WHERE stts_reverse_geo = '+GpsTracksModels.STTS_REVERSE_GEO_SC+' AND lat = ? AND lng = ? ORDER BY id DESC LIMIT 1;';
|
||||
let query = "SELECT * FROM t_gps_tracks_address WHERE stts_reverse_geo = " + GpsTracksModels.STTS_REVERSE_GEO_SC + " AND lat = ? AND lng = ? ORDER BY id DESC LIMIT 1;";
|
||||
params.push(lat, lng);
|
||||
db.query(query, params, function (err, resp) {
|
||||
if (err) {
|
||||
@ -437,7 +459,6 @@ class GpsTracksModels {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -479,7 +500,6 @@ class GpsTracksModels {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -521,52 +541,57 @@ class GpsTracksModels {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
static listSpawnZone(filter = {}) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let params = [], select = '', where = '', join = '', order_by = '', group_by = '', limit = '';
|
||||
let params = [],
|
||||
select = "",
|
||||
where = "",
|
||||
join = "",
|
||||
order_by = "",
|
||||
group_by = "",
|
||||
limit = "";
|
||||
|
||||
if (filter['zone_id']) {
|
||||
where += ' AND spawn.zone_id = ?';
|
||||
params.push(filter['zone_id']);
|
||||
if (filter["zone_id"]) {
|
||||
where += " AND spawn.zone_id = ?";
|
||||
params.push(filter["zone_id"]);
|
||||
}
|
||||
if (filter['ord_id']) {
|
||||
where += ' AND spawn.ord_id = ?';
|
||||
params.push(filter['ord_id']);
|
||||
if (filter["ord_id"]) {
|
||||
where += " AND spawn.ord_id = ?";
|
||||
params.push(filter["ord_id"]);
|
||||
}
|
||||
if (filter['ord_pck_drop_id']) {
|
||||
where += ' AND spawn.ord_pck_drop_id = ?';
|
||||
params.push(filter['ord_pck_drop_id']);
|
||||
if (filter["ord_pck_drop_id"]) {
|
||||
where += " AND spawn.ord_pck_drop_id = ?";
|
||||
params.push(filter["ord_pck_drop_id"]);
|
||||
}
|
||||
if (filter['drv_id']) {
|
||||
where += ' AND spawn.drv_id = ?';
|
||||
params.push(filter['drv_id']);
|
||||
if (filter["drv_id"]) {
|
||||
where += " AND spawn.drv_id = ?";
|
||||
params.push(filter["drv_id"]);
|
||||
}
|
||||
if (filter['vhc_id']) {
|
||||
where += ' AND spawn.vhc_id = ?';
|
||||
params.push(filter['vhc_id']);
|
||||
if (filter["vhc_id"]) {
|
||||
where += " AND spawn.vhc_id = ?";
|
||||
params.push(filter["vhc_id"]);
|
||||
}
|
||||
if (filter['source']) {
|
||||
where += ' AND spawn.source = ?';
|
||||
params.push(filter['source']);
|
||||
if (filter["source"]) {
|
||||
where += " AND spawn.source = ?";
|
||||
params.push(filter["source"]);
|
||||
}
|
||||
if (typeof filter['leave_at_d'] != undefined) {
|
||||
where += ' AND spawn.leave_at_d = ?';
|
||||
params.push(filter['leave_at_d']);
|
||||
if (typeof filter["leave_at_d"] != undefined) {
|
||||
where += " AND spawn.leave_at_d = ?";
|
||||
params.push(filter["leave_at_d"]);
|
||||
}
|
||||
|
||||
if (filter['group_by']) {
|
||||
group_by = filter['group_by'];
|
||||
if (filter["group_by"]) {
|
||||
group_by = filter["group_by"];
|
||||
}
|
||||
if (filter['order_by']) {
|
||||
order_by = filter['order_by'];
|
||||
if (filter["order_by"]) {
|
||||
order_by = filter["order_by"];
|
||||
}
|
||||
if (filter['limit']) {
|
||||
limit = ' LIMIT ' + filter['limit'];
|
||||
if (filter["limit"]) {
|
||||
limit = " LIMIT " + filter["limit"];
|
||||
}
|
||||
|
||||
let query = `SELECT
|
||||
@ -590,7 +615,6 @@ class GpsTracksModels {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = GpsTracksModels;
|
||||
@ -1,7 +1,6 @@
|
||||
const db = require(`../config/dbMysqlConn`);
|
||||
|
||||
class VhcModels {
|
||||
|
||||
static STTS_ACTIVE = 1;
|
||||
static STTS_INACTIVE = 2;
|
||||
|
||||
@ -11,13 +10,13 @@ class VhcModels {
|
||||
static async getVhcByDeviceId(device_id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let params = [];
|
||||
let query = 'SELECT';
|
||||
query += ' v.id as vid,v.nopol1,v.nopol2,v.nopol3,v.sum_milleage,vd.speed_limit';
|
||||
query += ' ,da.fullname as da_name,da.phone as da_phone,da.phone_code as da_phone_code';
|
||||
query += ' FROM t_vehicles AS v';
|
||||
query += ' INNER JOIN t_vehicles_detail AS vd ON v.id = vd.vid';
|
||||
query += ' LEFT JOIN t_drivers AS da ON v.a_did = da.id';
|
||||
query += ' WHERE v.dlt is null AND v.status = ? AND v.device_id = ?';
|
||||
let query = "SELECT";
|
||||
query += " v.id as vid,v.nopol1,v.nopol2,v.nopol3,v.sum_milleage,vd.speed_limit";
|
||||
query += " ,da.fullname as da_name,da.phone as da_phone,da.phone_code as da_phone_code";
|
||||
query += " FROM t_vehicles AS v";
|
||||
query += " INNER JOIN t_vehicles_detail AS vd ON v.id = vd.vid";
|
||||
query += " LEFT JOIN t_drivers AS da ON v.a_did = da.id";
|
||||
query += " WHERE v.dlt is null AND v.status = ? AND v.device_id = ?";
|
||||
params.push(VhcModels.STTS_ACTIVE, device_id);
|
||||
db.query(query, params, function (err, resp) {
|
||||
if (err) {
|
||||
@ -33,16 +32,16 @@ class VhcModels {
|
||||
static async getVhcById(id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let params = [];
|
||||
let query = 'SELECT';
|
||||
query += ' v.id as vid,v.nopol1,v.nopol2,v.nopol3,v.sum_milleage,vd.speed_limit';
|
||||
query += ' ,da.fullname as da_name,da.phone as da_phone,da.phone_code as da_phone_code';
|
||||
query += ' ,v.is_in_ord,v.ord_id,v.ord_code,v.ord_ids';
|
||||
query += ' FROM t_vehicles AS v';
|
||||
query += ' INNER JOIN t_vehicles_detail AS vd ON v.id = vd.vid';
|
||||
query += ' LEFT JOIN t_drivers AS da ON v.a_did = da.id';
|
||||
let query = "SELECT";
|
||||
query += " v.id as vid,v.nopol1,v.nopol2,v.nopol3,v.sum_milleage,vd.speed_limit";
|
||||
query += " ,da.fullname as da_name,da.phone as da_phone,da.phone_code as da_phone_code";
|
||||
query += " ,v.is_in_ord,v.ord_id,v.ord_code,v.ord_ids";
|
||||
query += " FROM t_vehicles AS v";
|
||||
query += " INNER JOIN t_vehicles_detail AS vd ON v.id = vd.vid";
|
||||
query += " LEFT JOIN t_drivers AS da ON v.a_did = da.id";
|
||||
// query += ' WHERE v.dlt is null AND v.status = ? AND v.id = ?';
|
||||
query += ' WHERE v.dlt is null AND v.id = ?';
|
||||
query += ' LIMIT 1';
|
||||
query += " WHERE v.dlt is null AND v.id = ?";
|
||||
query += " LIMIT 1";
|
||||
params.push(id);
|
||||
db.query(query, params, function (err, resp) {
|
||||
if (err) {
|
||||
@ -91,11 +90,22 @@ class VhcModels {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
static async incrementMileage(vhc_id, add_km) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const query = `
|
||||
UPDATE t_vehicles
|
||||
SET sum_milleage = COALESCE(sum_milleage, 0) + ?
|
||||
WHERE id = ? AND dlt IS NULL
|
||||
`;
|
||||
db.query(query, [add_km, vhc_id], (err, result) => {
|
||||
if (err) return reject(err);
|
||||
resolve(result);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = VhcModels;
|
||||
Reference in New Issue
Block a user