import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Topic } from 'kkk-lib/article/entity/topic.entity'; import { Category } from 'kkk-lib/article/entity/category.entity'; @Injectable() export class TopicService { constructor( @InjectRepository(Topic) private readonly repository: Repository, ) {} async findAllByParams(query: any): Promise { let querySql = 'select * from topic a where 1=1 '; let countSql = 'select count(*) as total from topic a where 1=1 '; const whereSql = ''; let limit = 10; let offset = 0; // if(query['title']){ // whereSql = ` ${whereSql} AND a.title='${query['title']}' ` // } // // if(query['keyword']){ // whereSql = ` ${whereSql} AND ( a.title like '%${query['keyword']}%' OR a.content like '%${query['keyword']}%' ) ` // } if (query['limit']) { limit = query['limit']; } if (query['offset']) { offset = query['offset']; } if (query['page']) { const page = query['page']; offset = limit * (page - 1); } querySql = querySql + whereSql + ' limit ' + limit + ' offset ' + offset + ' '; countSql = countSql + whereSql; const queryList = await this.repository.query(querySql); const countRet = await this.repository.query(countSql); const total = countRet[0]['total']; const retPage = Math.ceil(offset / limit) + 1; const pageCount = Math.ceil(total / limit); return { data: queryList, total: total, page: retPage, pageCount: pageCount, limit: limit, offset: offset, }; } }