diff --git a/common/dao/corp.go b/common/dao/corp.go new file mode 100644 index 0000000..3d03336 --- /dev/null +++ b/common/dao/corp.go @@ -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 +} diff --git a/common/model/approval_checkin.go b/common/model/approval_checkin.go index bd17aa3..7a4a3eb 100644 --- a/common/model/approval_checkin.go +++ b/common/model/approval_checkin.go @@ -27,6 +27,7 @@ type UsernameCount struct { type ApprovalCheckin struct { Id int64 + CorpId int64 Username string Month string SpNo string diff --git a/common/model/approval_payment.go b/common/model/approval_payment.go index 0ffe5db..12adcb6 100644 --- a/common/model/approval_payment.go +++ b/common/model/approval_payment.go @@ -10,6 +10,7 @@ import ( type ApprovalPayment struct { Id int64 + CorpId int64 Username string Month string SpNo string diff --git a/common/model/approval_refund.go b/common/model/approval_refund.go index 0ff83c1..8e8f236 100644 --- a/common/model/approval_refund.go +++ b/common/model/approval_refund.go @@ -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 diff --git a/common/model/approval_vacation.go b/common/model/approval_vacation.go index 4f79e20..7cab79b 100644 --- a/common/model/approval_vacation.go +++ b/common/model/approval_vacation.go @@ -12,6 +12,7 @@ var () type ApprovalVacation struct { Id int64 + CorpId int64 Username string Month string SpNo string diff --git a/common/model/corp.go b/common/model/corp.go new file mode 100644 index 0000000..12ec832 --- /dev/null +++ b/common/model/corp.go @@ -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 +} diff --git a/conf/server.conf.prod b/conf/server.conf.prod index 6879c2d..992ca25 100644 --- a/conf/server.conf.prod +++ b/conf/server.conf.prod @@ -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] diff --git a/server/controller/qyweixin.go b/server/controller/qyweixin.go index c6ad052..a7d3935 100644 --- a/server/controller/qyweixin.go +++ b/server/controller/qyweixin.go @@ -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, diff --git a/server/server.go b/server/server.go index d3675dc..7b018d2 100644 --- a/server/server.go +++ b/server/server.go @@ -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) diff --git a/server/service/qyweixin_approve.go b/server/service/qyweixin_approve.go index af6c0b6..8923fc3 100644 --- a/server/service/qyweixin_approve.go +++ b/server/service/qyweixin_approve.go @@ -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()) diff --git a/worker/approval.go b/worker/approval.go index 3badd8c..c919dd2 100644 --- a/worker/approval.go +++ b/worker/approval.go @@ -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 { + } } diff --git a/worker/worker.go b/worker/worker.go index cf9f5a2..e66267f 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -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号计算工资信息