import { Controller, Param, Post, Inject, Get, Query, Put, Body, Files, Del, } from '@midwayjs/core'; import { WpProductService } from '../service/wp_product.service'; import { errorResponse, successResponse } from '../utils/response.util'; import { ApiOkResponse } from '@midwayjs/swagger'; import { BooleanRes, WpProductListRes } from '../dto/reponse.dto'; import { QueryWpProductDTO, UpdateVariationDTO, UpdateWpProductDTO, BatchSyncProductsDTO, BatchUpdateTagsDTO, BatchUpdateProductsDTO, } from '../dto/wp_product.dto'; import { ProductsRes, } from '../dto/reponse.dto'; @Controller('/wp_product') export class WpProductController { // 移除控制器内的配置站点引用,统一由服务层处理站点数据 @Inject() private readonly wpProductService: WpProductService; // 平台服务保留按需注入 @ApiOkResponse({ type: BooleanRes, }) @Del('/:id') async delete(@Param('id') id: number) { return errorResponse('接口已废弃,请改用 /site-api/:siteId/products 删除'); } @ApiOkResponse({ type: BooleanRes, }) @Post('/import/:siteId') async importProducts(@Param('siteId') siteId: number, @Files() files) { try { if (!files || files.length === 0) { throw new Error('请上传文件'); } await this.wpProductService.importProducts(siteId, files[0]); return successResponse(true); } catch (error) { console.error('导入失败:', error); return errorResponse(error.message || '导入失败'); } } @ApiOkResponse({ type: BooleanRes, }) @Post('/setconstitution') async setConstitution(@Body() body: any) { try { return successResponse(true); } catch (error) { return errorResponse(error.message || '设置失败'); } } @ApiOkResponse({ type: BooleanRes, }) @Post('/batch-update') async batchUpdateProducts(@Body() body: BatchUpdateProductsDTO) { try { await this.wpProductService.batchUpdateProducts(body); return successResponse(true); } catch (error) { return errorResponse(error.message || '批量更新失败'); } } @ApiOkResponse({ type: BooleanRes, }) @Post('/batch-update-tags') async batchUpdateTags(@Body() body: BatchUpdateTagsDTO) { try { await this.wpProductService.batchUpdateTags(body.ids, body.tags); return successResponse(true); } catch (error) { return errorResponse(error.message || '批量更新标签失败'); } } @ApiOkResponse({ type: BooleanRes, }) @Post('/sync/:siteId') async syncProducts(@Param('siteId') siteId: number) { try { const result = await this.wpProductService.syncSite(siteId); return successResponse(result); } catch (error) { console.log(error); return errorResponse('同步失败'); } } @ApiOkResponse({ type: BooleanRes, }) @Post('/batch-sync-to-site/:siteId') async batchSyncToSite( @Param('siteId') siteId: number, @Body() body: BatchSyncProductsDTO ) { try { await this.wpProductService.batchSyncToSite(siteId, body.productIds); return successResponse(true, '批量同步成功'); } catch (error) { console.error('批量同步失败:', error); return errorResponse(error.message || '批量同步失败'); } } @ApiOkResponse({ type: WpProductListRes, }) @Get('/list') async getWpProducts(@Query() query: QueryWpProductDTO) { return errorResponse('接口已废弃,请改用 /site-api/:siteId/products 列表'); } @ApiOkResponse({ type: BooleanRes }) @Post('/updateState/:id') async updateWPProductState( @Param('id') id: number, @Body() body: any, // todo ) { try { const res = await this.wpProductService.updateProductStatus(id, body?.status, body?.stock_status); return successResponse(res); } catch (error) { return errorResponse(error.message); } } /** * 创建产品接口 * @param siteId 站点 ID * @param body 创建数据 */ @ApiOkResponse({ type: BooleanRes, }) @Post('/siteId/:siteId/products') async createProduct( @Param('siteId') siteId: number, @Body() body: any ) { return errorResponse('接口已废弃,请改用 /site-api/:siteId/products 创建'); } /** * 更新产品接口 * @param productId 产品 ID * @param body 更新数据 */ @ApiOkResponse({ type: BooleanRes, }) @Put('/siteId/:siteId/products/:productId') async updateProduct( @Param('siteId') siteId: number, @Param('productId') productId: string, @Body() body: UpdateWpProductDTO ) { return errorResponse('接口已废弃,请改用 /site-api/:siteId/products/:id 更新'); } @ApiOkResponse({ type: BooleanRes, }) @Post('/sync-to-product/:id') async syncToProduct(@Param('id') id: number) { try { await this.wpProductService.syncToProduct(id); return successResponse(true); } catch (error) { return errorResponse(error.message); } } /** * 更新变体接口 * @param productId 产品 ID * @param variationId 变体 ID * @param body 更新数据 */ @Put('/siteId/:siteId/products/:productId/variations/:variationId') async updateVariation( @Param('siteId') siteId: number, @Param('productId') productId: string, @Param('variationId') variationId: string, @Body() body: UpdateVariationDTO ) { return errorResponse('接口已废弃,请改用 /site-api/:siteId/products/:productId/variations/:variationId 更新'); } @ApiOkResponse({ description: '通过name搜索产品/订单', type: ProductsRes, }) @Get('/search') async searchProducts(@Query('name') name: string) { try { // 调用服务获取产品数据 const products = await this.wpProductService.findProductsByName(name); return successResponse(products); } catch (error) { return errorResponse(error.message || '获取数据失败'); } } }