|
@@ -1,9 +1,14 @@
|
|
|
package admin
|
|
|
|
|
|
import (
|
|
|
+ "archive/zip"
|
|
|
+ "bytes"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
+ "io"
|
|
|
+ "os"
|
|
|
+ "path/filepath"
|
|
|
"server/dao"
|
|
|
"server/global"
|
|
|
"server/model/common/request"
|
|
@@ -17,11 +22,17 @@ func (pa *ProjectApi) QueryProjectList(c *gin.Context) {
|
|
|
var search request.SearchProject
|
|
|
err := c.ShouldBindJSON(&search)
|
|
|
if err != nil {
|
|
|
- response.FailWithMessage(err.Error(), c)
|
|
|
+ response.FailWithMessage("失败", c)
|
|
|
+ global.GVA_LOG.Error("QueryProjectList ====== " + err.Error())
|
|
|
return
|
|
|
}
|
|
|
|
|
|
list, total, err := projectService.QueryProjectList(search)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("失败", c)
|
|
|
+ global.GVA_LOG.Error("QueryProjectList ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
response.OkWithDetailed(response.PageResult{
|
|
|
List: list,
|
|
@@ -39,12 +50,36 @@ func (pa *ProjectApi) QueryProjectByCode(c *gin.Context) {
|
|
|
}
|
|
|
project, err := projectService.QueryProjectByCode(code)
|
|
|
if err != nil {
|
|
|
- response.FailWithMessage(err.Error(), c)
|
|
|
+ response.FailWithMessage("失败", c)
|
|
|
+ global.GVA_LOG.Error("QueryProjectByCode ====== " + err.Error())
|
|
|
return
|
|
|
}
|
|
|
response.OkWithData(project, c)
|
|
|
}
|
|
|
|
|
|
+func (pa *ProjectApi) QueryFileList(c *gin.Context) {
|
|
|
+ var info request.SearchProjectFileList
|
|
|
+ err := c.ShouldBindJSON(&info)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("错误", c)
|
|
|
+ global.GVA_LOG.Error("QueryFileList ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ list, total, err := projectService.QueryFileList(info)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("失败", c)
|
|
|
+ global.GVA_LOG.Error("QueryFileList ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ response.OkWithDetailed(response.PageResult{
|
|
|
+ List: list,
|
|
|
+ Total: total,
|
|
|
+ Page: info.PageInfo.Page,
|
|
|
+ PageSize: info.PageInfo.PageSize,
|
|
|
+ }, "获取成功", c)
|
|
|
+}
|
|
|
+
|
|
|
func (pa *ProjectApi) QueryAllProject(c *gin.Context) {
|
|
|
projects, err := projectService.QueryAllProject()
|
|
|
if err != nil {
|
|
@@ -89,6 +124,39 @@ func (pa *ProjectApi) QueryCollections(c *gin.Context) {
|
|
|
response.OkWithData(collections, c)
|
|
|
}
|
|
|
|
|
|
+func (pa *ProjectApi) QueryReimbursementList(c *gin.Context) {
|
|
|
+ var info request.SearchProjectList
|
|
|
+ err := c.ShouldBindJSON(&info)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("失败", c)
|
|
|
+ global.GVA_LOG.Error("QueryReimbursementList ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ list, total, err := projectService.QueryReimbursementList(info)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("失败", c)
|
|
|
+ global.GVA_LOG.Error("QueryReimbursementList ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ response.OkWithDetailed(response.PageResult{
|
|
|
+ List: list,
|
|
|
+ Total: total,
|
|
|
+ Page: info.PageInfo.Page,
|
|
|
+ PageSize: info.PageInfo.PageSize,
|
|
|
+ }, "获取成功", c)
|
|
|
+}
|
|
|
+
|
|
|
+func (pa *ProjectApi) QueryReimbursement(c *gin.Context) {
|
|
|
+ code := c.Query("code")
|
|
|
+ reimbursement, err := projectService.QueryReimbursement(code)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("失败", c)
|
|
|
+ global.GVA_LOG.Error("QueryReimbursement ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ response.OkWithData(reimbursement, c)
|
|
|
+}
|
|
|
+
|
|
|
func (pa *ProjectApi) CreateProject(c *gin.Context) {
|
|
|
form, err := c.MultipartForm()
|
|
|
files := form.File["file"]
|
|
@@ -107,11 +175,10 @@ func (pa *ProjectApi) CreateProject(c *gin.Context) {
|
|
|
fmt.Println(files)
|
|
|
for _, file := range files {
|
|
|
dst := file.Filename
|
|
|
- fmt.Println(file)
|
|
|
- fmt.Println(file.Filename)
|
|
|
projectFile := dao.ProjectFile{
|
|
|
- Name: dst,
|
|
|
- Path: "./uploads/file/" + dst,
|
|
|
+ ProjectCode: project.Code,
|
|
|
+ Name: dst,
|
|
|
+ Path: "./uploads/file/" + project.Code + "/立项" + "/" + dst,
|
|
|
}
|
|
|
|
|
|
project.Files = append(project.Files, projectFile)
|
|
@@ -132,18 +199,48 @@ func (pa *ProjectApi) CreateProject(c *gin.Context) {
|
|
|
}
|
|
|
|
|
|
func (pa *ProjectApi) CreateProjectFile(c *gin.Context) {
|
|
|
- var projectFile dao.ProjectFile
|
|
|
- err := c.ShouldBindJSON(&projectFile)
|
|
|
+ form, err := c.MultipartForm()
|
|
|
if err != nil {
|
|
|
- response.FailWithMessage(err.Error(), c)
|
|
|
+ response.FailWithMessage("失败", c)
|
|
|
return
|
|
|
}
|
|
|
- err = projectService.CreateProjectFile(projectFile)
|
|
|
+ files := form.File["file"]
|
|
|
+
|
|
|
+ genre := c.PostForm("genre")
|
|
|
+ code := c.PostForm("code")
|
|
|
+ genreId, err := strconv.Atoi(genre)
|
|
|
if err != nil {
|
|
|
- response.FailWithMessage(err.Error(), c)
|
|
|
+ response.FailWithMessage("错误", c)
|
|
|
+ global.GVA_LOG.Error("CreateProjectFile ====== " + err.Error())
|
|
|
return
|
|
|
}
|
|
|
- response.OkWithMessage("创建成功", c)
|
|
|
+ fileGenre, err := fileService.QueryFileGenreById(genreId)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("错误", c)
|
|
|
+ global.GVA_LOG.Error("CreateProjectFile ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, file := range files {
|
|
|
+ dst := file.Filename
|
|
|
+ projectFile := dao.ProjectFile{
|
|
|
+ ProjectCode: code,
|
|
|
+ Name: dst,
|
|
|
+ Path: "./uploads/file/" + code + "/" + fileGenre.Name + "/" + dst,
|
|
|
+ }
|
|
|
+
|
|
|
+ // gin 简单做了封装,拷贝了文件流
|
|
|
+ if err := c.SaveUploadedFile(file, "./uploads/file/"+dst); err != nil {
|
|
|
+ response.FailWithMessage("上传失败", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = projectService.CreateProjectFile(projectFile)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("上传失败", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ response.OkWithMessage("上传成功", c)
|
|
|
}
|
|
|
|
|
|
func (pa *ProjectApi) CreateWorkingHours(c *gin.Context) {
|
|
@@ -178,6 +275,23 @@ func (pa *ProjectApi) CreateCollection(c *gin.Context) {
|
|
|
response.OkWithMessage("创建成功", c)
|
|
|
}
|
|
|
|
|
|
+func (pa *ProjectApi) CreateReimbursement(c *gin.Context) {
|
|
|
+ var reimbursement dao.Reimbursement
|
|
|
+ err := c.ShouldBindJSON(&reimbursement)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("失败", c)
|
|
|
+ global.GVA_LOG.Error("CreateReimbursement ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = projectService.CreateReimbursement(reimbursement)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("失败", c)
|
|
|
+ global.GVA_LOG.Error("CreateReimbursement ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ response.OkWithMessage("新建成功", c)
|
|
|
+}
|
|
|
+
|
|
|
func (pa *ProjectApi) UpdateProject(c *gin.Context) {
|
|
|
var project dao.Project
|
|
|
err := c.ShouldBindJSON(&project)
|
|
@@ -224,6 +338,23 @@ func (pa *ProjectApi) UpdateCollection(c *gin.Context) {
|
|
|
response.OkWithMessage("修改成功", c)
|
|
|
}
|
|
|
|
|
|
+func (pa *ProjectApi) UpdateReimbursement(c *gin.Context) {
|
|
|
+ var reimbursement dao.Reimbursement
|
|
|
+ err := c.ShouldBindJSON(&reimbursement)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("失败", c)
|
|
|
+ global.GVA_LOG.Error("UpdateReimbursement ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = projectService.UpdateReimbursement(reimbursement)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("失败", c)
|
|
|
+ global.GVA_LOG.Error("UpdateReimbursement ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ response.OkWithMessage("修改成功", c)
|
|
|
+}
|
|
|
+
|
|
|
func (pa *ProjectApi) DeleteProject(c *gin.Context) {
|
|
|
code := c.Query("code")
|
|
|
if code == "" {
|
|
@@ -284,3 +415,87 @@ func (pa *ProjectApi) DeleteCollection(c *gin.Context) {
|
|
|
}
|
|
|
response.OkWithMessage("删除成功", c)
|
|
|
}
|
|
|
+
|
|
|
+func (pa *ProjectApi) DeleteReimbursement(c *gin.Context) {
|
|
|
+ var reimbursement dao.Reimbursement
|
|
|
+ err := c.ShouldBindJSON(&reimbursement)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("失败", c)
|
|
|
+ global.GVA_LOG.Error("DeleteReimbursement ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = projectService.DeleteReimbursement(reimbursement)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("失败", c)
|
|
|
+ global.GVA_LOG.Error("DeleteReimbursement ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ response.OkWithMessage("删除成功", c)
|
|
|
+}
|
|
|
+
|
|
|
+func (pa *ProjectApi) ProjectFilesDownload(c *gin.Context) {
|
|
|
+ var info request.SearchProjectFileList
|
|
|
+ err := c.ShouldBindJSON(&info)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("错误", c)
|
|
|
+ global.GVA_LOG.Error("ProjectFilesDownload ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ files, err := projectService.QueryFilesByNameOrGenre(info)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("错误", c)
|
|
|
+ global.GVA_LOG.Error("ProjectFilesDownload ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ zipBuffer := &bytes.Buffer{}
|
|
|
+ zipWriter := zip.NewWriter(zipBuffer)
|
|
|
+
|
|
|
+ for _, file := range files {
|
|
|
+ err := addFileToZip(zipWriter, file.Path)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("错误", c)
|
|
|
+ global.GVA_LOG.Error("ProjectFilesDownload ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := zipWriter.Close(); err != nil {
|
|
|
+ response.FailWithMessage("错误", c)
|
|
|
+ global.GVA_LOG.Error("ProjectFilesDownload ====== " + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ c.Header("Content-Type", "application/zip") // 表示是文件流,唤起浏览器下载,一般设置了这个,就要设置文件名
|
|
|
+ c.Header("Content-Disposition", "attachment; filename="+info.Code+".zip") // 用来指定下载下来的文件名
|
|
|
+ c.Header("Content-Transfer-Encoding", "binary") // 表示传输过程中的编码形式,乱码问题可能就是因为它
|
|
|
+ c.Data(0, "application/zip", zipBuffer.Bytes())
|
|
|
+}
|
|
|
+
|
|
|
+// 添加文件道zip中
|
|
|
+func addFileToZip(w *zip.Writer, filename string) error {
|
|
|
+ file, err := os.Open(filename)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ defer file.Close()
|
|
|
+
|
|
|
+ fileInfo, err := file.Stat()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ header, err := zip.FileInfoHeader(fileInfo)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ header.Name = filepath.Base(filename)
|
|
|
+
|
|
|
+ writer, err := w.CreateHeader(header)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = io.Copy(writer, file)
|
|
|
+ return err
|
|
|
+}
|