/* * @description: 标段相关数据操作相关 * @Author: CP * @Date: 2020-09-28 10:31:31 * @FilePath: \construction_management\services\bidsection_service.go */ package services import ( "errors" "fmt" "log" "strconv" "time" "github.com/kataras/iris/v12" "go.mod/dao" "go.mod/datasource" "go.mod/models" "go.mod/web/utils" "go.mod/web/viewmodels" ) //定义标段Service接口 type BidsectionService interface { ValidRule(iris.Context) (viewmodels.Bidsection, error) Create(viewmodels.Bidsection) error } //返回service操作类 type bidsectionService struct { dao *dao.BidsectionDao treeDao *dao.TreeDao } //创建标段service func NewBidsectionService() BidsectionService { return &bidsectionService{ dao: dao.NewBidsectionDao(datasource.InstanceDbMaster()), treeDao: dao.NewTreeDao(datasource.InstanceDbMaster()), } } // 文件夹规则验证 func (s *bidsectionService) ValidRule(ctx iris.Context) (viewmodels.Bidsection, error) { bidsectionVaild := viewmodels.Bidsection{} err := ctx.ReadJSON(&bidsectionVaild) if err != nil { log.Println("folder-ValidRule-ReadForm转换异常, error=", err) return bidsectionVaild, err } err = bidsectionVaild.Validate() if err != nil { log.Println("标段验证, error=", err) return bidsectionVaild, err } return bidsectionVaild, nil } // 新增一个标段 func (s *bidsectionService) Create(data viewmodels.Bidsection) error { // 类型校验 folder := models.CmTree{} folder.Name = data.Name ProjectId, err := strconv.Atoi(data.ProjectId) if err != nil { return err } // 获得该目录ID Id, err := utils.GetDecryptId(data.FolderId) if err != nil { return err } // 该目录中是否有目录 -判断是否是叶子节点--TODO folderList := s.treeDao.GetChildFolder(Id) if len(folderList) > 0 { return errors.New("该目录中存在其他目录,不能新增标段") } // 创建在树中的标段 // 获得该深度的文件夹最大序号 serial := 0 treeNode := s.treeDao.Get(Id, ProjectId) if treeNode.Id == 0 { return errors.New("上级目录不正确") } depth := treeNode.Depth + 1 attribution := fmt.Sprintf("%s%d-", treeNode.Attribution, treeNode.Serial) datalist := s.treeDao.GetALLDepthByAttribution(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 = Id folder.ProjectId = ProjectId folder.Serial = serial folder.Depth = depth folder.Isfolder = 0 folder.CreateTime = time.Now() folder.UpdateTime = time.Now() // 创建标段和标段在树中的联系 err = s.dao.Create(&folder) if err != nil { return err } return nil }