enterprise/server/service/payment.go

83 lines
2.4 KiB
Go

package service
import (
"context"
"enterprise/common/config"
"enterprise/common/dao"
"enterprise/common/model"
"enterprise/server/api"
"enterprise/server/session"
log "github.com/sirupsen/logrus"
"github.com/smartwalle/alipay/v3"
"github.com/smbrave/goutil"
"github.com/spf13/cast"
"time"
)
type Payment struct {
}
func NewPayment() *Payment {
return &Payment{}
}
func (p *Payment) List(sess *session.AdminSession, req *api.ListPaymentReq) (int64, int64, interface{}) {
payments, total, err := dao.NewApprovalPaymentDao().Query(req.Page, req.Size, sess.GetCorpId(), req.Username, req.StartDay, req.EndDay, req.Payee, req.Payee)
session.CheckDBError(err)
items := make([]*api.Payment, 0)
for _, payment := range payments {
i := new(api.Payment)
i.From(payment)
items = append(items, i)
}
totalAmount, err := dao.NewApprovalPaymentDao().QueryAmount(sess.GetCorpId(), req.Username, req.StartDay, req.EndDay, req.Payee, req.Payee)
session.CheckDBError(err)
return total, totalAmount, items
}
func (p *Payment) Suggest(sess *session.AdminSession, field string) interface{} {
counts, err := dao.NewApprovalPaymentDao().Count(sess.GetCorpId(),
time.Now().AddDate(-1, 0, 0).Format("2006-01-02"), "", field)
session.CheckDBError(err)
for _, c := range counts {
c.Name = c.Key
}
return counts
}
func (p *Payment) AlipayAuth(sess *session.AdminSession, authCode, state string) (string, *model.StaffUser) {
corp, err := dao.NewCorpDao().GetByHost(sess.GetHeader().Host)
session.CheckDBError(err)
session.CheckNilError(corp)
cli := config.GetAliPayClient("batiao")
var req alipay.SystemOauthToken
req.Code = authCode
req.GrantType = "authorization_code"
res, err := cli.SystemOauthToken(context.Background(), req)
if err != nil {
log.Errorf("error :%s", err.Error())
panic(config.ErrInternal.New().Append(err))
}
if !res.IsSuccess() {
log.Errorf(goutil.EncodeJSON(res))
panic(config.ErrInternal.New().Append(goutil.EncodeJSON(res)))
}
staffUser, err := dao.NewStaffUserDao().Get(cast.ToInt64(state))
session.CheckDBError(err)
session.CheckNilError(staffUser)
payee := staffUser.GetPayee()
oldUid := payee.AlipayUid
payee.AlipayUid = res.UserId
staffUser.Payee = goutil.EncodeJSON(payee)
err = dao.NewStaffUserDao().Update(staffUser)
session.CheckDBError(err)
log.Errorf("staffuser[%d] alipayUid[%s]->alipayUid[%s]", staffUser.Id, oldUid, res.UserId)
return res.UserId, staffUser
}