163 lines
4.7 KiB
TypeScript
163 lines
4.7 KiB
TypeScript
import { Controller, Get, Post, Body, Param, Query, Delete } from '@nestjs/common';
|
|
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiQuery } from '@nestjs/swagger';
|
|
import { RegionRepository } from '../../infrastructure/persistence/repositories/region.repository';
|
|
import { AdminOnly } from '../../shared/guards/jwt-auth.guard';
|
|
|
|
class CreateProvinceDto {
|
|
code: string;
|
|
name: string;
|
|
}
|
|
|
|
class CreateCityDto {
|
|
provinceId: string;
|
|
code: string;
|
|
name: string;
|
|
}
|
|
|
|
class AssignUserRegionDto {
|
|
accountSequence: string;
|
|
cityId: string;
|
|
assignedBy?: string;
|
|
}
|
|
|
|
class BulkInitializeRegionsDto {
|
|
regions: {
|
|
provinceCode: string;
|
|
provinceName: string;
|
|
cities: { code: string; name: string }[];
|
|
}[];
|
|
}
|
|
|
|
@ApiTags('Regions')
|
|
@Controller('regions')
|
|
@ApiBearerAuth()
|
|
export class RegionController {
|
|
constructor(private readonly regionRepo: RegionRepository) {}
|
|
|
|
// ==================== Province ====================
|
|
|
|
@Get('provinces')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '获取所有省份' })
|
|
async getAllProvinces() {
|
|
return this.regionRepo.findAllProvinces();
|
|
}
|
|
|
|
@Get('provinces/:id')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '根据ID获取省份' })
|
|
async getProvinceById(@Param('id') id: string) {
|
|
return this.regionRepo.findProvinceById(id);
|
|
}
|
|
|
|
@Post('provinces')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '创建省份' })
|
|
async createProvince(@Body() dto: CreateProvinceDto) {
|
|
return this.regionRepo.createProvince(dto);
|
|
}
|
|
|
|
// ==================== City ====================
|
|
|
|
@Get('cities')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '获取所有城市' })
|
|
async getAllCities() {
|
|
return this.regionRepo.findAllCities();
|
|
}
|
|
|
|
@Get('cities/by-province/:provinceId')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '根据省份获取城市' })
|
|
async getCitiesByProvince(@Param('provinceId') provinceId: string) {
|
|
return this.regionRepo.findCitiesByProvince(provinceId);
|
|
}
|
|
|
|
@Get('cities/:id')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '根据ID获取城市' })
|
|
async getCityById(@Param('id') id: string) {
|
|
return this.regionRepo.findCityById(id);
|
|
}
|
|
|
|
@Post('cities')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '创建城市' })
|
|
async createCity(@Body() dto: CreateCityDto) {
|
|
return this.regionRepo.createCity(dto);
|
|
}
|
|
|
|
// ==================== User Region Mapping ====================
|
|
|
|
@Get('user-mappings/:accountSequence')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '获取用户区域映射' })
|
|
async getUserRegion(@Param('accountSequence') accountSequence: string) {
|
|
return this.regionRepo.findUserRegion(accountSequence);
|
|
}
|
|
|
|
@Get('user-mappings/by-city/:cityId')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '根据城市获取用户列表' })
|
|
@ApiQuery({ name: 'limit', required: false, type: Number })
|
|
@ApiQuery({ name: 'offset', required: false, type: Number })
|
|
async getUsersByCity(
|
|
@Param('cityId') cityId: string,
|
|
@Query('limit') limit?: number,
|
|
@Query('offset') offset?: number,
|
|
) {
|
|
return this.regionRepo.findUsersByCity(cityId, {
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
});
|
|
}
|
|
|
|
@Get('user-mappings/by-province/:provinceId')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '根据省份获取用户列表' })
|
|
@ApiQuery({ name: 'limit', required: false, type: Number })
|
|
@ApiQuery({ name: 'offset', required: false, type: Number })
|
|
async getUsersByProvince(
|
|
@Param('provinceId') provinceId: string,
|
|
@Query('limit') limit?: number,
|
|
@Query('offset') offset?: number,
|
|
) {
|
|
return this.regionRepo.findUsersByProvince(provinceId, {
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
});
|
|
}
|
|
|
|
@Post('user-mappings')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '分配用户到区域' })
|
|
async assignUserToRegion(@Body() dto: AssignUserRegionDto) {
|
|
return this.regionRepo.assignUserToRegion(dto);
|
|
}
|
|
|
|
@Delete('user-mappings/:accountSequence')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '移除用户区域映射' })
|
|
async removeUserFromRegion(@Param('accountSequence') accountSequence: string) {
|
|
await this.regionRepo.removeUserFromRegion(accountSequence);
|
|
return { success: true };
|
|
}
|
|
|
|
// ==================== Statistics & Initialization ====================
|
|
|
|
@Get('stats')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '获取区域统计' })
|
|
async getRegionStats() {
|
|
return this.regionRepo.getRegionStats();
|
|
}
|
|
|
|
@Post('bulk-initialize')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '批量初始化区域数据' })
|
|
async bulkInitializeRegions(@Body() dto: BulkInitializeRegionsDto) {
|
|
await this.regionRepo.bulkInitializeRegions(dto.regions);
|
|
return { success: true, message: 'Regions initialized successfully' };
|
|
}
|
|
}
|