package test
|
|
import (
|
"bytes"
|
"fmt"
|
"github.com/gin-gonic/gin"
|
"github.com/h2non/filetype"
|
"go.uber.org/zap"
|
"net/http"
|
"srm/global"
|
"srm/model/common/request"
|
"srm/model/common/response"
|
"srm/model/test"
|
testReq "srm/model/test/request"
|
"srm/service"
|
"strconv"
|
"time"
|
)
|
|
type ContractApi struct {
|
}
|
|
var conService = service.ServiceGroupApp.TestServiceGroup.ContractService
|
|
// CreateContract 创建Contract
|
// @Tags Contract
|
// @Summary 创建Contract
|
// @Security ApiKeyAuth
|
// @accept multipart/form-data
|
// @Produce multipart/form-data
|
// @Param file formData file true "上传文件"
|
// @Param name formData string true "文件名称"
|
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
|
// @Router /con/createContract [post]
|
func (conApi *ContractApi) CreateContract(c *gin.Context) {
|
name := c.PostForm("name")
|
file, err := c.FormFile("file")
|
if err != nil {
|
c.JSON(400, gin.H{"error": err.Error()})
|
return
|
}
|
|
fileContent := make([]byte, file.Size)
|
f, _ := file.Open()
|
defer f.Close()
|
f.Read(fileContent)
|
|
// 重置文件指针
|
f.Seek(0, 0)
|
|
// 使用 filetype 库检测文件类型
|
kind, _ := filetype.Match(fileContent)
|
if kind == filetype.Unknown {
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Unknown file type"})
|
return
|
}
|
|
contract := test.Contract{
|
FileName: name,
|
FileContent: fileContent,
|
FileType: kind.MIME.Value,
|
}
|
|
if err, id := conService.CreateContract(&contract); err != nil {
|
global.GVA_LOG.Error("创建失败!", zap.Error(err))
|
response.FailWithMessage("创建失败", c)
|
} else {
|
//response.OkWithMessage("创建成功", c)
|
response.OkWithData(gin.H{"id": id, "name": name}, c)
|
}
|
}
|
|
// DeleteContract 删除Contract
|
// @Tags Contract
|
// @Summary 删除Contract
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data body test.Contract true "删除Contract"
|
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
|
// @Router /con/deleteContract [delete]
|
func (conApi *ContractApi) DeleteContract(c *gin.Context) {
|
var con test.Contract
|
err := c.ShouldBindJSON(&con)
|
if err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
if err := conService.DeleteContract(con); err != nil {
|
global.GVA_LOG.Error("删除失败!", zap.Error(err))
|
response.FailWithMessage("删除失败", c)
|
} else {
|
response.OkWithMessage("删除成功", c)
|
}
|
}
|
|
// DeleteContractByIds 批量删除Contract
|
// @Tags Contract
|
// @Summary 批量删除Contract
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data body request.IdsReq true "批量删除Contract"
|
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
|
// @Router /con/deleteContractByIds [delete]
|
func (conApi *ContractApi) DeleteContractByIds(c *gin.Context) {
|
var IDS request.IdsReq
|
err := c.ShouldBindJSON(&IDS)
|
if err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
if err := conService.DeleteContractByIds(IDS); err != nil {
|
global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
|
response.FailWithMessage("批量删除失败", c)
|
} else {
|
response.OkWithMessage("批量删除成功", c)
|
}
|
}
|
|
// UpdateContract 更新Contract
|
// @Tags Contract
|
// @Summary 更新Contract
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data body test.Contract true "更新Contract"
|
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
|
// @Router /con/updateContract [put]
|
func (conApi *ContractApi) UpdateContract(c *gin.Context) {
|
var con test.Contract
|
err := c.ShouldBindJSON(&con)
|
if err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
if err := conService.UpdateContract(con); err != nil {
|
global.GVA_LOG.Error("更新失败!", zap.Error(err))
|
response.FailWithMessage("更新失败", c)
|
} else {
|
response.OkWithMessage("更新成功", c)
|
}
|
}
|
|
// FindContract 用id查询Contract
|
// @Tags Contract
|
// @Summary 用id查询Contract
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data query test.Contract true "用id查询Contract"
|
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
|
// @Router /con/findContract [get]
|
func (conApi *ContractApi) FindContract(c *gin.Context) {
|
var con test.Contract
|
err := c.ShouldBindQuery(&con)
|
if err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
if recon, err := conService.GetContract(con.ID); err != nil {
|
global.GVA_LOG.Error("查询失败!", zap.Error(err))
|
response.FailWithMessage("查询失败", c)
|
} else {
|
response.OkWithData(gin.H{"recon": recon}, c)
|
}
|
}
|
|
// GetContractList 分页获取Contract列表
|
// @Tags Contract
|
// @Summary 分页获取Contract列表
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data query testReq.ContractSearch true "分页获取Contract列表"
|
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
|
// @Router /con/getContractList [get]
|
func (conApi *ContractApi) GetContractList(c *gin.Context) {
|
var pageInfo testReq.ContractSearch
|
err := c.ShouldBindQuery(&pageInfo)
|
if err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
if list, total, err := conService.GetContractInfoList(pageInfo); err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
} else {
|
response.OkWithDetailed(response.PageResult{
|
List: list,
|
Total: total,
|
Page: pageInfo.Page,
|
PageSize: pageInfo.PageSize,
|
}, "获取成功", c)
|
}
|
}
|
|
// PreviewContract 预览Contract
|
// @Tags Contract
|
// @Summary 预览Contract
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data query test.Contract true "用id查询Contract"
|
// @Success 200 {string} string "{"success":true,"data":{},"msg":"预览成功"}"
|
// @Router /previewContract [get]
|
func (conApi *ContractApi) PreviewContract(c *gin.Context) {
|
var con test.Contract
|
err := c.ShouldBindQuery(&con)
|
if err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
|
id := c.Query("id")
|
if id == "" {
|
response.FailWithMessage("id不能为空", c)
|
return
|
}
|
|
val64, err := strconv.ParseUint(id, 10, 64)
|
if err != nil {
|
response.FailWithMessage("id格式错误", c)
|
return
|
}
|
|
// Convert uint64 to uint
|
conId := uint(val64)
|
|
contract, err := conService.GetContract(conId)
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
} else {
|
//c.Writer.Header().Set("Content-Type", "application/octect-stream")
|
//c.Writer.Header().Set("Content-Disposition", "attachment;filename="+contract.FileName)
|
//c.Writer.Write(contract.FileContent)
|
//c.Data(200, "application/octect-stream", contract.FileContent)
|
//reader := bytes.NewReader(contract.FileContent)
|
//c.DataFromReader(http.StatusOK, int64(len(contract.FileContent)), "application/pdf", reader, map[string]string{"Content-Disposition": fmt.Sprintf("attachment; filename=%s", contract.FileName)})
|
reader := bytes.NewReader(contract.FileContent)
|
c.Header("Content-Type", contract.FileType)
|
c.Writer.Header().Set("Content-Disposition", "inline; filename="+contract.FileName)
|
http.ServeContent(c.Writer, c.Request, contract.FileName, time.Now(), reader)
|
}
|
}
|
|
// DownloadContract 下载Contract
|
// @Tags Contract
|
// @Summary 下载Contract
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data query test.Contract true "用id查询Contract"
|
// @Success 200 {string} string "{"success":true,"data":{},"msg":"下载成功"}"
|
// @Router /downloadContract [get]
|
func (conApi *ContractApi) DownloadContract(c *gin.Context) {
|
var con test.Contract
|
err := c.ShouldBindQuery(&con)
|
if err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
|
id := c.Query("id")
|
if id == "" {
|
response.FailWithMessage("id不能为空", c)
|
return
|
}
|
|
val64, err := strconv.ParseUint(id, 10, 64)
|
if err != nil {
|
response.FailWithMessage("id格式错误", c)
|
return
|
}
|
|
// Convert uint64 to uint
|
conId := uint(val64)
|
|
contract, err := conService.GetContract(conId)
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
} else {
|
reader := bytes.NewReader(contract.FileContent)
|
// 设置必要的头信息
|
c.Header("Content-Description", "File Transfer")
|
c.Header("Content-Transfer-Encoding", "binary")
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", contract.FileName))
|
c.Header("Content-Type", contract.FileType)
|
|
// 将文件作为响应体发送
|
http.ServeContent(c.Writer, c.Request, contract.FileName, time.Now(), reader)
|
c.File(contract.FileName)
|
}
|
}
|