Sfoglia il codice sorgente

移动目录和标段

caipin 4 anni fa
parent
commit
9fa894ec84

+ 80 - 7
dao/tree_dao.go

@@ -8,6 +8,7 @@ package dao
 
 import (
 	"errors"
+	"fmt"
 	"strconv"
 	"strings"
 
@@ -29,7 +30,7 @@ func NewTreeDao(engine *xorm.Engine) *TreeDao {
 
 //id获得数据
 func (d *TreeDao) Get(id int) *models.CmTree {
-	data := &models.CmTree{Id: id}
+	data := &models.CmTree{Id: id, Isdelete: 0}
 	//Get取到值后,会自动赋值到data中
 	ok, err := d.engine.Get(data)
 	if ok && err == nil {
@@ -45,7 +46,7 @@ func (d *TreeDao) GetBidsection(id int) []models.CmTree {
 	datalist := make([]models.CmTree, 0)
 	err := d.engine.
 		Asc("serial").
-		Where("parent_id=? and isfolder=0", id).
+		Where("parent_id=? and isfolder=0 and isdelete=0", id).
 		Find(&datalist)
 
 	if err != nil {
@@ -60,7 +61,22 @@ func (d *TreeDao) GetAllDepth(depth int, projectId int) []models.CmTree {
 	datalist := make([]models.CmTree, 0)
 	err := d.engine.
 		Asc("serial").
-		Where("depth=? and project_id=?", depth, projectId).
+		Where("depth=? and project_id=? and isdelete=0", depth, projectId).
+		Find(&datalist)
+
+	if err != nil {
+		return datalist
+	} else {
+		return datalist
+	}
+}
+
+// 获得某一深度的某一归属 结构数据(不包含子集) 正序
+func (d *TreeDao) GetALLDepthByAttribution(depth int, projectId int, attribution string) []models.CmTree {
+	datalist := make([]models.CmTree, 0)
+	err := d.engine.
+		Asc("serial").
+		Where("depth=? and project_id=? and attribution like ? and isdelete=0", depth, projectId, attribution+"%").
 		Find(&datalist)
 
 	if err != nil {
@@ -75,7 +91,7 @@ func (d *TreeDao) GetFolderAndBid(projectId int, attribution string) []models.Cm
 	datalist := make([]models.CmTree, 0)
 	err := d.engine.
 		Asc("serial").
-		Where("project_id=? and attribution like ?", projectId, attribution+"%").
+		Where("project_id=? and attribution like ? and isdelete=0", projectId, attribution+"%").
 		Find(&datalist)
 	if err != nil {
 		return datalist
@@ -97,7 +113,7 @@ func (d *TreeDao) DeleteFolderAndBid(id int, projectId int, attribution string)
 	// 删除树结构中 目录和资源
 	data := &models.CmTree{Isdelete: 1}
 	_, err = session.
-		Where("id=? or (project_id=? and attribution like ?)", id, projectId, attribution+"%").
+		Where("id=? or (project_id=? and attribution like ?) and isdelete=0", id, projectId, attribution+"%").
 		Update(data)
 	if err != nil {
 		session.Rollback()
@@ -116,7 +132,7 @@ func (d *TreeDao) DeleteFolderAndBid(id int, projectId int, attribution string)
 			idList = append(idList, strconv.Itoa(bidData.Id))
 		}
 		inId := strings.Join(idList, ",")
-		_, err = session.Exec("UPDATE from cm_tender SET `isdelete` = 1 where id in (?)", inId)
+		_, err = session.Exec("UPDATE cm_tender SET `isdelete` = 1 where id in (?)", inId)
 		if err != nil {
 			session.Rollback()
 			return errors.New("删除标段出错")
@@ -132,12 +148,69 @@ func (d *TreeDao) DeleteFolderAndBid(id int, projectId int, attribution string)
 	return nil
 }
 
+// 移动目录
+func (d *TreeDao) Move(treeNode *models.CmTree, moveFolder *models.CmTree) error {
+	session := d.engine.NewSession()
+	defer session.Close()
+	err := session.Begin()
+	if err != nil {
+		return errors.New("移动出错-db")
+	}
+
+	// 被移动跟目录-父亲节点转换
+	data := &models.CmTree{ParentId: moveFolder.Id}
+	_, err = session.
+		Where("id=? and isdelete=0", treeNode.Id).
+		Update(data)
+	if err != nil {
+		session.Rollback()
+		return errors.New("移动目录出错")
+	}
+
+	// 移动目录的归属和标段的归属关系--TODO 效率问题在修改
+	// 1-原来目录的归属
+	attribution := fmt.Sprintf("%s%d-", treeNode.Attribution, treeNode.Serial)
+	// 2-移动后目录的归属
+	movrAttribution := fmt.Sprintf("%s%d-", moveFolder.Attribution, moveFolder.Serial)
+	// 3-获得移动后最大序列号
+	depth := moveFolder.Depth + 1
+	fmt.Println(depth)
+	datalist := d.GetALLDepthByAttribution(depth, moveFolder.ProjectId, movrAttribution)
+	maxIndex := len(datalist)
+	serial := 0
+	if maxIndex != 0 {
+		serial = datalist[maxIndex-1].Serial + 1
+	}
+	// 4-移动原目录货标段-最大序号
+	_, err = session.Exec("UPDATE  cm_tree SET `attribution` = ?,`serial` = ? where id = ? and isdelete=0", movrAttribution, serial, treeNode.Id)
+	if err != nil {
+		session.Rollback()
+		return errors.New("移动目录或标段出错")
+	}
+
+	// 5-移动原目录下所有子目录和标段
+	movrAttribution = fmt.Sprintf("%s%d-", movrAttribution, serial)
+	_, err = session.Exec("UPDATE  cm_tree SET `attribution` = replace(`attribution`, '"+attribution+"', '"+movrAttribution+"') where attribution like ? and isdelete=0", attribution+"%")
+	//_, err = session.Exec("UPDATE from cm_tree SET `attribution` = replace(`attribution`, ?, ?) where attribution like ?", attribution,movrAttribution,attribution+"%")
+	if err != nil {
+		session.Rollback()
+		return errors.New("移动目录或标段出错")
+	}
+
+	err = session.Commit()
+	if err != nil {
+		session.Rollback()
+		return errors.New("删除出错-db")
+	}
+	return nil
+}
+
 // 获得项目文件夹
 func (d *TreeDao) GetAllTree(projectId int) []models.CmTree {
 	datalist := make([]models.CmTree, 0)
 	err := d.engine.
 		Asc("id").
-		Where("project_id=?", projectId).
+		Where("project_id=? and isdelete=0", projectId).
 		Find(&datalist)
 
 	if err != nil {

+ 42 - 22
services/project_account_service.go

@@ -39,28 +39,6 @@ func NewProjectAccountService() ProjectAccountService {
 	}
 }
 
-// 获得一个项目用户
-func (s *projectAccountService) Get(id int) *viewmodels.ProjectAccount {
-	modelsAccount := s.dao.Get(id)
-	viewAccountData := viewmodels.ProjectAccount{}
-	if modelsAccount.Id == 0 {
-		viewAccountData.Id = "0"
-		return &viewAccountData
-	}
-
-	aesId, _ := comm.AesEncrypt(strconv.Itoa(modelsAccount.Id), conf.SignSecret)
-	aesProjectId, _ := comm.AesEncrypt(strconv.Itoa(modelsAccount.ProjectId), conf.SignSecret)
-	viewAccountData.Id = aesId
-	viewAccountData.ProjectId = aesProjectId
-	viewAccountData.Account = modelsAccount.Account
-	viewAccountData.Name = modelsAccount.Name
-	viewAccountData.Company = modelsAccount.Company
-	viewAccountData.Role = modelsAccount.Role
-	viewAccountData.Mobile = modelsAccount.Mobile
-	viewAccountData.Telephone = modelsAccount.Telephone
-	return &viewAccountData
-}
-
 // 用户规则验证
 func (s *projectAccountService) ValidRule(ctx iris.Context) (viewmodels.ProjectAccount, error) {
 	accountVaild := viewmodels.ProjectAccount{}
@@ -79,6 +57,32 @@ func (s *projectAccountService) ValidRule(ctx iris.Context) (viewmodels.ProjectA
 	return accountVaild, nil
 }
 
+// 获得一个项目用户
+func (s *projectAccountService) Get(id int) *viewmodels.ProjectAccount {
+	modelsAccount := s.dao.Get(id)
+	viewAccountData := viewmodels.ProjectAccount{}
+	if modelsAccount.Id == 0 {
+		viewAccountData.Id = "0"
+		return &viewAccountData
+	}
+
+	viewAccountData = makeProjectAccountVM(modelsAccount)
+	return &viewAccountData
+	// viewAccountData = makeProjectAccountVM(modelsAccount)
+
+	// aesId, _ := comm.AesEncrypt(strconv.Itoa(modelsAccount.Id), conf.SignSecret)
+	// aesProjectId, _ := comm.AesEncrypt(strconv.Itoa(modelsAccount.ProjectId), conf.SignSecret)
+	// viewAccountData.Id = aesId
+	// viewAccountData.ProjectId = aesProjectId
+	// viewAccountData.Account = modelsAccount.Account
+	// viewAccountData.Name = modelsAccount.Name
+	// viewAccountData.Company = modelsAccount.Company
+	// viewAccountData.Role = modelsAccount.Role
+	// viewAccountData.Mobile = modelsAccount.Mobile
+	// viewAccountData.Telephone = modelsAccount.Telephone
+	// return &viewAccountData
+}
+
 // 保存用户信息
 func (s *projectAccountService) Save(viewAccount viewmodels.ProjectAccount, id int) error {
 
@@ -93,3 +97,19 @@ func (s *projectAccountService) Save(viewAccount viewmodels.ProjectAccount, id i
 
 	return err
 }
+
+// 构造视图层models
+func makeProjectAccountVM(modelsAccount *models.CmProjectAccount) viewmodels.ProjectAccount {
+	viewAccountData := viewmodels.ProjectAccount{}
+	aesId, _ := comm.AesEncrypt(strconv.Itoa(modelsAccount.Id), conf.SignSecret)
+	aesProjectId, _ := comm.AesEncrypt(strconv.Itoa(modelsAccount.ProjectId), conf.SignSecret)
+	viewAccountData.Id = aesId
+	viewAccountData.ProjectId = aesProjectId
+	viewAccountData.Account = modelsAccount.Account
+	viewAccountData.Name = modelsAccount.Name
+	viewAccountData.Company = modelsAccount.Company
+	viewAccountData.Role = modelsAccount.Role
+	viewAccountData.Mobile = modelsAccount.Mobile
+	viewAccountData.Telephone = modelsAccount.Telephone
+	return viewAccountData
+}

+ 3 - 3
services/project_service.go

@@ -92,7 +92,7 @@ func (s *projectService) Get(projectVM *viewmodels.Project) {
 	s.dao.Get(&projectCM)
 	if projectCM.Id != 0 {
 		// 项目信息补充完整
-		projectB := makeProjectVM(projectCM)
+		projectB := makeProjectVM(&projectCM)
 		projectVM = &projectB
 	} else {
 		projectVM.Id = "0"
@@ -108,7 +108,7 @@ func (s *projectService) GetList(projectVM viewmodels.Project) []viewmodels.Proj
 
 		projectData := s.dao.GetListByCode(projectVM.Code)
 		for _, data := range projectData {
-			projectVM = makeProjectVM(data)
+			projectVM = makeProjectVM(&data)
 			datalist = append(datalist, projectVM)
 		}
 		return datalist
@@ -117,7 +117,7 @@ func (s *projectService) GetList(projectVM viewmodels.Project) []viewmodels.Proj
 }
 
 // 构造视图层models
-func makeProjectVM(projectCM models.CmProject) viewmodels.Project {
+func makeProjectVM(projectCM *models.CmProject) viewmodels.Project {
 	projectVM := viewmodels.Project{}
 	id, _ := comm.AesEncrypt(strconv.Itoa(projectCM.Id), conf.SignSecret)
 	projectVM.Id = id

+ 39 - 11
services/tree_service.go

@@ -32,6 +32,7 @@ type TreeService interface {
 	Rename(viewmodels.Tree) error
 	GetFolderAndBid(string) ([]models.CmTree, error)
 	DeleteFolderAndBid(string) error
+	Move(int, int) error
 }
 
 //返回service操作类
@@ -137,27 +138,35 @@ func (s *treeService) Create(data viewmodels.Tree) error {
 	}
 
 	// 获得该深度的文件夹最大序号
-	datalist := s.dao.GetAllDepth(data.Depth, ProjectId)
-	maxIndex := len(datalist)
 	serial := 0
-	if maxIndex != 0 {
-		serial = datalist[maxIndex-1].Serial + 1
-	}
-
-	// 设置归属
-	if data.Depth != 0 {
+	depth := data.Depth + 1
+	if data.Depth == -1 {
+		// 1-创建顶级目录--获得最大序号
+		datalist := s.dao.GetAllDepth(depth, ProjectId)
+		maxIndex := len(datalist)
+		if maxIndex != 0 {
+			serial = datalist[maxIndex-1].Serial + 1
+		}
+	} else {
+		// 1-创建次目录-获得次级目录最大序号
 		treeNode := s.dao.Get(IdInt)
 		if treeNode.Id == 0 {
 			return errors.New("上级目录不正确")
 		}
-
-		folder.Attribution = treeNode.Attribution + strconv.Itoa(treeNode.Serial) + "-"
+		attribution := fmt.Sprintf("%s%d-", treeNode.Attribution, treeNode.Serial)
+		datalist := s.dao.GetALLDepthByAttribution(data.Depth, ProjectId, attribution)
+		maxIndex := len(datalist)
+		if maxIndex != 0 {
+			serial = datalist[maxIndex-1].Serial + 1
+		}
+		// 设置归属
+		folder.Attribution = attribution //treeNode.Attribution + strconv.Itoa(treeNode.Serial) + "-"
 		folder.ParentId = IdInt
 	}
 
 	folder.ProjectId = ProjectId
 	folder.Serial = serial
-	folder.Depth = data.Depth
+	folder.Depth = depth
 	folder.Isfolder = 1
 	folder.CreateTime = time.Now()
 	folder.UpdateTime = time.Now()
@@ -233,6 +242,25 @@ func (s *treeService) DeleteFolderAndBid(id string) error {
 	return nil
 }
 
+// 移动目录
+func (s *treeService) Move(id int, moveId int) error {
+	// 获得项目目录
+	treeNode := s.dao.Get(id)
+	if treeNode.Id == 0 {
+		return errors.New("目录或标段解析错误")
+	}
+	moveFolder := s.dao.Get(moveId)
+	if moveFolder.Id == 0 {
+		return errors.New("目录解析错误")
+	}
+
+	err := s.dao.Move(treeNode, moveFolder)
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
 // 创建一颗树
 func maketree(Data []*viewmodels.Tree, node *viewmodels.Tree) { //参数为父节点,添加父节点的子节点指针切片
 	childs, _ := havechild(Data, node) //判断节点是否有子节点并返回

+ 37 - 0
web/api/tree_api.go

@@ -13,6 +13,7 @@ import (
 	"github.com/kataras/iris/v12"
 	"go.mod/services"
 	"go.mod/web/utils"
+	"go.mod/web/viewmodels"
 )
 
 type TreeApi struct {
@@ -146,5 +147,41 @@ func (c *TreeApi) Delete() {
 		"code": 0,
 		"msg":  "删除成功",
 	})
+}
+
+// 移动文件夹
+func (c *TreeApi) PostMove() {
+	// 获得目录ID和移动目录ID
+	ProjectAccountVM := viewmodels.ProjectAccount{}
+	err := c.Ctx.ReadJSON(&ProjectAccountVM)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+		return
+	}
+	id, err := utils.GetDecryptId(ProjectAccountVM.Id)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+		return
+	}
+	moveId, err := utils.GetDecryptId(ProjectAccountVM.MoveId)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+		return
+	}
+
+	// 移动目录
+	err = c.ServiceTree.Move(id, moveId)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{
+			"code": -1,
+			"msg":  fmt.Sprintf("%s", err),
+		})
+		return
+	}
+	c.Ctx.JSON(iris.Map{
+		"code": 0,
+		"msg":  "移动成功",
+	})
 
+	fmt.Println(ProjectAccountVM)
 }

+ 13 - 0
web/utils/utils.go

@@ -57,3 +57,16 @@ func GetProjectAccountId(ctx iris.Context) (int, error) {
 	}
 	return identityIdInt, nil
 }
+
+// 获得解密后的ID
+func GetDecryptId(id string) (int, error) {
+	id, err := comm.AesDecrypt(id, conf.SignSecret)
+	if err != nil {
+		return 0, errors.New("ID 解析错误")
+	}
+	idInt, err := strconv.Atoi(id)
+	if err != nil {
+		return 0, errors.New("ID 转换错误")
+	}
+	return idInt, nil
+}

+ 2 - 0
web/viewmodels/project_account.go

@@ -17,6 +17,8 @@ type ProjectAccount struct {
 	Role      string `form:"role"`
 	Mobile    string `form:"mobile"`
 	Telephone string `form:"telephone"`
+
+	MoveId string `form:"moveId"`
 	// LastLogin    int       `xorm:"comment('最后登录时间') INT(11)"`
 	// AccountGroup     int       `xorm:"comment('所属账号组') TINYINT(4)"`
 	// EnterpriseId     int       `xorm:"comment('企业id') INT(11)"`