初始
This commit is contained in:
667
internal/app/system/logic/documentData/document_data.go
Normal file
667
internal/app/system/logic/documentData/document_data.go
Normal file
@ -0,0 +1,667 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成logic操作代码。
|
||||
// 生成日期:2023-12-27 09:30:04
|
||||
// 生成路径: internal/app/system/logic/document_data.go
|
||||
// 生成人:gfast
|
||||
// desc:工程资料>资料
|
||||
// company:云南奇讯科技有限公司
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gmeta"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/common/coryCommon"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/consts"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
|
||||
ct "github.com/tiger1103/gfast/v3/internal/app/system/logic/context"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/service"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterDocumentData(New())
|
||||
}
|
||||
|
||||
func New() *sDocumentData {
|
||||
return &sDocumentData{}
|
||||
}
|
||||
|
||||
type sDocumentData struct{}
|
||||
|
||||
func (s *sDocumentData) SonFileList(ctx context.Context, req *system.SonFileReq) (res *system.SonFileRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
res = new(system.SonFileRes)
|
||||
var dataList []*model.DocumentDataInfoRes
|
||||
m := dao.DocumentData.Ctx(ctx).
|
||||
Where("type", "1").
|
||||
Where("pid", req.IdStr)
|
||||
err = m.Where(dao.DocumentData.Columns().ProjectId, req.Project).Scan(&dataList)
|
||||
res.List = dataList
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败!")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentData) DataFileQueryFunc(ctx context.Context, req *system.DataFileQueryReq) (res *system.DataFileQueryRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
res = new(system.DataFileQueryRes)
|
||||
var dataList []*model.DocumentDataListTwoRes
|
||||
m := dao.DocumentData.Ctx(ctx).As("a").
|
||||
LeftJoin("sys_project", "b", "a.project_id = b.id").
|
||||
Where("a."+dao.DocumentData.Columns().Type, "1").
|
||||
Where("a." + dao.DocumentData.Columns().Suffix + " in ('.docx','.xlsx','.pptx','.doc','.xls','.ppt')")
|
||||
if req.Project > 0 {
|
||||
m = m.Where("a."+dao.DocumentData.Columns().ProjectId, req.Project)
|
||||
}
|
||||
if strings.Trim(req.FileName, " ") != "" {
|
||||
m = m.Where("a."+dao.DocumentData.Columns().Name+" like ?", "%"+req.FileName+"%")
|
||||
}
|
||||
m.Fields("a.*,b.project_name as projectName,b.short_name as shortName").Scan(&dataList)
|
||||
for i := range dataList {
|
||||
// 对 URL 进行编码
|
||||
//split := strings.Split(dataList[i].FilenPath, "/")
|
||||
//s2 := split[len(split)-1]
|
||||
s2 := dataList[i].FilenPath[6 : len(dataList[i].FilenPath)-1]
|
||||
encodedPath := url.QueryEscape(s2)
|
||||
dataList[i].FilenPathCoding = strings.ReplaceAll(dataList[i].FilenPath, s2, encodedPath)
|
||||
}
|
||||
res.List = dataList
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败!")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentData) DataRecyclingStationFunc(ctx context.Context, req *system.DataRecyclingStationReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
if req.Type == "1" {
|
||||
_, err = dao.DocumentData.Ctx(ctx).Unscoped().Where(dao.DocumentData.Columns().Id+" in (?)", req.Ids).Update(g.Map{"deleted_at": nil})
|
||||
} else {
|
||||
var pathArr []string
|
||||
//删除还需要删除下面的所有子数据
|
||||
var idArr []int64
|
||||
for i := range req.Ids {
|
||||
idList := RecursiveDeletion(ctx, req.Ids[i])
|
||||
if idList != nil {
|
||||
idArr = append(idArr, idList...)
|
||||
}
|
||||
data, _ := dao.DocumentData.Ctx(ctx).Unscoped().Where("id", req.Ids[i]).Fields("filen_path").Value()
|
||||
pathArr = append(pathArr, data.String())
|
||||
}
|
||||
idArr = append(idArr, req.Ids...)
|
||||
_, err = dao.DocumentData.Ctx(ctx).Unscoped().Delete(dao.DocumentData.Columns().Id+" in (?)", idArr)
|
||||
//如果删除成功,那么删除文件/文件夹
|
||||
for _, data := range pathArr {
|
||||
os.RemoveAll(coryCommon.FileToFunc(data, 2))
|
||||
}
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func RecursiveDeletion(ctx context.Context, id int64) (ids []int64) {
|
||||
//1、获取某个文件的字符串id_str
|
||||
value, _ := dao.DocumentData.Ctx(ctx).Unscoped().Where("id", id).Fields("id_str").Value()
|
||||
if value.String() == "" {
|
||||
return nil
|
||||
}
|
||||
//2、查看当前需要删除的数据下面是否有子数据
|
||||
var dlr []*model.DocumentDataListRes
|
||||
dao.DocumentData.Ctx(ctx).Unscoped().Where("pid", value.String()).Fields("id,id_str").Scan(&dlr)
|
||||
for i := range dlr {
|
||||
ids = append(ids, dlr[i].Id)
|
||||
deletion := RecursiveDeletion(ctx, dlr[i].Id)
|
||||
ids = append(ids, deletion...)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func (s *sDocumentData) AllList(ctx context.Context, req *system.AllDocumentDataSearchReq) (res *system.AllDocumentDataSearchRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
res = new(system.AllDocumentDataSearchRes)
|
||||
var dataList []*model.DocumentDataInfoRes
|
||||
m := dao.DocumentData.Ctx(ctx)
|
||||
if req.Type == "2" {
|
||||
m = m.Unscoped().WhereNotNull("deleted_at")
|
||||
} else if req.Type == "3" {
|
||||
m = m.Unscoped()
|
||||
}
|
||||
err = m.Where(dao.DocumentData.Columns().ProjectId, req.ProjectId).OrderDesc(dao.DocumentData.Columns().CreatedAt).Scan(&dataList)
|
||||
res.List = dataList
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败!")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentData) OnlineMobileFunc(ctx context.Context, req *system.OnlineMobileReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
//获取当前用户的用户名
|
||||
dataFolder := ct.New().GetLoginUser(ctx).UserName
|
||||
str := strconv.FormatInt(req.ProjectId, 10)
|
||||
dataFolder = coryCommon.Template2 + "/" + dataFolder + str
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
var dir *model.DocumentDataInfoRes
|
||||
err = dao.DocumentData.Ctx(ctx).WherePri(req.TemplateId).Scan(&dir)
|
||||
var ddir *model.DocumentDataInfoRes
|
||||
err = dao.DocumentData.Ctx(ctx).WherePri(req.DataId).Scan(&ddir)
|
||||
src := coryCommon.FileToFunc(dir.FilenPath, 2)
|
||||
num := req.Type
|
||||
if num == "1" {
|
||||
//1、将模板文件copy到资料文件夹下
|
||||
dst := coryCommon.FileToFunc(ddir.FilenPath, 2) + "/" + dir.Name + dir.Suffix
|
||||
err = coryCommon.MoveFile(src, dst)
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
res := model.DocumentDataInfoRes{
|
||||
Pid: coryCommon.SHA256(ddir.IdStr),
|
||||
Name: dir.Name,
|
||||
Suffix: dir.Suffix,
|
||||
FilenPath: coryCommon.ResourcePublicToFunc(dst, 1),
|
||||
Type: "1",
|
||||
}
|
||||
_, err = dao.DocumentData.Ctx(ctx).Insert(res)
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
} else if num == "2" {
|
||||
dst := coryCommon.FileToFunc(ddir.FilenPath, 2)
|
||||
//复制文件夹到指定文件夹下
|
||||
split := strings.Split(src, "/")
|
||||
err = coryCommon.MoveFolder(src, dst+"/"+split[len(split)-1])
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
//遍历文件夹,存储数据
|
||||
one, err := coryCommon.Traversal(ctx, dst+"/"+dir.Name, ddir.IdStr, dao.DocumentData.Table(), dataFolder, ddir.ProjectId, "2")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
_, err = g.DB().Model(dao.DocumentData.Table()).Ctx(ctx).Insert(one)
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
} else {
|
||||
//删除原本的文件夹及下面的所有数据
|
||||
_, err = dao.DocumentData.Ctx(ctx).Unscoped().Where("filen_path like ?", "%"+dir.FilenPath+"%").Delete()
|
||||
}
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err, "操作失败!")
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentData) OnlineImportFunc(ctx context.Context, req *system.OnlineImportReq) (err error) {
|
||||
//获取当前用户的用户名
|
||||
dataFolder := ct.New().GetLoginUser(ctx).UserName
|
||||
str := strconv.FormatInt(req.ProjectId, 10)
|
||||
dataFolder = coryCommon.Template2 + "/" + dataFolder + str
|
||||
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
var dir []*model.DocumentInfoRes
|
||||
err = dao.Document.Ctx(ctx).WherePri(req.TemplateId).Scan(&dir)
|
||||
var ddir *model.DocumentDataInfoRes
|
||||
err = dao.DocumentData.Ctx(ctx).WherePri(req.DataId).Scan(&ddir)
|
||||
for i := range dir {
|
||||
src := coryCommon.FileToFunc(dir[i].FilenPath, 2)
|
||||
num := req.Type
|
||||
if num == "1" {
|
||||
var fileName = dir[i].Name
|
||||
//1、将模板文件copy到资料文件夹下
|
||||
dst := coryCommon.FileToFunc(ddir.FilenPath, 2) + "/" + fileName + dir[i].Suffix
|
||||
// 检查源文件夹是否存在
|
||||
_, err := os.Stat(dst)
|
||||
if err == nil {
|
||||
fileName = fileName + coryCommon.FileName("zl") + dir[i].Suffix
|
||||
dst = coryCommon.FileToFunc(ddir.FilenPath, 2) + "/" + fileName
|
||||
}
|
||||
err = coryCommon.CopyFile(src, dst)
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
res := model.DocumentDataInfoRes{
|
||||
Pid: ddir.IdStr,
|
||||
Name: fileName,
|
||||
Suffix: dir[i].Suffix,
|
||||
FilenPath: coryCommon.ResourcePublicToFunc(dst, 1),
|
||||
Type: "1",
|
||||
ProjectId: req.ProjectId,
|
||||
}
|
||||
_, err = dao.DocumentData.Ctx(ctx).Insert(res)
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
} else if num == "2" {
|
||||
dst := coryCommon.FileToFunc(ddir.FilenPath, 2) + "/" + dir[i].Name
|
||||
// 检查源文件夹是否存在
|
||||
_, err := os.Stat(dst)
|
||||
if err == nil {
|
||||
err = errors.New("当前文件夹已存在!")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
//复制文件夹到指定文件夹下
|
||||
err = coryCommon.CopyDirectory(src, dst)
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
//遍历文件夹,存储数据
|
||||
one, err := coryCommon.Traversal(ctx, dst, ddir.IdStr, dao.DocumentData.Table(), dataFolder, ddir.ProjectId, "2")
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
_, err = g.DB().Model(dao.DocumentData.Table()).Ctx(ctx).Insert(one)
|
||||
liberr.ErrIsNil(ctx, err, "新增失败!")
|
||||
}
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err, "操作失败!")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentData) TreeStructureDataFunc(ctx context.Context, req *system.TreeStructureDataReq) (res *system.TreeStructureDataRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
res = new(system.TreeStructureDataRes)
|
||||
var entity []*model.TreeStructureDataRes
|
||||
err = dao.DocumentData.Ctx(ctx).Where("pid = '0'").Where("project_id", req.ProjectId).WithAll().Scan(&entity)
|
||||
res.List = entity
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败!")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func RecursiveQueryFunc(dr []*model.DocumentDataInfoRes, idStr string) (idStrs []string) {
|
||||
for _, data := range dr {
|
||||
if data.Pid == idStr {
|
||||
idStrs = append(idStrs, data.IdStr)
|
||||
strs := RecursiveQueryFunc(dr, data.IdStr)
|
||||
if len(strs) > 0 {
|
||||
idStrs = append(idStrs, strs...)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentData) List(ctx context.Context, req *system.DocumentDataSearchReq) (listRes *system.DocumentDataSearchRes, err error) {
|
||||
listRes = new(system.DocumentDataSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.DocumentData.Ctx(ctx).Where(dao.DocumentData.Columns().Type, "1").Where(dao.DocumentData.Columns().ProjectId, req.ProjectId)
|
||||
//如果查询全部那么就先递归获取到所有的pid然后去查询,否则就只查询前端传递过来的数据
|
||||
//if req.Switch == "1" {
|
||||
// var dr []*model.DocumentDataInfoRes
|
||||
// err = dao.DocumentData.Ctx(ctx).Where("type = 2").Scan(&dr)
|
||||
// strs := RecursiveQueryFunc(dr, req.IdStr)
|
||||
// strs = append(strs, req.IdStr)
|
||||
// m = m.Where(dao.DocumentData.Columns().Pid+" in (?)", strs)
|
||||
//} else {
|
||||
m = m.Where(dao.DocumentData.Columns().Pid, req.IdStr)
|
||||
//}
|
||||
if req.Name != "" {
|
||||
m = m.Where(dao.DocumentData.Columns().Name+" like ?", "%"+req.Name+"%")
|
||||
}
|
||||
if len(req.DateRange) != 0 {
|
||||
m = m.Where(dao.DocumentData.Columns().CreatedAt+" >=? AND "+dao.DocumentData.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
|
||||
}
|
||||
if !strings.EqualFold(req.IsPaging, "YES") {
|
||||
listRes.Total, err = m.Count()
|
||||
liberr.ErrIsNil(ctx, err, "获取总行数失败")
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
listRes.CurrentPage = req.PageNum
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = consts.PageSize
|
||||
}
|
||||
m = m.Fields(system.DocumentDataSearchRes{}).Page(req.PageNum, req.PageSize)
|
||||
}
|
||||
order := "id desc"
|
||||
if req.OrderBy != "" {
|
||||
order = req.OrderBy
|
||||
}
|
||||
var res []*model.DocumentDataInfoRes
|
||||
err = m.Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
listRes.List = make([]*model.DocumentDataListRes, len(res))
|
||||
for k, v := range res {
|
||||
// 对 URL 进行编码
|
||||
s2 := v.FilenPath[6 : len(v.FilenPath)-1]
|
||||
encodedPath := url.QueryEscape(s2)
|
||||
listRes.List[k] = &model.DocumentDataListRes{
|
||||
Id: v.Id,
|
||||
Name: v.Name,
|
||||
FilenPath: v.FilenPath,
|
||||
FilenPathCoding: strings.ReplaceAll(v.FilenPath, s2, encodedPath),
|
||||
Suffix: v.Suffix,
|
||||
CreatedAt: v.CreatedAt,
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentData) GetById(ctx context.Context, id int64) (res *model.DocumentDataInfoRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.DocumentData.Ctx(ctx).WithAll().Where(dao.DocumentData.Columns().Id, id).Scan(&res)
|
||||
// 对 URL 进行编码
|
||||
//split := strings.Split(res.FilenPath, "/")
|
||||
//s2 := split[len(split)-1]
|
||||
s2 := res.FilenPath[6 : len(res.FilenPath)-1]
|
||||
encodedPath := url.QueryEscape(s2)
|
||||
res.FilenPathCoding = strings.ReplaceAll(res.FilenPath, s2, encodedPath)
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentData) Add(ctx context.Context, req *system.DocumentDataAddReq) (err error) {
|
||||
//大文件上传
|
||||
req.FilePath.Url = strings.ReplaceAll(req.FilePath.Url, "/resource/public/", "/file/")
|
||||
req.FilePath.Name = strings.Replace(req.FilePath.Name, " ", "", -1)
|
||||
//获取当前用户的用户名
|
||||
dataFolder := ct.New().GetLoginUser(ctx).UserName
|
||||
str := strconv.FormatInt(req.ProjectId, 10)
|
||||
dataFolder = coryCommon.Template2 + "/" + dataFolder + str
|
||||
// 判断文件夹是否存在,没有就新建
|
||||
mkPath := coryCommon.GetCWD() + dataFolder
|
||||
if _, err := os.Stat(mkPath); os.IsNotExist(err) {
|
||||
// 创建文件夹
|
||||
err := os.MkdirAll(mkPath, 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
if req.FileType == "1" { //1、压缩文件夹导入
|
||||
filenPath := ""
|
||||
if req.Pid != "" {
|
||||
value, _ := dao.DocumentData.Ctx(ctx).Where("id_str", req.Pid).Fields("filen_path").Value()
|
||||
filenPath = value.String()
|
||||
}
|
||||
newPath := strings.ReplaceAll(req.FilePath.Url, "/file", coryCommon.GetCWD()+"/resource/public")
|
||||
path, err := coryCommon.FileZipFunc(newPath, filenPath, dataFolder) //解压相对路径,并删除压缩文件,返回解压后的相对路径
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
one, err := coryCommon.Traversal(ctx, path, req.Pid, dao.DocumentData.Table(), dataFolder, req.ProjectId, "2") //遍历解压后的文件,插入数据
|
||||
//err = Traversal(ctx, "resource/public/masterMask/资料模板")
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
_, err = g.DB().Model(dao.DocumentData.Table()).Ctx(ctx).Insert(one)
|
||||
liberr.ErrIsNil(ctx, err, "新增失败!")
|
||||
} else if req.FileType == "2" { //2、文件导入
|
||||
wornPath := filepath.ToSlash(strings.ReplaceAll(req.FilePath.Url, "/file/", coryCommon.GetCWD()+"/resource/public/"))
|
||||
value, _ := dao.DocumentData.Ctx(ctx).Where("id_str", req.Pid).Fields("filen_path").Value()
|
||||
newPath := strings.Replace(value.String(), "/file/", coryCommon.GetCWD()+"/resource/public/", 1)
|
||||
//2、文件存在就随机给一个文件名(当前文件名+随机文件名)
|
||||
_, err = os.Stat(newPath + "/" + req.FilePath.Name)
|
||||
if err == nil {
|
||||
split := strings.Split(req.FilePath.Name, ".")
|
||||
name := split[0] + "" + coryCommon.FileName("zl") + "." + strings.Split(req.FilePath.Name, ".")[1]
|
||||
newPath = newPath + "/" + name
|
||||
req.FilePath.Name = name
|
||||
} else {
|
||||
newPath = newPath + "/" + req.FilePath.Name
|
||||
}
|
||||
//3、文件移动
|
||||
err = os.Rename(wornPath, newPath)
|
||||
//4、数据库记录文件
|
||||
_, err = dao.DocumentData.Ctx(ctx).Insert(model.DocumentDataListRes{
|
||||
Pid: req.Pid,
|
||||
Name: strings.Split(req.FilePath.Name, ".")[0],
|
||||
Suffix: req.FilePath.FileType,
|
||||
FilenPath: filepath.ToSlash(strings.Replace(newPath, coryCommon.GetCWD()+"/resource/public/", "/file/", 1)),
|
||||
Type: "1",
|
||||
ProjectId: req.ProjectId,
|
||||
})
|
||||
} else { //3、新建文件夹
|
||||
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err, "新增失败!")
|
||||
return
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentData) NewFolderDataFunc(ctx context.Context, req *system.NewFolderDataReq) (err error) {
|
||||
//获取当前用户的用户名
|
||||
dataFolder := ct.New().GetLoginUser(ctx).UserName
|
||||
str := strconv.FormatInt(req.ProjectId, 10)
|
||||
dataFolder = coryCommon.Template2 + "/" + dataFolder + str
|
||||
// 判断文件夹是否存在,没有就新建
|
||||
mkPath := coryCommon.GetCWD() + dataFolder
|
||||
if _, err := os.Stat(mkPath); os.IsNotExist(err) {
|
||||
// 创建文件夹
|
||||
err := os.MkdirAll(mkPath, 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//1、组装数据
|
||||
resEntity := model.DocumentDataListRes{
|
||||
Name: req.FileName,
|
||||
Suffix: "",
|
||||
Type: "2",
|
||||
}
|
||||
path := ""
|
||||
if req.Pid != "" {
|
||||
value, _ := dao.DocumentData.Ctx(ctx).Where("id_str", req.Pid).Fields("filen_path").Value()
|
||||
path = value.String() + "/" + req.FileName
|
||||
resEntity.Pid = req.Pid
|
||||
} else {
|
||||
template := strings.Replace(dataFolder, "/resource/public/", "/file/", 1)
|
||||
path = template + "/" + req.FileName
|
||||
resEntity.Pid = "0"
|
||||
}
|
||||
//2、判断是否重名,不重复就新增
|
||||
count, _ := dao.DocumentData.Ctx(ctx).Unscoped().Where("filen_path", path).Count()
|
||||
if count > 0 {
|
||||
err = errors.New("文件夹已重复!")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
} else {
|
||||
resEntity.IdStr = coryCommon.SHA256(path)
|
||||
resEntity.FilenPath = path
|
||||
resEntity.ProjectId = req.ProjectId
|
||||
_, insertDataErr := dao.DocumentData.Ctx(ctx).Insert(resEntity)
|
||||
if insertDataErr != nil {
|
||||
err = errors.New("新增文件夹失败!")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
err = coryCommon.CreateDirectory(strings.Replace(path, "/file/", coryCommon.GetCWD()+"/resource/public/", 1))
|
||||
if err != nil {
|
||||
//err = errors.New("新增文件夹失败!")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err, "新增文件夹失败!")
|
||||
})
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
func (s *sDocumentData) Edit(ctx context.Context, req *system.DocumentDataEditReq) (err error) {
|
||||
if len(strings.ReplaceAll(req.Name, " ", "")) == 0 {
|
||||
err = errors.New("输入的名称不能为空!")
|
||||
return err
|
||||
}
|
||||
if req.Type == "1" {
|
||||
err = updateFile(ctx, req)
|
||||
} else if req.Type == "2" {
|
||||
err = updateFolder(ctx, req)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
type TreeStructureResTwo struct {
|
||||
gmeta.Meta `orm:"table:document_data"`
|
||||
Id int64 `json:"id"`
|
||||
IdStr string `json:"idStr"`
|
||||
Pid string `json:"pid"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
FilenPath string `json:"filenPath"`
|
||||
TreeStructureResTwo []*TreeStructureResTwo `json:"treeStructureResTwo" orm:"with:pid=id_str"`
|
||||
}
|
||||
|
||||
func RecursiveQueryEntityFunc(ctx context.Context, entity []*TreeStructureResTwo) (idStrs []int64) {
|
||||
for i := range entity {
|
||||
idStrs = append(idStrs, entity[i].Id)
|
||||
if len(entity[i].TreeStructureResTwo) > 0 {
|
||||
strs := RecursiveQueryEntityFunc(ctx, entity[i].TreeStructureResTwo)
|
||||
idStrs = append(idStrs, strs...)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 修改文件夹
|
||||
func updateFolder(ctx context.Context, req *system.DocumentDataEditReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
|
||||
//1、判断修改后的文件夹是否有重复,如果有就提示当前文件夹已存在!
|
||||
var dif *model.DocumentInfoRes
|
||||
dao.DocumentData.Ctx(ctx).WherePri(req.Id).Scan(&dif)
|
||||
replace := strings.Replace(dif.FilenPath, dif.Name, req.Name, 1)
|
||||
s2 := coryCommon.FileToFunc(replace, 2)
|
||||
if _, err = os.Stat(s2); err == nil {
|
||||
err = errors.New("当前文件夹已存在!")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
} else {
|
||||
count := strings.Count(dif.FilenPath, dif.Name)
|
||||
hx := coryCommon.SHA256(strings.ReplaceAll(dif.FilenPath, dif.FilenPath, replace))
|
||||
//2、如果不存在,那么就修改文件夹的同时,还得修改路径,以及及其子文件/文件夹下的所有路径
|
||||
var entity []*TreeStructureResTwo
|
||||
err = dao.DocumentData.Ctx(ctx).Unscoped().Where("pid", dif.IdStr).WithAll().Scan(&entity)
|
||||
strs := RecursiveQueryEntityFunc(ctx, entity)
|
||||
_, err = dao.DocumentData.Ctx(ctx).Unscoped().Where("id in (?)", strs).Update(g.Map{"filen_path": gdb.Raw("CONCAT(" +
|
||||
"SUBSTRING_INDEX(filen_path, '" + dif.Name + "', " + strconv.Itoa(count) + ")," +
|
||||
"'" + req.Name + "'," +
|
||||
"SUBSTRING_INDEX(filen_path, '" + dif.Name + "', -1)" +
|
||||
")")})
|
||||
if err != nil {
|
||||
err = errors.New("修改失败!")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
//3、重新生成并修改文件夹的id_str等信息
|
||||
_, err = dao.DocumentData.Ctx(ctx).Where("id", dif.Id).Update(g.Map{
|
||||
"id_str": hx,
|
||||
"name": req.Name,
|
||||
"filen_path": gdb.Raw("CONCAT(" +
|
||||
"SUBSTRING_INDEX(filen_path, '" + dif.Name + "', " + strconv.Itoa(count) + ")," +
|
||||
"'" + req.Name + "'," +
|
||||
"SUBSTRING_INDEX(filen_path, '" + dif.Name + "', -1)" +
|
||||
")"),
|
||||
})
|
||||
_, err = dao.DocumentData.Ctx(ctx).Unscoped().Where("pid", dif.IdStr).Update(g.Map{"pid": hx})
|
||||
//4、修改文件夹
|
||||
oldPath := filepath.Join(coryCommon.FileToFunc(dif.FilenPath, 2))
|
||||
newPath := filepath.Join(filepath.Dir(oldPath), req.Name)
|
||||
err = os.Rename(oldPath, newPath)
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||||
return
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 修改文件
|
||||
func updateFile(ctx context.Context, req *system.DocumentDataEditReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
var dif *model.DocumentDataInfoRes
|
||||
err = dao.DocumentData.Ctx(ctx).WherePri(req.Id).Scan(&dif)
|
||||
//1、判断修改后的名字是否有重复,如果有就提示当前文件名已存在!
|
||||
replace := strings.Replace(dif.FilenPath, dif.Name, req.Name, 1)
|
||||
s2 := coryCommon.FileToFunc(replace, 2)
|
||||
if _, err = os.Stat(s2); err == nil {
|
||||
err = errors.New("当前文件名已存在!")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
//2、如果不存在,那么就修改文件名的同时,还得修改路径
|
||||
_, err = dao.DocumentData.Ctx(ctx).WherePri(req.Id).Update(do.DocumentData{
|
||||
Name: req.Name,
|
||||
FilenPath: replace,
|
||||
})
|
||||
//3、修改具体文件的文件名
|
||||
if err == nil {
|
||||
oldPath := coryCommon.FileToFunc(dif.FilenPath, 2)
|
||||
newPath := filepath.Join(filepath.Dir(oldPath), req.Name+dif.Suffix)
|
||||
err = os.Rename(oldPath, newPath)
|
||||
if err != nil {
|
||||
err = errors.New("修改失败")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentData) Delete(ctx context.Context, ids []int64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//1、先判断删除的是文件还是文件夹,然后进行修改需要删除的文件或文件夹名称
|
||||
var dataList []*model.DocumentDataInfoRes
|
||||
err = dao.DocumentData.Ctx(ctx).Where(dao.DocumentData.Columns().Id+" in (?)", ids).Scan(&dataList)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
if len(dataList) > 0 {
|
||||
for i := range dataList {
|
||||
t := dataList[i].Type
|
||||
upEntity := system.DocumentDataEditReq{
|
||||
Id: dataList[i].Id,
|
||||
Name: dataList[i].Name + coryCommon.FileName("delete"),
|
||||
Type: t,
|
||||
}
|
||||
if t == "1" {
|
||||
err = updateFile(ctx, &upEntity)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
} else {
|
||||
err = updateFolder(ctx, &upEntity)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
}
|
||||
}
|
||||
//1、删除文件夹
|
||||
_, err = dao.DocumentData.Ctx(ctx).Delete(dao.DocumentData.Columns().Id+" in (?)", ids)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user