| | |
| | | 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 SalesReturnService struct{} |
| | | |
| | | func (SalesReturnService) AddSalesReturn(salesReturn *model.SalesReturn) int { |
| | | func (slf SalesReturnService) AddSalesReturn(salesReturn *model.SalesReturn) int { |
| | | salesReturn.AmountShouldRefund = decimal.Zero |
| | | salesReturn.AmountHasRefund = decimal.Zero |
| | | salesReturn.AmountTotal = decimal.Zero |
| | | for _, product := range salesReturn.Products { |
| | | salesReturn.AmountTotal = salesReturn.AmountTotal.Add(product.Amount.Mul(product.Price)) |
| | | product.Id = 0 |
| | | } |
| | | salesReturn.AmountShouldRefund = salesReturn.AmountTotal |
| | | err := model.NewSalesReturnSearch().Create(salesReturn) |
| | | if err != nil { |
| | | return ecode.SalesReturnExist |
| | |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (SalesReturnService) DeleteSalesReturn(id int) int { |
| | | _, err := model.NewSalesReturnSearch().SetId(id).Find() |
| | | func (slf SalesReturnService) DeleteSalesReturn(id int) int { |
| | | _, err := model.NewSalesReturnSearch().SetId(id).First() |
| | | if err != nil { |
| | | return ecode.SalesReturnNotExist |
| | | } |
| | | |
| | | err = model.NewSalesReturnSearch().SetId(id).Delete() |
| | | err = model.WithTransaction(func(db *gorm.DB) error { |
| | | err = model.NewSalesReturnSearch().SetOrm(db).SetId(id).Delete() |
| | | if err != nil { |
| | | return err |
| | | } |
| | | err = model.NewSalesRefundSearch().SetOrm(db).SetSourceType(constvar.RefundSourceTypeSalesReturn).SetSourceId(id).Delete() |
| | | if err != nil { |
| | | return err |
| | | } |
| | | return nil |
| | | }) |
| | | if err != nil { |
| | | return ecode.SalesReturnNotExist |
| | | } |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (slf SalesReturnService) BatchDeleteSalesReturn(ids []int) (failIds []int, code int) { |
| | | for _, id := range ids { |
| | | code = slf.DeleteSalesReturn(id) |
| | | if code != ecode.OK { |
| | | failIds = append(failIds, id) |
| | | } |
| | | } |
| | | return failIds, code |
| | | } |
| | | |
| | | func (SalesReturnService) UpdateSalesReturn(salesReturn *model.SalesReturn) int { |
| | | // check salesReturn exist |
| | | _, err := model.NewSalesReturnSearch().SetId(salesReturn.Id).Find() |
| | | _, err := model.NewSalesReturnSearch().SetId(salesReturn.Id).First() |
| | | if err != nil { |
| | | return ecode.SalesReturnNotExist |
| | | } |
| | | |
| | | err = model.NewSalesReturnSearch().SetId(salesReturn.Id).Update(salesReturn) |
| | | salesReturn.AmountShouldRefund = decimal.Zero |
| | | salesReturn.AmountHasRefund = decimal.Zero |
| | | salesReturn.AmountTotal = decimal.Zero |
| | | for _, product := range salesReturn.Products { |
| | | salesReturn.AmountTotal = salesReturn.AmountTotal.Add(product.Amount.Mul(product.Price)) |
| | | } |
| | | salesReturn.AmountShouldRefund = salesReturn.AmountTotal |
| | | |
| | | old, err := model.NewSalesReturnSearch().SetId(salesReturn.Id).SetPreload(true).First() |
| | | if err != nil { |
| | | return ecode.SalesReturnSetErr |
| | | return ecode.DBErr |
| | | } |
| | | newProducts, removedProducts := NewProductsService().PickDiffProducts(salesReturn.Products, old.Products) |
| | | err = model.WithTransaction(func(db *gorm.DB) error { |
| | | err := model.NewSalesReturnSearch().SetId(salesReturn.Id).Update(salesReturn) |
| | | 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.SalesReturnProduct |
| | | for _, p := range newProducts { |
| | | rel = append(rel, &model.SalesReturnProduct{ |
| | | SalesReturnId: salesReturn.Id, |
| | | ProductId: p.Id, |
| | | }) |
| | | } |
| | | err = model.NewSalesReturnProductSearch().CreateBatch(rel) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | } |
| | | return nil |
| | | }) |
| | | |
| | | if err != nil { |
| | | return ecode.DBErr |
| | | } |
| | | |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (SalesReturnService) GetSalesReturnList(page, pageSize int, keyword string) ([]*model.SalesReturn, int64, int) { |
| | | func (SalesReturnService) GetSalesReturnList(params request.GetSalesReturnList, memberIds []int) ([]*model.SalesReturn, int64, int) { |
| | | // get contact list |
| | | contacts, total, err := model.NewSalesReturnSearch().SetKeyword(keyword).SetPage(page, pageSize).FindAll() |
| | | contacts, total, err := model.NewSalesReturnSearch(). |
| | | SetKeywordType(params.KeywordType). |
| | | SetKeyword(params.Keyword). |
| | | SetPage(params.Page, params.PageSize). |
| | | SetSourceId(params.SourceId). |
| | | SetSourceType(params.SourceType). |
| | | SetClientId(params.ClientId). |
| | | SetPreload(true). |
| | | SetMemberIds(memberIds). |
| | | FindAll() |
| | | if err != nil { |
| | | return nil, 0, ecode.SalesReturnListErr |
| | | } |
| | | return contacts, total, ecode.OK |
| | | } |
| | | |
| | | func (SalesReturnService) GetSalesReturnListByIds(ids []int) ([]*model.SalesReturn, int) { |
| | | // get contact list |
| | | salesReturns, err := model.NewSalesReturnSearch(). |
| | | SetIds(ids). |
| | | Find() |
| | | if err != nil { |
| | | return nil, ecode.SalesReturnListErr |
| | | } |
| | | return salesReturns, ecode.OK |
| | | } |