package services import ( "errors" "fmt" "log" "strconv" "time" "go.mod/comm" "go.mod/conf" "go.mod/lib" "go.mod/models" "go.mod/web/utils" "go.mod/web/viewmodels" ) // 获得合同项目节 func (s *contractService) GetSecionTree(operation string) *viewmodels.TreeSectionContract { dataList := make([]models.CmTreeContracts, 0) if operation == "1" { dataList = s.treeContractDao.GetDepth() } else { dataList = s.treeContractDao.GetAll() } sectionList := s.makeSectionTreeView(dataList) // Node := sectionRoot //父节点 Node := sectionList[0] //父节点 comm.MakeSectionContract(sectionList, Node) return Node } func (s *contractService) GetSection(id int) (string, *viewmodels.TreeSectionDetail, *viewmodels.TreeSectionDetail) { name := s.treeContractDao.GetDetailName(id) pdfData := s.treeContractDao.GetDetailWithPdf(id) excelData := s.treeContractDao.GetDetailWithExcel(id) return name, pdfData, excelData } // 获得合同项目节-不包含合同 func (s *contractService) GetSecionTreeNotContract(bidsectionId int, projectId int, treeType int) *viewmodels.TreeSectionContract { dataList := s.treeContractDao.GetAllNotContract(bidsectionId, projectId, treeType) sectionList := s.makeSectionTreeView(dataList) // Node := sectionRoot //父节点 Node := sectionList[0] //父节点 comm.MakeSectionContract(sectionList, Node) return Node } // 获得项目节树和孩子们下的合同数据-未使用 func (s *contractService) GetSectionTreeContract(attribution string) []*viewmodels.Contracts { s.treeContractDao.GetAttribution(attribution) return nil } // 设置合同项目节初始数据-根据模板导入 func (s *contractService) SetSection() error { // 获得模板数据 templateTree := make([]*lib.ItemSectionTemplateTree, 0) templateTree = lib.NewItemSectionData().TemplateList1 // if templateNumber == 1 { // } else if templateNumber == 2 { // templateTree = lib.NewItemSectionData().TemplateList2 // } else { // return errors.New("找不到合同项目节模板") // } // 1.组合数据-写入库中 sectionTreeList := make([]*models.CmTreeContracts, 0) for _, item := range templateTree { section := &models.CmTreeContracts{} section.TreeId = item.Id section.ParentId = item.ParentId section.Name = item.Name section.Depth = item.Depth section.Serial = item.Serial section.Attribution = item.Attribution section.Code = fmt.Sprintf("%s%d", item.Attribution, item.Serial) section.Code2 = item.Code2 section.CreateTime = time.Now() // err := s.treeContractDao.Create(section) // if err != nil { // log.Println("设置合同项目节模板错误 err=", err) // } sectionTreeList = append(sectionTreeList, section) } err := s.treeContractDao.CreateAll(sectionTreeList) if err != nil { log.Println("设置合同项目节模板错误 err=", err) return errors.New("设置合同项目节模板错误") } return nil } // 新增项目节 func (s *contractService) ContractSectionAdd(sectionData *viewmodels.TreeSectionContract) (*models.CmTreeContracts, error) { // 1.验证项目节ID treeId, err := utils.GetDecryptId(sectionData.Id) if err != nil { return nil, err } sectionFather := s.treeContractDao.Get(treeId) if sectionFather.Id == 0 { return nil, errors.New("未找到项目节") } // 1-1 深度为>=2才能新增项目节 // if sectionFather.Depth < 3 { // return errors.New("请在项目节第三层开始编辑") // } // 1-2 项目节是否有合同 if sectionFather.ContractId != 0 { return nil, errors.New("合同项禁止添加子项") } // 2 获得最大序号 // 2-1 孩子节点 childrenList := s.treeContractDao.GetChildren(treeId) // 2-2.检查是否可以添加项目节 // 新建合同,项目层级必须在第二层以下,当第一层没有孩子可以添加合同 if sectionFather.Depth == 0 { if len(childrenList) != 0 { return nil, errors.New("该项目节不允许添加合同,请在下一层级新建合同") } } // 2-1 最大序号 serial := 1 if len(childrenList) != 0 { serial = childrenList[len(childrenList)-1].Serial + 1 } // 2-2 归属 attribution := fmt.Sprintf("%s%d-", sectionFather.Attribution, sectionFather.Serial) code := fmt.Sprintf("%s%d", attribution, serial) // 2-3获得新增ID lastId := s.treeContractDao.GetLastId() // 新增项目节 sectionCM := &models.CmTreeContracts{} sectionCM.Id = lastId.Id + 1 sectionCM.TreeId = lastId.Id + 1 sectionCM.ParentId = sectionFather.TreeId sectionCM.Name = sectionData.Name sectionCM.Depth = sectionFather.Depth + 1 sectionCM.Code2 = sectionData.Code2 sectionCM.Serial = serial sectionCM.Attribution = attribution sectionCM.Code = code data, err := s.treeContractDao.Create(sectionCM) if err != nil { return nil, err } return data, nil } // 新增项目节 func (s *contractService) SectionAdd(sectionData *viewmodels.TreeSectionContract) (*models.CmTreeContracts, error) { // 1.验证项目节ID treeId, err := utils.GetDecryptId(sectionData.Id) if err != nil { return nil, err } sectionFather := s.treeContractDao.Get(treeId) // if sectionFather.Id == 0 { // return nil, errors.New("未找到合同项目节") // } // 1-1 深度为>=2才能新增项目节 // if sectionFather.Depth < 3 { // return errors.New("请在项目节第三层开始编辑") // } // 1-2 项目节是否有合同 if sectionFather.ContractId != 0 { return nil, errors.New("合同项禁止添加子项") } // 2 获得最大序号 // 2-1 孩子节点 childrenList := s.treeContractDao.GetChildren(treeId) // 2-1 最大序号 serial := 1 if len(childrenList) != 0 { serial = childrenList[len(childrenList)-1].Serial + 1 } // 2-2 归属 attribution := fmt.Sprintf("%s%d-", sectionFather.Attribution, sectionFather.Serial) code := fmt.Sprintf("%s%d", attribution, serial) // 2-3获得新增ID lastId := s.treeContractDao.GetLastId() // 新增项目节 sectionCM := &models.CmTreeContracts{} sectionCM.Id = lastId.Id + 1 sectionCM.TreeId = lastId.Id + 1 sectionCM.ParentId = sectionFather.TreeId sectionCM.Name = sectionData.Name sectionCM.Depth = sectionFather.Depth + 1 sectionCM.Serial = serial sectionCM.Attribution = attribution sectionCM.CreateTime = time.Now() sectionCM.Code = code sectionCM.Code2 = sectionData.Code2 if sectionFather.Id == 0 { sectionCM.Depth = 0 sectionCM.Attribution = "" sectionCM.Code = fmt.Sprintf("%d", serial) } data, err := s.treeContractDao.Create(sectionCM) if err != nil { return nil, err } return data, nil } // 保存名称 func (s *contractService) SectionSave(sectionData *viewmodels.TreeSectionContract) error { // 1.验证项目节ID treeId, err := utils.GetDecryptId(sectionData.Id) if err != nil { return err } section := s.treeContractDao.Get(treeId) if section.Id == 0 { return errors.New("未找到合同项目节") } if section.Name == sectionData.Name && section.Code2 == sectionData.Code2 { return nil } // 1-1 深度为>=2才能新增项目节 // if section.Depth < 2 { // return errors.New("请在项目节第三层开始编辑") // } // 2.保存 sectionCM := &models.CmTreeContracts{} sectionCM.Name = sectionData.Name sectionCM.Code2 = sectionData.Code2 sectionCM.Id = section.Id fmt.Println("-------------------", sectionCM) err = s.treeContractDao.Save(sectionCM, []string{"Name", "Code2"}) if err != nil { return errors.New("保存失败") } return nil } // 更新序号 func (s *contractService) UpdateSerial(sectionData *viewmodels.TreeSectionContract) error { // 1.验证项目节ID treeId, err := utils.GetDecryptId(sectionData.Id) if err != nil { return err } section := s.treeContractDao.Get(treeId) if section.Id == 0 { return errors.New("未找到合同项目节") } if section.Serial == sectionData.Serial { return nil } // 1-1 深度为>=2才能新增项目节 // if section.Depth < 2 { // return errors.New("请在项目节第三层开始编辑") // } err = s.treeContractDao.UpdateSerial(section, sectionData.Serial) if err != nil { return errors.New("更新失败") } return nil } // 项目节删除 func (s *contractService) SectionDelete(treeId int) error { // 1.验证项目节ID section := s.treeContractDao.Get(treeId) if section.Id == 0 { return errors.New("未找到合同项目节") } // 1-1 深度为>=1才能新增项目节 if section.Depth < 1 { return errors.New("请在项目节第三层开始编辑") } // 1-2 有合同的不能编辑(包含孩子节点) contractList := s.treeContractDao.GetAttributionContract(section) if len(contractList) != 0 { return errors.New("该项目节存在合同") } err := s.treeContractDao.Delete(section) if err != nil { return err } return nil } // 项目节的层级移动 func (s *contractService) MoveDepth(sectionData *viewmodels.TreeSectionContract) error { // 1.验证项目节ID treeId, err := utils.GetDecryptId(sectionData.Id) if err != nil { return err } section := s.treeContractDao.Get(treeId) if section.Id == 0 { return errors.New("未找到合同项目节") } // 1-1 深度为>=2才能新增项目节 // if section.Depth < 2 { // return errors.New("请在项目节第三层开始编辑") // } // 1-2 有合同的不能编辑(包含孩子节点) // contractList := s.GetSectionTreeContract(section.Attribution, section.BidsectionId) // if len(contractList) != 0 { // return errors.New("该项目节已存在合同") // } // 2.层级降级-同级有前一个兄弟节点 elderBrother := &models.CmTreeContracts{} if sectionData.Operation == "downDepth" { // 获得前一个兄弟节点 elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId) if len(elderBrotherList) == 0 { return errors.New("项目节不能降级") } elderBrother = &elderBrotherList[0] // 前一个兄弟节点 不能有合同 if elderBrother.ContractId != 0 { return errors.New(elderBrother.Code + "已存在合同,不能降级") } } else if sectionData.Operation == "upDepth" { // 3.层级升级-只要有父亲都能升级 // 2-1 父节点深度为2不能升级 // if (section.Depth - 1) < 2 { // return errors.New("请在项目节第三层开始编辑") // } // 获得父亲节点 if section.ParentId == 0 { return errors.New("项目节不能升级") } } else { return errors.New("参数错误") } // 4.执行升降级 err = s.treeContractDao.MoveDepth(section, elderBrother, sectionData.Operation) if err != nil { return err } return nil } // 项目节的排序移动 func (s *contractService) MoveSerial(sectionData *viewmodels.TreeSectionContract) error { // 1.验证项目节ID treeId, err := utils.GetDecryptId(sectionData.Id) if err != nil { return err } section := s.treeContractDao.Get(treeId) if section.Id == 0 { return errors.New("未找到合同项目节") } // 1-1 深度为>=2才能新增项目节 // if section.Depth < 2 { // return errors.New("请在项目节第三层开始编辑") // } // 1-2 有合同的不能编辑(包含孩子节点) // contractList := s.GetSectionTreeContract(section.Attribution, section.BidsectionId, section.ProjectId, treeType) // if len(contractList) != 0 { // return errors.New("该项目节已存在合同") // } // 2.下移 brother := &models.CmTreeContracts{} if sectionData.Operation == "downSerial" { // 获得下一个兄弟 youngerBrotherList := s.treeContractDao.GetYoungerBrother(section.Serial, section.Depth, section.ParentId) if len(youngerBrotherList) == 0 { return errors.New("项目节不能下移") } brother = &youngerBrotherList[0] } else if sectionData.Operation == "upSerial" { // 获得上一个兄弟 elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId) if len(elderBrotherList) == 0 { return errors.New("项目节不能上移") } brother = &elderBrotherList[0] } else { return errors.New("参数错误") } // 4.执行升降级 err = s.treeContractDao.MoveSerial(section, brother, sectionData.Operation) if err != nil { return err } return nil } // 构造项目节树 func (s *contractService) makeSectionTreeView(dataList []models.CmTreeContracts) []*viewmodels.TreeSectionContract { sectionList := make([]*viewmodels.TreeSectionContract, 0) // 生成根 sectionRoot := &viewmodels.TreeSectionContract{} id, _ := comm.AesEncrypt(strconv.Itoa(0), conf.SignSecret) parentId, _ := comm.AesEncrypt(strconv.Itoa(-1), conf.SignSecret) sectionRoot.Id = id sectionRoot.Name = "root" sectionRoot.ParentId = parentId // sectionRoot.Children = make([]*viewmodels.TreeSectionContract, 0) sectionList = append(sectionList, sectionRoot) for _, data := range dataList { section := s.makeSectionView(&data) sectionList = append(sectionList, section) } return sectionList } // 构造一个项目节View func (s *contractService) makeSectionView(data *models.CmTreeContracts) *viewmodels.TreeSectionContract { section := &viewmodels.TreeSectionContract{} id, _ := comm.AesEncrypt(strconv.Itoa(data.TreeId), conf.SignSecret) parentId, _ := comm.AesEncrypt(strconv.Itoa(data.ParentId), conf.SignSecret) contractId, _ := comm.AesEncrypt(strconv.Itoa(data.ContractId), conf.SignSecret) section.Id = id section.Name = data.Name section.ParentId = parentId section.Depth = data.Depth + 1 section.Serial = data.Serial section.Attribution = data.Attribution section.Code = data.Code section.Code2 = data.Code2 section.ContractId = contractId // section.Children = make([]*viewmodels.TreeSectionContract, 0) section.ElderBrother = true section.IsEnd = false section.Name = data.Name section.CreateTime = data.CreateTime.Format(conf.SysTimeform) return section } // 更新项目节-合同id func (s *contractService) UpdateContractId(id int, contractId int) error { err := s.treeContractDao.UpdateContract(id, contractId) return err }