From 530fed8ec225453572d57b15c200ab062c335457 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期三, 01 十一月 2023 19:20:21 +0800 Subject: [PATCH] 公海member_id使用0 --- service/serviceContract.go | 75 ++++++++++++++++++++++++++++++------- 1 files changed, 60 insertions(+), 15 deletions(-) diff --git a/service/serviceContract.go b/service/serviceContract.go index 6da4367..c4fed1c 100644 --- a/service/serviceContract.go +++ b/service/serviceContract.go @@ -1,10 +1,11 @@ package service import ( - "aps_crm/constvar" "aps_crm/model" + "aps_crm/model/request" "aps_crm/pkg/ecode" "github.com/shopspring/decimal" + "gorm.io/gorm" ) type SContractService struct{} @@ -32,28 +33,59 @@ func (SContractService) UpdateServiceContract(serviceContract *model.ServiceContract) int { // check serviceContract exist - old, err := model.NewServiceContractSearch().SetId(serviceContract.Id).First() + old, err := model.NewServiceContractSearch().SetId(serviceContract.Id).SetPreload(true).First() if err != nil { return ecode.SContractNotExist } var totalAmount decimal.Decimal totalAmount = serviceContract.AmountTotal newProducts, removedProducts := NewProductsService().PickDiffProducts(serviceContract.Products, old.Products) - for _, product := range newProducts { + for _, product := range serviceContract.Products { totalAmount = totalAmount.Add(product.Amount.Mul(product.Price)) - } - removedProductIds := make([]uint, 0, len(removedProducts)) - for _, product := range removedProducts { - totalAmount = totalAmount.Sub(product.Amount.Mul(product.Price)) - removedProductIds = append(removedProductIds, product.Id) } serviceContract.AmountTotal = totalAmount.Round(2) serviceContract.AmountReceivable = serviceContract.AmountTotal.Sub(serviceContract.AmountReceived) serviceContract.AmountUnInvoiced = serviceContract.AmountTotal.Sub(serviceContract.AmountInvoiced) + err = model.WithTransaction(func(db *gorm.DB) error { + err := model.NewServiceContractSearch().SetId(serviceContract.Id).Update(serviceContract) + if err != nil { + return err + } + if len(removedProducts) > 0 { + removedProductIds := make([]uint, 0, len(removedProducts)) + for _, product := range removedProducts { + removedProductIds = append(removedProductIds, product.Id) + } + err = model.NewProductSearch(db).SetIds(removedProductIds).Delete() + if err != nil { + return err + } + } + if len(newProducts) > 0 { + for _, p := range newProducts { + p.Id = 0 + } + err = model.NewProductSearch(db).CreateBatch(newProducts) + if err != nil { + return err + } + var rel []*model.ServiceContractProduct + for _, p := range newProducts { + rel = append(rel, &model.ServiceContractProduct{ + ServiceContractId: serviceContract.Id, + ProductId: p.Id, + }) + } + err = model.NewServiceContractProductSearch().CreateBatch(rel) + if err != nil { + return err + } + } + return nil + }) - err = model.NewServiceContractSearch().SetId(serviceContract.Id).Update(serviceContract) if err != nil { - return ecode.SContractSetErr + return ecode.DBErr } return ecode.OK @@ -77,13 +109,26 @@ return ecode.OK } -func (SContractService) GetServiceContractList(page, pageSize int, queryClass constvar.ServiceContractQueryClass, keywordType constvar.ServiceContractKeywordType, keyword string) ([]*model.ServiceContract, int64, int) { +func (SContractService) BatchDeleteServiceContract(ids []int) int { + err := model.NewServiceContractSearch().DeleteByIds(ids) + if err != nil { + return ecode.DBErr + } + return ecode.OK +} + +func (SContractService) GetServiceContractList(params request.GetServiceContractList, memberIds []int) ([]*model.ServiceContract, int64, int) { // get contact list contacts, total, err := model.NewServiceContractSearch(). - SetKeyword(keyword). - SetKeywordType(keywordType). - SetQueryClass(queryClass). - SetPage(page, pageSize). + SetKeyword(params.Keyword). + SetKeywordType(params.KeywordType). + SetQueryClass(params.QueryClass). + SetPage(params.Page, params.PageSize). + SetSalesDetailsId(params.SalesDetailsId). + SetQuotationId(params.QuotationId). + SetSaleChanceId(params.SaleChanceId). + SetContactId(params.ContactId). + SetMemberIds(memberIds). SetPreload(true). Find() if err != nil { -- Gitblit v1.8.0