初始
This commit is contained in:
		
							
								
								
									
										526
									
								
								internal/app/system/logic/documentReport/document_report.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										526
									
								
								internal/app/system/logic/documentReport/document_report.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,526 @@ | ||||
| // ========================================================================== | ||||
| // GFast自动生成logic操作代码。 | ||||
| // 生成日期:2024-03-15 10:24:21 | ||||
| // 生成路径: internal/app/system/logic/document_report.go | ||||
| // 生成人:gfast | ||||
| // desc:科研及专题报告 | ||||
| // company:云南奇讯科技有限公司 | ||||
| // ========================================================================== | ||||
|  | ||||
| package logic | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"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" | ||||
| 	ct "github.com/tiger1103/gfast/v3/internal/app/system/logic/context" | ||||
| 	logic "github.com/tiger1103/gfast/v3/internal/app/system/logic/documentCompletion" | ||||
| 	"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.RegisterDocumentReport(New()) | ||||
| } | ||||
|  | ||||
| func New() *sDocumentReport { | ||||
| 	return &sDocumentReport{} | ||||
| } | ||||
|  | ||||
| type sDocumentReport struct{} | ||||
|  | ||||
| func (s *sDocumentReport) CompletionDataRecyclingStationFunc(ctx context.Context, req *system.ReportDataRecyclingStationReq) (err error) { | ||||
| 	err = g.Try(ctx, func(ctx context.Context) { | ||||
| 		if req.Type == "1" { | ||||
| 			_, err = dao.DocumentReport.Ctx(ctx).Unscoped().Where(dao.DocumentReport.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.DocumentReport.Ctx(ctx).Unscoped().Where("id", req.Ids[i]).Fields("filen_path").Value() | ||||
| 				pathArr = append(pathArr, data.String()) | ||||
| 			} | ||||
| 			idArr = append(idArr, req.Ids...) | ||||
| 			_, err = dao.DocumentReport.Ctx(ctx).Unscoped().Delete(dao.DocumentReport.Columns().Id+" in (?)", idArr) | ||||
| 			//如果删除成功,那么删除文件/文件夹 | ||||
| 			for _, data := range pathArr { | ||||
| 				os.RemoveAll(coryCommon.FileToFunc(data, 2)) | ||||
| 			} | ||||
| 		} | ||||
| 		liberr.ErrIsNil(ctx, err, "删除失败") | ||||
| 	}) | ||||
| 	return | ||||
| } | ||||
|  | ||||
| func (s *sDocumentReport) RecycleBinListFunc(ctx context.Context, req *system.ReportRecycleBinListReq) (res *system.ReportRecycleBinListRes, err error) { | ||||
| 	err = g.Try(ctx, func(ctx context.Context) { | ||||
| 		res = new(system.ReportRecycleBinListRes) | ||||
| 		var dataList []*model.DocumentReportInfoRes | ||||
| 		m := dao.DocumentReport.Ctx(ctx) | ||||
| 		if req.Type == "2" { | ||||
| 			m = m.Unscoped().WhereNotNull("deleted_at") | ||||
| 		} else if req.Type == "3" { | ||||
| 			m = m.Unscoped() | ||||
| 		} | ||||
| 		err = m.Where(dao.DocumentReport.Columns().ProjectId, req.ProjectId).Scan(&dataList) | ||||
| 		res.List = dataList | ||||
| 		liberr.ErrIsNil(ctx, err, "获取数据失败!") | ||||
| 	}) | ||||
| 	return | ||||
| } | ||||
|  | ||||
| func (s *sDocumentReport) SelectByPidFunc(ctx context.Context, req *system.ReportSelectByPidReq) (res *system.ReportSelectByPidRes, err error) { | ||||
| 	res = new(system.ReportSelectByPidRes) | ||||
| 	//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.DocumentReport.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 *sDocumentReport) TreeStructureFunc(ctx context.Context, req *system.ReportTreeStructureReq) (res *system.ReportTreeStructureRes, err error) { | ||||
| 	res = new(system.ReportTreeStructureRes) | ||||
| 	err = g.Try(ctx, func(ctx context.Context) { | ||||
| 		var completion []*model.DocumentReportListRes | ||||
| 		//1、获取到当前项目下的所有顶级文件及目录 | ||||
| 		sql := dao.DocumentReport.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 *sDocumentReport) CompletionNewFolderDataFunc(ctx context.Context, req *system.ReportNewFolderDataReq) (err error) { | ||||
| 	dataFolder, err := logic.AddFileOperation(ctx, coryCommon.Completion, req.ProjectId) | ||||
| 	err = g.Try(ctx, func(ctx context.Context) { | ||||
| 		//1、组装数据 | ||||
| 		resEntity := model.DocumentReportListRes{ | ||||
| 			Name:   req.FileName, | ||||
| 			Suffix: "", | ||||
| 			Type:   "2", | ||||
| 		} | ||||
| 		path := "" | ||||
| 		if req.Pid != "" { | ||||
| 			value, _ := dao.DocumentReport.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.DocumentReport.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.State = "4" | ||||
| 			resEntity.ProjectId = req.ProjectId | ||||
| 			_, insertDataErr := dao.DocumentReport.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 *sDocumentReport) List(ctx context.Context, req *system.DocumentReportSearchReq) (listRes *system.DocumentReportSearchRes, err error) { | ||||
| 	listRes = new(system.DocumentReportSearchRes) | ||||
| 	err = g.Try(ctx, func(ctx context.Context) { | ||||
| 		m := dao.DocumentReport.Ctx(ctx).WithAll() | ||||
| 		if req.Id != "" { | ||||
| 			m = m.Where(dao.DocumentReport.Columns().Id+" = ?", req.Id) | ||||
| 		} | ||||
| 		if req.IdStr != "" { | ||||
| 			m = m.Where(dao.DocumentReport.Columns().IdStr+" = ?", req.IdStr) | ||||
| 		} | ||||
| 		if req.Pid != "" { | ||||
| 			m = m.Where(dao.DocumentReport.Columns().Pid+" = ?", req.Pid) | ||||
| 		} | ||||
| 		if req.Name != "" { | ||||
| 			m = m.Where(dao.DocumentReport.Columns().Name+" like ?", "%"+req.Name+"%") | ||||
| 		} | ||||
| 		if req.FilenPath != "" { | ||||
| 			m = m.Where(dao.DocumentReport.Columns().FilenPath+" = ?", req.FilenPath) | ||||
| 		} | ||||
| 		if req.Type != "" { | ||||
| 			m = m.Where(dao.DocumentReport.Columns().Type+" = ?", req.Type) | ||||
| 		} | ||||
| 		if req.Suffix != "" { | ||||
| 			m = m.Where(dao.DocumentReport.Columns().Suffix+" = ?", req.Suffix) | ||||
| 		} | ||||
| 		if req.CreateBy != "" { | ||||
| 			m = m.Where(dao.DocumentReport.Columns().CreateBy+" = ?", req.CreateBy) | ||||
| 		} | ||||
| 		if req.UpdateBy != "" { | ||||
| 			m = m.Where(dao.DocumentReport.Columns().UpdateBy+" = ?", req.UpdateBy) | ||||
| 		} | ||||
| 		if len(req.DateRange) != 0 { | ||||
| 			m = m.Where(dao.DocumentReport.Columns().CreatedAt+" >=? AND "+dao.DocumentReport.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1]) | ||||
| 		} | ||||
| 		if req.ProjectId != "" { | ||||
| 			m = m.Where(dao.DocumentReport.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.DocumentReportInfoRes | ||||
| 		err = m.Fields(system.DocumentReportSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res) | ||||
| 		liberr.ErrIsNil(ctx, err, "获取数据失败") | ||||
| 		listRes.List = make([]*model.DocumentReportListRes, len(res)) | ||||
| 		for k, v := range res { | ||||
| 			listRes.List[k] = &model.DocumentReportListRes{ | ||||
| 				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 *sDocumentReport) GetById(ctx context.Context, id int64) (res *model.DocumentReportInfoRes, err error) { | ||||
| 	err = g.Try(ctx, func(ctx context.Context) { | ||||
| 		err = dao.DocumentReport.Ctx(ctx).WithAll().Where(dao.DocumentReport.Columns().Id, id).Scan(&res) | ||||
| 		res.FilenPathCoding = coryCommon.URLCoding(res.FilenPath) | ||||
| 		liberr.ErrIsNil(ctx, err, "获取信息失败") | ||||
| 	}) | ||||
| 	return | ||||
| } | ||||
|  | ||||
| func (s *sDocumentReport) Add(ctx context.Context, req *system.DocumentReportAddReq) (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 := logic.AddFileOperation(ctx, coryCommon.Report, 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.DocumentReport.Table(), | ||||
| 			} | ||||
| 			ct := ct.New().GetLoginUser(ctx).Id | ||||
| 			formatInt := strconv.FormatInt(int64(ct), 10) | ||||
| 			if req.FileType == "1" { //1、压缩文件夹导入 | ||||
| 				filenPath := "" | ||||
| 				if req.Pid != "" { | ||||
| 					value, _ := dao.DocumentReport.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.DocumentReport.Table(), dataFolder, req.ProjectId, "2") //遍历解压后的文件,插入数据 | ||||
| 				liberr.ErrIsNil(ctx, err) | ||||
| 				for i := range one { | ||||
| 					one[i].CreateBy = formatInt | ||||
| 				} | ||||
| 				insertData, err := g.DB().Model(dao.DocumentReport.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.DocumentReport.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、文件存在就随机给一个文件名(当前文件名+随机文件名) | ||||
| 				fmt.Println("0000?              ", newPath) | ||||
| 				_, 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.DocumentReportListRes{ | ||||
| 					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, | ||||
| 				} | ||||
| 				if req.Pid == "" { | ||||
| 					res.Pid = "0" | ||||
| 				} else { | ||||
| 					res.Pid = req.Pid | ||||
| 				} | ||||
| 				insertData, err := dao.DocumentReport.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 *sDocumentReport) Edit(ctx context.Context, req *system.DocumentReportEditReq) (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 *sDocumentReport) Delete(ctx context.Context, ids []int64) (err error) { | ||||
| 	err = g.Try(ctx, func(ctx context.Context) { | ||||
| 		//1、先判断删除的是文件还是文件夹,然后进行修改需要删除的文件或文件夹名称 | ||||
| 		var dataList []*model.DocumentReportInfoRes | ||||
| 		err = dao.DocumentReport.Ctx(ctx).Where(dao.DocumentReport.Columns().Id+" in (?)", ids).Scan(&dataList) | ||||
| 		liberr.ErrIsNil(ctx, err, "删除失败") | ||||
| 		if len(dataList) > 0 { | ||||
| 			for i := range dataList { | ||||
| 				t := dataList[i].Type | ||||
| 				upEntity := system.DocumentReportEditReq{ | ||||
| 					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.DocumentReport.Ctx(ctx).Delete(dao.DocumentReport.Columns().Id+" in (?)", ids) | ||||
| 			liberr.ErrIsNil(ctx, err, "删除失败") | ||||
| 		} | ||||
| 	}) | ||||
| 	return | ||||
| } | ||||
|  | ||||
| type TreeStructureResTwo struct { | ||||
| 	gmeta.Meta          `orm:"table:document_report"` | ||||
| 	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.DocumentReportEditReq) (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.DocumentReportInfoRes | ||||
| 			dao.DocumentReport.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.DocumentReport.Ctx(ctx).Unscoped().Where("pid", dif.IdStr).WithAll().Scan(&entity) | ||||
| 				strs := RecursiveQueryEntityFunc(ctx, entity) | ||||
| 				_, err = dao.DocumentReport.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.DocumentReport.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.DocumentReport.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.DocumentReportEditReq) (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.DocumentReportInfoRes | ||||
| 			err = dao.DocumentReport.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.DocumentReport.Ctx(ctx).WherePri(req.Id).Update(do.DocumentReport{ | ||||
| 				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 RecursiveDeletion(ctx context.Context, id int64) (ids []int64) { | ||||
| 	//1、获取某个文件的字符串id_str | ||||
| 	value, _ := dao.DocumentReport.Ctx(ctx).Unscoped().Where("id", id).Fields("id_str").Value() | ||||
| 	if value.String() == "" { | ||||
| 		return nil | ||||
| 	} | ||||
| 	//2、查看当前需要删除的数据下面是否有子数据 | ||||
| 	var dlr []*model.DocumentReportListRes | ||||
| 	dao.DocumentReport.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 | ||||
| } | ||||
		Reference in New Issue
	
	Block a user