enterprise/worker/approval.go

115 lines
2.7 KiB
Go

package worker
import (
"enterprise/common/config"
"enterprise/common/dao"
"enterprise/common/model"
"git.u8t.cn/open/gosdk/qyweixin"
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 := qyweixin.NewAppApprove(&qyweixin.AppConfig{Corpid: cfg.Corpid, Secret: cfg.ApproveSecret, Agent: 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 detail.SpStatus != 2 {
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 *qyweixin.ApproveDetail) {
dbDao := dao.NewApprovalCheckinDao()
newData := new(model.ApprovalCheckin)
newData.From(detail)
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 *qyweixin.ApproveDetail) {
dbDao := dao.NewApprovalRefundDao()
newData := new(model.ApprovalRefund)
newData.From(detail)
old, err := dbDao.GetBySpNo(detail.SpNo)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
if old != nil {
return
}
_, err = dbDao.Create(newData)
if err != nil {
log.Errorf("db error :%s", err.Error())
}
}
func (s *Approval) saveVacation(detail *qyweixin.ApproveDetail) {
dbDao := dao.NewApprovalVacationDao()
newData := new(model.ApprovalVacation)
newData.From(detail)
old, err := dbDao.GetBySpNo(detail.SpNo)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
if old != nil {
return
}
_, err = dbDao.Create(newData)
if err != nil {
log.Errorf("db error :%s", err.Error())
}
}