This commit is contained in:
jiangyong27 2025-01-08 12:35:29 +08:00
parent f3cc2dc3aa
commit 21965eca8b
12 changed files with 166 additions and 41 deletions

51
common/dao/corp.go Normal file
View File

@ -0,0 +1,51 @@
package dao
import (
"enterprise/common/model"
"gorm.io/gorm"
"time"
)
type CorpDao struct {
}
func NewCorpDao() *CorpDao {
return &CorpDao{}
}
func (d *CorpDao) TableName() string {
return "corp"
}
func (d *CorpDao) Create(o *model.Corp) (int64, error) {
o.CreateTime = time.Now().Unix()
res := GetDB().Table(d.TableName()).Create(o)
return o.Id, res.Error
}
func (d *CorpDao) Update(o *model.Corp) error {
o.UpdateTime = time.Now().Unix()
tx := GetDB().Table(d.TableName())
res := tx.Save(o)
return res.Error
}
func (d *CorpDao) Delete(id int64) error {
res := GetDB().Table(d.TableName()).Delete(&model.Corp{}, id)
return res.Error
}
func (d *CorpDao) Get(id int64) (*model.Corp, error) {
var u model.Corp
tx := GetDB().Table(d.TableName())
tx = tx.Where("id = ?", id)
res := tx.First(&u)
if res.Error == gorm.ErrRecordNotFound {
return nil, nil
}
if res.Error != nil {
return nil, res.Error
}
return &u, nil
}

View File

