fix: product create duplicated issue

This commit is contained in:
louis 2024-04-09 15:24:54 +08:00
parent 2d71fdf001
commit 2df310b1c0
4 changed files with 34 additions and 6 deletions

View File

@ -57,7 +57,6 @@ export class ProductService {
keyword: `%${keyword}%`
}
);
}
return paginate<ProductEntity>(sqb, {
page,
@ -69,9 +68,9 @@ export class ProductService {
*
*/
async create(dto: ProductDto): Promise<void> {
const { name, companyId } = dto;
const { name, companyId, productSpecification } = dto;
const isExsit = await this.productRepository.findOne({
where: { name, company: { id: companyId } }
where: { productSpecification, name, company: { id: companyId } }
});
if (isExsit) {
throw new BusinessException(ErrorEnum.PRODUCT_EXIST);
@ -86,6 +85,13 @@ export class ProductService {
*/
async update(id: number, { fileIds, ...data }: Partial<ProductUpdateDto>): Promise<void> {
await this.entityManager.transaction(async manager => {
const { name, companyId, productSpecification } = data;
const isExsit = await this.productRepository.findOne({
where: { productSpecification, name, company: { id: companyId } }
});
if (isExsit) {
throw new BusinessException(ErrorEnum.PRODUCT_EXIST);
}
await manager.update(
ProductEntity,
id,

View File

@ -116,7 +116,6 @@ export class DeptService {
}
const deptTree = await this.deptRepository.findTrees({
depth: 2,
relations: ['parent']
});

View File

@ -1,5 +1,9 @@
import { ApiHideProperty } from '@nestjs/swagger';
import { Exclude } from 'class-transformer';
import pinyin from 'pinyin';
import {
BeforeInsert,
BeforeUpdate,
Column,
Entity,
JoinColumn,
@ -32,6 +36,25 @@ export class UserEntity extends CommonEntity {
@Column({ nullable: true })
nickname: string;
@ApiHideProperty()
@Column({
name: 'name_pinyin',
type: 'varchar',
length: 255,
nullable: true,
comment: '产品名称的拼音'
})
namePinyin: string;
@BeforeInsert()
@BeforeUpdate()
updateNamePinyin() {
this.namePinyin = pinyin(this.nickname, {
style: pinyin.STYLE_NORMAL,
heteronym: false
}).join('');
}
@Column({ name: 'avatar', nullable: true })
avatar: string;

View File

@ -5,7 +5,7 @@ import Redis from 'ioredis';
import { isEmpty, isNil, isNumber } from 'lodash';
import { EntityManager, In, Like, Repository } from 'typeorm';
import pinyin from 'pinyin';
import { BusinessException } from '~/common/exceptions/biz.exception';
import { ErrorEnum } from '~/constants/error-code.constant';
import { ROOT_ROLE_ID, SYS_USER_INITPASSWORD } from '~/constants/system.constant';
@ -268,7 +268,7 @@ export class UserService {
if (keyword) {
//关键字模糊查询product的name,productNumber,productSpecification
queryBuilder.andWhere(
'(user.nickname like :keyword or dept.name like :keyword)',
'(user.nickname like :keyword or user.namePinyin like :keyword or dept.name like :keyword)',
{
keyword: `%${keyword}%`
}