qixiaoning
9 天以前 eac932eb827c93e2e998ac1210c3f5e548af0dbf
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
package models
 
type Dictionary struct {
    Id          string `gorm:"primary_key;column:id;" json:"id"`
    Value       string `gorm:"column:value" json:"value"`             //值
    Name        string `gorm:"column:name" json:"name"`               //名称
    Type        string `gorm:"column:type" json:"type"`               //类型
    Description string `gorm:"column:description" json:"description"` //描述
    Sort        int    `gorm:"column:sort;default:1" json:"sort"`
    ParentId    string `gorm:"column:parent_id;default:0" json:"parent_id"`
}
 
const (
    TYPE_VEHICLE_COLOR = "nVehicleColor" //车身颜色
    TYPE_VEHICLE_TYPE  = "nVehicleType"  //车辆类型
    TYPE_PLATE_TYPE    = "nPlateType"    //车牌类型
    TYPE_VEHICLE_BRAND = "nVehicleBrand" //车辆品牌
)
 
func (Dictionary) TableName() string {
    return "dictionary"
}
 
func (dic *Dictionary) FindAll() (dics []Dictionary, err error) {
    if err := db.Table(dic.TableName()).Find(&dics).Error; err != nil {
        return nil, err
    }
    return dics, nil
}
 
func (dic *Dictionary) FindByType(dicType string) (dics []Dictionary, err error) {
    if err := db.Table(dic.TableName()).Where("type=?", dicType).Order("sort asc").Scan(&dics).Error; err != nil {
        return nil, err
    }
    return dics, nil
}
 
func (dic *Dictionary) FindByParentId(parentId string) (dics []Dictionary, err error) {
    if err := db.Table(dic.TableName()).Where("parent_id=?", parentId).Scan(&dics).Error; err != nil {
        return nil, err
    }
    return dics, nil
}
 
func (dic *Dictionary) Insert() (bool, error) {
    if err := db.Table(dic.TableName()).Create(&dic).Error; err != nil {
        return false, err
    }
    return true, nil
}
 
func (dic *Dictionary) Update() (bool, error) {
    if err := db.Table(dic.TableName()).Update(&dic).Error; err != nil {
        return false, err
    }
    return true, nil
}
 
func (dic *Dictionary) SelectById(id string) (bool, error) {
    result := db.Table(dic.TableName()).Where("id=?", id).First(&dic)
    if result.Error != nil {
        return false, err
    }
    return result.RecordNotFound(), nil
}