platform.service.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { GenericCrudService } from '@d8d/shared-crud';
  2. import { DataSource, Repository, Like, Not } from 'typeorm';
  3. import { Platform } from '../entities/platform.entity';
  4. export class PlatformService extends GenericCrudService<Platform> {
  5. constructor(dataSource: DataSource) {
  6. super(dataSource, Platform);
  7. }
  8. /**
  9. * 创建平台 - 覆盖父类方法,添加名称唯一性检查
  10. */
  11. override async create(data: Partial<Platform>, userId?: string | number): Promise<Platform> {
  12. // 检查平台名称是否已存在(只检查正常状态的平台)
  13. if (data.platformName) {
  14. const existingPlatform = await this.repository.findOne({
  15. where: { platformName: data.platformName, status: 1 }
  16. });
  17. if (existingPlatform) {
  18. throw new Error('平台名称已存在');
  19. }
  20. }
  21. // 设置默认值
  22. const platformData = {
  23. contactPerson: '',
  24. contactPhone: '',
  25. contactEmail: '',
  26. ...data,
  27. status: 1,
  28. createTime: new Date(),
  29. updateTime: new Date()
  30. };
  31. return super.create(platformData, userId);
  32. }
  33. /**
  34. * 更新平台 - 覆盖父类方法,添加存在性和名称重复检查
  35. */
  36. override async update(id: number, data: Partial<Platform>, userId?: string | number): Promise<Platform | null> {
  37. // 检查平台是否存在(只检查正常状态的平台)
  38. const platform = await this.repository.findOne({ where: { id, status: 1 } });
  39. if (!platform) {
  40. throw new Error('平台不存在');
  41. }
  42. // 检查平台名称是否与其他平台重复(只检查正常状态的平台)
  43. if (data.platformName && data.platformName !== platform.platformName) {
  44. const existingPlatform = await this.repository.findOne({
  45. where: { platformName: data.platformName, id: Not(id), status: 1 }
  46. });
  47. if (existingPlatform) {
  48. throw new Error('平台名称已存在');
  49. }
  50. }
  51. // 设置更新时间
  52. const updateData = {
  53. ...data,
  54. updateTime: new Date()
  55. };
  56. // 现在可以使用父类的update方法,因为主键字段名已改为'id'
  57. return super.update(id, updateData, userId);
  58. }
  59. /**
  60. * 删除平台 - 覆盖父类方法,改为软删除(设置status为0)
  61. */
  62. override async delete(id: number, userId?: string | number): Promise<boolean> {
  63. // 改为软删除:设置status为0
  64. const result = await this.repository.update({ id }, { status: 0 });
  65. return result.affected === 1;
  66. }
  67. /**
  68. * 获取所有平台(分页) - 自定义方法,返回源服务的格式
  69. */
  70. async findAll(skip?: number, take?: number): Promise<{ data: Platform[], total: number }> {
  71. const [data, total] = await this.repository.findAndCount({
  72. where: { status: 1 }, // 只返回正常状态的平台
  73. skip: skip ?? 0,
  74. take: take ?? 10,
  75. order: { id: 'DESC' }
  76. });
  77. return { data, total };
  78. }
  79. /**
  80. * 按名称搜索平台 - 自定义方法
  81. */
  82. async searchByName(name: string, skip?: number, take?: number): Promise<{ data: Platform[], total: number }> {
  83. const [data, total] = await this.repository.findAndCount({
  84. where: {
  85. platformName: Like(`%${name}%`),
  86. status: 1 // 只返回正常状态的平台
  87. },
  88. skip: skip ?? 0,
  89. take: take ?? 10,
  90. order: { id: 'DESC' }
  91. });
  92. return { data, total };
  93. }
  94. /**
  95. * 获取单个平台 - 自定义方法
  96. */
  97. async findOne(id: number): Promise<Platform | null> {
  98. return this.repository.findOne({ where: { id, status: 1 } }); // 只返回正常状态的平台
  99. }
  100. }