2024-10-07 03:21:16 +08:00
|
|
|
import { Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
|
|
|
|
import { InjectEntityModel } from '@midwayjs/typeorm';
|
|
|
|
|
import { Repository } from 'typeorm';
|
2024-11-01 18:09:32 +08:00
|
|
|
import { BaseService, ListReq, ValidateException } from '@certd/lib-server';
|
|
|
|
|
import { CnameProviderEntity } from '../entity/cname-provider.js';
|
|
|
|
|
import { CommonProviders } from './common-provider.js';
|
2024-10-07 03:21:16 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 授权
|
|
|
|
|
*/
|
|
|
|
|
@Provide()
|
|
|
|
|
@Scope(ScopeEnum.Singleton)
|
|
|
|
|
export class CnameProviderService extends BaseService<CnameProviderEntity> {
|
|
|
|
|
@InjectEntityModel(CnameProviderEntity)
|
|
|
|
|
repository: Repository<CnameProviderEntity>;
|
|
|
|
|
|
2024-10-09 02:34:28 +08:00
|
|
|
//@ts-ignore
|
2024-10-07 03:21:16 +08:00
|
|
|
getRepository() {
|
|
|
|
|
return this.repository;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getDefault() {
|
2024-11-01 18:09:32 +08:00
|
|
|
return await this.repository.findOne({ where: { isDefault: true, disabled: false } });
|
2024-10-07 03:21:16 +08:00
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 新增
|
|
|
|
|
* @param param 数据
|
|
|
|
|
*/
|
|
|
|
|
async add(param: any) {
|
|
|
|
|
const def = await this.getDefault();
|
|
|
|
|
if (!def) {
|
|
|
|
|
param.isDefault = true;
|
|
|
|
|
}
|
|
|
|
|
const res = await super.add(param);
|
|
|
|
|
if (param.isDefault) {
|
|
|
|
|
await this.setDefault(res.id);
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改
|
|
|
|
|
* @param param 数据
|
|
|
|
|
*/
|
|
|
|
|
async update(param: any) {
|
|
|
|
|
await super.update(param);
|
|
|
|
|
if (param.isDefault) {
|
|
|
|
|
await this.setDefault(param.id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async delete(ids: any) {
|
2024-10-10 22:13:07 +08:00
|
|
|
if (!ids) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-10-07 03:21:16 +08:00
|
|
|
if (!(ids instanceof Array)) {
|
|
|
|
|
ids = [ids];
|
|
|
|
|
}
|
|
|
|
|
for (const id of ids) {
|
|
|
|
|
const info = await this.info(id);
|
|
|
|
|
if (info.isDefault) {
|
|
|
|
|
throw new ValidateException('默认的CNAME服务不能删除,请先修改为非默认值');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
await super.delete(ids);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async setDefault(id: number) {
|
|
|
|
|
await this.transaction(async em => {
|
|
|
|
|
await em.getRepository(CnameProviderEntity).update({ isDefault: true }, { isDefault: false });
|
|
|
|
|
await em.getRepository(CnameProviderEntity).update({ id }, { isDefault: true });
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async setDisabled(id: number, disabled: boolean) {
|
|
|
|
|
await this.repository.update({ id }, { disabled });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getByPriority() {
|
|
|
|
|
const def = await this.getDefault();
|
|
|
|
|
if (def) {
|
|
|
|
|
return def;
|
|
|
|
|
}
|
2024-11-01 18:09:32 +08:00
|
|
|
const founds = await this.repository.find({ take: 1, order: { createTime: 'DESC' }, where: { disabled: false } });
|
2024-10-10 22:13:07 +08:00
|
|
|
if (founds && founds.length > 0) {
|
|
|
|
|
return founds[0];
|
2024-10-07 03:21:16 +08:00
|
|
|
}
|
2024-11-01 18:09:32 +08:00
|
|
|
return CommonProviders[0] as CnameProviderEntity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async list(req: ListReq): Promise<any[]> {
|
|
|
|
|
const list = await super.list(req);
|
|
|
|
|
|
|
|
|
|
return [...list, ...CommonProviders];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async info(id: any, infoIgnoreProperty?: any): Promise<any | null> {
|
|
|
|
|
if (id < 0) {
|
|
|
|
|
//使用公共provider
|
|
|
|
|
return CommonProviders.find(p => p.id === id);
|
|
|
|
|
}
|
|
|
|
|
return await super.info(id, infoIgnoreProperty);
|
2024-10-07 03:21:16 +08:00
|
|
|
}
|
|
|
|
|
}
|