/* * @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/dao" "go.mod/datasource" "go.mod/models" "go.mod/web/viewmodels" ) //定义项目用户Service接口 type ContractService interface { ValidRuleDepth(ctx iris.Context) (*viewmodels.TreeSectionContract, error) ValidRuleTemplate(ctx iris.Context) (*viewmodels.TreeSectionContract, error) ValidRuleSectionAdd(ctx iris.Context) (*viewmodels.TreeSectionContract, error) ValidRuleSectionDelete(ctx iris.Context) (*viewmodels.TreeSectionContract, error) ValidRuleGet(ctx iris.Context) (*viewmodels.TreeSectionContract, error) ValidRuleSerial(ctx iris.Context) (*viewmodels.TreeSectionContract, error) ValidRuleContractAdd(ctx iris.Context) (*viewmodels.Contracts, error) Get(treeId int, bidsectionId int, projectId int) *viewmodels.TreeSectionContract GetSectionTreeContract(attribution string, bidsectionId int, projectId int) []*viewmodels.Contracts GetSecionTree(bidsectionId int, projectId int) *viewmodels.TreeSectionContract SetSection(templateNumber int, bidsectionId int, projectIdInt int) error SectionAdd(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error SectionSave(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error UpdateSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error SectionDelete(treeId int, bidsectionId int, projectId int) error MoveDepth(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error MoveSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error GetContract(contractId int) *models.CmContracts Add(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error } //返回service操作类 type contractService struct { treeContractDao *dao.TreeContractDao contractDao *dao.ContractDao // dao *dao.ProjectAccountDao // projectDao *dao.ProjectDao // bidsectionDao *dao.BidsectionDao // bidAccountDao *dao.BidAccountDao } //创建项目用户service func NewContractService() ContractService { return &contractService{ treeContractDao: dao.NewTreeContractDao(datasource.InstanceDbMaster()), contractDao: dao.NewContractDao(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) ValidRuleSectionAdd(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.ValidateSectionAdd() if err != nil { log.Println("参数验证错误, error=", err) return treeSectionVaild, err } return treeSectionVaild, nil } // 模板规则新增项目节 func (s *contractService) ValidRuleSectionDelete(ctx iris.Context) (*viewmodels.TreeSectionContract, error) { treeSectionVaild := &viewmodels.TreeSectionContract{} err := ctx.ReadForm(treeSectionVaild) if err != nil { log.Println("folder-ValidRule-ReadForm转换异常, error=", err) return treeSectionVaild, err } err = treeSectionVaild.ValidateSectionDelete() if err != nil { log.Println("参数验证错误, error=", err) return treeSectionVaild, err } return treeSectionVaild, nil } func (s *contractService) ValidRuleGet(ctx iris.Context) (*viewmodels.TreeSectionContract, error) { treeSectionVaild := &viewmodels.TreeSectionContract{} err := ctx.ReadForm(treeSectionVaild) if err != nil { log.Println("folder-ValidRule-ReadForm转换异常, error=", err) return treeSectionVaild, err } err = treeSectionVaild.ValidateSectionDelete() if err != nil { log.Println("参数验证错误, error=", err) return treeSectionVaild, err } return treeSectionVaild, nil } // 验证序号相关 func (s *contractService) ValidRuleSerial(ctx iris.Context) (*viewmodels.TreeSectionContract, error) { treeSectionVaild := &viewmodels.TreeSectionContract{} err := ctx.ReadForm(treeSectionVaild) if err != nil { log.Println("folder-ValidRule-ReadForm转换异常, error=", err) return treeSectionVaild, err } err = treeSectionVaild.ValidateSectionSerial() if err != nil { log.Println("参数验证错误, error=", err) return treeSectionVaild, err } return treeSectionVaild, nil } // 新增合同参数验证 func (s *contractService) ValidRuleContractAdd(ctx iris.Context) (*viewmodels.Contracts, error) { // 创建一个存放前端传过来参数 contractsVaild := &viewmodels.Contracts{} // 存放raw的值,放入到contractsVaild err := ctx.ReadJSON(contractsVaild) if err != nil { log.Println("folder-ValidRule-ReadForm转换异常, error=", err) return contractsVaild, err } // 验证合同传参 err = contractsVaild.ValidateAdd() if err != nil { log.Println("参数验证错误, error=", err) return contractsVaild, err } return contractsVaild, nil } // 获得项目节 func (s *contractService) Get(treeId int, bidsectionId int, projectId int) *viewmodels.TreeSectionContract { // 1.获得项目节 section := s.treeContractDao.Get(treeId, bidsectionId, projectId) // 2.构造数据 sectionVM := s.makeSectionView(section) return sectionVM } // 获得合同详情 func (s *contractService) GetContract(contractId int) *models.CmContracts { return s.contractDao.Get(contractId) } // 新增合同 func (s *contractService) Add(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error { // 1. 项目节存在 contracts := s.treeContractDao.Get(treeId, bidsectionId, projectId) if contracts.Id == 0 { return errors.New("未找到项目节") } // k := int32(projectId) // 2.项目节是没有合同 if contracts.ContractId != 0 { return errors.New("该项目节上已经存在合同") } // 3.新增合同 --合计标段上的金额 contractsCm := &models.CmContracts{} contractsCm.Code = contractData.Code contractsCm.Name = contractData.Name contractsCm.ContractsType = contractData.ContractsType contractsCm.Price = contractData.Price contractsCm.Returned = "0" contractsCm.Paid = "0" contractsCm.TreeId = treeId contractsCm.ProjectId = projectId contractsCm.BidsectionId = bidsectionId contractsCm.Status = 0 contractsCm.CreateTime = time.Now() contractsCm.UpdateTime = time.Now() // 获得该标段下合同总数 - 总收入金额 contractList := s.treeContractDao.GetContract(bidsectionId, projectId) priceTotal := 0.00 for _, item := range contractList { contractPrice, _ := strconv.ParseFloat(item.ContractPrice, 64) priceTotal = priceTotal + contractPrice } // 合同总数 contractTotal := len(contractList) + 1 // 合同收入总金额 price, _ := strconv.ParseFloat(contractsCm.Price, 64) priceTotal = priceTotal + price // 保留2位小数 priceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", priceTotal), 64) err := s.contractDao.Add(contractsCm, contractTotal, priceTotal) if err != nil { return err } return nil }