作者 yangfu

fix: 工时管理修改,增加多渠道配置读取

POSTGRESQL_DB_NAME = allied_creation_dev
POSTGRESQL_HOST = 114.55.200.59
POSTGRESQL_PORT = 31543
POSTGRESQL_USER = postgres
POSTGRESQL_PASSWORD = eagle1010
DISABLE_SQL_GENERATE_COMMENT = false
SERVICE_ENV = test
HTTP_PORT = 8081
ENABLE_KAFKA_LOG11 = true
HTTPS_PORT = 8143
ALLIED_CREATION_USER_HOST = http://allied-creation-user-dev.fjmaimaimai.com
... ...
... ... @@ -18,6 +18,11 @@ import (
_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/beego"
)
//func init(){
// constant.Configurator = utils.BeegoAppConfigurator{}
// web.LoadAppConfig("ini","config/app.conf")
//}
func main() {
defer func() {
if r := recover(); r != nil {
... ...
... ... @@ -2,11 +2,10 @@ package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"reflect"
"strings"
"time"
"github.com/beego/beego/v2/core/validation"
)
type CreateAttendanceCommand struct {
... ... @@ -25,11 +24,11 @@ type CreateAttendanceCommand struct {
// 工段ID
SectionId int `cname:"工段ID" json:"sectionId" valid:"Required"`
// 生产日期
ProductDate time.Time `cname:"生产日期" json:"productDate,omitempty" valid:"Required"`
ProductDate string `cname:"生产日期" json:"productDate,omitempty" valid:"Required"`
// 签到
SignIn time.Time `cname:"上岗时间" json:"signIn,omitempty" valid:"Required"`
SignIn string `cname:"上岗时间" json:"signIn,omitempty" valid:"Required"`
// 签退
SignOut time.Time `cname:"下岗时间" json:"signOut,omitempty" valid:"Required"`
SignOut string `cname:"下岗时间" json:"signOut,omitempty" valid:"Required"`
// 考勤状态 1.未审核 2:审核
AttendanceStatus int `cname:"考勤状态" json:"attendanceStatus,omitempty"`
// 休息时长
... ... @@ -41,7 +40,14 @@ type CreateAttendanceCommand struct {
}
func (createAttendanceCommand *CreateAttendanceCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
if err := utils.ValidWorkTime(createAttendanceCommand.SignIn); err != nil {
validation.Error(err.Error())
return
}
if err := utils.ValidWorkTime(createAttendanceCommand.SignOut); err != nil {
validation.Error(err.Error())
return
}
validation.Range(createAttendanceCommand.AttendanceStatus, 1, 2, "attendanceStatus")
}
... ...
... ... @@ -109,6 +109,18 @@ func (attendanceService *AttendanceService) CreateAttendance(operateInfo *domain
if cmd.WorkTime-cmd.BreakTime > 0 {
workTime = cmd.WorkTime - cmd.BreakTime
}
signIn, err := xtime.ParseInLocation(time.Local, fmt.Sprintf("%v %v:00", cmd.ProductDate, cmd.SignIn))
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
signOut, err := xtime.ParseInLocation(time.Local, fmt.Sprintf("%v %v:00", cmd.ProductDate, cmd.SignOut))
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
productDate, err := xtime.ParseInLocation(time.Local, cmd.ProductDate)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
newAttendance := &domain.ProductAttendanceRecord{
//ProductAttendanceId: cmd.ProductAttendanceId,
CompanyId: operateInfo.CompanyId,
... ... @@ -116,8 +128,8 @@ func (attendanceService *AttendanceService) CreateAttendance(operateInfo *domain
AttendanceType: cmd.AttendanceType,
ProductWorker: user,
WorkStation: workStation,
SignIn: cmd.SignIn,
SignOut: cmd.SignOut,
SignIn: signIn,
SignOut: signOut,
AttendanceStatus: cmd.AttendanceStatus,
WorkTimeBefore: workTime,
WorkTimeAfter: 0,
... ... @@ -127,7 +139,7 @@ func (attendanceService *AttendanceService) CreateAttendance(operateInfo *domain
GroupName: productGroup.GroupName,
ProductGroupId: productGroup.ProductGroupId,
}),
ProductDate: xtime.New(cmd.ProductDate).BeginningOfDay(),
ProductDate: productDate,
}
if cmd.AttendanceStatus == domain.AttendanceApproved {
... ... @@ -142,12 +154,12 @@ func (attendanceService *AttendanceService) CreateAttendance(operateInfo *domain
attendanceRepository, _, _ = factory.FastPgAttendance(transactionContext, 0)
if !cmd.Prepared {
// 检查时间段内是否有重复的打卡记录
count, _, err := attendanceRecordDao.WorkerAttendanceRecordsByProductDate(operateInfo.CompanyId, operateInfo.OrgId, cmd.ProductWorkerId, newAttendance.ProductDate, cmd.SignIn, cmd.SignIn)
count, _, err := attendanceRecordDao.WorkerAttendanceRecordsByProductDate(operateInfo.CompanyId, operateInfo.OrgId, cmd.ProductWorkerId, newAttendance.ProductDate, newAttendance.SignIn, newAttendance.SignIn)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if count > 0 {
return nil, &application.ServiceError{Code: 10040001, Message: fmt.Sprintf("已存在员工%v的工时记录,是否继续新增", user.UserName)}
return nil, &application.ServiceError{Code: 10050001, Message: fmt.Sprintf("已存在员工%v的工时记录,是否继续新增", user.UserName)}
}
}
if attendance, err := attendanceRepository.Save(newAttendance); err != nil {
... ... @@ -157,7 +169,7 @@ func (attendanceService *AttendanceService) CreateAttendance(operateInfo *domain
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return attendance, nil
return struct{}{}, nil
}
}
... ...
... ... @@ -24,7 +24,7 @@ type CreateProductCalendarCommand struct {
// 上班班次 1:全天 2:白班 4:中班 8:夜班
WorkOn int `cname:"上班班次 1:全天 2:白班 4:中班 8:夜班" json:"workOn" valid:"Required"`
// 日历选择
CalendarSelected []int `cname:"日历选择" json:"calendarSelected" valid:"Required"`
CalendarSelected []int `cname:"日历选择" json:"calendarSelected" ` // valid:"Required"
// 上岗时间
InWorkAt string `cname:"上岗时间" json:"inWorkAt" valid:"Required"`
// 下岗时间
... ...
... ... @@ -21,8 +21,8 @@ type UpdateProductCalendarCommand struct {
SectionId int `cname:"工段ID" json:"sectionId" valid:"Required"`
// 上班班次 1:全天 2:白班 4:中班 8:夜班
WorkOn int `cname:"上班班次 1:全天 2:白班 4:中班 8:夜班" json:"workOn" valid:"Required"`
// 日历选择
CalendarSelected []int `cname:"日历选择" json:"calendarSelected" valid:"Required"`
// 日历选择 valid:"Required"
CalendarSelected []int `cname:"日历选择" json:"calendarSelected" `
// 上岗时间
InWorkAt string `cname:"上岗时间" json:"inWorkAt" valid:"Required"`
// 下岗时间
... ...
package query
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type GetProductGroupCalendarQuery struct {
// 生产班组ID
ProductGroupId int `cname:"工厂日历ID" json:"productGroupId" valid:"Required"`
}
func (getProductCalendarQuery *GetProductGroupCalendarQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (getProductCalendarQuery *GetProductGroupCalendarQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(getProductCalendarQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(getProductCalendarQuery).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -372,6 +372,43 @@ func (productCalendarService *ProductCalendarService) SearchProductCalendar(oper
return count, result, nil
}
// 返回生产班组的工厂日历
func (productCalendarService *ProductCalendarService) GetProductGroupCalendar(getProductCalendarQuery *query.GetProductGroupCalendarQuery) (interface{}, error) {
if err := getProductCalendarQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
productCalendarRepository, _, _ := factory.FastPgProductCalendar(transactionContext, 0)
_, productGroup, err := factory.FastPgProductGroup(transactionContext, getProductCalendarQuery.ProductGroupId)
if err != nil {
return nil, nil
}
_, productCalendar, err := productCalendarRepository.Find(map[string]interface{}{"workStationId": productGroup.WorkStation.WorkStationId, "workOn": productGroup.WorkOn, "limit": 1})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(productCalendar) == 0 {
return nil, nil
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
result := &dto.ProductCalendarDto{}
result.LoadDto(productCalendar[0], 0)
return result, nil
}
func NewProductCalendarService(options map[string]interface{}) *ProductCalendarService {
newProductCalendarService := &ProductCalendarService{}
return newProductCalendarService
... ...
... ... @@ -30,6 +30,12 @@ type SearchProductGroupQuery struct {
WorkshopName string `cname:"车间名称" json:"workshopName,omitempty"`
// 生产线名称
LineName string `cname:"生产线名称" json:"lineName,omitempty"`
// 车间ID
WorkshopId int `cname:"车间ID" json: workshopId,omitempty"`
// 生产线ID
LineId int `cname:"生产线ID" json:"lineId,omitempty"`
// 工段ID
SectionId int `json:"sectionId,omitempty"`
}
func (cmd *SearchProductGroupQuery) Valid(validation *validation.Validation) {
... ...
... ... @@ -578,9 +578,7 @@ func (productGroupService *ProductGroupService) SelectorProductGroup(operateInfo
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return map[string]interface{}{
"groups": results,
}, nil
return results, nil
}
func NewProductGroupService(options map[string]interface{}) *ProductGroupService {
... ...
... ... @@ -2,10 +2,11 @@ package constant
import (
"fmt"
"os"
"strconv"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
)
var Configurator utils.Configurator = utils.EnvConfigurator{}
var SERVICE_NAME = "allied-creation-manufacture"
var SERVICE_ENV = "dev"
var HTTP_PORT int = 8081
... ... @@ -35,46 +36,13 @@ const HeaderOrgId = "orgId"
const HeaderOrgIds = "orgIds"
func init() {
if os.Getenv("LOG_LEVEL") != "" {
LOG_LEVEL = os.Getenv("LOG_LEVEL")
}
//if os.Getenv("CUSTOMER_ACCOUNT") != "" {
// account := os.Getenv("CUSTOMER_ACCOUNT")
// accounts := strings.Split(account, CUSTOMER_ACCOUNT_DELIMITER)
// var tmpAccounts []int64
// for i := range accounts {
// v, err := strconv.ParseInt(accounts[i], 10, 64)
// if err != nil {
// panic(err)
// }
// tmpAccounts = append(tmpAccounts, v)
// }
// if len(tmpAccounts) > 0 {
// CUSTOMER_ACCOUNT = tmpAccounts
// }
//}
if os.Getenv("ALLIED_CREATION_BASIC_HOST") != "" {
ALLIED_CREATION_BASIC_HOST = os.Getenv("ALLIED_CREATION_BASIC_HOST")
}
if os.Getenv("ALLIED_CREATION_USER_HOST") != "" {
ALLIED_CREATION_USER_HOST = os.Getenv("ALLIED_CREATION_USER_HOST")
}
if os.Getenv("ALLIED_CREATION_COOPERATION_HOST") != "" {
ALLIED_CREATION_COOPERATION_HOST = os.Getenv("ALLIED_CREATION_COOPERATION_HOST")
}
//if os.Getenv("SMS_SERVE_HOST") != "" {
// SMS_SERVE_HOST = os.Getenv("SMS_SERVE_HOST")
//}
if os.Getenv("MMM_BYTE_BANK_HOST") != "" {
MMM_BYTE_BANK_HOST = os.Getenv("MMM_BYTE_BANK_HOST")
}
if os.Getenv("SERVICE_ENV") != "" {
SERVICE_ENV = os.Getenv("SERVICE_ENV")
}
if os.Getenv("HTTP_PORT") != "" {
HTTP_PORT, _ = strconv.Atoi(os.Getenv("HTTP_PORT"))
}
LOG_LEVEL = Configurator.DefaultString("LOG_LEVEL", LOG_LEVEL)
ALLIED_CREATION_BASIC_HOST = Configurator.DefaultString("ALLIED_CREATION_BASIC_HOST", ALLIED_CREATION_BASIC_HOST)
ALLIED_CREATION_USER_HOST = Configurator.DefaultString("ALLIED_CREATION_USER_HOST", ALLIED_CREATION_USER_HOST)
ALLIED_CREATION_COOPERATION_HOST = Configurator.DefaultString("ALLIED_CREATION_COOPERATION_HOST", ALLIED_CREATION_COOPERATION_HOST)
MMM_BYTE_BANK_HOST = Configurator.DefaultString("MMM_BYTE_BANK_HOST", MMM_BYTE_BANK_HOST)
SERVICE_ENV = Configurator.DefaultString("SERVICE_ENV", SERVICE_ENV)
HTTP_PORT = Configurator.DefaultInt("HTTP_PORT", HTTP_PORT)
SERVICE_NAME = fmt.Sprintf("%v-%v", SERVICE_NAME, SERVICE_ENV)
CACHE_PREFIX = SERVICE_NAME
}
... ...
package constant
import "os"
var (
// kafka 地址
KAFKA_HOST = "192.168.0.250:9092,192.168.0.251:9092,192.168.0.252:9092" //"106.75.231.90:9092"
... ... @@ -14,13 +12,6 @@ var (
)
func init() {
if os.Getenv("KAFKA_HOST") != "" {
KAFKA_HOST = os.Getenv("KAFKA_HOST")
}
//if os.Getenv("TOPIC_LOG_STASH") != "" {
// POSTGRESQL_PORT = os.Getenv("TOPIC_LOG_STASH")
//}
if os.Getenv("ENABLE_KAFKA_LOG") != "" {
ENABLE_KAFKA_LOG = true
}
KAFKA_HOST = Configurator.DefaultString("KAFKA_HOST", KAFKA_HOST)
ENABLE_KAFKA_LOG = Configurator.DefaultBool("ENABLE_KAFKA_LOG", ENABLE_KAFKA_LOG)
}
... ...
package constant
import (
"os"
"strconv"
"strings"
)
... ... @@ -20,18 +18,10 @@ var (
)
func init() {
if os.Getenv("MANUFACTURE_DEFAULT_COMPANYID") != "" {
MANUFACTURE_DEFAULT_COMPANYID, _ = strconv.Atoi(os.Getenv("MANUFACTURE_DEFAULT_COMPANYID"))
}
if os.Getenv("MANUFACTURE_DEFAULT_ORGID") != "" {
MANUFACTURE_DEFAULT_ORGID, _ = strconv.Atoi(os.Getenv("MANUFACTURE_DEFAULT_ORGID"))
}
if os.Getenv("MANUFACTURE_DEFAULT_WORKSHOPID") != "" {
MANUFACTURE_DEFAULT_WORKSHOPID, _ = strconv.Atoi(os.Getenv("MANUFACTURE_DEFAULT_WORKSHOPID"))
}
if os.Getenv("MANUFACTURE_PRODUCT_TYPE") != "" {
MANUFACTURE_PRODUCT_TYPE = os.Getenv("MANUFACTURE_PRODUCT_TYPE")
}
MANUFACTURE_DEFAULT_COMPANYID = Configurator.DefaultInt("MANUFACTURE_DEFAULT_COMPANYID", MANUFACTURE_DEFAULT_COMPANYID)
MANUFACTURE_DEFAULT_ORGID = Configurator.DefaultInt("MANUFACTURE_DEFAULT_ORGID", MANUFACTURE_DEFAULT_ORGID)
MANUFACTURE_DEFAULT_WORKSHOPID = Configurator.DefaultInt("MANUFACTURE_DEFAULT_WORKSHOPID", MANUFACTURE_DEFAULT_WORKSHOPID)
MANUFACTURE_PRODUCT_TYPE = Configurator.DefaultString("MANUFACTURE_PRODUCT_TYPE", MANUFACTURE_PRODUCT_TYPE)
productTypes := strings.Split(MANUFACTURE_PRODUCT_TYPE, ",")
for i, v := range productTypes {
MapProductType[i+1] = v
... ...
package constant
import "os"
var MQTT_TOPIC = "/MQTT"
//设备商提供的测试地址
... ... @@ -32,16 +30,8 @@ var MQTT_USER = "admin"
var MQTT_PASSWORD = "123456"
func init() {
if os.Getenv("MQTT_HOST") != "" {
MQTT_HOST = os.Getenv("MQTT_HOST")
}
if os.Getenv("MQTT_PORT") != "" {
MQTT_PORT = os.Getenv("MQTT_PORT")
}
if os.Getenv("MQTT_USER") != "" {
MQTT_USER = os.Getenv("MQTT_USER")
}
if os.Getenv("MQTT_PASSWORD") != "" {
MQTT_PASSWORD = os.Getenv("MQTT_PASSWORD")
}
MQTT_HOST = Configurator.DefaultString("MQTT_HOST", MQTT_HOST)
MQTT_PORT = Configurator.DefaultString("MQTT_PORT", MQTT_PORT)
MQTT_USER = Configurator.DefaultString("MQTT_USER", MQTT_USER)
MQTT_PASSWORD = Configurator.DefaultString("MQTT_PASSWORD", MQTT_PASSWORD)
}
... ...
... ... @@ -3,10 +3,6 @@
package constant
import (
"os"
)
var POSTGRESQL_DB_NAME = "terms"
var POSTGRESQL_USER = "postgres"
var POSTGRESQL_PASSWORD = "123456"
... ... @@ -17,25 +13,12 @@ var DISABLE_SQL_GENERATE_PRINT = false
var DISABLE_SQL_GENERATE_COMMENT = true
func init() {
if os.Getenv("POSTGRESQL_DB_NAME") != "" {
POSTGRESQL_DB_NAME = os.Getenv("POSTGRESQL_DB_NAME")
}
if os.Getenv("POSTGRESQL_USER") != "" {
POSTGRESQL_USER = os.Getenv("POSTGRESQL_USER")
}
if os.Getenv("POSTGRESQL_PASSWORD") != "" {
POSTGRESQL_PASSWORD = os.Getenv("POSTGRESQL_PASSWORD")
}
if os.Getenv("POSTGRESQL_HOST") != "" {
POSTGRESQL_HOST = os.Getenv("POSTGRESQL_HOST")
}
if os.Getenv("POSTGRESQL_PORT") != "" {
POSTGRESQL_PORT = os.Getenv("POSTGRESQL_PORT")
}
if os.Getenv("DISABLE_CREATE_TABLE") != "" {
DISABLE_CREATE_TABLE = true
}
if os.Getenv("DISABLE_SQL_GENERATE_PRINT") != "" {
DISABLE_SQL_GENERATE_PRINT = true
}
POSTGRESQL_HOST = Configurator.DefaultString("POSTGRESQL_HOST", POSTGRESQL_HOST)
POSTGRESQL_PORT = Configurator.DefaultString("POSTGRESQL_PORT", POSTGRESQL_PORT)
POSTGRESQL_DB_NAME = Configurator.DefaultString("POSTGRESQL_DB_NAME", POSTGRESQL_DB_NAME)
POSTGRESQL_USER = Configurator.DefaultString("POSTGRESQL_USER", POSTGRESQL_USER)
POSTGRESQL_PASSWORD = Configurator.DefaultString("POSTGRESQL_PASSWORD", POSTGRESQL_PASSWORD)
DISABLE_CREATE_TABLE = Configurator.DefaultBool("DISABLE_CREATE_TABLE", DISABLE_CREATE_TABLE)
DISABLE_SQL_GENERATE_PRINT = Configurator.DefaultBool("DISABLE_SQL_GENERATE_PRINT", DISABLE_SQL_GENERATE_PRINT)
}
... ...
package constant
import "os"
var (
REDIS_HOST = "127.0.0.1"
REDIS_PORT = "6379"
REDIS_AUTH = ""
REDIS_ADDRESS = ""
// 是否关闭仓储层缓存
DISABLE_REPOSITORY_CACHE = false
ENABLE_REPOSITORY_CACHE = true
// 缓存过期时间 单位秒
REPOSITORY_CACHE_EXPIRE = 30 * 60
REDIS_ADDRESS = ""
// redis 考勤机打卡消息队列
REDIS_ZKTECO_KEY = "allied-creation-zkteco"
// redis 车间数据消息队列
... ... @@ -19,21 +17,9 @@ var (
)
func init() {
if os.Getenv("REDIS_HOST") != "" {
REDIS_HOST = os.Getenv("REDIS_HOST")
REDIS_AUTH = os.Getenv("REDIS_AUTH")
}
if os.Getenv("REDIS_PORT") != "" {
REDIS_PORT = os.Getenv("REDIS_PORT")
}
if _, ok := os.LookupEnv("REDIS_AUTH"); ok {
REDIS_AUTH = os.Getenv("REDIS_AUTH")
}
if os.Getenv("ENABLE_REPOSITORY_CACHE") != "" {
DISABLE_REPOSITORY_CACHE = false
}
if os.Getenv("DISABLE_REPOSITORY_CACHE") != "" {
DISABLE_REPOSITORY_CACHE = true
}
REDIS_HOST = Configurator.DefaultString("REDIS_HOST", REDIS_HOST)
REDIS_PORT = Configurator.DefaultString("REDIS_PORT", REDIS_PORT)
REDIS_AUTH = Configurator.DefaultString("REDIS_AUTH", REDIS_AUTH)
ENABLE_REPOSITORY_CACHE = Configurator.DefaultBool("ENABLE_REPOSITORY_CACHE", ENABLE_REPOSITORY_CACHE)
REDIS_ADDRESS = REDIS_HOST + ":" + REDIS_PORT
}
... ...
... ... @@ -130,6 +130,7 @@ func (repository *ProductGroupRepository) FindOne(queryOptions map[string]interf
query.SetWhereByQueryOption("work_station->>'workStationId'=?", "workStationId")
query.SetWhereByQueryOption("work_station->>'workshopId'='?'", "workshopId")
query.SetWhereByQueryOption("work_station->>'lineId'='?'", "lineId")
query.SetWhereByQueryOption("work_station->>'sectionId'='?'", "sectionId")
if v, ok := queryOptions["includeDeleted"]; ok && v.(bool) {
query.AllWithDeleted()
}
... ...
package utils
import (
"github.com/beego/beego/v2/server/web"
"os"
"strconv"
)
type Configurator interface {
DefaultString(key string, defaultVal string) string
DefaultInt(key string, defaultVal int) int
DefaultInt64(key string, defaultVal int64) int64
DefaultBool(key string, defaultVal bool) bool
DefaultFloat(key string, defaultVal float64) float64
}
// EnvConfigurator read config from env param with default value
type EnvConfigurator struct{}
func (c EnvConfigurator) DefaultString(key string, defaultVal string) string {
if os.Getenv(key) != "" {
return os.Getenv(key)
}
return defaultVal
}
func (c EnvConfigurator) DefaultInt(key string, defaultVal int) int {
if os.Getenv(key) == "" {
return defaultVal
}
v, err := strconv.Atoi(os.Getenv(key))
if err != nil {
return defaultVal
}
return v
}
func (c EnvConfigurator) DefaultInt64(key string, defaultVal int64) int64 {
if os.Getenv(key) == "" {
return defaultVal
}
v, err := strconv.ParseInt(os.Getenv(key), 10, 64)
if err != nil {
return defaultVal
}
return v
}
func (c EnvConfigurator) DefaultBool(key string, defaultVal bool) bool {
if os.Getenv(key) == "" {
return defaultVal
}
v, err := strconv.ParseBool(os.Getenv(key))
if err != nil {
return defaultVal
}
return v
}
func (c EnvConfigurator) DefaultFloat(key string, defaultVal float64) float64 {
if os.Getenv(key) == "" {
return defaultVal
}
v, err := strconv.ParseFloat(os.Getenv(key), 64)
if err != nil {
return defaultVal
}
return v
}
// BeegoAppConfigurator read config from beego config file with default value
type BeegoAppConfigurator struct{}
func (c BeegoAppConfigurator) DefaultString(key string, defaultVal string) string {
return web.AppConfig.DefaultString(key, defaultVal)
}
func (c BeegoAppConfigurator) DefaultInt(key string, defaultVal int) int {
return web.AppConfig.DefaultInt(key, defaultVal)
}
func (c BeegoAppConfigurator) DefaultInt64(key string, defaultVal int64) int64 {
return web.AppConfig.DefaultInt64(key, defaultVal)
}
func (c BeegoAppConfigurator) DefaultBool(key string, defaultVal bool) bool {
return web.AppConfig.DefaultBool(key, defaultVal)
}
func (c BeegoAppConfigurator) DefaultFloat(key string, defaultVal float64) float64 {
return web.AppConfig.DefaultFloat(key, defaultVal)
}
... ...
package utils
import (
"github.com/stretchr/testify/assert"
"os"
"testing"
)
func TestEnvConfigurator(t *testing.T) {
os.Setenv("bool", "true")
os.Setenv("hello", "world")
os.Setenv("number", "10")
var config Configurator = EnvConfigurator{}
assert.Equal(t, "world", config.DefaultString("hello", "w"))
assert.Equal(t, "w", config.DefaultString("unknown", "w"))
assert.Equal(t, true, config.DefaultBool("bool", false))
assert.Equal(t, true, config.DefaultBool("unknown", true))
assert.Equal(t, 10, config.DefaultInt("number", 5))
assert.Equal(t, 5, config.DefaultInt("unknown", 5))
assert.Equal(t, int64(10), config.DefaultInt64("number", 5))
assert.Equal(t, int64(5), config.DefaultInt64("unknown", 5))
assert.Equal(t, float64(10), config.DefaultFloat("number", 5))
assert.Equal(t, float64(5), config.DefaultFloat("unknown", 5))
}
... ...
... ... @@ -78,3 +78,11 @@ func (controller *ProductCalendarController) SearchProductCalendar() {
total, data, err := productCalendarService.SearchProductCalendar(ParseOperateInfo(controller.BaseController), cmd)
ResponseGrid(controller.BaseController, total, data, err)
}
func (controller *ProductCalendarController) GetProductGroupCalendar() {
productCalendarService := service.NewProductCalendarService(nil)
cmd := &query.GetProductGroupCalendarQuery{}
Must(controller.Unmarshal(cmd))
data, err := productCalendarService.GetProductGroupCalendar(cmd)
controller.Response(data, err)
}
... ...
... ... @@ -14,4 +14,5 @@ func init() {
web.Router("/product-calendars/", &controllers.ProductCalendarController{}, "Get:ListProductCalendar")
web.Router("/product-calendars/search", &controllers.ProductCalendarController{}, "Post:SearchProductCalendar")
web.Router("/product-calendars/product-group-calendar", &controllers.ProductCalendarController{}, "Post:GetProductGroupCalendar")
}
... ...