channel.service.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. 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 }
  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. async update(id: number, data: Partial<Channel>, userId?: string | number): Promise<Channel | null> {
  38. // 检查渠道是否存在
  39. const channel = await this.repository.findOne({ where: { channelId: id } });
  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, channelId: Not(id) }
  47. });
  48. if (existingChannel) {
  49. throw new Error('渠道名称已存在');
  50. }
  51. }
  52. // 设置更新时间
  53. const updateData = {
  54. ...data,
  55. updateTime: new Date()
  56. };
  57. return super.update(id, updateData, userId);
  58. }
  59. /**
  60. * 删除渠道 - 覆盖父类方法,改为软删除(设置status为0)
  61. */
  62. 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: Channel[], total: number }> {
  71. const [data, total] = await this.repository.findAndCount({
  72. skip: skip ?? 0,
  73. take: take ?? 10,
  74. order: { channelId: 'DESC' }
  75. });
  76. return { data, total };
  77. }
  78. /**
  79. * 按名称搜索渠道 - 自定义方法
  80. */
  81. async searchByName(name: string, skip?: number, take?: number): Promise<{ data: Channel[], total: number }> {
  82. const [data, total] = await this.repository.findAndCount({
  83. where: {
  84. channelName: Like(`%${name}%`)
  85. },
  86. skip: skip ?? 0,
  87. take: take ?? 10,
  88. order: { channelId: 'DESC' }
  89. });
  90. return { data, total };
  91. }
  92. /**
  93. * 获取单个渠道 - 自定义方法
  94. */
  95. async findOne(channelId: number): Promise<Channel | null> {
  96. return this.repository.findOne({ where: { channelId } });
  97. }
  98. }