|
|
@@ -1,215 +0,0 @@
|
|
|
-import { DataSource, Repository } from 'typeorm';
|
|
|
-import { LocationEntity } from './location.entity';
|
|
|
-import { CreateLocationInput, UpdateLocationInput } from './location.schema';
|
|
|
-import { DisabledStatus } from '@/share/types';
|
|
|
-export class LocationService {
|
|
|
- private locationRepository: Repository<LocationEntity>;
|
|
|
-
|
|
|
- constructor(private dataSource: DataSource) {
|
|
|
- this.locationRepository = this.dataSource.getRepository(LocationEntity);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 创建地点
|
|
|
- */
|
|
|
- async create(input: CreateLocationInput): Promise<LocationEntity> {
|
|
|
- const location = this.locationRepository.create(input);
|
|
|
- return await this.locationRepository.save(location);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 更新地点
|
|
|
- */
|
|
|
- async update(id: number, input: UpdateLocationInput): Promise<LocationEntity | null> {
|
|
|
- const location = await this.locationRepository.findOne({
|
|
|
- where: { id },
|
|
|
- relations: ['province', 'city', 'district']
|
|
|
- });
|
|
|
- if (!location) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- Object.assign(location, input);
|
|
|
- return await this.locationRepository.save(location);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取地点详情
|
|
|
- */
|
|
|
- async findById(id: number): Promise<LocationEntity | null> {
|
|
|
- return await this.locationRepository.findOne({
|
|
|
- where: { id },
|
|
|
- relations: ['province', 'city', 'district']
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取地点列表
|
|
|
- */
|
|
|
- async findAll(params: {
|
|
|
- keyword?: string;
|
|
|
- provinceId?: number;
|
|
|
- cityId?: number;
|
|
|
- districtId?: number;
|
|
|
- isDisabled?: DisabledStatus;
|
|
|
- page?: number;
|
|
|
- pageSize?: number;
|
|
|
- sortBy?: string;
|
|
|
- sortOrder?: 'ASC' | 'DESC';
|
|
|
- }): Promise<{ data: LocationEntity[]; total: number }> {
|
|
|
- const {
|
|
|
- keyword,
|
|
|
- provinceId,
|
|
|
- cityId,
|
|
|
- districtId,
|
|
|
- isDisabled,
|
|
|
- page = 1,
|
|
|
- pageSize = 20,
|
|
|
- sortBy = 'createdAt',
|
|
|
- sortOrder = 'DESC'
|
|
|
- } = params;
|
|
|
-
|
|
|
- const queryBuilder = this.locationRepository
|
|
|
- .createQueryBuilder('location')
|
|
|
- .leftJoinAndSelect('location.province', 'province')
|
|
|
- .leftJoinAndSelect('location.city', 'city')
|
|
|
- .leftJoinAndSelect('location.district', 'district')
|
|
|
- .where('location.isDeleted = :isDeleted', { isDeleted: 0 });
|
|
|
-
|
|
|
- // 关键词搜索
|
|
|
- if (keyword) {
|
|
|
- queryBuilder.andWhere('(location.name LIKE :keyword OR location.address LIKE :keyword)', {
|
|
|
- keyword: `%${keyword}%`
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- // 省份筛选
|
|
|
- if (provinceId) {
|
|
|
- queryBuilder.andWhere('location.provinceId = :provinceId', { provinceId });
|
|
|
- }
|
|
|
-
|
|
|
- // 城市筛选
|
|
|
- if (cityId) {
|
|
|
- queryBuilder.andWhere('location.cityId = :cityId', { cityId });
|
|
|
- }
|
|
|
-
|
|
|
- // 区县筛选
|
|
|
- if (districtId) {
|
|
|
- queryBuilder.andWhere('location.districtId = :districtId', { districtId });
|
|
|
- }
|
|
|
-
|
|
|
- // 状态筛选
|
|
|
- if (isDisabled !== undefined) {
|
|
|
- queryBuilder.andWhere('location.isDisabled = :isDisabled', { isDisabled });
|
|
|
- }
|
|
|
-
|
|
|
- // 排序
|
|
|
- const orderBy = `location.${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.locationRepository.update(id, { isDeleted: 1 });
|
|
|
- return result.affected !== 0;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 启用/禁用地点
|
|
|
- */
|
|
|
- async toggleStatus(id: number, isDisabled: DisabledStatus): Promise<boolean> {
|
|
|
- const result = await this.locationRepository.update(id, { isDisabled });
|
|
|
- return result.affected !== 0;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 搜索地点
|
|
|
- */
|
|
|
- async searchLocations(params: {
|
|
|
- keyword?: string;
|
|
|
- provinceId?: number;
|
|
|
- cityId?: number;
|
|
|
- districtId?: number;
|
|
|
- limit?: number;
|
|
|
- }): Promise<LocationEntity[]> {
|
|
|
- const { keyword, provinceId, cityId, districtId, limit = 10 } = params;
|
|
|
-
|
|
|
- const queryBuilder = this.locationRepository
|
|
|
- .createQueryBuilder('location')
|
|
|
- .leftJoinAndSelect('location.province', 'province')
|
|
|
- .leftJoinAndSelect('location.city', 'city')
|
|
|
- .leftJoinAndSelect('location.district', 'district')
|
|
|
- .where('location.isDeleted = :isDeleted', { isDeleted: 0 })
|
|
|
- .andWhere('location.isDisabled = :isDisabled', { isDisabled: DisabledStatus.ENABLED });
|
|
|
-
|
|
|
- // 关键词搜索
|
|
|
- if (keyword) {
|
|
|
- queryBuilder.andWhere('(location.name LIKE :keyword OR location.address LIKE :keyword)', {
|
|
|
- keyword: `%${keyword}%`
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- // 省份筛选
|
|
|
- if (provinceId) {
|
|
|
- queryBuilder.andWhere('location.provinceId = :provinceId', { provinceId });
|
|
|
- }
|
|
|
-
|
|
|
- // 城市筛选
|
|
|
- if (cityId) {
|
|
|
- queryBuilder.andWhere('location.cityId = :cityId', { cityId });
|
|
|
- }
|
|
|
-
|
|
|
- // 区县筛选
|
|
|
- if (districtId) {
|
|
|
- queryBuilder.andWhere('location.districtId = :districtId', { districtId });
|
|
|
- }
|
|
|
-
|
|
|
- // 限制结果数量
|
|
|
- queryBuilder.take(limit);
|
|
|
-
|
|
|
- return await queryBuilder.getMany();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据省市区ID获取地点
|
|
|
- */
|
|
|
- async findByAreaIds(params: {
|
|
|
- provinceId?: number;
|
|
|
- cityId?: number;
|
|
|
- districtId?: number;
|
|
|
- }): Promise<LocationEntity[]> {
|
|
|
- const { provinceId, cityId, districtId } = params;
|
|
|
-
|
|
|
- const queryBuilder = this.locationRepository
|
|
|
- .createQueryBuilder('location')
|
|
|
- .leftJoinAndSelect('location.province', 'province')
|
|
|
- .leftJoinAndSelect('location.city', 'city')
|
|
|
- .leftJoinAndSelect('location.district', 'district')
|
|
|
- .where('location.isDeleted = :isDeleted', { isDeleted: 0 })
|
|
|
- .andWhere('location.isDisabled = :isDisabled', { isDisabled: DisabledStatus.ENABLED });
|
|
|
-
|
|
|
- if (provinceId) {
|
|
|
- queryBuilder.andWhere('location.provinceId = :provinceId', { provinceId });
|
|
|
- }
|
|
|
-
|
|
|
- if (cityId) {
|
|
|
- queryBuilder.andWhere('location.cityId = :cityId', { cityId });
|
|
|
- }
|
|
|
-
|
|
|
- if (districtId) {
|
|
|
- queryBuilder.andWhere('location.districtId = :districtId', { districtId });
|
|
|
- }
|
|
|
-
|
|
|
- return await queryBuilder.getMany();
|
|
|
- }
|
|
|
-}
|