| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- import { DataSource, Repository } from 'typeorm';
- import { AreaEntity, AreaLevel } from './area.entity';
- import { CreateAreaInput, UpdateAreaInput } from './area.schema';
- import { DisabledStatus } from '@/share/types';
- export class AreaService {
- private areaRepository: Repository<AreaEntity>;
- constructor(private dataSource: DataSource) {
- this.areaRepository = this.dataSource.getRepository(AreaEntity);
- }
- /**
- * 创建省市区
- */
- async create(input: CreateAreaInput): Promise<AreaEntity> {
- const area = this.areaRepository.create(input);
- return await this.areaRepository.save(area);
- }
- /**
- * 更新省市区
- */
- async update(id: number, input: UpdateAreaInput): Promise<AreaEntity | null> {
- const area = await this.areaRepository.findOne({ where: { id } });
- if (!area) {
- return null;
- }
- Object.assign(area, input);
- return await this.areaRepository.save(area);
- }
- /**
- * 获取省市区详情
- */
- async findById(id: number): Promise<AreaEntity | null> {
- return await this.areaRepository.findOne({
- where: { id },
- relations: ['parent', 'children']
- });
- }
- /**
- * 获取省市区列表
- */
- async findAll(params: {
- keyword?: string;
- level?: AreaLevel;
- parentId?: number;
- isDisabled?: DisabledStatus;
- page?: number;
- pageSize?: number;
- sortBy?: string;
- sortOrder?: 'ASC' | 'DESC';
- }): Promise<{ data: AreaEntity[]; total: number }> {
- const {
- keyword,
- level,
- parentId,
- isDisabled,
- page = 1,
- pageSize = 20,
- sortBy = 'createdAt',
- sortOrder = 'DESC'
- } = params;
- const queryBuilder = this.areaRepository
- .createQueryBuilder('area')
- .leftJoinAndSelect('area.parent', 'parent')
- .leftJoinAndSelect('area.children', 'children')
- .where('area.isDeleted = :isDeleted', { isDeleted: 0 });
- // 关键词搜索
- if (keyword) {
- queryBuilder.andWhere('(area.name LIKE :keyword OR area.code LIKE :keyword)', {
- keyword: `%${keyword}%`
- });
- }
- // 层级筛选
- if (level) {
- queryBuilder.andWhere('area.level = :level', { level });
- }
- // 父级ID筛选
- if (parentId !== undefined) {
- queryBuilder.andWhere('area.parentId = :parentId', { parentId });
- }
- // 状态筛选
- if (isDisabled !== undefined) {
- queryBuilder.andWhere('area.isDisabled = :isDisabled', { isDisabled });
- }
- // 排序
- const orderBy = `area.${sortBy}`;
- queryBuilder.orderBy(orderBy, sortOrder);
- // 分页
- const [data, total] = await queryBuilder
- .skip((page - 1) * pageSize)
- .take(pageSize)
- .getManyAndCount();
- return { data, total };
- }
- /**
- * 删除省市区(软删除)
- */
- async delete(id: number): Promise<boolean> {
- const result = await this.areaRepository.update(id, { isDeleted: 1 });
- return result.affected !== 0;
- }
- /**
- * 启用/禁用省市区
- */
- async toggleStatus(id: number, isDisabled: DisabledStatus): Promise<boolean> {
- const result = await this.areaRepository.update(id, { isDisabled });
- return result.affected !== 0;
- }
- /**
- * 根据层级获取省市区列表
- */
- async findByLevel(level: AreaLevel): Promise<AreaEntity[]> {
- return await this.areaRepository.find({
- where: {
- level,
- isDeleted: 0,
- isDisabled: DisabledStatus.ENABLED
- },
- order: { name: 'ASC' }
- });
- }
- /**
- * 根据父级ID获取子级省市区列表
- */
- async findByParentId(parentId: number): Promise<AreaEntity[]> {
- return await this.areaRepository.find({
- where: {
- parentId,
- isDeleted: 0,
- isDisabled: DisabledStatus.ENABLED
- },
- order: { name: 'ASC' }
- });
- }
- /**
- * 获取省市区完整路径
- */
- async getAreaPath(id: number): Promise<AreaEntity[]> {
- const path: AreaEntity[] = [];
- let currentArea = await this.areaRepository.findOne({
- where: { id },
- relations: ['parent']
- });
- while (currentArea) {
- path.unshift(currentArea);
- if (currentArea.parentId === null || currentArea.parentId === 0) {
- break;
- }
- currentArea = await this.areaRepository.findOne({
- where: { id: currentArea.parentId },
- relations: ['parent']
- });
- }
- return path;
- }
- /**
- * 获取省份列表
- */
- async getProvinces(): Promise<AreaEntity[]> {
- return await this.findByLevel(AreaLevel.PROVINCE);
- }
- /**
- * 根据省份ID获取城市列表
- */
- async getCitiesByProvinceId(provinceId: number): Promise<AreaEntity[]> {
- return await this.findByParentId(provinceId);
- }
- /**
- * 根据城市ID获取区县列表
- */
- async getDistrictsByCityId(cityId: number): Promise<AreaEntity[]> {
- return await this.findByParentId(cityId);
- }
- }
|