@ -27,6 +27,7 @@ type UsernameCount struct {
type ApprovalCheckin struct {
Id int64
CorpId int64
Username string
Month string
SpNo string

View File

@ -10,6 +10,7 @@ import (
type ApprovalPayment struct {
Id int64
CorpId int64
Username string
Month string
SpNo string

View File

@ -13,15 +13,12 @@ var (
ApprovalTypeRefund = "refund"
ApprovalTypeCheckin = "checkin"
ApprovalTypeVacation = "vacation"
ApprovalTpIdCheckin = "C4UCJS891Afmu1rE1Ws6cvph7YHqebWtt7KRFqh8c"
ApprovalTpIdRefund = "C4UE6NT3ZE7XzER9TBk2ynHEeqA11NE2GGCuBq5yH"
ApprovalTpIdVacation = "3WLJF6naF5jhnXvwisuPmE85wVMYcy1S1ZvYibkw"
ApprovalTpIdPayment = "C4WqcQBRQZoB5d15uKtjwuG6k32mc7ykJYuLF1ois"
ApprovalTypePayment = "payment"
)
type ApprovalRefund struct {
Id int64
CorpId int64
Username string
Month string
SpNo string

View File

@ -12,6 +12,7 @@ var ()
type ApprovalVacation struct {
Id int64
CorpId int64
Username string
Month string
SpNo string

32
common/model/corp.go Normal file
View File

@ -0,0 +1,32 @@
package model
import "encoding/json"
type CorpConfig struct {
CorpId string `json:"corpid"`
EnterpriseAgent string `json:"enterprise_agent"`
EnterpriseSecret string `json:"enterprise_secret"`
ApproveAgent string `json:"approve_agent"`
ApproveSecret string `json:"approve_secret"`
PaySecret string `json:"pay_secret"`
PayAgent string `json:"pay_agent"`
TplIdCheckin string `json:"tplid_checkin"`
TplIdRefund string `json:"tplid_refund"`
TplIdVacation string `json:"tplid_vacation"`
TplIdPayment string `json:"tplid_payment"`
}
type Corp struct {
Id int64
Name string
Config string
CreateTime int64
UpdateTime int64
}
func (c *Corp) GetConfig() *CorpConfig {
var cfg CorpConfig
json.Unmarshal([]byte(c.Config), &cfg)
return &cfg
}

View File

@ -23,14 +23,6 @@ addr="127.0.0.1:6379"
db=0
password=""
[qyweixin]
corpid = "ww43c49db2e88a17f8"
enterprise_agent = "1000009"
enterprise_secret = "oMB24UhKe50-XPTg7vhnwoTuhEXaq5XeiHPAUtF4hOs"
approve_agent = "3010040"
approve_secret = "xJOClC5V2pPon1azgrAzf5kq1TB72xZ3ScR7O5G3lQo"
pay_secret = "JCGsxntR4E7wrEEQvWGr8_wdKtRlw48n-W6zd8lbwc4"
pay_agent = "3010046"
[wxpay]

View File

@ -2,6 +2,7 @@ package controller
import (
"enterprise/common/config"
"enterprise/common/dao"
"enterprise/common/global"
"enterprise/server/service"
"enterprise/server/session"
@ -21,8 +22,10 @@ type QyWeixin struct {
func (q *QyWeixin) Approve(ctx *gin.Context) {
cfg := config.GetConfig()
cid := cast.ToInt64(ctx.Param("cid"))
reply := new(service.Approve).Reply
approve := service.NewApprove(cid)
reply := approve.Reply
qyApp := qyweixin.NewApp(&qyweixin.AppConfig{
Corpid: cfg.QyWeixin.Corpid,
Secret: cfg.QyWeixin.ApproveSecret,
@ -36,6 +39,7 @@ func (q *QyWeixin) Approve(ctx *gin.Context) {
}
func (q *QyWeixin) Pay(ctx *gin.Context) {
cid := cast.ToInt64(ctx.Param("cid"))
userid := ctx.Query("userid")
amount := cast.ToInt64(ctx.Query("amount"))
title := ctx.Query("title")
@ -43,11 +47,21 @@ func (q *QyWeixin) Pay(ctx *gin.Context) {
if pass != "1c9dea1fb85cf06ab0b4d946d49aa92f" {
panic("password error")
}
corp, err := dao.NewCorpDao().Get(cid)
if err != nil {
panic(config.ErrDb.New().Append(err))
}
if corp == nil {
panic(config.ErrInternal.New())
}
cfg := corp.GetConfig()
// 获取openid
approve := qyweixin.NewAppApprove(&qyweixin.AppConfig{
Corpid: config.GetConfig().QyWeixin.Corpid,
Secret: config.GetConfig().QyWeixin.EnterpriseSecret,
Agent: config.GetConfig().QyWeixin.EnterpriseAgent,
Corpid: cfg.CorpId,
Secret: cfg.EnterpriseSecret,
Agent: cfg.EnterpriseAgent,
})
openid, err := approve.GetOpenid(userid)
if err != nil {
@ -61,9 +75,9 @@ func (q *QyWeixin) Pay(ctx *gin.Context) {
req.Openid = openid
req.TotalAmount = amount
qyPay := qyweixin.NewAppPay(&qyweixin.PayConfig{
Corpid: config.GetConfig().QyWeixin.Corpid,
Secret: config.GetConfig().QyWeixin.PaySecret,
Agent: config.GetConfig().QyWeixin.PayAgent,
Corpid: cfg.CorpId,
Secret: cfg.PaySecret,
Agent: cfg.PayAgent,
SerialNumber: config.GetConfig().WxPay.PaySerialNumber,
ApiKey: config.GetConfig().WxPay.PayApiKeyV2,
MchId: config.GetConfig().WxPay.PayMchId,

View File

@ -14,8 +14,8 @@ func initRoutge(engine *gin.Engine) {
group := engine.Group("/")
apiGroup.Use(base.Recovery)
group.Use(base.Recovery)
apiGroup.Any("/qyweixin/approve", qyweixin.Approve)
apiGroup.Any("/qyweixin/pay", qyweixin.Pay)
apiGroup.Any("/qyweixin/approve/:cid", qyweixin.Approve)
apiGroup.Any("/qyweixin/pay/:cid", qyweixin.Pay)
group.GET("/staff/salary", staff.Salary)
group.GET("/staff/salary/history", staff.SalaryHistory)

View File

@ -19,23 +19,33 @@ import (
)
var (
SpStatusCreated = 1
SpStatusPassed = 2
SpStatusRefused = 3
SpStatusCanceled = 4
SpStatusCreated = 1
SpStatusPassed = 2
SpStatusRefused = 3
SpStatusCanceled = 4
SpStatusPassedCanceled = 6
)
type Approve struct {
corp *model.Corp
corpConfig *model.CorpConfig
approveClient *qyweixin.AppApprove
}
func NewApprove(cid int64) *Approve {
corp, _ := dao.NewCorpDao().Get(cid)
return &Approve{
corp: corp,
corpConfig: corp.GetConfig(),
}
}
func (b *Approve) Reply(msg message.MixMessage) *message.Reply {
if b.approveClient == nil {
cfg := config.GetConfig()
b.approveClient = qyweixin.NewAppApprove(&qyweixin.AppConfig{
Corpid: cfg.QyWeixin.Corpid,
Secret: cfg.QyWeixin.ApproveSecret,
Agent: cfg.QyWeixin.ApproveAgent,
Corpid: b.corpConfig.CorpId,
Secret: b.corpConfig.ApproveSecret,
Agent: b.corpConfig.ApproveAgent,
})
}
go b.handle(&msg)
@ -63,6 +73,8 @@ func (a *Approve) handleApprovalChange(msg *message.MixMessage) {
spStatus := msg.ApprovalInfo.SpStatus
spNo := msg.ApprovalInfo.SpNo
templateId := msg.ApprovalInfo.TemplateId
// && spStatus != SpStatusPassedCanceled
if spStatus != SpStatusPassed {
return
}
@ -74,13 +86,13 @@ func (a *Approve) handleApprovalChange(msg *message.MixMessage) {
}
log.Infof("spno: %s detail: %s", spNo, goutil.EncodeJSON(detail))
if templateId == model.ApprovalTpIdRefund {
if templateId == a.corpConfig.TplIdRefund {
a.handleRefund(detail)
} else if templateId == model.ApprovalTpIdVacation {
} else if templateId == a.corpConfig.TplIdVacation {
a.handleVacation(detail)
} else if templateId == model.ApprovalTpIdCheckin {
} else if templateId == a.corpConfig.TplIdCheckin {
a.handleCheckin(detail)
} else if templateId == model.ApprovalTpIdPayment {
} else if templateId == a.corpConfig.TplIdPayment {
a.handlePayment(detail)
}
}
@ -88,6 +100,7 @@ func (a *Approve) handleApprovalChange(msg *message.MixMessage) {
func (a *Approve) handlePayment(detail *qyweixin.ApproveDetail) {
newData := new(model.ApprovalPayment)
newData.From(detail)
newData.CorpId = a.corp.Id
dbDao := dao.NewApprovalPaymentDao()
old, err := dbDao.GetBySpNo(detail.SpNo)
@ -112,6 +125,7 @@ func (a *Approve) handlePayment(detail *qyweixin.ApproveDetail) {
func (a *Approve) handleVacation(detail *qyweixin.ApproveDetail) {
newData := new(model.ApprovalVacation)
newData.From(detail)
newData.CorpId = a.corp.Id
dbDao := dao.NewApprovalVacationDao()
old, err := dbDao.GetBySpNo(detail.SpNo)
@ -136,6 +150,7 @@ func (a *Approve) handleVacation(detail *qyweixin.ApproveDetail) {
func (a *Approve) handleCheckin(detail *qyweixin.ApproveDetail) {
newData := new(model.ApprovalCheckin)
newData.From(detail)
newData.CorpId = a.corp.Id
dbDao := dao.NewApprovalCheckinDao()
old, err := dbDao.GetBySpNo(detail.SpNo)
@ -170,9 +185,9 @@ func (a *Approve) handleRefundWxpay(data *model.ApprovalRefund) error {
req.Openid = openid
req.TotalAmount = int64(100 * data.RefundAmount)
qyPay := qyweixin.NewAppPay(&qyweixin.PayConfig{
Corpid: config.GetConfig().QyWeixin.Corpid,
Secret: config.GetConfig().QyWeixin.PaySecret,
Agent: config.GetConfig().QyWeixin.PayAgent,
Corpid: a.corpConfig.CorpId,
Secret: a.corpConfig.PaySecret,
Agent: a.corpConfig.PayAgent,
SerialNumber: config.GetConfig().WxPay.PaySerialNumber,
ApiKey: config.GetConfig().WxPay.PayApiKeyV2,
MchId: config.GetConfig().WxPay.PayMchId,
@ -215,6 +230,7 @@ func (a *Approve) handleRefundAlipay(staff *model.StaffInfo, data *model.Approva
func (a *Approve) handleRefund(detail *qyweixin.ApproveDetail) {
newData := new(model.ApprovalRefund)
newData.From(detail)
newData.CorpId = a.corp.Id
dbDao := dao.NewApprovalRefundDao()
old, err := dbDao.GetBySpNo(detail.SpNo)
@ -235,6 +251,10 @@ func (a *Approve) handleRefund(detail *qyweixin.ApproveDetail) {
return
}
//只有八条科技才自动付款
if a.corp.Id != 1000 {
return
}
staff, err := dao.NewStaffInfoDao().GetByUsername(newData.Username)
if err != nil {
log.Errorf("db error :%s", err.Error())

View File

@ -10,15 +10,25 @@ import (
)
type Approval struct {
corp *model.Corp
corpConfig *model.CorpConfig
}
func NewApproval(corp *model.Corp) *Approval {
return &Approval{
corp: corp,
corpConfig: corp.GetConfig(),
}
}
func (s *Approval) getTemplateId(tp string) string {
if tp == model.ApprovalTypeCheckin {
return model.ApprovalTpIdCheckin
return s.corpConfig.TplIdCheckin
} else if tp == model.ApprovalTypeRefund {
return model.ApprovalTpIdRefund
return s.corpConfig.TplIdRefund
} else if tp == model.ApprovalTypeVacation {
return model.ApprovalTpIdVacation
return s.corpConfig.TplIdVacation
} else if tp == model.ApprovalTypePayment {
return s.corpConfig.TplIdPayment
}
return ""
}
@ -50,6 +60,8 @@ func (s *Approval) Sync(month, tp string) {
s.saveRefund(detail)
} else if tp == model.ApprovalTypeVacation {
s.saveVacation(detail)
} else if tp == model.ApprovalTypePayment {
}
}

View File

@ -1,6 +1,7 @@
package worker
import (
"enterprise/common/dao"
"enterprise/common/model"
"github.com/go-co-op/gocron"
"time"
@ -11,7 +12,6 @@ func Init() error {
cron := gocron.NewScheduler(timezone)
staff := new(Staff)
checkIn := new(Checkin)
approve := new(Approval)
// 每天同步企业人事信息
cron.Every(1).Day().At("01:00").Do(func() {
@ -33,9 +33,13 @@ func Init() error {
cron.Every(1).Month(1).At("05:30").Do(func() {
lastMonth := time.Now().AddDate(0, -1, 0).Format("200601")
checkIn.SyncCheckinMonth("")
corp, _ := dao.NewCorpDao().Get(1000)
approve := NewApproval(corp)
approve.Sync(lastMonth, model.ApprovalTypeCheckin)
approve.Sync(lastMonth, model.ApprovalTypeVacation)
approve.Sync(lastMonth, model.ApprovalTypeRefund)
approve.Sync(lastMonth, model.ApprovalTypePayment)
})
// 1号计算工资信息