From 0161bb65a97f077864887e384a554ccbe57abf98 Mon Sep 17 00:00:00 2001 From: jiangyong27 Date: Tue, 8 Apr 2025 21:09:20 +0800 Subject: [PATCH] plugin --- cmd/server/enterprise-api.go | 2 +- cmd/worker/enterprise-worker.go | 2 +- common/dao/external_corp_order.go | 11 ++-- common/registry/salary.go | 31 ++++++++--- plugin/init.go | 11 ++++ .../salary_1000.go | 14 ++--- .../salary_1002.go | 10 ++-- plugin/staff.go | 52 ++++++++++++++++++ plugin/staff_1000.go | 21 ++++++++ plugin/staff_1002.go | 49 +++++++++++++++++ service/salary_calculator/init.go | 8 --- service/staff_salary.go | 2 +- worker/staff.go | 53 ++++++------------- 13 files changed, 196 insertions(+), 70 deletions(-) create mode 100644 plugin/init.go rename service/salary_calculator/salary_calculator_1000.go => plugin/salary_1000.go (79%) rename service/salary_calculator/salary_calculator_1002.go => plugin/salary_1002.go (86%) create mode 100644 plugin/staff.go create mode 100644 plugin/staff_1000.go create mode 100644 plugin/staff_1002.go delete mode 100644 service/salary_calculator/init.go diff --git a/cmd/server/enterprise-api.go b/cmd/server/enterprise-api.go index de00819..475efc9 100644 --- a/cmd/server/enterprise-api.go +++ b/cmd/server/enterprise-api.go @@ -3,8 +3,8 @@ package main import ( "enterprise/common/config" "enterprise/common/global" + _ "enterprise/plugin" "enterprise/server" - _ "enterprise/service/salary_calculator" ) func main() { diff --git a/cmd/worker/enterprise-worker.go b/cmd/worker/enterprise-worker.go index df02582..3ea4bcb 100644 --- a/cmd/worker/enterprise-worker.go +++ b/cmd/worker/enterprise-worker.go @@ -3,7 +3,7 @@ package main import ( "enterprise/common/config" "enterprise/common/global" - _ "enterprise/service/salary_calculator" + _ "enterprise/plugin" "enterprise/worker" ) diff --git a/common/dao/external_corp_order.go b/common/dao/external_corp_order.go index b5fcbc7..defa49f 100644 --- a/common/dao/external_corp_order.go +++ b/common/dao/external_corp_order.go @@ -22,14 +22,17 @@ func (d *ExternalCorpOrder) QueryOwnerOrder(owner string, startTime, endTime int tx := corpDB.Table(orderTable) var o []*model.ExternalCorpOrder - + tx.Where(orderTable+".status = ?", 2) if owner != "" { tx.Where("cp_user.username = ?", owner) } - tx.Where(orderTable+".status = ?", 2) - tx.Where(orderTable+".pay_time >= ?", startTime) - tx.Where(orderTable+".pay_time <= ?", endTime) + if startTime > 0 { + tx.Where(orderTable+".pay_time >= ?", startTime) + } + if endTime > 0 { + tx.Where(orderTable+".pay_time <= ?", endTime) + } tx.Joins(fmt.Sprintf("LEFT JOIN cp_user ON %s.admin_id=cp_user.id", orderTable)) tx = tx.Find(&o) diff --git a/common/registry/salary.go b/common/registry/salary.go index bfc32ff..66828ea 100644 --- a/common/registry/salary.go +++ b/common/registry/salary.go @@ -6,24 +6,35 @@ import ( ) var ( - salaryFactory map[string]SalaryCalculatorFactory + salaryFactory map[string]SalaryFactory + staffFactory map[string]StaffFactory ) func init() { - salaryFactory = make(map[string]SalaryCalculatorFactory) + salaryFactory = make(map[string]SalaryFactory) + staffFactory = make(map[string]StaffFactory) } -type SalaryCalculator interface { +type Salary interface { Calculate(salary *model.StaffSalary) map[string]interface{} } -type SalaryCalculatorFactory func(corp *model.Corp, user *model.StaffUser) SalaryCalculator +type Staff interface { + Monitor(user *model.StaffUser) +} -func RegisterSalaryCalculator(key string, factory SalaryCalculatorFactory) { +type SalaryFactory func(corp *model.Corp, user *model.StaffUser) Salary +type StaffFactory func(corp *model.Corp) Staff + +func RegisterSalary(key string, factory SalaryFactory) { salaryFactory[key] = factory } -func NewSalaryCalculator(corp *model.Corp, user *model.StaffUser) SalaryCalculator { +func RegisterStaff(key string, factory StaffFactory) { + staffFactory[key] = factory +} + +func NewSalary(corp *model.Corp, user *model.StaffUser) Salary { factory := salaryFactory[cast.ToString(corp.Id)+"_"+user.Username] if factory == nil { factory = salaryFactory[cast.ToString(corp.Id)] @@ -33,3 +44,11 @@ func NewSalaryCalculator(corp *model.Corp, user *model.StaffUser) SalaryCalculat } return factory(corp, user) } + +func NewStaff(corp *model.Corp) Staff { + factory := staffFactory[cast.ToString(corp.Id)] + if factory == nil { + return nil + } + return factory(corp) +} diff --git a/plugin/init.go b/plugin/init.go new file mode 100644 index 0000000..02c1072 --- /dev/null +++ b/plugin/init.go @@ -0,0 +1,11 @@ +package plugin + +import "enterprise/common/registry" + +func init() { + registry.RegisterSalary("1000", NewSalary1000) + registry.RegisterSalary("1002", NewSalary1002) + + registry.RegisterStaff("1000", NewStaff1000) + registry.RegisterStaff("1002", NewStaff1002) +} diff --git a/service/salary_calculator/salary_calculator_1000.go b/plugin/salary_1000.go similarity index 79% rename from service/salary_calculator/salary_calculator_1000.go rename to plugin/salary_1000.go index aa61958..5ff6bd5 100644 --- a/service/salary_calculator/salary_calculator_1000.go +++ b/plugin/salary_1000.go @@ -1,4 +1,4 @@ -package salary_calculator +package plugin import ( "enterprise/common/dao" @@ -14,19 +14,19 @@ const ( SalaryCalculatorOperation = "operation" //运营计算工资的方法 ) -type SalaryCalculator1000 struct { +type Salary1000 struct { corp *model.Corp user *model.StaffUser } -func NewSalaryCalculator1000(corp *model.Corp, user *model.StaffUser) registry.SalaryCalculator { - return &SalaryCalculator1000{ +func NewSalary1000(corp *model.Corp, user *model.StaffUser) registry.Salary { + return &Salary1000{ corp: corp, user: user, } } -func (s *SalaryCalculator1000) Calculate(salary *model.StaffSalary) map[string]interface{} { +func (s *Salary1000) Calculate(salary *model.StaffSalary) map[string]interface{} { monthTime, _ := time.ParseInLocation("200601", salary.Month, time.Local) data := make(map[string]interface{}) @@ -37,7 +37,7 @@ func (s *SalaryCalculator1000) Calculate(salary *model.StaffSalary) map[string]i return data } -func (s *SalaryCalculator1000) getAdOwnerProfit(salary *model.StaffSalary) float64 { +func (s *Salary1000) getAdOwnerProfit(salary *model.StaffSalary) float64 { monthTime, _ := time.ParseInLocation("200601", salary.Month, time.Local) startDay := monthTime.Format("2006-01-02") endDay := monthTime.AddDate(0, 1, -1).Format("2006-01-02") @@ -55,7 +55,7 @@ func (s *SalaryCalculator1000) getAdOwnerProfit(salary *model.StaffSalary) float return totalProfit } -func (s *SalaryCalculator1000) getKctAppProfit(month string) float64 { +func (s *Salary1000) getKctAppProfit(month string) float64 { monthTime, _ := time.ParseInLocation("200601", month, time.Local) diff --git a/service/salary_calculator/salary_calculator_1002.go b/plugin/salary_1002.go similarity index 86% rename from service/salary_calculator/salary_calculator_1002.go rename to plugin/salary_1002.go index 1893c61..6457d9d 100644 --- a/service/salary_calculator/salary_calculator_1002.go +++ b/plugin/salary_1002.go @@ -1,4 +1,4 @@ -package salary_calculator +package plugin import ( "enterprise/common/dao" @@ -8,19 +8,19 @@ import ( "time" ) -type SalaryCalculator1002 struct { +type Salary1002 struct { corp *model.Corp user *model.StaffUser } -func NewSalaryCalculator1002(corp *model.Corp, user *model.StaffUser) registry.SalaryCalculator { - return &SalaryCalculator1002{ +func NewSalary1002(corp *model.Corp, user *model.StaffUser) registry.Salary { + return &Salary1002{ corp: corp, user: user, } } -func (s *SalaryCalculator1002) Calculate(salary *model.StaffSalary) map[string]interface{} { +func (s *Salary1002) Calculate(salary *model.StaffSalary) map[string]interface{} { data := make(map[string]interface{}) //离职人员考勤异常 diff --git a/plugin/staff.go b/plugin/staff.go new file mode 100644 index 0000000..5ecf6f3 --- /dev/null +++ b/plugin/staff.go @@ -0,0 +1,52 @@ +package plugin + +import ( + "enterprise/common/global" + "enterprise/common/model" + "fmt" + log "github.com/sirupsen/logrus" + "strings" + "time" +) + +type Staff struct { +} + +func (s *Staff) MonitorWorkAge(user *model.StaffUser) { + nowDate := time.Now() + entryDate, _ := time.ParseInLocation("2006-01-02", user.EntryDate, time.Local) + OfficialDate, _ := time.ParseInLocation("2006-01-02", user.OfficialDate, time.Local) + + entryMonth := (nowDate.Year()-entryDate.Year())*12 + int(nowDate.Month()) - int(entryDate.Month()) + officalDay := (OfficialDate.Unix() - nowDate.Unix()) / 86400 + log.Infof("staff[%s] entryDate[%s] spanMonth[%.1f]", user.Username, user.EntryDate, float64(entryMonth)/12.0) + + salary := user.GetSalary() + if nowDate.Day() == 1 && entryMonth%6 == 0 { + message := make([]string, 0) + message = append(message, fmt.Sprintf("【员工半年提醒】[%s]", user.Realname)) + message = append(message, fmt.Sprintf("入职时间:%s", user.EntryDate)) + message = append(message, fmt.Sprintf("入职年限:%.1f", float64(entryMonth)/12)) + message = append(message, fmt.Sprintf("基本工资:%s", salary.Base)) + message = append(message, fmt.Sprintf("绩效工资:%s", salary.Target)) + message = append(message, fmt.Sprintf("身份证号:%s", user.Idno)) + + if err := global.SendCorpMessage(user.CorpId, []string{"jiangyong"}, strings.Join(message, "\n")); err != nil { + log.Errorf("send message error :%s", err.Error()) + } + } + + if officalDay > 0 && officalDay <= 30 && officalDay%7 == 0 { + message := make([]string, 0) + message = append(message, fmt.Sprintf("【员工转正提醒】[%s]", user.Realname)) + message = append(message, fmt.Sprintf("入职时间:%s", user.EntryDate)) + message = append(message, fmt.Sprintf("转正时间:%s", user.OfficialDate)) + message = append(message, fmt.Sprintf("基本工资:%s", salary.Base)) + message = append(message, fmt.Sprintf("绩效工资:%s", salary.Target)) + message = append(message, fmt.Sprintf("身份证号:%s", user.Idno)) + + if err := global.SendCorpMessage(user.CorpId, []string{"jiangyong"}, strings.Join(message, "\n")); err != nil { + log.Errorf("send message error :%s", err.Error()) + } + } +} diff --git a/plugin/staff_1000.go b/plugin/staff_1000.go new file mode 100644 index 0000000..7ea6b0c --- /dev/null +++ b/plugin/staff_1000.go @@ -0,0 +1,21 @@ +package plugin + +import ( + "enterprise/common/model" + "enterprise/common/registry" +) + +type Staff1000 struct { + Staff + corp *model.Corp +} + +func NewStaff1000(corp *model.Corp) registry.Staff { + return &Staff1000{ + corp: corp, + } +} + +func (s *Staff1000) Monitor(user *model.StaffUser) { + s.MonitorWorkAge(user) +} diff --git a/plugin/staff_1002.go b/plugin/staff_1002.go new file mode 100644 index 0000000..fd7eda0 --- /dev/null +++ b/plugin/staff_1002.go @@ -0,0 +1,49 @@ +package plugin + +import ( + "enterprise/common/dao" + "enterprise/common/global" + "enterprise/common/model" + "enterprise/common/registry" + "fmt" + log "github.com/sirupsen/logrus" + "strings" + "time" +) + +type Staff1002 struct { + Staff + corp *model.Corp +} + +func NewStaff1002(corp *model.Corp) registry.Staff { + return &Staff1002{ + corp: corp, + } +} + +func (s *Staff1002) Monitor(user *model.StaffUser) { + s.MonitorWorkAge(user) + + nowDate := time.Now() + entryDate, _ := time.ParseInLocation("2006-01-02", user.EntryDate, time.Local) + entryDay := (nowDate.Unix() - entryDate.Unix()) / 86400 + if entryDay != 20 && entryDay != 40 { + return + } + + // 1.订单 + orders, err := dao.NewExternalCorpOrder().QueryOwnerOrder(user.Username, 0, 0) + if err != nil { + log.Errorf("db error:%s", err.Error()) + return + } + + message := make([]string, 0) + message = append(message, fmt.Sprintf("【员工试用期提醒】[%s]", user.Realname)) + message = append(message, fmt.Sprintf("入职时间:%s", user.EntryDate)) + message = append(message, fmt.Sprintf("入职天数:%d", entryDay)) + message = append(message, fmt.Sprintf("订单总数:%d", len(orders))) + + global.SendCorpMessage(user.CorpId, []string{"jiangyong", "zhangping"}, strings.Join(message, "\n")) +} diff --git a/service/salary_calculator/init.go b/service/salary_calculator/init.go deleted file mode 100644 index 0e146cc..0000000 --- a/service/salary_calculator/init.go +++ /dev/null @@ -1,8 +0,0 @@ -package salary_calculator - -import "enterprise/common/registry" - -func init() { - registry.RegisterSalaryCalculator("1000", NewSalaryCalculator1000) - registry.RegisterSalaryCalculator("1002", NewSalaryCalculator1002) -} diff --git a/service/staff_salary.go b/service/staff_salary.go index 1adc3ec..18ae090 100644 --- a/service/staff_salary.go +++ b/service/staff_salary.go @@ -104,7 +104,7 @@ func (s *StaffSalary) CalcSalary(salary *model.StaffSalary, month, expr string) func (s *StaffSalary) calculate(corp *model.Corp, salary *model.StaffSalary, expression string) { //获取业务数据 - dataFactory := registry.NewSalaryCalculator(corp, s.user) + dataFactory := registry.NewSalary(corp, s.user) var biz interface{} = nil if dataFactory != nil { biz = dataFactory.Calculate(salary) diff --git a/worker/staff.go b/worker/staff.go index e6466d0..c16965c 100644 --- a/worker/staff.go +++ b/worker/staff.go @@ -4,6 +4,7 @@ import ( "enterprise/common/dao" "enterprise/common/global" "enterprise/common/model" + "enterprise/common/registry" "enterprise/service" "fmt" log "github.com/sirupsen/logrus" @@ -19,53 +20,31 @@ func NewStaff() *Staff { return &Staff{} } func (s *Staff) MontorWorkAge(corpId int64) { + corp, err := dao.NewCorpDao().Get(corpId) + if err != nil { + log.Errorf("db error :%s", err.Error()) + return + } + if corp == nil { + log.Errorf("corp[%d] is nil", corpId) + return + } + staffs, _, err := dao.NewStaffUserDao().Query(1, -1, corpId, model.StaffUserStatusOnline, "", "", "", "") if err != nil { log.Errorf("db error :%s", err.Error()) return } - nowDate := time.Now() + staffPlugin := registry.NewStaff(corp) + for _, staff := range staffs { //离职的忽略 - if staff.LeaveDate != "" { + if staff.LeaveDate != "" || staff.Status != model.StaffUserStatusOnline { continue } - - entryDate, _ := time.ParseInLocation("2006-01-02", staff.EntryDate, time.Local) - OfficialDate, _ := time.ParseInLocation("2006-01-02", staff.OfficialDate, time.Local) - - entryMonth := (nowDate.Year()-entryDate.Year())*12 + int(nowDate.Month()) - int(entryDate.Month()) - officalDay := (OfficialDate.Unix() - nowDate.Unix()) / 86400 - log.Infof("staff[%s] entryDate[%s] spanMonth[%.1f]", staff.Username, staff.EntryDate, float64(entryMonth)/12.0) - - salary := staff.GetSalary() - if nowDate.Day() == 1 && entryMonth%6 == 0 { - message := make([]string, 0) - message = append(message, fmt.Sprintf("【员工半年提醒】[%s]", staff.Realname)) - message = append(message, fmt.Sprintf("入职时间:%s", staff.EntryDate)) - message = append(message, fmt.Sprintf("入职年限:%.1f", float64(entryMonth)/12)) - message = append(message, fmt.Sprintf("基本工资:%s", salary.Base)) - message = append(message, fmt.Sprintf("绩效工资:%s", salary.Target)) - message = append(message, fmt.Sprintf("身份证号:%s", staff.Idno)) - - if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil { - log.Errorf("send message error :%s", err.Error()) - } - } - - if officalDay > 0 && officalDay <= 30 && officalDay%7 == 0 { - message := make([]string, 0) - message = append(message, fmt.Sprintf("【员工转正提醒】[%s]", staff.Realname)) - message = append(message, fmt.Sprintf("入职时间:%s", staff.EntryDate)) - message = append(message, fmt.Sprintf("转正时间:%s", staff.OfficialDate)) - message = append(message, fmt.Sprintf("基本工资:%s", salary.Base)) - message = append(message, fmt.Sprintf("绩效工资:%s", salary.Target)) - message = append(message, fmt.Sprintf("身份证号:%s", staff.Idno)) - - if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil { - log.Errorf("send message error :%s", err.Error()) - } + if staffPlugin != nil { + staffPlugin.Monitor(staff) } } }