diff --git a/server/service/calculator.go b/server/service/calculator.go index 1c709c6..f05ab5b 100644 --- a/server/service/calculator.go +++ b/server/service/calculator.go @@ -25,7 +25,7 @@ func (c *Calculator) Test(sess *session.AdminSession, req *api.TestCalculatorReq panic("不要乱来") } salaryServ := CommonService.NewStaffSalary(staffUser) - salary, err := salaryServ.CalcSalary(nil, time.Now().AddDate(0, -1, 0).Format("200601")) + salary, err := salaryServ.CalcSalary(nil, time.Now().AddDate(0, -1, 0).Format("200601"), req.Expression) if err != nil { log.Errorf("CalcSalary error :%s", err.Error()) panic(err.Error()) diff --git a/server/service/staff_salary.go b/server/service/staff_salary.go index 451693e..973c69b 100644 --- a/server/service/staff_salary.go +++ b/server/service/staff_salary.go @@ -85,7 +85,7 @@ func (s *StaffSalary) Create(sess *session.AdminSession, req *api.CreateSalaryRe salary, err := dao.NewStaffSalaryDao().GetBy(sess.GetCorpId(), user.Id, req.Month) session.CheckDBError(err) salaryServ := CommonService.NewStaffSalary(user) - salary, err = salaryServ.CalcSalary(salary, req.Month) + salary, err = salaryServ.CalcSalary(salary, req.Month, "") if err != nil { log.Errorf("CalcSalary error :%s", err.Error()) continue diff --git a/service/staff_salary.go b/service/staff_salary.go index b7b0c17..18d9def 100644 --- a/service/staff_salary.go +++ b/service/staff_salary.go @@ -23,7 +23,7 @@ func NewStaffSalary(user *model.StaffUser) *StaffSalary { } } -func (s *StaffSalary) CalcSalary(salary *model.StaffSalary, month string) (*model.StaffSalary, error) { +func (s *StaffSalary) CalcSalary(salary *model.StaffSalary, month, expr string) (*model.StaffSalary, error) { corp, err := dao.NewCorpDao().Get(s.user.CorpId) if err != nil { return nil, err @@ -100,12 +100,12 @@ func (s *StaffSalary) CalcSalary(salary *model.StaffSalary, month string) (*mode salary.HouseDeduct = goutil.If(userConfig.HouseDeduct != "", cast.ToFloat64(userConfig.HouseDeduct), cast.ToFloat64(corpConfig.HouseDeduct)) //计算工资 - s.calculate(corp, salary) + s.calculate(corp, salary, expr) s.formatFloat(salary) return salary, nil } -func (s *StaffSalary) calculate(corp *model.Corp, salary *model.StaffSalary) { +func (s *StaffSalary) calculate(corp *model.Corp, salary *model.StaffSalary, expression string) { //获取基础数据 data := s.getCalcData(salary) @@ -118,21 +118,20 @@ func (s *StaffSalary) calculate(corp *model.Corp, salary *model.StaffSalary) { salary.Extra = goutil.EncodeJSONIndent(data) salary.SetExtra("calculate", goutil.GetNowTime()) - //获取计算器表达式 - calculator, _ := dao.NewSalaryCalculatorDao().Get(cast.ToInt64(s.user.GetSalary().Calculator)) - if calculator == nil { - log.Errorf("calculator[%s] is nil", s.user.GetSalary().Calculator) - return + if expression == "" { + //获取计算器表达式 + calculator, _ := dao.NewSalaryCalculatorDao().Get(cast.ToInt64(s.user.GetSalary().Calculator)) + if calculator == nil { + log.Errorf("calculator[%s] is nil", s.user.GetSalary().Calculator) + return + } + expression = calculator.Expression } //执行表达式 jsrun := otto.New() jsrun.Set("data", data) - _, err := jsrun.Run(calculator.Expression) - if calculator == nil { - log.Errorf("Run[%s] error :%s", calculator.Expression, err.Error()) - return - } + _, err := jsrun.Run(expression) //出勤工资 attendSalary, err := jsrun.Get("attend_salary") diff --git a/worker/staff.go b/worker/staff.go index c2828b5..2f68337 100644 --- a/worker/staff.go +++ b/worker/staff.go @@ -129,7 +129,7 @@ func (s *Staff) SyncStaffSalary(corpId int64, month string) { } staffService := service.NewStaffSalary(staff) - salary, err = staffService.CalcSalary(salary, month) + salary, err = staffService.CalcSalary(salary, month, "") if err != nil { log.Errorf("username[%s] staffService.CalcSalary error :%s", staff.Username, err.Error()) continue