server: update fuel; update general proper city GET

This commit is contained in:
2025-11-14 17:05:57 +09:00
parent 41f5ac6fcf
commit b7c772185f
8 changed files with 159 additions and 26 deletions

View File

@ -63,6 +63,21 @@ import { GisModule } from './gis/gis.module';
autoLoadEntities: true, autoLoadEntities: true,
name: 'generalConnection' name: 'generalConnection'
}), }),
TypeOrmModule.forRoot({
type: 'mssql',
host: process.env.WORLDSTONE_DB_HOST,
port: Number(process.env.WORLDSTONE_DB_PORT) || 1433,
username: process.env.WORLDSTONE_DB_USER,
password: process.env.WORLDSTONE_DB_PASSWORD,
database: process.env.WORLDSTONE_DB_NAME,
options: {
encrypt: false,
trustServerCertificate: true
},
synchronize: false,
autoLoadEntities: true,
name: 'worldstoneConnection'
}),
// TypeOrmModule.forRoot({ // TypeOrmModule.forRoot({
// type: 'postgres', // type: 'postgres',
// host: process.env.PG_HOST, // host: process.env.PG_HOST,

View File

@ -0,0 +1,18 @@
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger"
import { IsNumberString, IsOptional } from "class-validator"
export class GetBoilersDTO {
@ApiProperty()
@IsNumberString()
city_id: number
@ApiPropertyOptional()
@IsNumberString()
@IsOptional()
offset?: number
@ApiPropertyOptional()
@IsNumberString()
@IsOptional()
limit?: number
}

View File

@ -0,0 +1,18 @@
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger"
import { IsNumberString, IsOptional } from "class-validator"
export class GetFuelsDTO {
@ApiProperty()
@IsNumberString()
id_fuels: number
@ApiPropertyOptional()
@IsNumberString()
@IsOptional()
offset?: number
@ApiPropertyOptional()
@IsNumberString()
@IsOptional()
limit?: number
}

View File

