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

213 lines
4.9 KiB
TypeScript
Raw Normal View History

2024-07-15 00:30:33 +08:00
import { ValidateException } from './exception/validation-exception.js';
import * as _ from 'lodash-es';
import { PermissionException } from './exception/permission-exception.js';
2023-01-29 13:44:19 +08:00
import { 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');
await dataSource.transaction(callback);
}
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
*/
async delete(ids) {
if (ids instanceof Array) {
await this.getRepository().delete(ids);
} else if (typeof ids === 'string') {
await this.getRepository().delete(ids.split(','));
} else {
//ids是一个condition
await this.getRepository().delete(ids);
}
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) {
if (!param.id) throw new ValidateException('no 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();
}
2023-06-27 09:29:43 +08:00
async checkUserId(id: any = 0, userId, userKey = 'userId') {
2023-01-29 13:44:19 +08:00
// @ts-ignore
const res = await this.getRepository().findOne({
// @ts-ignore
select: { [userKey]: true },
2023-06-25 15:30:18 +08:00
// @ts-ignore
2023-01-29 13:44:19 +08:00
where: {
2023-06-27 09:29:43 +08:00
// @ts-ignore
id,
2023-01-29 13:44:19 +08:00
},
});
// @ts-ignore
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('权限不足');
}
}