初始
This commit is contained in:
@ -0,0 +1,610 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成logic操作代码。
|
||||
// 生成日期:2024-03-15 10:25:00
|
||||
// 生成路径: internal/app/system/logic/document_completion.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/gconv"
|
||||
"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"
|
||||
logic "github.com/tiger1103/gfast/v3/internal/app/system/logic/busDesignAudit"
|
||||
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"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterDocumentCompletion(New())
|
||||
}
|
||||
|
||||
func New() *sDocumentCompletion {
|
||||
return &sDocumentCompletion{}
|
||||
}
|
||||
|
||||
type sDocumentCompletion struct{}
|
||||
|
||||
func (s *sDocumentCompletion) AuditDataListFunc(ctx context.Context, req *system.AuditDataListReq) (res *system.AuditDataListRes, err error) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentCompletion) RecycleBinListFunc(ctx context.Context, req *system.RecycleBinListReq) (res *system.RecycleBinListRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
res = new(system.RecycleBinListRes)
|
||||
var dataList []*model.DocumentCompletionInfoRes
|
||||
m := dao.DocumentCompletion.Ctx(ctx)
|
||||
if req.Type == "2" {
|
||||
m = m.Unscoped().WhereNotNull("deleted_at")
|
||||
} else if req.Type == "3" {
|
||||
m = m.Unscoped()
|
||||
}
|
||||
err = m.Where(dao.DocumentCompletion.Columns().ProjectId, req.ProjectId).Scan(&dataList)
|
||||
res.List = dataList
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败!")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentCompletion) CompletionDataRecyclingStationFunc(ctx context.Context, req *system.CompletionDataRecyclingStationReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
if req.Type == "1" {
|
||||
_, err = dao.DocumentCompletion.Ctx(ctx).Unscoped().Where(dao.DocumentCompletion.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.DocumentCompletion.Ctx(ctx).Unscoped().Where("id", req.Ids[i]).Fields("filen_path").Value()
|
||||
pathArr = append(pathArr, data.String())
|
||||
}
|
||||
idArr = append(idArr, req.Ids...)
|
||||
_, err = dao.DocumentCompletion.Ctx(ctx).Unscoped().Delete(dao.DocumentCompletion.Columns().Id+" in (?)", idArr)
|
||||
//如果删除成功,那么删除文件/文件夹
|
||||
for _, data := range pathArr {
|
||||
os.RemoveAll(coryCommon.FileToFunc(data, 2))
|
||||
}
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentCompletion) SelectByPidFunc(ctx context.Context, req *system.SelectByPidReq) (res *system.SelectByPidRes, err error) {
|
||||
res = new(system.SelectByPidRes)
|
||||
//1、组装路径
|
||||
dataFolder := ct.New().GetLoginUser(ctx).UserName
|
||||
dataFolder = coryCommon.Completion + "/" + dataFolder + req.PathName
|
||||
//2、获取pid
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
value, err := dao.DocumentCompletion.Ctx(ctx).
|
||||
Where("project_id", req.ProjectId).
|
||||
Where("filen_path", req.PathName).
|
||||
Fields("pid").
|
||||
Value()
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
res.Pid = value.String()
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentCompletion) TreeStructureFunc(ctx context.Context, req *system.DocumentTreeStructureReq) (res *system.DocumentTreeStructureRes, err error) {
|
||||
res = new(system.DocumentTreeStructureRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
var completion []*model.DocumentCompletionListRes
|
||||
//1、获取到当前项目下的所有顶级文件及目录
|
||||
sql := dao.DocumentCompletion.Ctx(ctx).
|
||||
Where("project_id", req.ProjectId).Where("state in ('3','4')")
|
||||
if req.Pid != "" {
|
||||
sql = sql.Where("pid", req.Pid)
|
||||
}
|
||||
sql = sql.WhereNull("deleted_at")
|
||||
err := sql.Order("type desc").Scan(&completion)
|
||||
for i := range completion {
|
||||
completion[i].FilenPathCoding = coryCommon.URLCoding(completion[i].FilenPath)
|
||||
}
|
||||
res.List = completion
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentCompletion) CompletionNewFolderDataFunc(ctx context.Context, req *system.CompletionNewFolderDataReq) (err error) {
|
||||
dataFolder, err := AddFileOperation(ctx, coryCommon.Completion, req.ProjectId)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//1、组装数据
|
||||
resEntity := model.DocumentCompletionListRes{
|
||||
Name: req.FileName,
|
||||
Suffix: "",
|
||||
Type: "2",
|
||||
}
|
||||
path := ""
|
||||
if req.Pid != "" {
|
||||
value, _ := dao.DocumentCompletion.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.DocumentCompletion.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
|
||||
resEntity.State = "4"
|
||||
resEntity.CreateBy = strconv.FormatInt(int64(ct.New().GetLoginUser(ctx).Id), 10)
|
||||
_, insertDataErr := dao.DocumentCompletion.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 {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err, "新增文件夹失败!")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentCompletion) List(ctx context.Context, req *system.DocumentCompletionSearchReq) (listRes *system.DocumentCompletionSearchRes, err error) {
|
||||
listRes = new(system.DocumentCompletionSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.DocumentCompletion.Ctx(ctx).WithAll()
|
||||
if req.Id != "" {
|
||||
m = m.Where(dao.DocumentCompletion.Columns().Id+" = ?", req.Id)
|
||||
}
|
||||
if req.IdStr != "" {
|
||||
m = m.Where(dao.DocumentCompletion.Columns().IdStr+" = ?", req.IdStr)
|
||||
}
|
||||
if req.Pid != "" {
|
||||
m = m.Where(dao.DocumentCompletion.Columns().Pid+" = ?", req.Pid)
|
||||
}
|
||||
if req.Name != "" {
|
||||
m = m.Where(dao.DocumentCompletion.Columns().Name+" like ?", "%"+req.Name+"%")
|
||||
}
|
||||
if req.FilenPath != "" {
|
||||
m = m.Where(dao.DocumentCompletion.Columns().FilenPath+" = ?", req.FilenPath)
|
||||
}
|
||||
if req.Type != "" {
|
||||
m = m.Where(dao.DocumentCompletion.Columns().Type+" = ?", req.Type)
|
||||
}
|
||||
if req.Suffix != "" {
|
||||
m = m.Where(dao.DocumentCompletion.Columns().Suffix+" = ?", req.Suffix)
|
||||
}
|
||||
if req.CreateBy != "" {
|
||||
m = m.Where(dao.DocumentCompletion.Columns().CreateBy+" = ?", req.CreateBy)
|
||||
}
|
||||
if req.UpdateBy != "" {
|
||||
m = m.Where(dao.DocumentCompletion.Columns().UpdateBy+" = ?", req.UpdateBy)
|
||||
}
|
||||
if len(req.DateRange) != 0 {
|
||||
m = m.Where(dao.DocumentCompletion.Columns().CreatedAt+" >=? AND "+dao.DocumentCompletion.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
|
||||
}
|
||||
if req.ProjectId != "" {
|
||||
m = m.Where(dao.DocumentCompletion.Columns().ProjectId+" = ?", gconv.Int64(req.ProjectId))
|
||||
}
|
||||
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
|
||||
}
|
||||
order := "id asc"
|
||||
if req.OrderBy != "" {
|
||||
order = req.OrderBy
|
||||
}
|
||||
var res []*model.DocumentCompletionInfoRes
|
||||
err = m.Fields(system.DocumentCompletionSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
listRes.List = make([]*model.DocumentCompletionListRes, len(res))
|
||||
for k, v := range res {
|
||||
listRes.List[k] = &model.DocumentCompletionListRes{
|
||||
Id: v.Id,
|
||||
IdStr: v.IdStr,
|
||||
Pid: v.Pid,
|
||||
Name: v.Name,
|
||||
FilenPath: v.FilenPath,
|
||||
Type: v.Type,
|
||||
Suffix: v.Suffix,
|
||||
CreateBy: v.CreateBy,
|
||||
UpdateBy: v.UpdateBy,
|
||||
CreatedAt: v.CreatedAt,
|
||||
ProjectId: v.ProjectId,
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentCompletion) GetById(ctx context.Context, id int64) (res []*model.SelectTabNameAndTabIdByDateRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//1、获取当前数据的pid
|
||||
value, err := dao.DocumentCompletion.Ctx(ctx).Where(dao.DocumentCompletion.Columns().Id, id).Fields("pid").Value()
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
var pid = value.String()
|
||||
//2、组装数据
|
||||
req := model.SelectTabNameAndTabIdByDateReq{
|
||||
TableName: dao.DocumentCompletion.Table(),
|
||||
TableId: id,
|
||||
}
|
||||
if pid != "" {
|
||||
//获取最终父节点id
|
||||
byId, err := SelectRecursionById(ctx, req.TableName, pid)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
req.FatherId = byId
|
||||
}
|
||||
//3、得到最终文件/文件夹的操作记录
|
||||
res, err = logic.SelectTabNameAndTabIdByDateFunc(ctx, &req)
|
||||
for i := range res {
|
||||
res[i].CreatedBy = coryCommon.SelectByString(ctx, coryCommon.IsNumeric(res[i].CreatedBy), res[i].CreatedBy)
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentCompletion) Add(ctx context.Context, req *system.DocumentCompletionAddReq) (err error) {
|
||||
if req.FilePath == nil {
|
||||
errors.New("未识别到文件上传!")
|
||||
return
|
||||
}
|
||||
//大文件上传
|
||||
req.FilePath.Url = strings.ReplaceAll(req.FilePath.Url, "/resource/public/", "/file/")
|
||||
req.FilePath.Name = strings.Replace(req.FilePath.Name, " ", "", -1)
|
||||
dataFolder, err := AddFileOperation(ctx, coryCommon.Completion, req.ProjectId)
|
||||
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) {
|
||||
designEntity := &system.BusDesignAuditAddReq{
|
||||
ProjectId: req.ProjectId,
|
||||
TableName: dao.DocumentCompletion.Table(),
|
||||
}
|
||||
ct := ct.New().GetLoginUser(ctx).Id
|
||||
formatInt := strconv.FormatInt(int64(ct), 10)
|
||||
if req.FileType == "1" { //1、压缩文件夹导入
|
||||
filenPath := ""
|
||||
if req.Pid != "" {
|
||||
value, _ := dao.DocumentCompletion.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")
|
||||
if filenPath == "" {
|
||||
filenPath = coryCommon.ResourcePublicToFunc(dataFolder, 0)
|
||||
}
|
||||
path, err := coryCommon.FileZipFunc(newPath, filenPath, dataFolder) //解压相对路径,并删除压缩文件,返回解压后的相对路径
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
one, err := coryCommon.Traversal(ctx, path, req.Pid, dao.DocumentCompletion.Table(), dataFolder, req.ProjectId, "2") //遍历解压后的文件,插入数据
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
for i := range one {
|
||||
one[i].CreateBy = formatInt
|
||||
}
|
||||
insertData, err := g.DB().Model(dao.DocumentCompletion.Table()).Ctx(ctx).Insert(one)
|
||||
id, err := insertData.LastInsertId()
|
||||
liberr.ErrIsNil(ctx, err, "新增失败!")
|
||||
designEntity.TableId = id
|
||||
} else if req.FileType == "2" { //2、文件导入
|
||||
wornPath := filepath.ToSlash(strings.ReplaceAll(req.FilePath.Url, "/file/", coryCommon.GetCWD()+"/resource/public/"))
|
||||
ve := ""
|
||||
//不等于空,那么证明文件应该在根目录下面,否则是在某个文件夹下面
|
||||
if req.Pid != "" {
|
||||
value, _ := dao.DocumentCompletion.Ctx(ctx).Where("id_str", req.Pid).Fields("filen_path").Value()
|
||||
ve = value.String()
|
||||
} else {
|
||||
ve = strings.Replace(dataFolder+"/", "/resource/public/", "/file/", 1)
|
||||
}
|
||||
newPath := strings.Replace(ve, "/file/", coryCommon.GetCWD()+"/resource/public/", 1)
|
||||
//2、文件存在就随机给一个文件名(当前文件名+随机文件名)
|
||||
_, err = os.Stat(newPath + "/" + req.FilePath.Name)
|
||||
if err == nil {
|
||||
split := strings.Split(req.FilePath.Name, ".")
|
||||
|
||||
name := strings.Join(split[0:len(split)-1], ",") + "" + coryCommon.FileName("completion") + "." + strings.Split(req.FilePath.Name, ".")[1]
|
||||
newPath = newPath + "/" + name
|
||||
req.FilePath.Name = name
|
||||
} else {
|
||||
newPath = newPath + "/" + req.FilePath.Name
|
||||
}
|
||||
//3、文件移动
|
||||
//newPath = filepath.ToSlash(newPath)
|
||||
newPath = strings.ReplaceAll(filepath.ToSlash(newPath), "//", "/")
|
||||
err = os.Rename(wornPath, newPath)
|
||||
|
||||
//4、数据库记录文件
|
||||
sp := strings.Split(req.FilePath.Name, ".")
|
||||
res := model.DocumentCompletionListRes{
|
||||
Name: strings.Join(sp[0:len(sp)-1], ","),
|
||||
Suffix: req.FilePath.FileType,
|
||||
FilenPath: filepath.ToSlash(strings.Replace(newPath, coryCommon.GetCWD()+"/resource/public/", "/file/", 1)),
|
||||
Type: "1",
|
||||
ProjectId: req.ProjectId,
|
||||
CreateBy: formatInt,
|
||||
}
|
||||
if req.Pid == "" {
|
||||
res.Pid = "0"
|
||||
} else {
|
||||
res.Pid = req.Pid
|
||||
}
|
||||
insertData, err := dao.DocumentCompletion.Ctx(ctx).Insert(res)
|
||||
id, err := insertData.LastInsertId()
|
||||
liberr.ErrIsNil(ctx, err, "新增失败!")
|
||||
designEntity.TableId = id
|
||||
}
|
||||
//只要是新增的数据全都要进入到审核表中,提供给项目经理进行审核
|
||||
err = service.BusDesignAudit().Add(ctx, designEntity)
|
||||
liberr.ErrIsNil(ctx, err, "数据进入审核流程失败")
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentCompletion) Edit(ctx context.Context, req *system.DocumentCompletionEditReq) (err error) {
|
||||
if req.Type == "1" {
|
||||
err = updateFile(ctx, req)
|
||||
} else if req.Type == "2" {
|
||||
err = updateFolder(ctx, req)
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDocumentCompletion) Delete(ctx context.Context, ids []int64) (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 dataList []*model.DocumentCompletionInfoRes
|
||||
err = dao.DocumentCompletion.Ctx(ctx).Where(dao.DocumentCompletion.Columns().Id+" in (?)", ids).Scan(&dataList)
|
||||
liberr.ErrIsNil(ctx, err, "获取该数据失败")
|
||||
if len(dataList) > 0 {
|
||||
for i := range dataList {
|
||||
//删除的数据是假删除,会将文件夹的名字添加一个前缀“delete”
|
||||
t := dataList[i].Type
|
||||
upEntity := system.DocumentCompletionEditReq{
|
||||
Id: dataList[i].Id,
|
||||
Name: dataList[i].Name + coryCommon.FileName("delete"),
|
||||
Type: t,
|
||||
}
|
||||
if t == "1" {
|
||||
err = updateFile(ctx, &upEntity)
|
||||
} else {
|
||||
err = updateFolder(ctx, &upEntity)
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
//需要记录删除数据的信息
|
||||
err = logic.AddApprovedMemo(ctx, dataList[i].Id, dataList[i].ProjectId, dao.DocumentCompletion.Table(), "3") //删除记录
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
//1、删除文件夹
|
||||
_, err = dao.DocumentCompletion.Ctx(ctx).Delete(dao.DocumentCompletion.Columns().Id+" in (?)", ids)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
}
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
type TreeStructureResTwo struct {
|
||||
gmeta.Meta `orm:"table:document_completion"`
|
||||
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
|
||||
}
|
||||
|
||||
// AddFileOperation 对文件进行操作(公共)
|
||||
func AddFileOperation(ctx context.Context, filePath string, projectId int64) (dataFolder string, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//获取当前用户的用户名
|
||||
//dataFolder = ct.New().GetLoginUser(ctx).UserName
|
||||
str := strconv.FormatInt(projectId, 10)
|
||||
//dataFolder = filePath + "/" + dataFolder + str
|
||||
dataFolder = filePath + "/" + str
|
||||
// 判断文件夹是否存在,没有就新建
|
||||
mkPath := coryCommon.GetCWD() + dataFolder
|
||||
if _, err := os.Stat(mkPath); os.IsNotExist(err) {
|
||||
// 创建文件夹
|
||||
err := os.MkdirAll(mkPath, 0777)
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 修改文件夹
|
||||
func updateFolder(ctx context.Context, req *system.DocumentCompletionEditReq) (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.DocumentCompletionInfoRes
|
||||
dao.DocumentCompletion.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.DocumentCompletion.Ctx(ctx).Unscoped().Where("pid", dif.IdStr).WithAll().Scan(&entity)
|
||||
strs := RecursiveQueryEntityFunc(ctx, entity)
|
||||
_, err = dao.DocumentCompletion.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)" +
|
||||
")"),
|
||||
"update_by": ct.New().GetLoginUser(ctx).Id})
|
||||
if err != nil {
|
||||
err = errors.New("修改失败!")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
//3、重新生成并修改文件夹的id_str等信息
|
||||
_, err = dao.DocumentCompletion.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.DocumentCompletion.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.DocumentCompletionEditReq) (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.DocumentCompletionInfoRes
|
||||
err = dao.DocumentCompletion.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.DocumentCompletion.Ctx(ctx).WherePri(req.Id).Update(do.DocumentCompletion{
|
||||
Name: req.Name,
|
||||
FilenPath: replace,
|
||||
UpdateBy: ct.New().GetLoginUser(ctx).Id,
|
||||
})
|
||||
//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 RecursiveDeletion(ctx context.Context, id int64) (ids []int64) {
|
||||
//1、获取某个文件的字符串id_str
|
||||
value, _ := dao.DocumentCompletion.Ctx(ctx).Unscoped().Where("id", id).Fields("id_str").Value()
|
||||
if value.String() == "" {
|
||||
return nil
|
||||
}
|
||||
//2、查看当前需要删除的数据下面是否有子数据
|
||||
var dlr []*model.DocumentCompletionListRes
|
||||
dao.DocumentCompletion.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
|
||||
}
|
||||
|
||||
// SelectRecursionById 递归获取父级的id
|
||||
func SelectRecursionById(ctx context.Context, tableName, pid string) (id int64, err error) {
|
||||
sql := `WITH RECURSIVE cte AS (
|
||||
SELECT *
|
||||
FROM ` + tableName + `
|
||||
WHERE pid = '` + pid + `'
|
||||
UNION ALL
|
||||
SELECT u.*
|
||||
FROM ` + tableName + ` u
|
||||
INNER JOIN cte ON u.id_str = cte.pid
|
||||
)
|
||||
SELECT id
|
||||
FROM cte
|
||||
ORDER BY id
|
||||
LIMIT 1`
|
||||
value, err := g.DB().GetValue(ctx, sql)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
id = value.Int64()
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user