refactor(service): 统一字典项关联字段命名和使用方式

将字典项关联字段从`dict_id`改为`dict`对象引用,简化关联查询
修复product.service.ts中flavors查询的错误条件
This commit is contained in:
tikkhun 2025-11-27 15:42:00 +08:00
parent d9800f341f
commit 46bdcf0c69
2 changed files with 17 additions and 17 deletions

View File

@ -132,19 +132,19 @@ export class ProductService {
.leftJoin(
DictItem,
'brand',
'brand.id = product.brandId AND brand.dict_id = :brandDictId',
'brand.id = product.brandId AND brand.dict = :brandDictId',
{ brandDictId: brandDict?.id }
)
.leftJoin(
DictItem,
'flavor',
'flavor.id = product.flavorsId AND flavor.dict_id = :flavorDictId',
'flavor.id = product.flavorsId AND flavor.dict = :flavorDictId',
{ flavorDictId: flavorDict?.id }
)
.leftJoin(
DictItem,
'strength',
'strength.id = product.strengthId AND strength.dict_id = :strengthDictId',
'strength.id = product.strengthId AND strength.dict = :strengthDictId',
{ strengthDictId: strengthDict?.id }
)
.select([
@ -202,7 +202,7 @@ export class ProductService {
// 查找字典项
let item = await this.dictItemModel.findOne({
where: { title: itemTitle, dict_id: dict.id },
where: { title: itemTitle, dict: { id: dict.id } },
});
// 如果字典项不存在,则创建
@ -210,7 +210,7 @@ export class ProductService {
item = new DictItem();
item.title = itemTitle;
item.name = itemName || itemTitle; // 如果没有提供 name则使用 title
item.dict_id = dict.id;
item.dict = dict;
await this.dictItemModel.save(item);
}
@ -348,7 +348,7 @@ export class ProductService {
}
// 设置查询条件
const where: any = { title, dict_id: brandDict.id };
const where: any = { title, dict: { id: brandDict.id } };
if (id) where.id = Not(id);
// 统计数量
@ -378,7 +378,7 @@ export class ProductService {
}
// 设置查询条件
const where: any = { dict_id: brandDict.id };
const where: any = { dict: { id: brandDict.id } };
if (title) {
where.title = Like(`%${title}%`);
}
@ -399,7 +399,7 @@ export class ProductService {
}
// 返回所有品牌
return this.dictItemModel.find({ where: { dict_id: brandDict.id } });
return this.dictItemModel.find({ where: { dict: { id: brandDict.id } } });
}
async createBrand(createBrandDTO: CreateBrandDTO): Promise<DictItem> {
@ -419,7 +419,7 @@ export class ProductService {
const brand = new DictItem();
brand.title = title;
brand.name = name;
brand.dict_id = brandDict.id;
brand.dict = brandDict;
// 保存到数据库
return await this.dictItemModel.save(brand);
@ -486,7 +486,7 @@ export class ProductService {
...pagination,
};
}
const where: any = { dict_id: flavorsDict.id };
const where: any = { dict: { id: flavorsDict.id } };
if (title) {
where.title = Like(`%${title}%`);
}
@ -500,7 +500,7 @@ export class ProductService {
if (!flavorsDict) {
return [];
}
return this.dictItemModel.find({ where: { dict_id: flavorsDict.id } });
return this.dictItemModel.find({ where: { id: flavorsDict.id } });
}
async createFlavors(createFlavorsDTO: CreateFlavorsDTO): Promise<DictItem> {
@ -514,7 +514,7 @@ export class ProductService {
const flavors = new DictItem();
flavors.title = title;
flavors.name = name;
flavors.dict_id = flavorsDict.id;
flavors.dict = flavorsDict;
return await this.dictItemModel.save(flavors);
}
@ -549,7 +549,7 @@ export class ProductService {
if (!strengthDict) {
return false;
}
const where: any = { title, dict_id: strengthDict.id };
const where: any = { title, dict: { id: strengthDict.id } };
if (id) where.id = Not(id);
const count = await this.dictItemModel.count({
where,
@ -570,7 +570,7 @@ export class ProductService {
...pagination,
};
}
const where: any = { dict_id: strengthDict.id };
const where: any = { dict: { id: strengthDict.id } };
if (title) {
where.title = Like(`%${title}%`);
}
@ -584,7 +584,7 @@ export class ProductService {
if (!strengthDict) {
return [];
}
return this.dictItemModel.find({ where: { dict_id: strengthDict.id } });
return this.dictItemModel.find({ where: { dict: { id: strengthDict.id } } });
}
async createStrength(createStrengthDTO: CreateStrengthDTO): Promise<DictItem> {
@ -598,7 +598,7 @@ export class ProductService {
const strength = new DictItem();
strength.title = title;
strength.name = name;
strength.dict_id = strengthDict.id;
strength.dict = strengthDict;
return await this.dictItemModel.save(strength);
}

View File

@ -90,7 +90,7 @@ export class SiteService {
async disable(id: string | number, disabled: boolean) {
// 设置站点禁用状态true -> 1, false -> 0
await this.siteModel.update({ id: Number(id) }, { isDisabled: disabled ? 1 : 0 });
await this.siteModel.update({ id: Number(id) }, { isDisabled: disabled });
return true;
}
}