@ -1,4 +1,4 @@
import { Body, Controller, Get, Post } from '@nestjs/common' import { Body, Controller, Get, Post, Query } from '@nestjs/common'
import { FuelService } from './fuel.service' import { FuelService } from './fuel.service'
import { CreateExpenseDto } from './dto/create-expense' import { CreateExpenseDto } from './dto/create-expense'
import { CreateLimitDto } from './dto/create-limit' import { CreateLimitDto } from './dto/create-limit'
@ -7,6 +7,8 @@ import { FuelLimitDto } from './dto/limit'
import { ApiOkResponse } from '@nestjs/swagger' import { ApiOkResponse } from '@nestjs/swagger'
import { FuelExpenseDto } from './dto/expense' import { FuelExpenseDto } from './dto/expense'
import { FuelTransferDto } from './dto/transfer' import { FuelTransferDto } from './dto/transfer'
import { GetBoilersDTO } from './dto/get-boilers'
import { GetFuelsDTO } from './dto/get-fuels'
@Controller('fuel') @Controller('fuel')
export class FuelController { export class FuelController {
@ -17,7 +19,22 @@ export class FuelController {
return this.fuelService.getColumnDataTypes('BoilersFuelsExpenses') return this.fuelService.getColumnDataTypes('BoilersFuelsExpenses')
} }
@Get('/boilers')//✅
async getBoilers(@Query() getBoilersDTO: GetBoilersDTO) {
const { city_id, offset, limit } = getBoilersDTO
return this.fuelService.getBoilers(city_id, offset, limit)
}
@Get('/fuel-types')
async getFuelTypes() {
return this.fuelService.getFuelTypes()
}
@Get('/fuels')
async getFuels(@Query() getFuelsDTO: GetFuelsDTO) {
const { id_fuels, offset, limit } = getFuelsDTO
return this.fuelService.getFuels(id_fuels)
}
// Fuel limits // Fuel limits
@Get('/limits') @Get('/limits')
@ -35,8 +52,6 @@ export class FuelController {
return this.fuelService.addBoilersFuelsLimit(createLimitDto) return this.fuelService.addBoilersFuelsLimit(createLimitDto)
} }
// Fuel expenses // Fuel expenses
@Get('/expenses') @Get('/expenses')
@ApiOkResponse({ @ApiOkResponse({
@ -54,7 +69,6 @@ export class FuelController {
} }
// Fuel transfer // Fuel transfer
@Get('/transfer') @Get('/transfer')
@ApiOkResponse({ @ApiOkResponse({

View File

@ -4,13 +4,18 @@ import { DataSource } from 'typeorm';
import { CreateExpenseDto } from './dto/create-expense'; import { CreateExpenseDto } from './dto/create-expense';
import { CreateLimitDto } from './dto/create-limit'; import { CreateLimitDto } from './dto/create-limit';
import { CreateTransferDto } from './dto/create-transfer'; import { CreateTransferDto } from './dto/create-transfer';
import { GetFuelsDTO } from './dto/get-fuels';
@Injectable() @Injectable()
export class FuelService { export class FuelService {
constructor( constructor(
@InjectDataSource('fuelConnection') private dataSource: DataSource @InjectDataSource('fuelConnection') private dataSource: DataSource,
@InjectDataSource('generalConnection') private generalDataSource: DataSource,
@InjectDataSource('worldstoneConnection') private wsDataSource: DataSource
) { } ) { }
// GET
async getColumnDataTypes(table_name: string): Promise<any[]> { async getColumnDataTypes(table_name: string): Promise<any[]> {
const result = await this.dataSource.query(` const result = await this.dataSource.query(`
SELECT SELECT
@ -22,6 +27,61 @@ export class FuelService {
return result 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 = ${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[]> { async getBoilersFuelsExpenses(): Promise<any[]> {
const result = await this.dataSource.query(` const result = await this.dataSource.query(`
SELECT * SELECT *
@ -31,13 +91,21 @@ export class FuelService {
} }
async getBoilersFuelsLimits(): Promise<any[]> { async getBoilersFuelsLimits(): Promise<any[]> {
const result = await this.dataSource.query(` const result = await this.wsDataSource.query(`
SELECT * SELECT *
FROM "BoilersFuelsLimits"; FROM "vBoilerLimits";
`) `)
return result return result
} }
async getFuelsTransfer(): Promise<any[]> {
const result = await this.dataSource.query(`
SELECT * FROM "FuelsTransfer";
`)
return result
}
// ADD
async addBoilersFuelsExpense(createExpenseDto: CreateExpenseDto): Promise<any[]> { async addBoilersFuelsExpense(createExpenseDto: CreateExpenseDto): Promise<any[]> {
const result = await this.dataSource.query(` const result = await this.dataSource.query(`
INSERT INTO dbo.BoilersFuelsExpenses (id_boiler, id_fuel, date, value) VALUES ($1, $2, $3, $4) INSERT INTO dbo.BoilersFuelsExpenses (id_boiler, id_fuel, date, value) VALUES ($1, $2, $3, $4)
@ -52,13 +120,6 @@ export class FuelService {
return result return result
} }
async getFuelsTransfer(): Promise<any[]> {
const result = await this.dataSource.query(`
SELECT * FROM "FuelsTransfer";
`)
return result
}
async addFuelsTransfer(createTransferDto: CreateTransferDto): Promise<any[]> { async addFuelsTransfer(createTransferDto: CreateTransferDto): Promise<any[]> {
const result = await this.dataSource.query(` const result = await this.dataSource.query(`
INSERT INTO dbo.FuelsTransfer (id_out, id_in, id_fuel, date, value) VALUES ($1, $2, $3, $4, $5) INSERT INTO dbo.FuelsTransfer (id_out, id_in, id_fuel, date, value) VALUES ($1, $2, $3, $4, $5)

View File

@ -1,7 +1,11 @@
import { ApiPropertyOptional } from "@nestjs/swagger" import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger"
import { IsNumberString, IsOptional, IsString } from "class-validator" import { IsNumberString, IsOptional, IsString } from "class-validator"
export class GetCitiesDTO { export class GetCitiesDTO {
@ApiProperty()
@IsNumberString()
region_id: number
@ApiPropertyOptional() @ApiPropertyOptional()
@IsNumberString() @IsNumberString()
@IsOptional() @IsOptional()

View File

@ -19,9 +19,9 @@ export class GeneralController {
@Get('/cities')//✅ @Get('/cities')//✅
async getCities(@Query() getCitiesDTO: GetCitiesDTO) { async getCities(@Query() getCitiesDTO: GetCitiesDTO) {
const { offset, limit, search, id } = getCitiesDTO const { region_id, offset, limit, search } = getCitiesDTO
return this.generalService.getCities(offset, limit, search, id) return this.generalService.getCities(region_id, offset, limit, search)
} }
@Get('/types')//✅ @Get('/types')//✅

View File

@ -9,13 +9,16 @@ export class GeneralService {
private dataSource: DataSource, private dataSource: DataSource,
@InjectDataSource('generalConnection') @InjectDataSource('generalConnection')
private generalDataSource: DataSource private generalDataSource: DataSource,
@InjectDataSource('worldstoneConnection')
private wsDataSource: DataSource
) { } ) { }
async getRegions(): Promise<any[]> { async getRegions(): Promise<any[]> {
const generalDatabase = '_isGeneral' const generalDatabase = 'isWorldstone'
const result = await this.generalDataSource.query(` const result = await this.wsDataSource.query(`
SELECT SELECT
r.*, r.*,
CASE WHEN r.capital IS NOT NULL THEN c.longitude END AS capital_longitude, CASE WHEN r.capital IS NOT NULL THEN c.longitude END AS capital_longitude,
@ -39,12 +42,12 @@ export class GeneralService {
return result return result
} }
async getCities(offset?: number, limit?: number, search?: string, id?: number): Promise<any[]> { async getCities(id_region: number, offset?: number, limit?: number, search?: string): Promise<any[]> {
const generalDatabase = '_isGeneral' const generalDatabase = 'isWorldstone'
const result = await this.generalDataSource.query(` const result = await this.wsDataSource.query(`
SELECT * FROM ${generalDatabase}..Cities SELECT * FROM ${generalDatabase}..vCities
${id ? `WHERE id = ${id}` : ''} ${id_region ? `WHERE id_region = ${id_region}` : ''}
${search ? `WHERE name LIKE '%${search || ''}%'` : ''} ${search ? `WHERE name LIKE '%${search || ''}%'` : ''}
ORDER BY id ORDER BY id
OFFSET ${Number(offset) || 0} ROWS OFFSET ${Number(offset) || 0} ROWS