diff --git a/server/api/salary.go b/server/api/salary.go index e0cf044..bbbefb7 100644 --- a/server/api/salary.go +++ b/server/api/salary.go @@ -43,6 +43,11 @@ type UpdateSalaryReq struct { Status string `json:"status"` } +type PaySalaryReq struct { + SalaryId string `json:"salary_id"` + PayType string `json:"pay_type"` +} + type ListSalaryReq struct { BaseRequest StartMonth string `form:"start_month"` diff --git a/server/api/staff.go b/server/api/staff.go index fa33b83..3df29af 100644 --- a/server/api/staff.go +++ b/server/api/staff.go @@ -52,6 +52,13 @@ type StaffCreateReq struct { Username string `json:"username"` } +type StaffPayReq struct { + StaffId string `json:"staff_id"` + PayType string `json:"pay_type"` + Title string `json:"title"` + Amount string `json:"amount"` +} + type StaffUpdateReq struct { Id string `json:"id"` Phone string `json:"phone"` diff --git a/server/controller/payment.go b/server/controller/payment.go index 36503ec..8c528dc 100644 --- a/server/controller/payment.go +++ b/server/controller/payment.go @@ -46,8 +46,11 @@ func (q *Payment) Pay(ctx *gin.Context) { if req.Password != corp.GetConfig().Password { panic(config.ErrParam.New().Append("密码错误")) } + user, err := dao.NewStaffUserDao().GetByUsername(corp.Id, req.Username) + session.CheckDBError(err) + session.CheckNilError(user, fmt.Sprintf("用户[%s]不存在", req.Username)) - service.NewPay().Pay(corp, &req) + service.NewPay().Pay(corp, user, req.Title, req.PayType, req.Amount) ctx.JSON(http.StatusOK, session.NewRspOk()) } diff --git a/server/controller/salary.go b/server/controller/salary.go index 146eb18..126e27a 100644 --- a/server/controller/salary.go +++ b/server/controller/salary.go @@ -40,6 +40,14 @@ func (s *Salary) Update(ctx *gin.Context) { ctx.JSON(http.StatusOK, session.NewRspOk()) } +func (s *Salary) Pay(ctx *gin.Context) { + var req api.PaySalaryReq + session.CheckParamError(ctx.ShouldBind(&req)) + ctx.Keys[session.ContextRequest] = req + service.NewStaffSalary().Pay(ctx.Keys[session.ContextSession].(*session.AdminSession), &req) + ctx.JSON(http.StatusOK, session.NewRspOk()) +} + func (s *Salary) Delete(ctx *gin.Context) { id := cast.ToInt64(ctx.Query("id")) dao.NewStaffSalaryDao().Delete(id) diff --git a/server/controller/staff.go b/server/controller/staff.go index 8b5a9ab..2af43fd 100644 --- a/server/controller/staff.go +++ b/server/controller/staff.go @@ -61,6 +61,15 @@ func (s *Staff) Create(ctx *gin.Context) { ctx.JSON(http.StatusOK, session.NewRspOk()) } +func (s *Staff) Pay(ctx *gin.Context) { + sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + var req api.StaffPayReq + session.CheckParamError(ctx.ShouldBindJSON(&req)) + ctx.Keys[session.ContextRequest] = req + service.NewStaffUser().Pay(sess, &req) + ctx.JSON(http.StatusOK, session.NewRspOk()) +} + func (s *Staff) Update(ctx *gin.Context) { var req api.StaffUpdateReq sess := ctx.Keys[session.ContextSession].(*session.AdminSession) diff --git a/server/server.go b/server/server.go index ddb7d18..ff297ba 100644 --- a/server/server.go +++ b/server/server.go @@ -34,10 +34,12 @@ func initRoutge(engine *gin.Engine) { apiGroup.GET("/staff/salary", controller.NewSalary().List) apiGroup.POST("/staff/salary", controller.NewSalary().Create) + apiGroup.POST("/staff/salary/pay", controller.NewSalary().Pay) apiGroup.PUT("/staff/salary", controller.NewSalary().Update) apiGroup.DELETE("/staff/salary", controller.NewSalary().Delete) apiGroup.GET("/staff/suggest", controller.NewStaff().Suggest) + apiGroup.POST("/staff/pay", controller.NewStaff().Pay) apiGroup.POST("/staff", controller.NewStaff().Create) apiGroup.PUT("/staff", controller.NewStaff().Update) apiGroup.DELETE("/staff", controller.NewStaff().Delete) diff --git a/server/service/pay.go b/server/service/pay.go index 5d998bf..500bc7a 100644 --- a/server/service/pay.go +++ b/server/service/pay.go @@ -6,7 +6,6 @@ import ( "enterprise/common/dao" "enterprise/common/global" "enterprise/common/model" - "enterprise/server/api" "enterprise/server/session" "fmt" "git.u8t.cn/open/gosdk/qyweixin" @@ -58,52 +57,48 @@ func (p *Pay) AlipayAuth(sess *session.AdminSession, authCode, state string) (st return res.UserId, staffUser } -func (p *Pay) Pay(corp *model.Corp, req *api.PayReq) { - - user, err := dao.NewStaffUserDao().GetByUsername(corp.Id, req.Username) - session.CheckDBError(err) - session.CheckNilError(user, fmt.Sprintf("用户[%s]不存在", req.Username)) - - payType := "" +func (p *Pay) Pay(corp *model.Corp, user *model.StaffUser, title, payType string, amount int64) { + var err error + realPayType := "" payee := user.GetPayee() - if req.PayType == "alipay" { + if payType == "alipay" { if payee.AlipayUid == "" { panic("请绑定支付宝账号") } - err = p.payAlipay(corp, user, req) - payType = "alipay" + err = p.payAlipay(corp, user, title, amount) + realPayType = "alipay" } else { - err = p.payWeixin(corp, user, req) - payType = "weixin" + err = p.payWeixin(corp, user, title, amount) + realPayType = "weixin" } message := make([]string, 0) - message = append(message, fmt.Sprintf("【企业转账】[%s]", req.Username)) - message = append(message, fmt.Sprintf("发放金额:%s", goutil.FormatMoney(req.Amount))) - message = append(message, fmt.Sprintf("支付类型:%s", payType)) - message = append(message, fmt.Sprintf("员工名称:%s", req.Username)) - message = append(message, fmt.Sprintf("费用说明:%s", req.Title)) + message = append(message, fmt.Sprintf("【企业转账】[%s]", user.Username)) + message = append(message, fmt.Sprintf("发放金额:%s", goutil.FormatMoney(amount))) + message = append(message, fmt.Sprintf("支付类型:%s", realPayType)) + message = append(message, fmt.Sprintf("员工名称:%s", user.Username)) + message = append(message, fmt.Sprintf("费用说明:%s", title)) if err != nil { message = append(message, fmt.Sprintf("错误信息:%s", err.Error())) global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")) - log.Errorf("pay req[%s] error :%s", goutil.EncodeJSON(req), err.Error()) + log.Errorf("pay req[%s:%d] error :%s", title, amount, err.Error()) panic(config.ErrInternal.New().Append(err)) } global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")) } -func (p *Pay) payAlipay(corp *model.Corp, user *model.StaffUser, req *api.PayReq) error { +func (p *Pay) payAlipay(corp *model.Corp, user *model.StaffUser, title string, amount int64) error { corpConfig := corp.GetConfig() userPayee := user.GetPayee() cli := config.GetAliPayClient(goutil.If(corpConfig.PayChannel != "", corpConfig.PayChannel, "batiao")) var payReq alipay.FundTransUniTransfer - payReq.Remark = req.Title + payReq.Remark = title payReq.OutBizNo = cast.ToString(goutil.GetBigID(0, 0)) - payReq.TransAmount = goutil.FormatMoney(req.Amount) + payReq.TransAmount = goutil.FormatMoney(amount) payReq.ProductCode = "TRANS_ACCOUNT_NO_PWD" payReq.BizScene = "DIRECT_TRANSFER" - payReq.OrderTitle = req.Title + payReq.OrderTitle = title payee := new(alipay.PayeeInfo) payReq.PayeeInfo = payee payee.IdentityType = "ALIPAY_USER_ID" @@ -120,7 +115,7 @@ func (p *Pay) payAlipay(corp *model.Corp, user *model.StaffUser, req *api.PayReq return nil } -func (p *Pay) payWeixin(corp *model.Corp, user *model.StaffUser, req *api.PayReq) error { +func (p *Pay) payWeixin(corp *model.Corp, user *model.StaffUser, title string, amount int64) error { cfg := corp.GetConfig() @@ -130,18 +125,18 @@ func (p *Pay) payWeixin(corp *model.Corp, user *model.StaffUser, req *api.PayReq Secret: cfg.EnterpriseSecret, Agent: cfg.EnterpriseAgent, }) - openid, err := approve.GetOpenid(req.Username) + openid, err := approve.GetOpenid(user.Username) if err != nil { - log.Errorf("GetOpenid[%s] error :%s ", req.Username, err.Error()) + log.Errorf("GetOpenid[%s] error :%s ", user.Username, err.Error()) return err } // 支付费用 var payReq qyweixin.PayReq payReq.BillNo = cast.ToString(goutil.GetBigID(0, 0)) - payReq.Title = req.Title + payReq.Title = title payReq.Openid = openid - payReq.TotalAmount = req.Amount + payReq.TotalAmount = amount qyPay := qyweixin.NewAppPay(&qyweixin.PayConfig{ Corpid: cfg.CorpId, Secret: cfg.PaySecret, @@ -154,7 +149,7 @@ func (p *Pay) payWeixin(corp *model.Corp, user *model.StaffUser, req *api.PayReq }) if err = qyPay.PayMoney(&payReq); err != nil { - log.Errorf("pay req[%s] error :%s", goutil.EncodeJSON(req), err.Error()) + log.Errorf("pay req[%s:%d] error :%s", title, amount, err.Error()) return err } return nil diff --git a/server/service/staff_salary.go b/server/service/staff_salary.go index 4c49e72..967fbf2 100644 --- a/server/service/staff_salary.go +++ b/server/service/staff_salary.go @@ -11,6 +11,7 @@ import ( "fmt" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" + "github.com/smbrave/goutil" "github.com/spf13/cast" excelize "github.com/xuri/excelize/v2" "os" @@ -123,6 +124,33 @@ func (s *StaffSalary) Update(sess *session.AdminSession, req *api.UpdateSalaryRe session.CheckDBError(err) } +func (s *StaffSalary) Pay(sess *session.AdminSession, req *api.PaySalaryReq) { + + salary, err := dao.NewStaffSalaryDao().Get(cast.ToInt64(req.SalaryId)) + session.CheckDBError(err) + session.CheckNilError(salary, "工资单") + if salary.Status == model.StaffSalaryStatusPayed { + panic("工资单已支付") + } + + corp, err := dao.NewCorpDao().Get(sess.GetCorpId()) + session.CheckDBError(err) + session.CheckNilError(corp, "企业不存在") + + user, err := dao.NewStaffUserDao().Get(salary.UserId) + session.CheckDBError(err) + session.CheckNilError(user, "用户不存在") + + title := fmt.Sprintf("%s工资", salary.Month) + + log.Errorf("title[%s] stalary:%s req:%s", title, goutil.EncodeJSON(salary), goutil.EncodeJSON(req)) + salary.Status = model.StaffSalaryStatusPayed + dao.NewStaffSalaryDao().Update(salary) + + return + //NewPay().Pay(corp, user, title, req.PayType, int64(salary.GetRealSalary()*100)) +} + func (s *StaffSalary) Agent(cid int64, month string, ctx *gin.Context) { xls := ctx.Query("xls") staffSalarys, err := dao.NewStaffSalaryDao().QueryAll(cid, month) diff --git a/server/service/staff_user.go b/server/service/staff_user.go index 838b42d..dbbbf6a 100644 --- a/server/service/staff_user.go +++ b/server/service/staff_user.go @@ -189,6 +189,21 @@ func (s *StaffUser) Update(sess *session.AdminSession, req *api.StaffUpdateReq) session.CheckDBError(err) } +func (s *StaffUser) Pay(sess *session.AdminSession, req *api.StaffPayReq) { + corp, err := dao.NewCorpDao().Get(sess.GetCorpId()) + session.CheckDBError(err) + session.CheckNilError(corp, "企业不存在") + + var payReq api.PayReq + payReq.Title = req.Title + payReq.Amount = cast.ToInt64(req.Amount) + + user, err := dao.NewStaffUserDao().Get(cast.ToInt64(req.StaffId)) + session.CheckDBError(err) + session.CheckNilError(user, "用户不存在") + NewPay().Pay(corp, user, req.Title, req.PayType, cast.ToInt64(req.Amount)) +} + func (s *StaffUser) getHrAssiant(corpId int64) *qyweixin.AppHr { corp, err := dao.NewCorpDao().Get(corpId) session.CheckDBError(err)