import { BaseService } from '@/common/base';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Like, Repository } from 'typeorm';
import { Create{{upcaseName name}}Dto } from './dto/create-{{fileName name}}.dto';
import { Find{{upcaseName name}}Dto } from './dto/find-{{fileName name}}.dto';
import { Update{{upcaseName name}}Dto } from './dto/update-{{fileName name}}.dto';
import { {{upcaseName name}} } from './entities/{{fileName name}}.entity';

@Injectable()
export class {{upcaseName name}}Service extends BaseService {
  constructor(@InjectRepository({{upcaseName name}}) private {{lowcaseName name}}Repository: Repository<{{upcaseName name}}>) {
    super();
  }

  /**
   * 新增{{cnName}}
   */
  async create(create{{upcaseName name}}Dto: Create{{upcaseName name}}Dto) {
    const {{lowcaseName name}} = this.{{lowcaseName name}}Repository.create(create{{upcaseName name}}Dto);
    await this.{{lowcaseName name}}Repository.save({{lowcaseName name}});
    return {{lowcaseName name}}.id;
  }

  /**
   * 条件/分页查询
   */
  async findMany(find{{upcaseName name}}dto: Find{{upcaseName name}}Dto) {
    const { page, size } = find{{upcaseName name}}dto;
    const { skip, take } = this.formatPagination(page, size, true);
    return this.{{lowcaseName name}}Repository.findAndCount({ skip, take });
  }

  /**
   * 根据ID查询
   */
  findOne(idOrOptions: number | Partial<{{upcaseName name}}>) {
    const where = typeof idOrOptions === 'number' ? { id: idOrOptions } : (idOrOptions as any);
    return this.{{lowcaseName name}}Repository.findOne({ where });
  }

  /**
   * 根据ID更新
   */
  update(id: number, update{{upcaseName name}}Dto: Update{{upcaseName name}}Dto) {
    return this.{{lowcaseName name}}Repository.update(id, update{{upcaseName name}}Dto);
  }

  /**
   * 根据ID删除(软删除)
   */
  remove(id: number) {
    return this.{{lowcaseName name}}Repository.softDelete(id);
  }
}
