liujiandao
2023-10-13 5fa1de02759b9646e8987312736699755990e960
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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 (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))
    }
    salesReturn.AmountShouldRefund = salesReturn.AmountTotal
    err := model.NewSalesReturnSearch().Create(salesReturn)
    if err != nil {
        return ecode.SalesReturnExist
    }
 
    return ecode.OK
}
 
func (slf SalesReturnService) DeleteSalesReturn(id int) int {
    _, err := model.NewSalesReturnSearch().SetId(id).First()
    if err != nil {
        return ecode.SalesReturnNotExist
    }
    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).First()
    if err != nil {
        return ecode.SalesReturnNotExist
    }
 
    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.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(params request.GetSalesReturnList) ([]*model.SalesReturn, int64, int) {
    // get contact list
    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).
        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
}