package service import ( "aps_crm/constvar" "aps_crm/model" "aps_crm/pkg/ecode" ) type SalesRefundService struct{} func (SalesRefundService) AddSalesRefund(salesRefund *model.SalesRefund) int { salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SourceId).Find() if err != nil { return ecode.SalesReturnNotExist } if code := CheckProduct(salesReturnRecord.Products, salesRefund.Products); code != ecode.OK { return code } err = model.NewSalesRefundSearch().Create(salesRefund) if err != nil { return ecode.SalesRefundExist } return ecode.OK } func CheckProduct(returnProducts, refundProducts []*model.Product) int { returnProductsMap := make(map[uint]*model.Product, len(returnProducts)) for _, product := range returnProducts { returnProductsMap[product.Id] = product } for _, product := range refundProducts { if returnProductsMap[product.Id] == nil { return ecode.SalesRefundProductNotExist } if returnProductsMap[product.Id].Price != product.Price || product.Amount.GreaterThan(returnProductsMap[product.Id].Amount) { return ecode.SalesRefundProductPriceOrAmountErr } } return ecode.OK } func (SalesRefundService) DeleteSalesRefund(id int) int { _, err := model.NewSalesRefundSearch().SetId(id).Find() if err != nil { return ecode.SalesRefundNotExist } err = model.NewSalesRefundSearch().SetId(id).Delete() if err != nil { return ecode.SalesRefundNotExist } return ecode.OK } func (SalesRefundService) UpdateSalesRefund(salesRefund *model.SalesRefund) int { // check salesRefund exist _, err := model.NewSalesRefundSearch().SetId(salesRefund.Id).Find() if err != nil { return ecode.SalesRefundNotExist } salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SourceId).Find() if err != nil { return ecode.SalesReturnNotExist } if code := CheckProduct(salesReturnRecord.Products, salesRefund.Products); code != ecode.OK { return code } err = model.NewSalesRefundSearch().SetId(salesRefund.Id).Update(salesRefund) if err != nil { return ecode.SalesRefundSetErr } return ecode.OK } func (SalesRefundService) GetSalesRefundList(page, pageSize int, keywordType constvar.SalesRefundKeywordType, keyword string) ([]*model.SalesRefund, int64, int) { // get contact list contacts, total, err := model.NewSalesRefundSearch(). SetKeywordType(keywordType). SetKeyword(keyword). SetPreload(true). SetPage(page, pageSize).FindAll() if err != nil { return nil, 0, ecode.SalesRefundListErr } return contacts, total, ecode.OK }