From 1f56a9fbec099b5a0fe438ef020aab0ecd84282a Mon Sep 17 00:00:00 2001 From: jiangyong27 Date: Wed, 26 Mar 2025 20:22:25 +0800 Subject: [PATCH] calc test --- common/model/staff_salary.go | 6 +++++ server/api/calculator.go | 5 ++++ server/api/salary.go | 1 + server/controller/calculator.go | 9 +++++++ server/server.go | 1 + server/service/calculator.go | 42 +++++++++++++++++++++++++++++++++ server/service/staff_salary.go | 3 +++ service/staff_salary.go | 6 +++++ 8 files changed, 73 insertions(+) create mode 100644 server/service/calculator.go diff --git a/common/model/staff_salary.go b/common/model/staff_salary.go index 853a01e..d80237b 100644 --- a/common/model/staff_salary.go +++ b/common/model/staff_salary.go @@ -8,6 +8,11 @@ import ( "strings" ) +var ( + StaffSalaryStatusWait = 1 + StaffSalaryStatusPayed = 2 +) + type StaffSalary struct { Id int64 CorpId int64 @@ -30,6 +35,7 @@ type StaffSalary struct { UpdateTime int64 Extra string Comment string + Status int } func (s *StaffSalary) GetShouldSalary() float64 { diff --git a/server/api/calculator.go b/server/api/calculator.go index e726590..e6fbcb3 100644 --- a/server/api/calculator.go +++ b/server/api/calculator.go @@ -25,6 +25,11 @@ type CreateCalculatorReq struct { Expression string `json:"expression"` } +type TestCalculatorReq struct { + StaffId string `json:"staff_id"` + Expression string `json:"expression"` +} + type UpdateCalculatorReq struct { Id string `json:"id"` Name string `json:"name"` diff --git a/server/api/salary.go b/server/api/salary.go index fe8cace..e0cf044 100644 --- a/server/api/salary.go +++ b/server/api/salary.go @@ -40,6 +40,7 @@ type UpdateSalaryReq struct { OtherIncome string `json:"other_income"` OtherDeduct string `json:"other_deduct"` PersonalDeduct string `json:"personal_deduct"` + Status string `json:"status"` } type ListSalaryReq struct { diff --git a/server/controller/calculator.go b/server/controller/calculator.go index 07253f4..baf58ad 100644 --- a/server/controller/calculator.go +++ b/server/controller/calculator.go @@ -4,6 +4,7 @@ import ( "enterprise/common/dao" "enterprise/common/model" "enterprise/server/api" + "enterprise/server/service" "enterprise/server/session" "github.com/gin-gonic/gin" "github.com/smbrave/goutil" @@ -75,3 +76,11 @@ func (c *Calculator) List(ctx *gin.Context) { ctx.JSON(http.StatusOK, session.NewListRsp(total, items)) } + +// Delete 删除指定的计算项 +func (c *Calculator) Test(ctx *gin.Context) { + var req api.TestCalculatorReq + sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + data := service.NewCalculator().Test(sess, &req) + ctx.JSON(http.StatusOK, session.NewRsp(data)) +} diff --git a/server/server.go b/server/server.go index d283673..ddb7d18 100644 --- a/server/server.go +++ b/server/server.go @@ -44,6 +44,7 @@ func initRoutge(engine *gin.Engine) { apiGroup.GET("/staff", controller.NewStaff().List) apiGroup.GET("/calculator", controller.NewCalculator().List) apiGroup.POST("/calculator", controller.NewCalculator().Create) + apiGroup.POST("/calculator/test", controller.NewCalculator().Test) apiGroup.PUT("/calculator", controller.NewCalculator().Update) apiGroup.DELETE("/calculator", controller.NewCalculator().Delete) diff --git a/server/service/calculator.go b/server/service/calculator.go new file mode 100644 index 0000000..ee140ab --- /dev/null +++ b/server/service/calculator.go @@ -0,0 +1,42 @@ +package service + +import ( + "enterprise/common/dao" + "enterprise/server/api" + "enterprise/server/session" + CommonService "enterprise/service" + log "github.com/sirupsen/logrus" + "github.com/spf13/cast" + "time" +) + +type Calculator struct { +} + +func NewCalculator() *Calculator { + return &Calculator{} +} + +func (c *Calculator) Test(sess *session.AdminSession, req *api.TestCalculatorReq) interface{} { + staffUser, err := dao.NewStaffUserDao().Get(cast.ToInt64(req.StaffId)) + session.CheckDBError(err) + session.CheckNilError(staffUser, "员工不存在") + salaryServ := CommonService.NewStaffSalary(staffUser) + salary, err := salaryServ.CalcSalary(nil, time.Now().AddDate(0, -1, 0).Format("200601")) + if err != nil { + log.Errorf("CalcSalary error :%s", err.Error()) + panic(err.Error()) + } + if salary == nil { + panic("没有工资") + } + + apiSalary := new(api.Salary) + apiSalary.From(salary) + userSalary := staffUser.GetSalary() + apiSalary.Realname = staffUser.Realname + apiSalary.TargetSalary = userSalary.Target + apiSalary.BaseSalary = userSalary.Base + + return apiSalary +} diff --git a/server/service/staff_salary.go b/server/service/staff_salary.go index 03a1f4a..4c49e72 100644 --- a/server/service/staff_salary.go +++ b/server/service/staff_salary.go @@ -116,6 +116,9 @@ func (s *StaffSalary) Update(sess *session.AdminSession, req *api.UpdateSalaryRe if req.PersonalDeduct != "" { salary.PersonalDeduct = cast.ToFloat64(req.PersonalDeduct) } + if req.Status != "" { + salary.Status = cast.ToInt(req.Status) + } err = dao.NewStaffSalaryDao().Update(salary) session.CheckDBError(err) } diff --git a/service/staff_salary.go b/service/staff_salary.go index 1045d71..b7b0c17 100644 --- a/service/staff_salary.go +++ b/service/staff_salary.go @@ -60,7 +60,13 @@ func (s *StaffSalary) CalcSalary(salary *model.StaffSalary, month string) (*mode salary.CorpId = s.user.CorpId salary.UserId = s.user.Id salary.Username = s.user.Username + salary.Status = model.StaffSalaryStatusWait } + //已支付过的工资不能再计算了 + if salary != nil && salary.Status == model.StaffSalaryStatusPayed { + return salary, nil + } + //社保和公积金 salary.SocialDeduct = 0 salary.HouseDeduct = 0