zhangqian
2023-10-13 13194e787d51e4ce07dfc35341d536fb5db7aaa3
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
package service
 
import (
    "aps_crm/model"
    "aps_crm/pkg/ecode"
    "aps_crm/pkg/mysqlx"
)
 
type QuotationService struct{}
 
func (QuotationService) AddQuotation(quotation *model.Quotation) int {
    err := model.NewQuotationSearch(nil).Create(quotation)
    if err != nil {
        return ecode.QuotationExist
    }
 
    return ecode.OK
}
 
func (QuotationService) UpdateQuotation(quotation *model.Quotation) int {
    // check quotation exist
    tmp, err := model.NewQuotationSearch(nil).SetId(quotation.Id).Find()
    if err != nil {
        return ecode.QuotationNotExist
    }
 
    if len(quotation.Products) == 0 {
        err = model.NewQuotationSearch(nil).SetId(quotation.Id).Update(quotation)
        if err != nil {
            return ecode.QuotationSetErr
        }
    } else {
        tx := mysqlx.GetDB().Begin()
        err = tx.Model(tmp).Association("Products").Clear()
        if err != nil {
            tx.Rollback()
            return ecode.QuotationSetErr
        }
 
        err = model.NewQuotationSearch(tx).SetId(quotation.Id).Update(quotation)
        if err != nil {
            tx.Rollback()
            return ecode.QuotationSetErr
        }
 
        for _, product := range quotation.Products {
            if product.Id == 0 {
                err = model.NewProductSearch(tx).Create(&product)
                if err != nil {
                    tx.Rollback()
                    return ecode.QuotationSetErr
                }
            } else {
                err = model.NewProductSearch(tx).SetId(product.Id).Update(&product)
                if err != nil {
                    tx.Rollback()
                    return ecode.QuotationSetErr
                }
            }
            err = tx.Model(&tmp).Association("Products").Append(&product)
            if err != nil {
                tx.Rollback()
                return ecode.QuotationSetErr
            }
        }
        tx.Commit()
    }
 
    return ecode.OK
}
 
func (QuotationService) GetQuotationList(page, pageSize int, data map[string]interface{}) ([]*model.Quotation, int64, int) {
    // get contact list
    contacts, total, err := model.NewQuotationSearch(nil).SetPage(page, pageSize).SetSearchMap(data).FindAll()
    if err != nil {
        return nil, 0, ecode.QuotationListErr
    }
    return contacts, total, ecode.OK
}
 
func (QuotationService) DeleteQuotation(ids []int) int {
    // delete client
    err := model.NewQuotationSearch(nil).SetIds(ids).Delete()
    if err != nil {
        return ecode.QuotationDeleteErr
    }
    return ecode.OK
}