130 lines
4.5 KiB
TypeScript
130 lines
4.5 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectDataSource } from '@nestjs/typeorm';
|
|
import { DataSource } from 'typeorm';
|
|
import { CreateExpenseDto } from './dto/create-expense';
|
|
import { CreateLimitDto } from './dto/create-limit';
|
|
import { CreateTransferDto } from './dto/create-transfer';
|
|
import { GetFuelsDTO } from './dto/get-fuels';
|
|
|
|
@Injectable()
|
|
export class FuelService {
|
|
constructor(
|
|
@InjectDataSource('fuelConnection') private dataSource: DataSource,
|
|
@InjectDataSource('generalConnection') private generalDataSource: DataSource,
|
|
@InjectDataSource('worldstoneConnection') private wsDataSource: DataSource
|
|
) { }
|
|
|
|
// GET
|
|
|
|
async getColumnDataTypes(table_name: string): Promise<any[]> {
|
|
const result = await this.dataSource.query(`
|
|
SELECT
|
|
COLUMN_NAME,
|
|
DATA_TYPE
|
|
FROM INFORMATION_SCHEMA.COLUMNS
|
|
WHERE TABLE_NAME = @0
|
|
`, [table_name])
|
|
return result
|
|
}
|
|
|
|
async getFuelTypes() {
|
|
const result = await this.wsDataSource.query(`
|
|
SELECT * FROM TFuels WHERE NOT id = 0
|
|
`)
|
|
return result
|
|
}
|
|
|
|
async getFuels(id_fuels: GetFuelsDTO['id_fuels']) {
|
|
const result = await this.wsDataSource.query(`
|
|
SELECT * FROM dFuelsParameters
|
|
WHERE id_fuels = @0
|
|
`, [Number(id_fuels)])
|
|
return result
|
|
}
|
|
|
|
async getBoilers(city_id: number, offset?: number, limit?: number): Promise<any[]> {
|
|
// const result = await this.wsDataSource.query(`
|
|
// SELECT * FROM isWorldstone..vBoilers"
|
|
// WHERE id_city = ${city_id}
|
|
// ORDER BY id_object
|
|
// OFFSET ${offset || 0} ROWS
|
|
// FETCH NEXT ${limit || 100} ROWS ONLY
|
|
// `)
|
|
|
|
// return result
|
|
|
|
const result = await this.wsDataSource.query(`
|
|
SELECT
|
|
b.*,
|
|
COALESCE(
|
|
(SELECT fp.*
|
|
FROM isFuels.dbo.BoilersFuels bf
|
|
INNER JOIN isWorldstone.dbo.dFuelsParameters fp ON bf.id_fuels = fp.id
|
|
WHERE bf.id_boilers = b.id_object
|
|
FOR JSON PATH),
|
|
'[]'
|
|
) AS id_fuels
|
|
FROM isWorldstone.dbo.vBoilers b
|
|
WHERE id_city = @0
|
|
ORDER BY id_object
|
|
OFFSET @1 ROWS
|
|
FETCH NEXT @2 ROWS ONLY
|
|
`, [city_id, Number(offset || 0), Number(limit || 100)])
|
|
|
|
if (Array.isArray(result)) {
|
|
return result.map(row => ({
|
|
...row,
|
|
id_fuels: JSON.parse(row.id_fuels)
|
|
}))
|
|
} else {
|
|
return result
|
|
}
|
|
|
|
}
|
|
|
|
async getBoilersFuelsExpenses(): Promise<any[]> {
|
|
const result = await this.dataSource.query(`
|
|
SELECT *
|
|
FROM "BoilersFuelsExpenses";
|
|
`)
|
|
return result
|
|
}
|
|
|
|
async getBoilersFuelsLimits(): Promise<any[]> {
|
|
const result = await this.wsDataSource.query(`
|
|
SELECT *
|
|
FROM "vBoilerLimits";
|
|
`)
|
|
return result
|
|
}
|
|
|
|
async getFuelsTransfer(): Promise<any[]> {
|
|
const result = await this.dataSource.query(`
|
|
SELECT * FROM "FuelsTransfer";
|
|
`)
|
|
return result
|
|
}
|
|
|
|
// ADD
|
|
async addBoilersFuelsExpense(createExpenseDto: CreateExpenseDto): Promise<any[]> {
|
|
const result = await this.dataSource.query(`
|
|
INSERT INTO dbo.BoilersFuelsExpenses (id_boiler, id_fuel, date, value) VALUES ($1, $2, $3, $4)
|
|
`, [createExpenseDto.id_boiler, createExpenseDto.id_fuel, createExpenseDto.date, createExpenseDto.value])
|
|
return result
|
|
}
|
|
|
|
async addBoilersFuelsLimit(createLimitDto: CreateLimitDto): Promise<any[]> {
|
|
const result = await this.dataSource.query(`
|
|
INSERT INTO dbo.BoilersFuelsLimits (id_boiler, id_fuel, value, month, year) VALUES ($1, $2, $3, $4, $5)
|
|
`, [createLimitDto.id_boiler, createLimitDto.id_fuel, createLimitDto.value, createLimitDto.month, createLimitDto.year])
|
|
return result
|
|
}
|
|
|
|
async addFuelsTransfer(createTransferDto: CreateTransferDto): Promise<any[]> {
|
|
const result = await this.dataSource.query(`
|
|
INSERT INTO dbo.FuelsTransfer (id_out, id_in, id_fuel, date, value) VALUES ($1, $2, $3, $4, $5)
|
|
`, [createTransferDto.id_out, createTransferDto.id_in, createTransferDto.id_fuel, createTransferDto.date, createTransferDto.value])
|
|
return result
|
|
}
|
|
}
|