diff --git a/cmd/enterprise.go b/cmd/enterprise.go index 111fc07..526df9a 100644 --- a/cmd/enterprise.go +++ b/cmd/enterprise.go @@ -5,9 +5,10 @@ import ( "enterprise/common/global" "enterprise/server" "enterprise/worker" + "time" ) -func main() { +func main1() { config.LoadServerConfig() global.InitGlobal() @@ -20,8 +21,12 @@ func main() { } } -func main2() { +func main() { config.LoadServerConfig() global.InitGlobal() //cfg := config.GetConfig() + for i := 0; i < 10; i++ { + new(worker.Approval).SyncCheckinMonth(time.Now().AddDate(0, 0-i, 0).Format("200601")) + } + } diff --git a/common/dao/approval_checkin.go b/common/dao/approval_checkin.go new file mode 100644 index 0000000..e0e3c27 --- /dev/null +++ b/common/dao/approval_checkin.go @@ -0,0 +1,66 @@ +package dao + +import ( + "enterprise/common/model" + "gorm.io/gorm" + "time" +) + +type ApprovalCheckinDao struct { +} + +func NewApprovalCheckinDao() *ApprovalCheckinDao { + return &ApprovalCheckinDao{} +} + +func (d *ApprovalCheckinDao) TableName() string { + return "approval_checkin" +} + +func (d *ApprovalCheckinDao) Create(o *model.ApprovalCheckin) (int64, error) { + o.CreateTime = time.Now().Unix() + res := GetDB().Table(d.TableName()).Create(o) + return o.Id, res.Error +} + +func (d *ApprovalCheckinDao) Update(o *model.ApprovalCheckin) error { + o.UpdateTime = time.Now().Unix() + tx := GetDB().Table(d.TableName()) + res := tx.Save(o) + return res.Error +} + +func (d *ApprovalCheckinDao) Delete(id int64) error { + res := GetDB().Table(d.TableName()).Delete(&model.ApprovalCheckin{}, id) + return res.Error +} + +func (d *ApprovalCheckinDao) Get(id int64) (*model.ApprovalCheckin, error) { + var u model.ApprovalCheckin + 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 +} + +func (d *ApprovalCheckinDao) GetBySpNo(spNo string) (*model.ApprovalCheckin, error) { + var u model.ApprovalCheckin + tx := GetDB().Table(d.TableName()) + tx = tx.Where("sp_no = ?", spNo) + 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 new file mode 100644 index 0000000..5c2d866 --- /dev/null +++ b/common/model/approval_checkin.go @@ -0,0 +1,16 @@ +package model + +var () + +type ApprovalCheckin struct { + Id int64 + Username string + SpNo string + CheckinType string + CheckinDate string + CheckinTime string + CheckinRemark string + ApplyTime string + CreateTime int64 + UpdateTime int64 +} diff --git a/common/model/approval_refund.go b/common/model/approval_refund.go index d820fe5..d255c01 100644 --- a/common/model/approval_refund.go +++ b/common/model/approval_refund.go @@ -13,7 +13,7 @@ type ApprovalRefund struct { RefundDate string RefundAmount float64 RefundRemark string - ApplyTime int64 + ApplyTime string Status int CreateTime int64 UpdateTime int64 diff --git a/common/weixin/qyweixin_approve.go b/common/weixin/qyweixin_approve.go index aaa4443..6afcf69 100644 --- a/common/weixin/qyweixin_approve.go +++ b/common/weixin/qyweixin_approve.go @@ -50,7 +50,8 @@ type ApplyValue struct { Files []struct { FileId string `json:"file_id"` } `json:"files"` - Vacation *Vacation `json:"vacation"` + Vacation *Vacation `json:"vacation"` + PunchCorrection interface{} `json:"punch_correction"` } type ApplyContent struct { @@ -103,16 +104,31 @@ func (d *ApproveDetail) GetValue(title string) string { value = content.Value.NewMoney } else if content.Control == "File" { value = content.Value.Files[0].FileId - } else if content.Control == "Vacation" { + } else if content.Control == "Vacation" { //请假 : 请假类型,请假时长 tp := content.Value.Vacation.Selector.Options[0].Value[0].Text duration := cast.ToString(content.Value.Vacation.Attendance.DateRange.NewDuration) value = tp + "," + duration + } else if content.Control == "PunchCorrection" { //补卡:日期,时间,状态 + if d.SpNo == "202312010001" { + fmt.Println(goutil.EncodeJSON(d.ApplyData)) + } + mp := cast.ToStringMap(content.Value.PunchCorrection) + ddate := cast.ToString(mp["daymonthyear"]) + dtime := cast.ToString(mp["time"]) + if ddate == "" { + ddate = dtime + } + value = ddate + "," + dtime + "," + cast.ToString(mp["state"]) } return value } return "" } +func (d *ApproveDetail) String() string { + return goutil.EncodeJSONIndent(d) +} + func (d *ApproveDetail) GetUserid() string { return d.Applyer.Userid } @@ -135,6 +151,10 @@ func (q *QyWeixinApprove) GetDetail(spNo string) (*ApproveDetail, error) { return nil, err } var rsp ApproveDetailRsp + + mp := make(map[string]interface{}) + json.Unmarshal(rspBody, &mp) + //fmt.Println(goutil.EncodeJSONIndent(mp)) if err := json.Unmarshal(rspBody, &rsp); err != nil { log.Errorf("get body[%s] json error :%s", string(rspBody), err.Error()) return nil, err diff --git a/server/service/qyweixin_approve.go b/server/service/qyweixin_approve.go index 3d2b9bc..d78a4df 100644 --- a/server/service/qyweixin_approve.go +++ b/server/service/qyweixin_approve.go @@ -28,8 +28,10 @@ type Approve struct { } func (b *Approve) Reply(msg message.MixMessage) *message.Reply { - cfg := config.GetConfig() - b.approveClient = weixin.NewQyWeixinApprove(cfg.QyWeixin.Corpid, cfg.QyWeixin.ApproveSecret, cfg.QyWeixin.ApproveAgent) + if b.approveClient == nil { + cfg := config.GetConfig() + b.approveClient = weixin.NewQyWeixinApprove(cfg.QyWeixin.Corpid, cfg.QyWeixin.ApproveSecret, cfg.QyWeixin.ApproveAgent) + } go b.handle(&msg) return &message.Reply{message.MsgTypeText, message.NewText("")} } @@ -58,10 +60,13 @@ func (a *Approve) handleApprovalChange(msg *message.MixMessage) { if spStatus != SpStatusPassed { return } + if spName == "费用报销" { a.handleRefund(spNo) } else if spName == "请假" { a.handleHoliday(spNo) + } else if spName == "打卡补卡" { + a.handleCheckin(spNo) } } @@ -69,6 +74,9 @@ func (a *Approve) handleHoliday(spNo string) { } +func (a *Approve) handleCheckin(spNo string) { + +} func (a *Approve) handleRefund(spNo string) { detail, err := a.approveClient.GetDetail(spNo) if err != nil { @@ -85,36 +93,17 @@ func (a *Approve) handleRefund(spNo string) { isUpdate := true if refund == nil { refund = new(model.ApprovalRefund) + refund.SpNo = detail.SpNo isUpdate = false } refund.Username = detail.Applyer.Userid - refund.SpNo = detail.SpNo - refund.ApplyTime = detail.ApplyTime + refund.ApplyTime = goutil.TimeToDateTime(detail.ApplyTime) refund.Status = model.ApprovalRefundStatusCreated - for _, content := range detail.ApplyData.Contents { - key := content.Title[0].Text - var value string - if content.Control == "Selector" { - value = content.Value.Selector.Options[0].Value[0].Text - } else if content.Control == "Text" || content.Control == "Textarea" { - value = content.Value.Text - } else if content.Control == "Date" { - value = content.Value.Date.Timestamp - } else if content.Control == "Money" { - value = content.Value.NewMoney - } else if content.Control == "File" { - value = content.Value.Files[0].FileId - } - if key == "报销类型" { - refund.RefundType = value - } else if key == "发生时间" { - refund.RefundDate = time.Unix(cast.ToInt64(value), 0).Format("2006-01-02") - } else if key == "报销费用" { - refund.RefundAmount = cast.ToFloat64(value) - } else if key == "报销说明" { - refund.RefundRemark = value - } - } + refund.RefundType = detail.GetValue("报销类型") + refund.RefundDate = time.Unix(cast.ToInt64(detail.GetValue("发生时间")), 0).Format("2006-01-02") + refund.RefundAmount = cast.ToFloat64(detail.GetValue("报销费用")) + refund.RefundRemark = detail.GetValue("报销说明") + if isUpdate { err = dao.NewApprovalRefundDao().Update(refund) } else { diff --git a/worker/approval.go b/worker/approval.go new file mode 100644 index 0000000..cf169f5 --- /dev/null +++ b/worker/approval.go @@ -0,0 +1,67 @@ +package worker + +import ( + "enterprise/common/config" + "enterprise/common/dao" + "enterprise/common/model" + "enterprise/common/weixin" + log "github.com/sirupsen/logrus" + "github.com/smbrave/goutil" + "github.com/spf13/cast" + "strings" + "time" +) + +type Approval struct { +} + +func (s *Approval) SyncCheckinMonth(month string) { + templateId := "C4UCJS891Afmu1rE1Ws6cvph7YHqebWtt7KRFqh8c" + 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 + } + //result := make(map[string]float64) + for _, spNo := range spNos { + detail, err := approve.GetDetail(spNo) + if err != nil { + log.Errorf("approve GetDetail error :%s", err.Error()) + continue + } + + value := strings.SplitN(detail.GetValue("补卡"), ",", 3) + + ac, err := dao.NewApprovalCheckinDao().GetBySpNo(spNo) + if err != nil { + log.Errorf("db error :%s", err.Error()) + continue + } + isNew := false + if ac == nil { + ac = new(model.ApprovalCheckin) + ac.SpNo = detail.SpNo + isNew = true + } + + ac.Username = detail.GetUserid() + ac.ApplyTime = goutil.TimeToDateTime(detail.ApplyTime) + ac.CheckinRemark = detail.GetValue("补卡事由") + ac.CheckinDate = goutil.TimeToDate(cast.ToInt64(value[0])) + ac.CheckinTime = goutil.TimeToDateTime(cast.ToInt64(value[1])) + ac.CheckinType = value[2] + if isNew { + _, err = dao.NewApprovalCheckinDao().Create(ac) + } else { + err = dao.NewApprovalCheckinDao().Update(ac) + } + if err != nil { + log.Errorf("db error :%s", err.Error()) + } + } + return +} diff --git a/worker/checkin.go b/worker/checkin.go index 7f10f43..ad9cc9c 100644 --- a/worker/checkin.go +++ b/worker/checkin.go @@ -10,10 +10,10 @@ import ( "time" ) -type CheckIn struct { +type Checkin struct { } -func (c *CheckIn) SyncCheckinMonth(month string) error { +func (c *Checkin) SyncCheckinMonth(month string) error { if month == "" { month = time.Now().AddDate(0, -1, 0).Format("200601") } @@ -40,7 +40,7 @@ func (c *CheckIn) SyncCheckinMonth(month string) error { } return nil } -func (c *CheckIn) SyncCheckinDay(day string) { +func (c *Checkin) SyncCheckinDay(day string) { cfg := config.GetConfig() qyw := weixin.NewQyWeixinCheckin(cfg.QyWeixin.Corpid, cfg.QyWeixin.CheckinSecret, cfg.QyWeixin.CheckinAgent) if day == "" { @@ -63,7 +63,7 @@ func (c *CheckIn) SyncCheckinDay(day string) { } return } -func (c *CheckIn) saveToDB(user *weixin.UserCheckIn) error { +func (c *Checkin) saveToDB(user *weixin.UserCheckIn) error { checkin, err := dao.NewCheckinDao().GetByDay(user.UserId, user.Day) if err != nil { log.Errorf("db error :%s", err.Error()) diff --git a/worker/worker.go b/worker/worker.go index f32856f..82da6d0 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -9,7 +9,7 @@ func Init() error { timezone, _ := time.LoadLocation("Asia/Shanghai") cron := gocron.NewScheduler(timezone) staff := new(Staff) - checkIn := new(CheckIn) + checkIn := new(Checkin) // 每天同步企业人事信息 cron.Every(1).Day().At("01:00").Do(func() {