approval checkin

This commit is contained in:
jiangyong 2024-01-07 18:53:21 +08:00
parent a868f820bb
commit 4dee3b1036
9 changed files with 201 additions and 38 deletions

View File

@ -5,9 +5,10 @@ import (
"enterprise/common/global" "enterprise/common/global"
"enterprise/server" "enterprise/server"
"enterprise/worker" "enterprise/worker"
"time"
) )
func main() { func main1() {
config.LoadServerConfig() config.LoadServerConfig()
global.InitGlobal() global.InitGlobal()
@ -20,8 +21,12 @@ func main() {
} }
} }
func main2() { func main() {
config.LoadServerConfig() config.LoadServerConfig()
global.InitGlobal() global.InitGlobal()
//cfg := config.GetConfig() //cfg := config.GetConfig()
for i := 0; i < 10; i++ {
new(worker.Approval).SyncCheckinMonth(time.Now().AddDate(0, 0-i, 0).Format("200601"))
}
} }

View File

@ -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
}

View File

@ -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
}

View File

@ -13,7 +13,7 @@ type ApprovalRefund struct {
RefundDate string RefundDate string
RefundAmount float64 RefundAmount float64
RefundRemark string RefundRemark string
ApplyTime int64 ApplyTime string
Status int Status int
CreateTime int64 CreateTime int64
UpdateTime int64 UpdateTime int64

View File

@ -50,7 +50,8 @@ type ApplyValue struct {
Files []struct { Files []struct {
FileId string `json:"file_id"` FileId string `json:"file_id"`
} `json:"files"` } `json:"files"`
Vacation *Vacation `json:"vacation"` Vacation *Vacation `json:"vacation"`
PunchCorrection interface{} `json:"punch_correction"`
} }
type ApplyContent struct { type ApplyContent struct {
@ -103,16 +104,31 @@ func (d *ApproveDetail) GetValue(title string) string {
value = content.Value.NewMoney value = content.Value.NewMoney
} else if content.Control == "File" { } else if content.Control == "File" {
value = content.Value.Files[0].FileId 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 tp := content.Value.Vacation.Selector.Options[0].Value[0].Text
duration := cast.ToString(content.Value.Vacation.Attendance.DateRange.NewDuration) duration := cast.ToString(content.Value.Vacation.Attendance.DateRange.NewDuration)
value = tp + "," + duration 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 value
} }
return "" return ""
} }
func (d *ApproveDetail) String() string {
return goutil.EncodeJSONIndent(d)
}
func (d *ApproveDetail) GetUserid() string { func (d *ApproveDetail) GetUserid() string {
return d.Applyer.Userid return d.Applyer.Userid
} }
@ -135,6 +151,10 @@ func (q *QyWeixinApprove) GetDetail(spNo string) (*ApproveDetail, error) {
return nil, err return nil, err
} }
var rsp ApproveDetailRsp 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 { if err := json.Unmarshal(rspBody, &rsp); err != nil {
log.Errorf("get body[%s] json error :%s", string(rspBody), err.Error()) log.Errorf("get body[%s] json error :%s", string(rspBody), err.Error())
return nil, err return nil, err

View File

@ -28,8 +28,10 @@ type Approve struct {
} }
func (b *Approve) Reply(msg message.MixMessage) *message.Reply { func (b *Approve) Reply(msg message.MixMessage) *message.Reply {
cfg := config.GetConfig() if b.approveClient == nil {
b.approveClient = weixin.NewQyWeixinApprove(cfg.QyWeixin.Corpid, cfg.QyWeixin.ApproveSecret, cfg.QyWeixin.ApproveAgent) cfg := config.GetConfig()
b.approveClient = weixin.NewQyWeixinApprove(cfg.QyWeixin.Corpid, cfg.QyWeixin.ApproveSecret, cfg.QyWeixin.ApproveAgent)
}
go b.handle(&msg) go b.handle(&msg)
return &message.Reply{message.MsgTypeText, message.NewText("")} return &message.Reply{message.MsgTypeText, message.NewText("")}
} }
@ -58,10 +60,13 @@ func (a *Approve) handleApprovalChange(msg *message.MixMessage) {
if spStatus != SpStatusPassed { if spStatus != SpStatusPassed {
return return
} }
if spName == "费用报销" { if spName == "费用报销" {
a.handleRefund(spNo) a.handleRefund(spNo)
} else if spName == "请假" { } else if spName == "请假" {
a.handleHoliday(spNo) 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) { func (a *Approve) handleRefund(spNo string) {
detail, err := a.approveClient.GetDetail(spNo) detail, err := a.approveClient.GetDetail(spNo)
if err != nil { if err != nil {
@ -85,36 +93,17 @@ func (a *Approve) handleRefund(spNo string) {
isUpdate := true isUpdate := true
if refund == nil { if refund == nil {
refund = new(model.ApprovalRefund) refund = new(model.ApprovalRefund)
refund.SpNo = detail.SpNo
isUpdate = false isUpdate = false
} }
refund.Username = detail.Applyer.Userid refund.Username = detail.Applyer.Userid
refund.SpNo = detail.SpNo refund.ApplyTime = goutil.TimeToDateTime(detail.ApplyTime)
refund.ApplyTime = detail.ApplyTime
refund.Status = model.ApprovalRefundStatusCreated refund.Status = model.ApprovalRefundStatusCreated
for _, content := range detail.ApplyData.Contents { refund.RefundType = detail.GetValue("报销类型")
key := content.Title[0].Text refund.RefundDate = time.Unix(cast.ToInt64(detail.GetValue("发生时间")), 0).Format("2006-01-02")
var value string refund.RefundAmount = cast.ToFloat64(detail.GetValue("报销费用"))
if content.Control == "Selector" { refund.RefundRemark = detail.GetValue("报销说明")
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
}
}
if isUpdate { if isUpdate {
err = dao.NewApprovalRefundDao().Update(refund) err = dao.NewApprovalRefundDao().Update(refund)
} else { } else {

67
worker/approval.go Normal file
View File

@ -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
}

View File

@ -10,10 +10,10 @@ import (
"time" "time"
) )
type CheckIn struct { type Checkin struct {
} }
func (c *CheckIn) SyncCheckinMonth(month string) error { func (c *Checkin) SyncCheckinMonth(month string) error {
if month == "" { if month == "" {
month = time.Now().AddDate(0, -1, 0).Format("200601") month = time.Now().AddDate(0, -1, 0).Format("200601")
} }
@ -40,7 +40,7 @@ func (c *CheckIn) SyncCheckinMonth(month string) error {
} }
return nil return nil
} }
func (c *CheckIn) SyncCheckinDay(day string) { func (c *Checkin) SyncCheckinDay(day string) {
cfg := config.GetConfig() cfg := config.GetConfig()
qyw := weixin.NewQyWeixinCheckin(cfg.QyWeixin.Corpid, cfg.QyWeixin.CheckinSecret, cfg.QyWeixin.CheckinAgent) qyw := weixin.NewQyWeixinCheckin(cfg.QyWeixin.Corpid, cfg.QyWeixin.CheckinSecret, cfg.QyWeixin.CheckinAgent)
if day == "" { if day == "" {
@ -63,7 +63,7 @@ func (c *CheckIn) SyncCheckinDay(day string) {
} }
return 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) checkin, err := dao.NewCheckinDao().GetByDay(user.UserId, user.Day)
if err != nil { if err != nil {
log.Errorf("db error :%s", err.Error()) log.Errorf("db error :%s", err.Error())

View File

@ -9,7 +9,7 @@ func Init() error {
timezone, _ := time.LoadLocation("Asia/Shanghai") timezone, _ := time.LoadLocation("Asia/Shanghai")
cron := gocron.NewScheduler(timezone) cron := gocron.NewScheduler(timezone)
staff := new(Staff) staff := new(Staff)
checkIn := new(CheckIn) checkIn := new(Checkin)
// 每天同步企业人事信息 // 每天同步企业人事信息
cron.Every(1).Day().At("01:00").Do(func() { cron.Every(1).Day().At("01:00").Do(func() {