enterprise/worker/approval.go

115 lines
2.7 KiB
Go

package worker
import (
"enterprise/common/config"
"enterprise/common/dao"
"enterprise/common/model"
"enterprise/common/weixin"
log "github.com/sirupsen/logrus"
"time"
)
type Approval struct {
}
func (s *Approval) getTemplateId(tp string) string {
if tp == model.ApprovalTypeCheckin {
return model.ApprovalTpIdCheckin
} else if tp == model.ApprovalTypeRefund {
return model.ApprovalTpIdRefund
} else if tp == model.ApprovalTypeVacation {
return model.ApprovalTpIdVacation
}
return ""
}
func (s *Approval) Sync(month, tp string) {
templateId := s.getTemplateId(tp)
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
}
for _, spNo := range spNos {
detail, err := approve.GetDetail(spNo)
if err != nil {
log.Errorf("approve GetDetail error :%s", err.Error())
continue
}
if tp == model.ApprovalTypeCheckin {
s.saveCheckin(detail)
} else if tp == model.ApprovalTypeRefund {
s.saveRefund(detail)
} else if tp == model.ApprovalTypeVacation {
s.saveVacation(detail)
}
}
return
}
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())
}
}
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())
}
}