This commit is contained in:
jiangyong27 2025-03-29 11:13:23 +08:00
parent 80f8f8ba7e
commit a3ccda3b9d
4 changed files with 15 additions and 16 deletions

View File

@ -25,7 +25,7 @@ func (c *Calculator) Test(sess *session.AdminSession, req *api.TestCalculatorReq
panic("不要乱来") panic("不要乱来")
} }
salaryServ := CommonService.NewStaffSalary(staffUser) 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 { if err != nil {
log.Errorf("CalcSalary error :%s", err.Error()) log.Errorf("CalcSalary error :%s", err.Error())
panic(err.Error()) panic(err.Error())

View File

@ -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) salary, err := dao.NewStaffSalaryDao().GetBy(sess.GetCorpId(), user.Id, req.Month)
session.CheckDBError(err) session.CheckDBError(err)
salaryServ := CommonService.NewStaffSalary(user) salaryServ := CommonService.NewStaffSalary(user)
salary, err = salaryServ.CalcSalary(salary, req.Month) salary, err = salaryServ.CalcSalary(salary, req.Month, "")
if err != nil { if err != nil {
log.Errorf("CalcSalary error :%s", err.Error()) log.Errorf("CalcSalary error :%s", err.Error())
continue continue

View File

@ -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) corp, err := dao.NewCorpDao().Get(s.user.CorpId)
if err != nil { if err != nil {
return nil, err 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)) 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) s.formatFloat(salary)
return salary, nil 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) data := s.getCalcData(salary)
@ -118,21 +118,20 @@ func (s *StaffSalary) calculate(corp *model.Corp, salary *model.StaffSalary) {
salary.Extra = goutil.EncodeJSONIndent(data) salary.Extra = goutil.EncodeJSONIndent(data)
salary.SetExtra("calculate", goutil.GetNowTime()) salary.SetExtra("calculate", goutil.GetNowTime())
//获取计算器表达式 if expression == "" {
calculator, _ := dao.NewSalaryCalculatorDao().Get(cast.ToInt64(s.user.GetSalary().Calculator)) //获取计算器表达式
if calculator == nil { calculator, _ := dao.NewSalaryCalculatorDao().Get(cast.ToInt64(s.user.GetSalary().Calculator))
log.Errorf("calculator[%s] is nil", s.user.GetSalary().Calculator) if calculator == nil {
return log.Errorf("calculator[%s] is nil", s.user.GetSalary().Calculator)
return
}
expression = calculator.Expression
} }
//执行表达式 //执行表达式
jsrun := otto.New() jsrun := otto.New()
jsrun.Set("data", data) jsrun.Set("data", data)
_, err := jsrun.Run(calculator.Expression) _, err := jsrun.Run(expression)
if calculator == nil {
log.Errorf("Run[%s] error :%s", calculator.Expression, err.Error())
return
}
//出勤工资 //出勤工资
attendSalary, err := jsrun.Get("attend_salary") attendSalary, err := jsrun.Get("attend_salary")

View File

@ -129,7 +129,7 @@ func (s *Staff) SyncStaffSalary(corpId int64, month string) {
} }
staffService := service.NewStaffSalary(staff) staffService := service.NewStaffSalary(staff)
salary, err = staffService.CalcSalary(salary, month) salary, err = staffService.CalcSalary(salary, month, "")
if err != nil { if err != nil {
log.Errorf("username[%s] staffService.CalcSalary error :%s", staff.Username, err.Error()) log.Errorf("username[%s] staffService.CalcSalary error :%s", staff.Username, err.Error())
continue continue