import { Init, Provide } from '@midwayjs/core'; import { BaseService } from '@cool-midway/core'; import { InjectEntityModel } from '@midwayjs/typeorm'; import { Repository } from 'typeorm'; import { CardCasesEntity } from '../entity/cases'; /** * 后台按名片拉取案例的参数格式 */ export interface GetCasesByCardIdParams { cardId: number | string; } /** * 出海案例作品集服务 * 提供出海案例的标准 CRUD 及按名片筛选能力 */ @Provide() export class CardCasesService extends BaseService { @InjectEntityModel(CardCasesEntity) cardCasesEntity: Repository; @Init() async init() { await super.init(); this.setEntity(this.cardCasesEntity); } /** * 按名片 cardId 获取案例列表 * 严格按照 sortOrder 升序排列 * * @param params 名片 ID 或对象参数 */ async getCasesByCardId( params: number | string | GetCasesByCardIdParams ): Promise { const cardId = typeof params === 'object' && params !== null ? params.cardId : params; const numericCardId = Number(cardId); if (!numericCardId) { return []; } return this.cardCasesEntity.find({ where: { cardId: numericCardId }, order: { sortOrder: 'ASC', id: 'DESC' }, }); } }