yinbentan
2024-06-28 1003bcc738159a9dd0dbc0934279ed94a1a72535
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
package service
 
import (
    "errors"
    "github.com/spf13/cast"
    "gorm.io/gorm"
    "strings"
    "time"
    "wms/constvar"
    "wms/models"
)
 
type SystemConfigService struct {
}
 
func NewSystemConfigService() *SystemConfigService {
    return &SystemConfigService{}
}
 
func (slf SystemConfigService) ParamsCheck(params models.SystemConfig) (err error) {
    var oldRecord *models.SystemConfig
    if params.Id != 0 {
        oldRecord, err = models.NewSystemConfigSearch().SetID(params.ID).First()
        if err == gorm.ErrRecordNotFound {
            return errors.New("配置不存在")
        }
    }
    if oldRecord == nil || params.ConfigType != oldRecord.ConfigType {
        _, err = models.NewSystemConfigSearch().SetConfigType(params.ConfigType).First()
        if err != gorm.ErrRecordNotFound {
            return errors.New("配置项重复")
        }
    }
 
    switch params.ConfigType {
    case constvar.SystemConfigTypeInventoryCutOffPoint:
        _, _, err = slf.CheckInventoryCutOffPoint(params.Val)
        if err != nil {
            return err
        }
    }
 
    return nil
}
 
func (slf SystemConfigService) CheckInventoryCutOffPoint(val string) (day int, timeStr string, err error) {
    if !strings.Contains(val, "-") {
        err = errors.New("库存结算时间点应该用分隔符'-'分隔日期和时间")
        return
    }
    arr := strings.Split(val, "-")
    if len(arr) != 2 {
        err = errors.New("库存结算时间点应该用分隔符'-'分隔日期和时间,且只能有一个分隔符")
        return
    }
    day = cast.ToInt(arr[0])
    if day < 0 || day > 28 {
        err = errors.New("库存结算时间点日期不正确,应在1~28之间")
        return
    }
    timeStr = arr[1]
    _, err = time.ParseInLocation("15:04", arr[1], time.Local)
    return
}
 
func (slf SystemConfigService) GetInventoryCutOffPoint() (day int, timeStr string, err error) {
    config, err := models.NewSystemConfigSearch().SetConfigType(constvar.SystemConfigTypeInventoryCutOffPoint).First()
    if err != nil {
        err = errors.New("库存结算时间点未配置")
        return
    }
    return slf.CheckInventoryCutOffPoint(config.Val)
}