enterprise/worker/approval.go

115 lines
2.7 KiB
Go
Raw Normal View History

2024-01-07 18:53:21 +08:00
package worker
import (
"enterprise/common/config"
"enterprise/common/dao"
"enterprise/common/model"
"enterprise/common/weixin"
log "github.com/sirupsen/logrus"
"time"
)
type Approval struct {
}
2024-01-07 22:54:33 +08:00
func (s *Approval) getTemplateId(tp string) string {
if tp == model.ApprovalTypeCheckin {
return "C4UCJS891Afmu1rE1Ws6cvph7YHqebWtt7KRFqh8c"
} else if tp == model.ApprovalTypeRefund {
return "C4UE6NT3ZE7XzER9TBk2ynHEeqA11NE2GGCuBq5yH"
} else if tp == model.ApprovalTypeVacation {
return "3WLJF6naF5jhnXvwisuPmE85wVMYcy1S1ZvYibkw"
}
return ""
}
func (s *Approval) Sync(month, tp string) {
templateId := s.getTemplateId(tp)
2024-01-07 18:53:21 +08:00
cfg := config.GetConfig().QyWeixin
approve := weixin.NewQyWeixinApprove(cfg.Corpid, cfg.ApproveSecret, cfg.ApproveAgent)
startTime, _ := time.ParseInLocation("200601", month, time.Local)
endTime := startTime.AddDate(0, 1, 0)
spNos, err := approve.GetList(startTime.Unix(), endTime.Unix()-1, templateId)
if err != nil {
log.Errorf("approve getlist error :%s", err.Error())
return
}
2024-01-07 22:54:33 +08:00
2024-01-07 18:53:21 +08:00
for _, spNo := range spNos {
detail, err := approve.GetDetail(spNo)
if err != nil {
log.Errorf("approve GetDetail error :%s", err.Error())
continue
}
2024-01-07 22:54:33 +08:00
if tp == model.ApprovalTypeCheckin {
s.saveCheckin(detail)
} else if tp == model.ApprovalTypeRefund {
s.saveRefund(detail)
} else if tp == model.ApprovalTypeVacation {
s.saveVacation(detail)
}
}
2024-01-07 18:53:21 +08:00
2024-01-07 22:54:33 +08:00
return
}
2024-01-07 18:53:21 +08:00
2024-01-07 22:54:33 +08:00
func (s *Approval) saveCheckin(detail *weixin.ApproveDetail) {
dbDao := dao.NewApprovalCheckinDao()
newData := detail.ToCheckin()
old, err := dbDao.GetBySpNo(detail.SpNo)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
if old == nil {
_, err = dbDao.Create(newData)
} else {
newData.Id = old.Id
newData.CreateTime = old.CreateTime
err = dbDao.Update(newData)
}
if err != nil {
log.Errorf("db error :%s", err.Error())
}
}
2024-01-07 18:53:21 +08:00
2024-01-07 22:54:33 +08:00
func (s *Approval) saveRefund(detail *weixin.ApproveDetail) {
dbDao := dao.NewApprovalRefundDao()
newData := detail.ToRefund()
old, err := dbDao.GetBySpNo(detail.SpNo)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
if old == nil {
_, err = dbDao.Create(newData)
} else {
newData.Id = old.Id
newData.CreateTime = old.CreateTime
err = dbDao.Update(newData)
}
if err != nil {
log.Errorf("db error :%s", err.Error())
}
}
func (s *Approval) saveVacation(detail *weixin.ApproveDetail) {
dbDao := dao.NewApprovalVacationDao()
newData := detail.ToVacation()
old, err := dbDao.GetBySpNo(detail.SpNo)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
if old == nil {
_, err = dbDao.Create(newData)
} else {
newData.Id = old.Id
newData.CreateTime = old.CreateTime
err = dbDao.Update(newData)
}
if err != nil {
log.Errorf("db error :%s", err.Error())
2024-01-07 18:53:21 +08:00
}
}