enterprise/worker/approval.go

118 lines
2.8 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"
log "github.com/sirupsen/logrus"
2024-01-22 23:21:37 +08:00
"gitlab.batiao8.com/open/gosdk/qyweixin"
2024-01-07 18:53:21 +08:00
"time"
)
type Approval struct {
}
2024-01-07 22:54:33 +08:00
func (s *Approval) getTemplateId(tp string) string {
if tp == model.ApprovalTypeCheckin {
2024-01-07 23:23:37 +08:00
return model.ApprovalTpIdCheckin
2024-01-07 22:54:33 +08:00
} else if tp == model.ApprovalTypeRefund {
2024-01-07 23:23:37 +08:00
return model.ApprovalTpIdRefund
2024-01-07 22:54:33 +08:00
} else if tp == model.ApprovalTypeVacation {
2024-01-07 23:23:37 +08:00
return model.ApprovalTpIdVacation
2024-01-07 22:54:33 +08:00
}
return ""
}
func (s *Approval) Sync(month, tp string) {
templateId := s.getTemplateId(tp)
2024-01-07 18:53:21 +08:00
cfg := config.GetConfig().QyWeixin
2024-01-22 23:21:37 +08:00
approve := qyweixin.NewAppApprove(&qyweixin.AppConfig{Corpid: cfg.Corpid, Secret: cfg.ApproveSecret, Agent: cfg.ApproveAgent})
2024-01-07 18:53:21 +08:00
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-22 23:21:37 +08:00
func (s *Approval) saveCheckin(detail *qyweixin.ApproveDetail) {
2024-01-07 22:54:33 +08:00
dbDao := dao.NewApprovalCheckinDao()
2024-01-22 23:21:37 +08:00
newData := new(model.ApprovalCheckin)
newData.From(detail)
2024-01-07 22:54:33 +08:00
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-22 23:21:37 +08:00
func (s *Approval) saveRefund(detail *qyweixin.ApproveDetail) {
2024-01-07 22:54:33 +08:00
dbDao := dao.NewApprovalRefundDao()
2024-01-22 23:21:37 +08:00
newData := new(model.ApprovalRefund)
newData.From(detail)
2024-01-07 22:54:33 +08:00
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-22 23:21:37 +08:00
func (s *Approval) saveVacation(detail *qyweixin.ApproveDetail) {
2024-01-07 22:54:33 +08:00
dbDao := dao.NewApprovalVacationDao()
2024-01-22 23:21:37 +08:00
newData := new(model.ApprovalVacation)
newData.From(detail)
2024-01-07 22:54:33 +08:00
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
}
}