后端重构

This commit is contained in:
Yvan
2026-01-08 19:28:58 +08:00
parent 8800c069d9
commit 9cb6b965e0
49 changed files with 4907 additions and 14 deletions
+28
View File
@@ -0,0 +1,28 @@
package example
import "kra/internal/biz/example"
// ApiGroup Example API组
type ApiGroup struct {
FileUploadApi
CustomerApi
AttachmentCategoryApi
}
// 业务层依赖
var (
fileUploadUsecase *example.FileUploadUsecase
customerUsecase *example.CustomerUsecase
attachmentCategoryUsecase *example.AttachmentCategoryUsecase
)
// InitUsecases 初始化业务层依赖
func InitUsecases(
fileUpload *example.FileUploadUsecase,
customer *example.CustomerUsecase,
attachmentCategory *example.AttachmentCategoryUsecase,
) {
fileUploadUsecase = fileUpload
customerUsecase = customer
attachmentCategoryUsecase = attachmentCategory
}
@@ -0,0 +1,116 @@
package example
import (
"kra/internal/biz/example"
"kra/pkg/response"
"github.com/gin-gonic/gin"
)
type AttachmentCategoryApi struct{}
// AddCategory 创建/更新分类
// @Summary 创建/更新分类
// @Tags ExaAttachmentCategory
// @Accept json
// @Produce json
// @Param data body CategoryRequest true "分类信息"
// @Success 200 {object} response.Response
// @Router /attachmentCategory/addCategory [post]
func (api *AttachmentCategoryApi) AddCategory(c *gin.Context) {
var req CategoryRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.FailWithMessage("参数错误", c)
return
}
category := &example.AttachmentCategory{
ID: req.ID,
Name: req.Name,
Pid: req.Pid,
}
if err := attachmentCategoryUsecase.AddCategory(c.Request.Context(), category); err != nil {
response.FailWithMessage("创建/更新失败:"+err.Error(), c)
return
}
response.OkWithMessage("创建/更新成功", c)
}
// DeleteCategory 删除分类
// @Summary 删除分类
// @Tags ExaAttachmentCategory
// @Accept json
// @Produce json
// @Param data body DeleteCategoryRequest true "分类ID"
// @Success 200 {object} response.Response
// @Router /attachmentCategory/deleteCategory [post]
func (api *AttachmentCategoryApi) DeleteCategory(c *gin.Context) {
var req DeleteCategoryRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.FailWithMessage("参数错误", c)
return
}
if req.ID == 0 {
response.FailWithMessage("参数错误", c)
return
}
if err := attachmentCategoryUsecase.DeleteCategory(c.Request.Context(), req.ID); err != nil {
response.FailWithMessage("删除失败", c)
return
}
response.OkWithMessage("删除成功", c)
}
// GetCategoryList 获取分类列表
// @Summary 获取分类列表
// @Tags ExaAttachmentCategory
// @Accept json
// @Produce json
// @Success 200 {object} response.Response{data=[]CategoryResponse}
// @Router /attachmentCategory/getCategoryList [get]
func (api *AttachmentCategoryApi) GetCategoryList(c *gin.Context) {
list, err := attachmentCategoryUsecase.GetCategoryList(c.Request.Context())
if err != nil {
response.FailWithMessage("获取分类列表失败", c)
return
}
respList := toCategoryResponseList(list)
response.OkWithData(respList, c)
}
// Request/Response 结构
type CategoryRequest struct {
ID uint `json:"ID"`
Name string `json:"name"`
Pid uint `json:"pid"`
}
type DeleteCategoryRequest struct {
ID uint `json:"id"`
}
type CategoryResponse struct {
ID uint `json:"ID"`
Name string `json:"name"`
Pid uint `json:"pid"`
Children []CategoryResponse `json:"children"`
}
func toCategoryResponseList(categories []*example.AttachmentCategory) []CategoryResponse {
result := make([]CategoryResponse, len(categories))
for i, cat := range categories {
result[i] = CategoryResponse{
ID: cat.ID,
Name: cat.Name,
Pid: cat.Pid,
Children: toCategoryResponseList(cat.Children),
}
}
return result
}
@@ -0,0 +1,192 @@
package example
import (
"kra/internal/biz/example"
"kra/pkg/response"
"kra/pkg/utils"
"github.com/gin-gonic/gin"
)
type CustomerApi struct{}
// CreateExaCustomer 创建客户
// @Summary 创建客户
// @Tags ExaCustomer
// @Accept json
// @Produce json
// @Param data body CustomerRequest true "客户信息"
// @Success 200 {object} response.Response
// @Router /customer/customer [post]
func (api *CustomerApi) CreateExaCustomer(c *gin.Context) {
var req CustomerRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
customer := &example.Customer{
CustomerName: req.CustomerName,
CustomerPhoneData: req.CustomerPhoneData,
SysUserID: utils.GetUserID(c),
SysUserAuthorityID: utils.GetUserAuthorityId(c),
}
if err := customerUsecase.CreateExaCustomer(c.Request.Context(), customer); err != nil {
response.FailWithMessage("创建失败: "+err.Error(), c)
return
}
response.OkWithMessage("创建成功", c)
}
// DeleteExaCustomer 删除客户
// @Summary 删除客户
// @Tags ExaCustomer
// @Accept json
// @Produce json
// @Param data body DeleteCustomerRequest true "客户ID"
// @Success 200 {object} response.Response
// @Router /customer/customer [delete]
func (api *CustomerApi) DeleteExaCustomer(c *gin.Context) {
var req DeleteCustomerRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
if err := customerUsecase.DeleteExaCustomer(c.Request.Context(), req.ID); err != nil {
response.FailWithMessage("删除失败: "+err.Error(), c)
return
}
response.OkWithMessage("删除成功", c)
}
// UpdateExaCustomer 更新客户
// @Summary 更新客户
// @Tags ExaCustomer
// @Accept json
// @Produce json
// @Param data body CustomerRequest true "客户信息"
// @Success 200 {object} response.Response
// @Router /customer/customer [put]
func (api *CustomerApi) UpdateExaCustomer(c *gin.Context) {
var req CustomerRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
customer := &example.Customer{
ID: req.ID,
CustomerName: req.CustomerName,
CustomerPhoneData: req.CustomerPhoneData,
}
if err := customerUsecase.UpdateExaCustomer(c.Request.Context(), customer); err != nil {
response.FailWithMessage("更新失败: "+err.Error(), c)
return
}
response.OkWithMessage("更新成功", c)
}
// GetExaCustomer 获取客户信息
// @Summary 获取客户信息
// @Tags ExaCustomer
// @Accept json
// @Produce json
// @Param data body GetCustomerRequest true "客户ID"
// @Success 200 {object} response.Response{data=CustomerResponse}
// @Router /customer/customer [get]
func (api *CustomerApi) GetExaCustomer(c *gin.Context) {
var req GetCustomerRequest
if err := c.ShouldBindQuery(&req); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
customer, err := customerUsecase.GetExaCustomer(c.Request.Context(), req.ID)
if err != nil {
response.FailWithMessage("获取失败: "+err.Error(), c)
return
}
response.OkWithDetailed(toCustomerResponse(customer), "获取成功", c)
}
// GetExaCustomerList 分页获取客户列表
// @Summary 分页获取客户列表
// @Tags ExaCustomer
// @Accept json
// @Produce json
// @Param data body GetCustomerListRequest true "分页参数"
// @Success 200 {object} response.Response{data=response.PageResult}
// @Router /customer/customerList [get]
func (api *CustomerApi) GetExaCustomerList(c *gin.Context) {
var req GetCustomerListRequest
if err := c.ShouldBindQuery(&req); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
authorityId := utils.GetUserAuthorityId(c)
list, total, err := customerUsecase.GetCustomerInfoList(c.Request.Context(), authorityId, req.Page, req.PageSize)
if err != nil {
response.FailWithMessage("获取失败: "+err.Error(), c)
return
}
respList := make([]CustomerResponse, len(list))
for i, cust := range list {
respList[i] = *toCustomerResponse(cust)
}
response.OkWithDetailed(response.PageResult{
List: respList,
Total: total,
Page: req.Page,
PageSize: req.PageSize,
}, "获取成功", c)
}
// Request/Response 结构
type CustomerRequest struct {
ID uint `json:"ID"`
CustomerName string `json:"customerName"`
CustomerPhoneData string `json:"customerPhoneData"`
}
type DeleteCustomerRequest struct {
ID uint `json:"ID"`
}
type GetCustomerRequest struct {
ID uint `form:"ID"`
}
type GetCustomerListRequest struct {
Page int `form:"page"`
PageSize int `form:"pageSize"`
}
type CustomerResponse struct {
ID uint `json:"ID"`
CustomerName string `json:"customerName"`
CustomerPhoneData string `json:"customerPhoneData"`
SysUserID uint `json:"sysUserId"`
SysUserAuthorityID uint `json:"sysUserAuthorityID"`
SysUserName string `json:"sysUserName"`
}
func toCustomerResponse(c *example.Customer) *CustomerResponse {
return &CustomerResponse{
ID: c.ID,
CustomerName: c.CustomerName,
CustomerPhoneData: c.CustomerPhoneData,
SysUserID: c.SysUserID,
SysUserAuthorityID: c.SysUserAuthorityID,
SysUserName: c.SysUserName,
}
}
@@ -0,0 +1,198 @@
package example
import (
"strconv"
"kra/internal/biz/example"
"kra/pkg/response"
"github.com/gin-gonic/gin"
)
type FileUploadApi struct{}
// UploadFile 上传文件
// @Summary 上传文件
// @Tags ExaFileUploadAndDownload
// @Accept multipart/form-data
// @Produce json
// @Param file formance file true "上传文件"
// @Success 200 {object} response.Response{data=FileUploadResponse}
// @Router /fileUploadAndDownload/upload [post]
func (api *FileUploadApi) UploadFile(c *gin.Context) {
noSave := c.DefaultQuery("noSave", "0")
classIdStr := c.DefaultQuery("classId", "0")
classId, _ := strconv.Atoi(classIdStr)
_, header, err := c.Request.FormFile("file")
if err != nil {
response.FailWithMessage("接收文件失败", c)
return
}
file, err := fileUploadUsecase.UploadFile(c.Request.Context(), header, noSave, classId)
if err != nil {
response.FailWithMessage("上传失败: "+err.Error(), c)
return
}
response.OkWithDetailed(toFileUploadResponse(file), "上传成功", c)
}
// DeleteFile 删除文件
// @Summary 删除文件
// @Tags ExaFileUploadAndDownload
// @Accept json
// @Produce json
// @Param data body DeleteFileRequest true "文件ID"
// @Success 200 {object} response.Response
// @Router /fileUploadAndDownload/deleteFile [post]
func (api *FileUploadApi) DeleteFile(c *gin.Context) {
var req DeleteFileRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
if err := fileUploadUsecase.DeleteFile(c.Request.Context(), req.ID); err != nil {
response.FailWithMessage("删除失败: "+err.Error(), c)
return
}
response.OkWithMessage("删除成功", c)
}
// EditFileName 编辑文件名
// @Summary 编辑文件名
// @Tags ExaFileUploadAndDownload
// @Accept json
// @Produce json
// @Param data body EditFileNameRequest true "文件信息"
// @Success 200 {object} response.Response
// @Router /fileUploadAndDownload/editFileName [post]
func (api *FileUploadApi) EditFileName(c *gin.Context) {
var req EditFileNameRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
if err := fileUploadUsecase.EditFileName(c.Request.Context(), req.ID, req.Name); err != nil {
response.FailWithMessage("编辑失败: "+err.Error(), c)
return
}
response.OkWithMessage("编辑成功", c)
}
// GetFileList 分页获取文件列表
// @Summary 分页获取文件列表
// @Tags ExaFileUploadAndDownload
// @Accept json
// @Produce json
// @Param data body GetFileListRequest true "分页参数"
// @Success 200 {object} response.Response{data=response.PageResult}
// @Router /fileUploadAndDownload/getFileList [post]
func (api *FileUploadApi) GetFileList(c *gin.Context) {
var req GetFileListRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
searchReq := &example.FileUploadSearchReq{
Page: req.Page,
PageSize: req.PageSize,
Keyword: req.Keyword,
ClassId: req.ClassId,
}
list, total, err := fileUploadUsecase.GetFileRecordInfoList(c.Request.Context(), searchReq)
if err != nil {
response.FailWithMessage("获取失败: "+err.Error(), c)
return
}
respList := make([]FileUploadResponse, len(list))
for i, f := range list {
respList[i] = *toFileUploadResponse(f)
}
response.OkWithDetailed(response.PageResult{
List: respList,
Total: total,
Page: req.Page,
PageSize: req.PageSize,
}, "获取成功", c)
}
// ImportURL 导入URL
// @Summary 导入URL
// @Tags ExaFileUploadAndDownload
// @Accept json
// @Produce json
// @Param data body []FileUploadResponse true "文件列表"
// @Success 200 {object} response.Response
// @Router /fileUploadAndDownload/importURL [post]
func (api *FileUploadApi) ImportURL(c *gin.Context) {
var req []FileUploadResponse
if err := c.ShouldBindJSON(&req); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
files := make([]*example.FileUpload, len(req))
for i, r := range req {
files[i] = &example.FileUpload{
Name: r.Name,
ClassId: r.ClassId,
Url: r.Url,
Tag: r.Tag,
Key: r.Key,
}
}
if err := fileUploadUsecase.ImportURL(c.Request.Context(), files); err != nil {
response.FailWithMessage("导入失败: "+err.Error(), c)
return
}
response.OkWithMessage("导入成功", c)
}
// Request/Response 结构
type DeleteFileRequest struct {
ID uint `json:"id"`
}
type EditFileNameRequest struct {
ID uint `json:"id"`
Name string `json:"name"`
}
type GetFileListRequest struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
Keyword string `json:"keyword"`
ClassId int `json:"classId"`
}
type FileUploadResponse struct {
ID uint `json:"ID"`
Name string `json:"name"`
ClassId int `json:"classId"`
Url string `json:"url"`
Tag string `json:"tag"`
Key string `json:"key"`
}
func toFileUploadResponse(f *example.FileUpload) *FileUploadResponse {
return &FileUploadResponse{
ID: f.ID,
Name: f.Name,
ClassId: f.ClassId,
Url: f.Url,
Tag: f.Tag,
Key: f.Key,
}
}