zhangqian
2023-10-16 b8bb13639cc0dafff4c4b29131dfac33be07a1cb
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
package model
 
import (
    "aps_crm/pkg/mysqlx"
    "aps_crm/proto/code"
    "fmt"
    "gorm.io/gorm"
    "strconv"
    "time"
)
 
// WithTransaction : var funcs []func(db *gorm.DB) error,把相关函数添加进去
func WithTransaction(fns ...func(*gorm.DB) error) (err error) {
    tx := mysqlx.GetDB().Begin()
    defer func() {
        if r := recover(); r != nil {
            tx.Rollback()
            err = fmt.Errorf("%v", err)
        }
    }()
 
    for _, fn := range fns {
        err = fn(tx)
        if err != nil {
            tx.Rollback()
            return
        }
    }
    err = tx.Commit().Error
    return
}
 
func GetAutoCode(id int, codeStandard *code.CodeStandard) string {
    autoCode := ""
    var prefixValue string
    if codeStandard.AutoRule.PrefixMethod == 2 { // 来源单据
        prefixValue = ""
    } else { // 固定值
        prefixValue = codeStandard.AutoRule.PrefixValue
    }
    strMaxAutoIncr := strconv.Itoa(id)
    count := int(codeStandard.AutoRule.AutoLength) - len(strMaxAutoIncr)
    for i := 0; i < count; i++ {
        strMaxAutoIncr = "0" + strMaxAutoIncr
    }
 
    var suffixValue string
    if codeStandard.AutoRule.SuffixMethod == 2 { // 年月日+自增长
        suffixValue = fmt.Sprintf("%v%v", time.Now().Format("20060102"), strMaxAutoIncr)
    } else { // 自增长
        suffixValue = strMaxAutoIncr
    }
 
    autoCode = prefixValue + suffixValue
    return autoCode
}