240 lines
7.0 KiB
TypeScript
240 lines
7.0 KiB
TypeScript
|
|
import { Inject, Controller, Get, Post, Put, Del, Query, Body, Param, Files, Fields, ContentType } from '@midwayjs/core';
|
|
import { DictService } from '../service/dict.service';
|
|
import { CreateDictDTO, UpdateDictDTO, CreateDictItemDTO, UpdateDictItemDTO } from '../dto/dict.dto';
|
|
import { Validate } from '@midwayjs/validate';
|
|
import { Context } from '@midwayjs/koa';
|
|
import { successResponse, errorResponse, ApiResponse } from '../utils/response.util';
|
|
import { ApiOkResponse } from '@midwayjs/swagger';
|
|
import { BatchOperationResult } from '../dto/api.dto';
|
|
|
|
/**
|
|
* 字典管理
|
|
* @decorator Controller
|
|
*/
|
|
@Controller('/dict')
|
|
export class DictController {
|
|
@Inject()
|
|
dictService: DictService;
|
|
|
|
@Inject()
|
|
ctx: Context;
|
|
|
|
/**
|
|
* 批量导入字典
|
|
* @param files 上传的文件
|
|
*/
|
|
@Post('/import')
|
|
@Validate()
|
|
async importDicts(@Files() files: any) {
|
|
// 从上传的文件列表中获取第一个文件
|
|
const file = files[0];
|
|
// 调用服务层方法处理XLSX文件
|
|
const result = await this.dictService.importDictsFromXLSX(file.data);
|
|
// 返回导入结果
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 下载字典XLSX模板
|
|
*/
|
|
@Get('/template')
|
|
@ContentType('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
|
async downloadDictTemplate() {
|
|
// 设置下载文件的名称
|
|
this.ctx.set('Content-Disposition', 'attachment; filename=dict-template.xlsx');
|
|
// 返回XLSX模板内容
|
|
return this.dictService.getDictXLSXTemplate();
|
|
}
|
|
|
|
/**
|
|
* 获取单个字典及其所有字典项
|
|
* @param id 字典ID
|
|
*/
|
|
@Get('/:id')
|
|
async getDict(@Param('id') id: number) {
|
|
// 调用服务层方法,并关联查询字典项
|
|
return this.dictService.getDict({ id }, ['items']);
|
|
}
|
|
|
|
/**
|
|
* 获取字典列表
|
|
* @param title 字典标题 (模糊查询)
|
|
* @param name 字典名称 (模糊查询)
|
|
*/
|
|
@Get('/list')
|
|
async getDicts(@Query('title') title?: string, @Query('name') name?: string) {
|
|
// 调用服务层方法
|
|
return this.dictService.getDicts({ title, name });
|
|
}
|
|
|
|
/**
|
|
* 创建新字典
|
|
* @param createDictDTO 字典数据
|
|
*/
|
|
@Post('/')
|
|
@Validate()
|
|
async createDict(@Body() createDictDTO: CreateDictDTO) {
|
|
try {
|
|
// 调用服务层方法
|
|
const result = await this.dictService.createDict(createDictDTO);
|
|
return successResponse(result, '字典创建成功');
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '字典创建失败', error?.code || 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新字典
|
|
* @param id 字典ID
|
|
* @param updateDictDTO 待更新的字典数据
|
|
*/
|
|
@Put('/:id')
|
|
@Validate()
|
|
async updateDict(@Param('id') id: number, @Body() updateDictDTO: UpdateDictDTO) {
|
|
try {
|
|
// 调用服务层方法
|
|
const result = await this.dictService.updateDict(id, updateDictDTO);
|
|
return successResponse(result, '字典更新成功');
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '字典更新失败', error?.code || 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除字典
|
|
* @param id 字典ID
|
|
*/
|
|
@Del('/:id')
|
|
async deleteDict(@Param('id') id: number) {
|
|
try {
|
|
// 调用服务层方法
|
|
const result = await this.dictService.deleteDict(id);
|
|
return successResponse(result, '字典删除成功');
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '字典删除失败', error?.code || 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 批量导入字典项
|
|
* @param files 上传的文件
|
|
* @param fields FormData中的字段
|
|
*/
|
|
@ApiOkResponse({type:ApiResponse<BatchOperationResult>})
|
|
@Post('/item/import')
|
|
@Validate()
|
|
async importDictItems(@Files() files: any, @Fields() fields: { dictId: number }) {
|
|
// 获取第一个文件
|
|
const file = files[0];
|
|
// 从fields中获取字典ID
|
|
const dictId = fields.dictId;
|
|
// 调用服务层方法
|
|
const result = await this.dictService.importDictItemsFromXLSX(file.data, dictId);
|
|
// 返回结果
|
|
return successResponse(result)
|
|
}
|
|
|
|
/**
|
|
* 下载字典项XLSX模板
|
|
*/
|
|
@Get('/item/template')
|
|
@ContentType('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
|
async downloadDictItemTemplate() {
|
|
// 设置下载文件名
|
|
this.ctx.set('Content-Disposition', 'attachment; filename=dict-item-template.xlsx');
|
|
// 返回模板内容
|
|
return this.dictService.getDictItemXLSXTemplate();
|
|
}
|
|
|
|
/**
|
|
* 导出字典项为 XLSX 文件
|
|
* @param dictId 字典ID
|
|
*/
|
|
@Get('/item/export')
|
|
@ContentType('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
|
async exportDictItems(@Query('dictId') dictId: number) {
|
|
// 设置下载文件名
|
|
this.ctx.set('Content-Disposition', 'attachment; filename=dict-items.xlsx');
|
|
// 返回导出的 XLSX 文件内容
|
|
return this.dictService.exportDictItemsToXLSX(dictId);
|
|
}
|
|
|
|
/**
|
|
* 获取字典项列表
|
|
* @param dictId 字典ID (可选)
|
|
*/
|
|
@Get('/items')
|
|
async getDictItems(
|
|
@Query('dictId') dictId?: number,
|
|
@Query('name') name?: string,
|
|
@Query('title') title?: string,
|
|
) {
|
|
try {
|
|
// 调用服务层方法
|
|
const result = await this.dictService.getDictItems({ dictId, name, title });
|
|
return successResponse(result, '获取字典项列表成功');
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '获取字典项列表失败', error?.code || 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建新字典项
|
|
* @param createDictItemDTO 字典项数据
|
|
*/
|
|
@Post('/item')
|
|
@Validate()
|
|
async createDictItem(@Body() createDictItemDTO: CreateDictItemDTO) {
|
|
try {
|
|
// 调用服务层方法
|
|
const result = await this.dictService.createDictItem(createDictItemDTO);
|
|
return successResponse(result, '字典项创建成功');
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '字典项创建失败', error?.code || 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新字典项
|
|
* @param id 字典项ID
|
|
* @param updateDictItemDTO 待更新的字典项数据
|
|
*/
|
|
@Put('/item/:id')
|
|
@Validate()
|
|
async updateDictItem(@Param('id') id: number, @Body() updateDictItemDTO: UpdateDictItemDTO) {
|
|
try {
|
|
// 调用服务层方法
|
|
const result = await this.dictService.updateDictItem(id, updateDictItemDTO);
|
|
return successResponse(result, '字典项更新成功');
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '字典项更新失败', error?.code || 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除字典项
|
|
* @param id 字典项ID
|
|
*/
|
|
@Del('/item/:id')
|
|
async deleteDictItem(@Param('id') id: number) {
|
|
try {
|
|
// 调用服务层方法
|
|
const result = await this.dictService.deleteDictItem(id);
|
|
return successResponse(result, '字典项删除成功');
|
|
} catch (error) {
|
|
return errorResponse(error?.message || '字典项删除失败', error?.code || 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 根据字典名称获取字典项列表
|
|
* @param name 字典名称
|
|
*/
|
|
@Get('/items-by-name')
|
|
async getDictItemsByDictName(@Query('name') name: string) {
|
|
// 调用服务层方法
|
|
return this.dictService.getDictItemsByDictName(name);
|
|
}
|
|
}
|