From cb1a1c6f420a94dd99e4ffe99fcb2052276b26e7 Mon Sep 17 00:00:00 2001 From: jiangyong27 Date: Fri, 14 Mar 2025 14:29:55 +0800 Subject: [PATCH] config --- cmd/server/enterprise-api.go | 1 + cmd/worker/enterprise-worker.go | 1 + common/config/corp.go | 77 +++++++++++++++++++++++++++++++++ conf/corp/1000.json | 17 ++++++++ conf/corp/1002.json | 17 ++++++++ conf/corp/default.json | 1 + server/api/staff.go | 6 ++- server/controller/staff.go | 16 +++++++ server/service/staff_user.go | 11 ++++- 9 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 common/config/corp.go create mode 100644 conf/corp/1000.json create mode 100644 conf/corp/1002.json create mode 100644 conf/corp/default.json diff --git a/cmd/server/enterprise-api.go b/cmd/server/enterprise-api.go index 754cc1a..de00819 100644 --- a/cmd/server/enterprise-api.go +++ b/cmd/server/enterprise-api.go @@ -10,6 +10,7 @@ import ( func main() { config.LoadServerConfig() config.LoadAliPay() + config.LoadCorpConfig() global.InitGlobal() if err := server.Start(); err != nil { diff --git a/cmd/worker/enterprise-worker.go b/cmd/worker/enterprise-worker.go index 44f07e8..22843fc 100644 --- a/cmd/worker/enterprise-worker.go +++ b/cmd/worker/enterprise-worker.go @@ -10,6 +10,7 @@ import ( func main() { config.LoadServerConfig() config.LoadAliPay() + config.LoadCorpConfig() global.InitGlobal() worker.InitCorp() diff --git a/common/config/corp.go b/common/config/corp.go new file mode 100644 index 0000000..0d68ea4 --- /dev/null +++ b/common/config/corp.go @@ -0,0 +1,77 @@ +package config + +import ( + "encoding/json" + "fmt" + log "github.com/sirupsen/logrus" + "github.com/smbrave/goutil" + "github.com/spf13/cast" + "os" + "strings" +) + +var ( + corpConfig map[int64]map[string]interface{} +) + +func init() { + corpConfig = make(map[int64]map[string]interface{}) + +} + +// 配置优先级 环境变量>数据库配置>大于文件配置 +func LoadCorpConfig() { + fileList, err := goutil.FileList("conf/corp/") + if err != nil { + panic(err) + } + + filePath := "conf/corp/default.json" + cfgBody, _ := os.ReadFile(filePath) + defParams := make(map[string]interface{}) + if err := json.Unmarshal(cfgBody, &defParams); err != nil { + panic(err) + } + + for _, file := range fileList { + if !strings.HasSuffix(file, ".json") { + continue + } + appId := cast.ToInt64(strings.TrimRight(file, ".json")) + filePath = fmt.Sprintf("conf/corp/%s", file) + cfgBody, _ = os.ReadFile(filePath) + p := make(map[string]interface{}) + if err := json.Unmarshal(cfgBody, &p); err != nil { + panic(err) + } + allParmas := make(map[string]interface{}) + for k, v := range defParams { + allParmas[k] = v + } + for k, v := range p { + allParmas[k] = v + } + + log.Infof("corpId[%d] config[%s]", appId, goutil.EncodeJSON(allParmas)) + corpConfig[appId] = allParmas + } +} + +func GetCorpConfig(corpId int64, key string, def ...interface{}) interface{} { + + if _, ok := corpConfig[corpId]; !ok { + if len(def) == 0 { + return nil + } + return def[0] + } + cfg := corpConfig[corpId] + if v, ok := cfg[key]; ok { + return v + } + + if len(def) == 0 { + return nil + } + return def[0] +} diff --git a/conf/corp/1000.json b/conf/corp/1000.json new file mode 100644 index 0000000..79a6913 --- /dev/null +++ b/conf/corp/1000.json @@ -0,0 +1,17 @@ +{ + "staff_config": [ + { + "name": "目标绩效", + "key": "target" + }, + { + "name": "社保扣除", + "key": "social_deduct" + }, + { + "name": "公积金扣除", + "key": "house_deduct" + } + ] +} + diff --git a/conf/corp/1002.json b/conf/corp/1002.json new file mode 100644 index 0000000..79a6913 --- /dev/null +++ b/conf/corp/1002.json @@ -0,0 +1,17 @@ +{ + "staff_config": [ + { + "name": "目标绩效", + "key": "target" + }, + { + "name": "社保扣除", + "key": "social_deduct" + }, + { + "name": "公积金扣除", + "key": "house_deduct" + } + ] +} + diff --git a/conf/corp/default.json b/conf/corp/default.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/conf/corp/default.json @@ -0,0 +1 @@ +{} diff --git a/server/api/staff.go b/server/api/staff.go index a7df18a..bb5782c 100644 --- a/server/api/staff.go +++ b/server/api/staff.go @@ -20,7 +20,6 @@ type StaffUser struct { EntryDate string `json:"entry_date"` OfficialDate string `json:"official_date"` LeaveDate string `json:"leave_date"` - Config string `json:"config"` Status string `json:"status"` SalaryBase string `json:"salary_base"` @@ -31,6 +30,8 @@ type StaffUser struct { PayeeBankName string `json:"payee_bank_name"` PayeeBankCard string `json:"payee_bank_card"` PayeeApliayUid string `json:"payee_apliay_uid"` + + Config interface{} `json:"config"` } type StaffListReq struct { @@ -55,7 +56,6 @@ type StaffUpdateReq struct { EntryDate string `json:"entry_date"` OfficialDate string `json:"official_date"` LeaveDate string `json:"leave_date"` - Config string `json:"config"` Status string `json:"status"` SalaryBase string `json:"salary_base"` @@ -65,6 +65,8 @@ type StaffUpdateReq struct { PayeeBankName string `json:"payee_bank_name"` PayeeBankCard string `json:"payee_bank_card"` PayeeAlipayUid string `json:"payee_alipay_uid"` + + Config map[string]interface{} `json:"config"` } func (s *StaffUser) From(m *model.StaffUser) { diff --git a/server/controller/staff.go b/server/controller/staff.go index 0bf3f76..cfd8bb2 100644 --- a/server/controller/staff.go +++ b/server/controller/staff.go @@ -1,6 +1,8 @@ package controller import ( + "encoding/json" + "enterprise/common/config" "enterprise/common/dao" "enterprise/server/api" "enterprise/server/service" @@ -52,6 +54,9 @@ func (s *Staff) List(ctx *gin.Context) { staffs, total, err := dao.NewStaffUserDao().Query(req.Page, req.Size, sess.GetCorpId(), cast.ToInt(req.Status), req.Username, req.Realname, req.Phone, req.Idno) session.CheckDBError(err) items := make([]*api.StaffUser, 0) + + corpStaffConfig := cast.ToSlice(config.GetCorpConfig(sess.GetCorpId(), "staff_config", nil)) + for _, st := range staffs { i := new(api.StaffUser) i.From(st) @@ -59,6 +64,17 @@ func (s *Staff) List(ctx *gin.Context) { if calculator != nil { i.SalaryCalculatorName = calculator.Name } + + // config + var staffConfig map[string]interface{} + json.Unmarshal([]byte(st.Config), &staffConfig) + config := make([]map[string]interface{}, 0) + for _, kv := range corpStaffConfig { + obj := cast.ToStringMap(kv) + obj["value"] = staffConfig[cast.ToString(obj["key"])] + config = append(config, obj) + } + i.Config = config items = append(items, i) } diff --git a/server/service/staff_user.go b/server/service/staff_user.go index 9191b2b..a792a64 100644 --- a/server/service/staff_user.go +++ b/server/service/staff_user.go @@ -1,6 +1,7 @@ package service import ( + "encoding/json" "enterprise/common/dao" "enterprise/common/model" "enterprise/server/api" @@ -103,7 +104,6 @@ func (s *StaffUser) Update(sess *session.AdminSession, req *api.StaffUpdateReq) staffUser.Idno = goutil.If(req.Idno != "", req.Idno, staffUser.Idno) staffUser.EntryDate = goutil.If(req.EntryDate != "", req.EntryDate, staffUser.EntryDate) staffUser.OfficialDate = goutil.If(req.OfficialDate != "", req.OfficialDate, staffUser.OfficialDate) - staffUser.Config = goutil.If(req.Config != "", req.Config, staffUser.Config) staffSalary := staffUser.GetSalary() staffSalary.Base = goutil.If(req.SalaryBase != "", req.SalaryBase, staffSalary.Base) @@ -128,6 +128,15 @@ func (s *StaffUser) Update(sess *session.AdminSession, req *api.StaffUpdateReq) } } + if len(req.Config) != 0 { + var cfg map[string]interface{} + json.Unmarshal([]byte(staffUser.Config), &cfg) + for k, v := range req.Config { + cfg[k] = v + } + staffUser.Config = goutil.EncodeJSON(cfg) + } + err = dao.NewStaffUserDao().Update(staffUser) session.CheckDBError(err) }