/* * @description: 合同数据相关操作 * @Author: CP * @Date: 2020-10-27 11:28:24 * @FilePath: \construction_management\services\contract_service.go */ package services import ( "errors" "fmt" "log" "strconv" "time" "github.com/kataras/iris/v12" "go.mod/comm" "go.mod/conf" "go.mod/dao" "go.mod/datasource" "go.mod/lib" "go.mod/models" "go.mod/web/utils" "go.mod/web/viewmodels" ) //定义项目用户Service接口 type ContractService interface { ValidRuleDepth(ctx iris.Context) (*viewmodels.TreeSectionContract, error) ValidRuleTemplate(ctx iris.Context) (*viewmodels.TreeSectionContract, error) // Create(viewBidAccount viewmodels.BidAccount, projectId int) error // Delete(viewBidAccount viewmodels.BidAccount, projectId int) error GetAll() GetSeciontTree(bidsectionId int, projectId int) *viewmodels.TreeSectionContract SetSection(templateNumber int, bidsectionId int, projectIdInt int) error MoveDepth(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error MoveSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error } //返回service操作类 type contractService struct { treeContractDao *dao.TreeContractDao // dao *dao.ProjectAccountDao // projectDao *dao.ProjectDao // bidsectionDao *dao.BidsectionDao // bidAccountDao *dao.BidAccountDao } //创建项目用户service func NewContractService() ContractService { return &contractService{ treeContractDao: dao.NewTreeContractDao(datasource.InstanceDbMaster()), // projectAccountDao: dao.NewProjectAccountDao(datasource.InstanceDbMaster()), // projectDao: dao.NewProjectDao(datasource.InstanceDbMaster()), // bidsectionDao: dao.NewBidsectionDao(datasource.InstanceDbMaster()), // bidAccountDao: dao.NewBidAccountDao(datasource.InstanceDbMaster()), } } // 升级降级规则验证 func (s *contractService) ValidRuleDepth(ctx iris.Context) (*viewmodels.TreeSectionContract, error) { treeSectionVaild := &viewmodels.TreeSectionContract{} err := ctx.ReadJSON(treeSectionVaild) if err != nil { log.Println("folder-ValidRule-ReadForm转换异常, error=", err) return treeSectionVaild, err } err = treeSectionVaild.ValidateDepth() if err != nil { log.Println("参数验证错误, error=", err) return treeSectionVaild, err } return treeSectionVaild, nil } // 模板规则验证 func (s *contractService) ValidRuleTemplate(ctx iris.Context) (*viewmodels.TreeSectionContract, error) { treeSectionVaild := &viewmodels.TreeSectionContract{} err := ctx.ReadJSON(treeSectionVaild) if err != nil { log.Println("folder-ValidRule-ReadForm转换异常, error=", err) return treeSectionVaild, err } err = treeSectionVaild.ValidateTemplate() if err != nil { log.Println("参数验证错误, error=", err) return treeSectionVaild, err } return treeSectionVaild, nil } // 获得合同标段内容 func (s *contractService) GetAll() { } // 获得合同项目节 func (s *contractService) GetSeciontTree(bidsectionId int, projectId int) *viewmodels.TreeSectionContract { sectionList := make([]*viewmodels.TreeSectionContract, 0) dataList := s.treeContractDao.GetAll(bidsectionId, projectId) // 生成根 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 sectionList = append(sectionList, sectionRoot) for _, data := range dataList { section := &viewmodels.TreeSectionContract{} id, _ := comm.AesEncrypt(strconv.Itoa(data.TreeId), conf.SignSecret) parentId, _ := comm.AesEncrypt(strconv.Itoa(data.ParentId), conf.SignSecret) projectId, _ := comm.AesEncrypt(strconv.Itoa(data.ProjectId), 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.ProjectId = projectId section.ContractId = contractId section.Name = data.Name section.ContractCode = data.ContractCode section.ContractPrice = data.ContractPrice section.ContractReturned = data.ContractReturned section.ContractsPaid = data.ContractsPaid section.ContractStatus = data.ContractStatus section.CreateTime = data.CreateTime.Format(conf.SysTimeform) sectionList = append(sectionList, section) } Node := sectionRoot //父节点 comm.MakeSectionContract(sectionList, Node) return Node } // 设置合同项目节初始数据-根据模板导入 func (s *contractService) SetSection(templateNumber int, bidsectionId int, projectId int) error { // 获得模板数据 templateTree := make([]*lib.ItemSectionTemplateTree, 0) if templateNumber == 1 { templateTree = lib.NewItemSectionData().TemplateList1 } 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.ProjectId = projectId section.BidsectionId = bidsectionId 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.CreateTime = time.Now() section.ContractPrice = "0" section.ContractReturned = "0" section.ContractsPaid = "0" sectionTreeList = append(sectionTreeList, section) } err := s.treeContractDao.CreateAll(sectionTreeList) if err != nil { log.Println("设置合同项目节模板错误 err=", err) return errors.New("设置合同项目节模板错误") } return nil } // 项目节的层级移动 func (s *contractService) MoveDepth(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error { // 1.验证项目节ID treeId, err := utils.GetDecryptId(sectionData.Id) if err != nil { return err } section := s.treeContractDao.Get(treeId, bidsectionId, projectId) if section.Id == 0 { return errors.New("未找到合同项目节") } // 2.层级降级-同级有前一个兄弟节点 elderBrother := &models.CmTreeContracts{} if sectionData.Operation == "downDepth" { // 获得前一个兄弟节点 elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId) if len(elderBrotherList) == 0 { return errors.New("项目节不能降级") } elderBrother = &elderBrotherList[0] } else if sectionData.Operation == "upDepth" { // 3.层级升级-只要有父亲都能升级 // 获得父亲节点 if section.ParentId == 0 { return errors.New("项目节不能升级") } } else { return errors.New("参数错误") } // 4.执行升降级 err = s.treeContractDao.MoveDepth(section, elderBrother, sectionData.Operation, bidsectionId, projectId) if err != nil { return err } return nil } // 项目节的排序移动 func (s *contractService) MoveSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error { // 1.验证项目节ID treeId, err := utils.GetDecryptId(sectionData.Id) if err != nil { return err } section := s.treeContractDao.Get(treeId, bidsectionId, projectId) if section.Id == 0 { return errors.New("未找到合同项目节") } // 2.下移 brother := &models.CmTreeContracts{} if sectionData.Operation == "downSerial" { // 获得下一个兄弟 youngerBrotherList := s.treeContractDao.GetYoungerBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId) 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, bidsectionId, projectId) if len(elderBrotherList) == 0 { return errors.New("项目节不能上移") } brother = &elderBrotherList[0] } else { return errors.New("参数错误") } // 4.执行升降级 err = s.treeContractDao.MoveSerial(section, brother, sectionData.Operation, bidsectionId, projectId) if err != nil { return err } return nil }