forked from yoone/API
107 lines
3.2 KiB
TypeScript
107 lines
3.2 KiB
TypeScript
import { Provide } from '@midwayjs/core';
|
|
import { InjectEntityModel } from '@midwayjs/typeorm';
|
|
import { Repository, Like, In } from 'typeorm';
|
|
import { Category } from '../entity/category.entity';
|
|
import { CategoryAttribute } from '../entity/category_attribute.entity';
|
|
import { Dict } from '../entity/dict.entity';
|
|
|
|
@Provide()
|
|
export class CategoryService {
|
|
@InjectEntityModel(Category)
|
|
categoryModel: Repository<Category>;
|
|
|
|
@InjectEntityModel(CategoryAttribute)
|
|
categoryAttributeModel: Repository<CategoryAttribute>;
|
|
|
|
@InjectEntityModel(Dict)
|
|
dictModel: Repository<Dict>;
|
|
|
|
async getAll() {
|
|
return await this.categoryModel.find({
|
|
order: {
|
|
sort: 'DESC',
|
|
createdAt: 'DESC'
|
|
},
|
|
relations: ['attributes', 'attributes.attributeDict']
|
|
});
|
|
}
|
|
|
|
async getList(options: { current?: number; pageSize?: number }, name?: string) {
|
|
const { current = 1, pageSize = 10 } = options;
|
|
const where = name ? [
|
|
{ title: Like(`%${name}%`) },
|
|
{ name: Like(`%${name}%`) }
|
|
] : [];
|
|
|
|
const [list, total] = await this.categoryModel.findAndCount({
|
|
where: where.length ? where : undefined,
|
|
skip: (current - 1) * pageSize,
|
|
take: pageSize,
|
|
order: {
|
|
sort: 'DESC',
|
|
createdAt: 'DESC'
|
|
}
|
|
});
|
|
return { list, total };
|
|
}
|
|
|
|
async create(data: Partial<Category>) {
|
|
return await this.categoryModel.save(data);
|
|
}
|
|
|
|
async update(id: number, data: Partial<Category>) {
|
|
await this.categoryModel.update(id, data);
|
|
return await this.categoryModel.findOneBy({ id });
|
|
}
|
|
|
|
async delete(id: number) {
|
|
return await this.categoryModel.delete(id);
|
|
}
|
|
|
|
async findByName(name: string) {
|
|
return await this.categoryModel.findOneBy({ name });
|
|
}
|
|
|
|
async getCategoryAttributes(categoryId: number) {
|
|
const categoryAttributes = await this.categoryAttributeModel.find({
|
|
where: { category: { id: categoryId } },
|
|
relations: ['attributeDict', 'category'],
|
|
});
|
|
|
|
return categoryAttributes.map(ca => ca.attributeDict);
|
|
}
|
|
|
|
async createCategoryAttribute(categoryId: number, attributeDictIds: number[]) {
|
|
const category = await this.categoryModel.findOneBy({ id: categoryId });
|
|
if (!category) throw new Error('分类不存在');
|
|
|
|
const dicts = await this.dictModel.findBy({ id: In(attributeDictIds) });
|
|
if (dicts.length !== attributeDictIds.length) throw new Error('部分属性字典不存在');
|
|
|
|
// 检查是否已存在
|
|
const exist = await this.categoryAttributeModel.find({
|
|
where: {
|
|
category: { id: categoryId },
|
|
attributeDict: { id: In(attributeDictIds) }
|
|
},
|
|
relations: ['attributeDict']
|
|
});
|
|
|
|
const existIds = exist.map(e => e.attributeDict.id);
|
|
const newIds = attributeDictIds.filter(id => !existIds.includes(id));
|
|
|
|
const newRecords = newIds.map(id => {
|
|
const record = new CategoryAttribute();
|
|
record.category = category;
|
|
record.attributeDict = dicts.find(d => d.id === id);
|
|
return record;
|
|
});
|
|
|
|
return await this.categoryAttributeModel.save(newRecords);
|
|
}
|
|
|
|
async deleteCategoryAttribute(id: number) {
|
|
return await this.categoryAttributeModel.delete(id);
|
|
}
|
|
}
|