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
package service
 
import (
    "aps_crm/model"
    "aps_crm/pkg/ecode"
    "gorm.io/gorm"
)
 
type CountryService struct{}
 
func (CountryService) AddCountry(country *model.Country) int {
    err := model.NewCountrySearch(nil).Create(country)
    if err != nil {
        return ecode.CountryExist
    }
    return ecode.OK
}
 
func (CountryService) DeleteCountry(id int) int {
    _, err := model.NewCountrySearch(nil).SetId(id).First()
    if err == gorm.ErrRecordNotFound {
        return ecode.CountryNotExist
    }
 
    err = model.NewCountrySearch(nil).SetId(id).Delete()
    if err != nil {
        return ecode.CountryNotExist
    }
    return ecode.OK
}
 
func (CountryService) UpdateCountry(country *model.Country) int {
    _, err := model.NewCountrySearch(nil).SetId(country.Id).First()
    if err == gorm.ErrRecordNotFound {
        return ecode.CountryNotExist
    }
 
    err = model.NewCountrySearch(nil).SetId(country.Id).Update(country)
    if err != nil {
        return ecode.CountryNotExist
    }
    return ecode.OK
}
 
func (CountryService) GetCountryList() ([]*model.Country, int) {
    list, err := model.NewCountrySearch(nil).FindAll()
    if err != nil {
        return nil, ecode.CountryGetListErr
    }
 
    return list, ecode.OK
}