406 lines
10 KiB
TypeScript
406 lines
10 KiB
TypeScript
import {
|
|
Inject,
|
|
Post,
|
|
Put,
|
|
Get,
|
|
Body,
|
|
Param,
|
|
Del,
|
|
Query,
|
|
Controller,
|
|
} from '@midwayjs/core';
|
|
import { ProductService } from '../service/product.service';
|
|
import { errorResponse, successResponse } from '../utils/response.util';
|
|
import {
|
|
BatchSetSkuDTO,
|
|
CreateCategoryDTO,
|
|
CreateFlavorsDTO,
|
|
CreateProductDTO,
|
|
CreateStrengthDTO,
|
|
QueryCategoryDTO,
|
|
QueryFlavorsDTO,
|
|
QueryProductDTO,
|
|
QueryStrengthDTO,
|
|
UpdateCategoryDTO,
|
|
UpdateFlavorsDTO,
|
|
UpdateProductDTO,
|
|
UpdateStrengthDTO,
|
|
} from '../dto/product.dto';
|
|
import { ApiOkResponse } from '@midwayjs/swagger';
|
|
import {
|
|
BooleanRes,
|
|
ProductCatListRes,
|
|
ProductCatRes,
|
|
ProductListRes,
|
|
ProductRes,
|
|
ProductsRes,
|
|
} from '../dto/reponse.dto';
|
|
|
|
@Controller('/product')
|
|
export class ProductController {
|
|
@Inject()
|
|
productService: ProductService;
|
|
ProductRes;
|
|
|
|
@ApiOkResponse({
|
|
description: '通过name搜索产品',
|
|
type: ProductsRes,
|
|
})
|
|
@Get('/search')
|
|
async searchProducts(@Query('name') name: string) {
|
|
try {
|
|
// 调用服务获取产品数据
|
|
const products = await this.productService.findProductsByName(name);
|
|
return successResponse(products);
|
|
} catch (error) {
|
|
return errorResponse(error.message || '获取数据失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: ProductRes,
|
|
})
|
|
@Get('/sku/:sku')
|
|
async productBySku(@Param('sku') sku: string) {
|
|
try {
|
|
// 调用服务获取产品数据
|
|
const product = await this.productService.findProductBySku(sku);
|
|
return successResponse(product);
|
|
} catch (error) {
|
|
return errorResponse(error.message || '获取数据失败');
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
description: '成功返回产品列表',
|
|
type: ProductListRes,
|
|
})
|
|
@Get('/list')
|
|
async getProductList(
|
|
@Query() query: QueryProductDTO
|
|
): Promise<ProductListRes> {
|
|
const { current = 1, pageSize = 10, name, categoryId } = query;
|
|
try {
|
|
const data = await this.productService.getProductList(
|
|
{ current, pageSize },
|
|
name,
|
|
categoryId
|
|
);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
console.log(error);
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: ProductRes,
|
|
})
|
|
@Post('/')
|
|
async createProduct(@Body() productData: CreateProductDTO) {
|
|
try {
|
|
const data = this.productService.createProduct(productData);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: ProductRes,
|
|
})
|
|
@Put('/:id')
|
|
async updateProduct(
|
|
@Param('id') id: number,
|
|
@Body() productData: UpdateProductDTO
|
|
) {
|
|
try {
|
|
const data = this.productService.updateProduct(id, productData);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: ProductRes,
|
|
})
|
|
@Put('updateNameCn/:id/:nameCn')
|
|
async updateProductNameCn(
|
|
@Param('id') id: number,
|
|
@Param('nameCn') nameCn: string
|
|
) {
|
|
try {
|
|
const data = this.productService.updateProductNameCn(id, nameCn);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@ApiOkResponse({
|
|
type: BooleanRes,
|
|
})
|
|
@Del('/:id')
|
|
async deleteProduct(@Param('id') id: number) {
|
|
try {
|
|
const data = await this.productService.deleteProduct(id);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: ProductCatListRes,
|
|
})
|
|
@Get('/categories')
|
|
async getCategories(@Query() query: QueryCategoryDTO) {
|
|
const { current = 1, pageSize = 10, name } = query;
|
|
try {
|
|
let data = await this.productService.getCategoryList(
|
|
{ current, pageSize },
|
|
name
|
|
);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse()
|
|
@Get('/categorieAll')
|
|
async getCategorieAll() {
|
|
try {
|
|
let data = await this.productService.getCategoryAll();
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: ProductCatRes,
|
|
})
|
|
@Post('/category')
|
|
async createCategory(@Body() categoryData: CreateCategoryDTO) {
|
|
try {
|
|
const hasCategory = await this.productService.hasCategory(
|
|
categoryData.name
|
|
);
|
|
if (hasCategory) {
|
|
return errorResponse('分类已存在');
|
|
}
|
|
let data = await this.productService.createCategory(categoryData);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: ProductCatRes,
|
|
})
|
|
@Put('/category/:id')
|
|
async updateCategory(
|
|
@Param('id') id: number,
|
|
@Body() categoryData: UpdateCategoryDTO
|
|
) {
|
|
try {
|
|
const hasCategory = await this.productService.hasCategory(
|
|
categoryData.name
|
|
);
|
|
if (hasCategory) {
|
|
return errorResponse('分类已存在');
|
|
}
|
|
const data = this.productService.updateCategory(id, categoryData);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: BooleanRes,
|
|
})
|
|
@Del('/category/:id')
|
|
async deleteCategory(@Param('id') id: number) {
|
|
try {
|
|
const hasProducts = await this.productService.hasProductsInCategory(id);
|
|
if (hasProducts) throw new Error('该分类下有商品,无法删除');
|
|
const data = await this.productService.deleteCategory(id);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@Post('/batchSetSku')
|
|
@ApiOkResponse({
|
|
description: '批量设置 sku 的响应结果',
|
|
type: BooleanRes,
|
|
})
|
|
async batchSetSku(@Body() body: BatchSetSkuDTO) {
|
|
try {
|
|
const result = await this.productService.batchSetSku(body.skus);
|
|
return successResponse(result, '批量设置 sku 成功');
|
|
} catch (error) {
|
|
return errorResponse(error.message, 400);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse()
|
|
@Get('/flavorsAll')
|
|
async getFlavorsAll() {
|
|
try {
|
|
let data = await this.productService.getFlavorsAll();
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse()
|
|
@Get('/flavors')
|
|
async getFlavors(@Query() query: QueryFlavorsDTO) {
|
|
const { current = 1, pageSize = 10, name } = query;
|
|
try {
|
|
let data = await this.productService.getFlavorsList(
|
|
{ current, pageSize },
|
|
name
|
|
);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse()
|
|
@Post('/flavors')
|
|
async createFlavors(@Body() flavorsData: CreateFlavorsDTO) {
|
|
try {
|
|
const hasFlavors = await this.productService.hasFlavors(flavorsData.name);
|
|
if (hasFlavors) {
|
|
return errorResponse('分类已存在');
|
|
}
|
|
let data = await this.productService.createFlavors(flavorsData);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse()
|
|
@Put('/flavors/:id')
|
|
async updateFlavors(
|
|
@Param('id') id: number,
|
|
@Body() flavorsData: UpdateFlavorsDTO
|
|
) {
|
|
try {
|
|
const hasFlavors = await this.productService.hasFlavors(flavorsData.name);
|
|
if (hasFlavors) {
|
|
return errorResponse('分类已存在');
|
|
}
|
|
const data = this.productService.updateFlavors(id, flavorsData);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: BooleanRes,
|
|
})
|
|
@Del('/flavors/:id')
|
|
async deleteFlavors(@Param('id') id: number) {
|
|
try {
|
|
const hasProducts = await this.productService.hasProductsInFlavors(id);
|
|
if (hasProducts) throw new Error('该分类下有商品,无法删除');
|
|
const data = await this.productService.deleteFlavors(id);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse()
|
|
@Get('/strengthAll')
|
|
async getStrengthAll() {
|
|
try {
|
|
let data = await this.productService.getStrengthAll();
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse()
|
|
@Get('/strength')
|
|
async getStrength(@Query() query: QueryStrengthDTO) {
|
|
const { current = 1, pageSize = 10, name } = query;
|
|
try {
|
|
let data = await this.productService.getStrengthList(
|
|
{ current, pageSize },
|
|
name
|
|
);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse()
|
|
@Post('/strength')
|
|
async createStrength(@Body() strengthData: CreateStrengthDTO) {
|
|
try {
|
|
const hasStrength = await this.productService.hasStrength(
|
|
strengthData.name
|
|
);
|
|
if (hasStrength) {
|
|
return errorResponse('分类已存在');
|
|
}
|
|
let data = await this.productService.createStrength(strengthData);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse()
|
|
@Put('/strength/:id')
|
|
async updateStrength(
|
|
@Param('id') id: number,
|
|
@Body() strengthData: UpdateStrengthDTO
|
|
) {
|
|
try {
|
|
const hasStrength = await this.productService.hasStrength(
|
|
strengthData.name
|
|
);
|
|
if (hasStrength) {
|
|
return errorResponse('分类已存在');
|
|
}
|
|
const data = this.productService.updateStrength(id, strengthData);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOkResponse({
|
|
type: BooleanRes,
|
|
})
|
|
@Del('/strength/:id')
|
|
async deleteStrength(@Param('id') id: number) {
|
|
try {
|
|
const hasProducts = await this.productService.hasProductsInStrength(id);
|
|
if (hasProducts) throw new Error('该分类下有商品,无法删除');
|
|
const data = await this.productService.deleteStrength(id);
|
|
return successResponse(data);
|
|
} catch (error) {
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
}
|