expr
This commit is contained in:
parent
80f8f8ba7e
commit
a3ccda3b9d
|
@ -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())
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue