Files
certd/packages/libs/lib-server/src/basic/base-service.ts

218 lines
5.0 KiB
TypeScript
Raw Normal View History

2024-10-03 22:03:49 +08:00
import { ValidateException } from './exception/index.js';
2024-07-15 00:30:33 +08:00
import * as _ from 'lodash-es';
2024-10-03 22:03:49 +08:00
import { PermissionException } from './exception/index.js';
2024-10-10 02:15:05 +08:00
import { In, Repository } from 'typeorm';
2024-07-15 00:30:33 +08:00
import { Inject } from '@midwayjs/core';
2023-06-27 09:29:43 +08:00
import { TypeORMDataSourceManager } from '@midwayjs/typeorm';
2024-07-15 00:30:33 +08:00
import { EntityManager } from 'typeorm/entity-manager/EntityManager.js';
2023-01-29 13:44:19 +08:00
/**
*
*/
export abstract class BaseService<T> {
@Inject()
2023-06-27 09:29:43 +08:00
dataSourceManager: TypeORMDataSourceManager;
2023-01-29 13:44:19 +08:00
abstract getRepository(): Repository<T>;
2023-06-27 09:29:43 +08:00
async transaction(callback: (entityManager: EntityManager) => Promise<any>) {
const dataSource = this.dataSourceManager.getDataSource('default');
2024-09-02 16:59:49 +08:00
await dataSource.transaction(callback as any);
2023-06-27 09:29:43 +08:00
}
2023-01-29 13:44:19 +08:00
/**
* ID
* @param id ID
* @param infoIgnoreProperty
*/
async info(id, infoIgnoreProperty?): Promise<T | null> {
if (!id) {
throw new ValidateException('id不能为空');
}
2023-05-01 21:51:41 +08:00
const info = await this.getRepository().findOneBy({ id } as any);
2023-01-29 13:44:19 +08:00
if (info && infoIgnoreProperty) {
for (const property of infoIgnoreProperty) {
delete info[property];
}
}
return info;
}
/**
*
2023-06-27 09:29:43 +08:00
* @param options
2023-01-29 13:44:19 +08:00
*/
async find(options) {
return await this.getRepository().find(options);
}
/**
*
* @param ids ID集合 [1,2,3] 1,2,3
2024-10-10 02:15:05 +08:00
* @param where
2023-01-29 13:44:19 +08:00
*/
2024-10-10 02:15:05 +08:00
async delete(ids: any, where?: any) {
if (!ids) {
throw new ValidateException('ids不能为空');
2023-01-29 13:44:19 +08:00
}
2024-10-10 02:15:05 +08:00
if (typeof ids === 'string') {
ids = ids.split(',');
}
if (ids.length === 0) {
return;
}
await this.getRepository().delete({
id: In(ids),
...where,
});
2023-01-29 13:44:19 +08:00
await this.modifyAfter(ids);
}
/**
* |
* @param param
*/
async addOrUpdate(param) {
await this.getRepository().save(param);
}
/**
*
* @param param
*/
async add(param) {
2024-08-06 11:10:33 +08:00
const now = new Date();
2023-01-29 13:44:19 +08:00
param.createTime = now;
param.updateTime = now;
await this.addOrUpdate(param);
await this.modifyAfter(param);
return {
id: param.id,
};
}
/**
*
* @param param
*/
async update(param) {
2024-10-10 02:15:05 +08:00
if (!param.id) throw new ValidateException('id 不能为空');
2024-08-06 11:10:33 +08:00
param.updateTime = new Date();
2023-01-29 13:44:19 +08:00
await this.addOrUpdate(param);
await this.modifyAfter(param);
}
/**
* ||
* @param data
*/
async modifyAfter(data) {}
/**
*
* @param query bean
* @param page
* @param order
* @param buildQuery
*/
async page(query, page = { offset: 0, limit: 20 }, order, buildQuery) {
if (page.offset == null) {
page.offset = 0;
}
if (page.limit == null) {
page.limit = 20;
}
const qb = this.getRepository().createQueryBuilder('main');
if (order && order.prop) {
qb.addOrderBy('main.' + order.prop, order.asc ? 'ASC' : 'DESC');
2023-01-29 13:44:19 +08:00
}
qb.addOrderBy('id', 'DESC');
2023-01-29 13:44:19 +08:00
qb.offset(page.offset).limit(page.limit);
//根据bean query
if (query) {
let whereSql = '';
let index = 0;
_.forEach(query, (value, key) => {
if (!value) {
return;
}
if (index !== 0) {
whereSql += ' and ';
}
whereSql += ` main.${key} = :${key} `;
index++;
});
if (index > 0) {
qb.where(whereSql, query);
}
}
//自定义query
if (buildQuery) {
buildQuery(qb);
}
const list = await qb.getMany();
const total = await qb.getCount();
return {
records: list,
total,
offset: page.offset,
limit: page.limit,
};
}
/**
*
* @param query bean
* @param order
* @param buildQuery
*/
async list(query, order, buildQuery) {
const qb = this.getRepository().createQueryBuilder('main');
if (order && order.prop) {
qb.orderBy('main.' + order.prop, order.asc ? 'ASC' : 'DESC');
} else {
qb.orderBy('id', 'DESC');
}
//根据bean query
if (query) {
let whereSql = '';
let index = 0;
_.forEach(query, (value, key) => {
if (!value) {
return;
}
if (index !== 0) {
whereSql += ' and ';
}
whereSql += ` main.${key} = :${key} `;
index++;
});
if (index > 0) {
qb.where(whereSql, query);
}
}
//自定义query
if (buildQuery) {
buildQuery(qb);
}
return await qb.getMany();
}
async checkUserId(id: any = 0, userId: number, userKey = 'userId') {
2023-01-29 13:44:19 +08:00
const res = await this.getRepository().findOne({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2023-01-29 13:44:19 +08:00
// @ts-ignore
select: { [userKey]: true },
where: {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2023-06-27 09:29:43 +08:00
// @ts-ignore
id,
2023-01-29 13:44:19 +08:00
},
});
2023-06-27 09:29:43 +08:00
if (!res || res[userKey] === userId) {
2023-01-29 13:44:19 +08:00
return;
}
throw new PermissionException('权限不足');
}
}