channel.service.ts 3.5 KB

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