This commit is contained in:
jiangyong 2024-01-07 22:54:33 +08:00
parent 4dee3b1036
commit 598b205052
8 changed files with 304 additions and 94 deletions

View File

@ -3,12 +3,13 @@ package main
import ( import (
"enterprise/common/config" "enterprise/common/config"
"enterprise/common/global" "enterprise/common/global"
"enterprise/common/model"
"enterprise/server" "enterprise/server"
"enterprise/worker" "enterprise/worker"
"time" "time"
) )
func main1() { func main() {
config.LoadServerConfig() config.LoadServerConfig()
global.InitGlobal() global.InitGlobal()
@ -21,12 +22,12 @@ func main1() {
} }
} }
func main() { func main22() {
config.LoadServerConfig() config.LoadServerConfig()
global.InitGlobal() global.InitGlobal()
//cfg := config.GetConfig() //cfg := config.GetConfig()
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
new(worker.Approval).SyncCheckinMonth(time.Now().AddDate(0, 0-i, 0).Format("200601")) new(worker.Approval).Sync(time.Now().AddDate(0, 0-i, 0).Format("200601"), model.ApprovalTypeVacation)
} }
} }

View File

@ -0,0 +1,66 @@
package dao
import (
"enterprise/common/model"
"gorm.io/gorm"
"time"
)
type ApprovalVacationDao struct {
}
func NewApprovalVacationDao() *ApprovalVacationDao {
return &ApprovalVacationDao{}
}
func (d *ApprovalVacationDao) TableName() string {
return "approval_vacation"
}
func (d *ApprovalVacationDao) Create(o *model.ApprovalVacation) (int64, error) {
o.CreateTime = time.Now().Unix()
res := GetDB().Table(d.TableName()).Create(o)
return o.Id, res.Error
}
func (d *ApprovalVacationDao) Update(o *model.ApprovalVacation) error {
o.UpdateTime = time.Now().Unix()
tx := GetDB().Table(d.TableName())
res := tx.Save(o)
return res.Error
}
func (d *ApprovalVacationDao) Delete(id int64) error {
res := GetDB().Table(d.TableName()).Delete(&model.ApprovalVacation{}, id)
return res.Error
}
func (d *ApprovalVacationDao) Get(id int64) (*model.ApprovalVacation, error) {
var u model.ApprovalVacation
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 *ApprovalVacationDao) GetBySpNo(spNo string) (*model.ApprovalVacation, error) {
var u model.ApprovalVacation
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

@ -1,10 +1,9 @@
package model package model
var ()
type ApprovalCheckin struct { type ApprovalCheckin struct {
Id int64 Id int64
Username string Username string
Month string
SpNo string SpNo string
CheckinType string CheckinType string
CheckinDate string CheckinDate string

View File

@ -3,11 +3,15 @@ package model
var ( var (
ApprovalRefundStatusCreated = 1 ApprovalRefundStatusCreated = 1
ApprovalRefundStatusPayed = 2 ApprovalRefundStatusPayed = 2
ApprovalTypeRefund = "refund"
ApprovalTypeCheckin = "checkin"
ApprovalTypeVacation = "vacation"
) )
type ApprovalRefund struct { type ApprovalRefund struct {
Id int64 Id int64
Username string Username string
Month string
SpNo string SpNo string
RefundType string RefundType string
RefundDate string RefundDate string

View File

@ -0,0 +1,19 @@
package model
var ()
type ApprovalVacation struct {
Id int64
Username string
Month string
SpNo string
VacationType string
VacationDate string
VacationStartTime string
VacationEndTime string
VacationDuration float64
VacationRemark string
ApplyTime string
CreateTime int64
UpdateTime int64
}

View File

@ -3,10 +3,13 @@ package weixin
import ( import (
"encoding/json" "encoding/json"
butil "enterprise/base/util" butil "enterprise/base/util"
"enterprise/common/model"
"fmt" "fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil" "github.com/smbrave/goutil"
"github.com/spf13/cast" "github.com/spf13/cast"
"strings"
"time"
) )
type Applyer struct { type Applyer struct {
@ -104,14 +107,12 @@ 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) value = tp + "," + cast.ToString(content.Value.Vacation.Attendance.DateRange.NewBegin) +
value = tp + "," + duration "," + cast.ToString(content.Value.Vacation.Attendance.DateRange.NewEnd) +
"," + cast.ToString(content.Value.Vacation.Attendance.DateRange.NewDuration)
} else if content.Control == "PunchCorrection" { //补卡:日期,时间,状态 } else if content.Control == "PunchCorrection" { //补卡:日期,时间,状态
if d.SpNo == "202312010001" {
fmt.Println(goutil.EncodeJSON(d.ApplyData))
}
mp := cast.ToStringMap(content.Value.PunchCorrection) mp := cast.ToStringMap(content.Value.PunchCorrection)
ddate := cast.ToString(mp["daymonthyear"]) ddate := cast.ToString(mp["daymonthyear"])
dtime := cast.ToString(mp["time"]) dtime := cast.ToString(mp["time"])
@ -133,6 +134,53 @@ func (d *ApproveDetail) GetUserid() string {
return d.Applyer.Userid return d.Applyer.Userid
} }
func (d *ApproveDetail) ToVacation() *model.ApprovalVacation {
vacation := new(model.ApprovalVacation)
vacation.SpNo = d.SpNo
vacation.Username = d.GetUserid()
vacation.ApplyTime = goutil.TimeToDateTime(d.ApplyTime)
fields := strings.SplitN(d.GetValue("请假类型"), ",", 4)
dTime := cast.ToInt64(fields[1])
vacation.VacationType = fields[0]
vacation.VacationDate = goutil.TimeToDate(dTime)
vacation.VacationStartTime = goutil.TimeToDateTime(cast.ToInt64(fields[1]))
vacation.VacationEndTime = goutil.TimeToDateTime(cast.ToInt64(fields[2]))
vacation.VacationDuration = float64(cast.ToInt64(fields[3])) / float64(3600*8)
vacation.VacationRemark = d.GetValue("请假事由")
vacation.Month = time.Unix(dTime, 0).Format("200601")
return vacation
}
func (d *ApproveDetail) ToRefund() *model.ApprovalRefund {
refund := new(model.ApprovalRefund)
refund.SpNo = d.SpNo
refund.Username = d.GetUserid()
refund.ApplyTime = goutil.TimeToDateTime(d.ApplyTime)
refund.Status = model.ApprovalRefundStatusCreated
refund.RefundType = d.GetValue("报销类型")
refundTime := cast.ToInt64(d.GetValue("发生时间"))
refund.Month = time.Unix(refundTime, 0).Format("200601")
refund.RefundDate = goutil.TimeToDateTime(refundTime)
refund.RefundAmount = cast.ToFloat64(d.GetValue("报销费用"))
refund.RefundRemark = d.GetValue("报销说明")
return refund
}
func (d *ApproveDetail) ToCheckin() *model.ApprovalCheckin {
ac := new(model.ApprovalCheckin)
value := strings.SplitN(d.GetValue("补卡"), ",", 3)
ac.SpNo = d.SpNo
ac.Username = d.GetUserid()
ac.ApplyTime = goutil.TimeToDateTime(d.ApplyTime)
ac.CheckinRemark = d.GetValue("补卡事由")
ac.CheckinDate = goutil.TimeToDate(cast.ToInt64(value[0]))
ac.Month = time.Unix(cast.ToInt64(value[0]), 0).Format("200601")
ac.CheckinTime = goutil.TimeToDateTime(cast.ToInt64(value[1]))
ac.CheckinType = value[2]
return ac
}
func NewQyWeixinApprove(corpId, secret, agent string) *QyWeixinApprove { func NewQyWeixinApprove(corpId, secret, agent string) *QyWeixinApprove {
return &QyWeixinApprove{ return &QyWeixinApprove{
QyWeixin: QyWeixin{ QyWeixin: QyWeixin{

View File

@ -11,9 +11,7 @@ import (
"fmt" "fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil" "github.com/smbrave/goutil"
"github.com/spf13/cast"
"strings" "strings"
"time"
) )
var ( var (
@ -56,59 +54,87 @@ func (a *Approve) handle(msg *message.MixMessage) {
func (a *Approve) handleApprovalChange(msg *message.MixMessage) { func (a *Approve) handleApprovalChange(msg *message.MixMessage) {
spStatus := msg.ApprovalInfo.SpStatus spStatus := msg.ApprovalInfo.SpStatus
spNo := msg.ApprovalInfo.SpNo spNo := msg.ApprovalInfo.SpNo
spName := msg.ApprovalInfo.SpName templateId := msg.ApprovalInfo.TemplateId
if spStatus != SpStatusPassed { if spStatus != SpStatusPassed {
return return
} }
if spName == "费用报销" {
a.handleRefund(spNo)
} else if spName == "请假" {
a.handleHoliday(spNo)
} else if spName == "打卡补卡" {
a.handleCheckin(spNo)
}
}
func (a *Approve) handleHoliday(spNo string) {
}
func (a *Approve) handleCheckin(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 {
log.Errorf("get spn detail error :%s", err.Error()) log.Errorf("get spn detail error :%s", err.Error())
return return
} }
if templateId == model.ApprovalTypeRefund {
a.handleRefund(detail)
} else if templateId == model.ApprovalTypeVacation {
a.handleVacation(detail)
} else if templateId == model.ApprovalTypeCheckin {
a.handleCheckin(detail)
}
}
refund, err := dao.NewApprovalRefundDao().GetBySpNo(spNo) func (a *Approve) handleVacation(detail *weixin.ApproveDetail) {
newData := detail.ToVacation()
dbDao := dao.NewApprovalVacationDao()
old, err := dbDao.GetBySpNo(detail.SpNo)
if err != nil { if err != nil {
log.Errorf("db error :%s", err.Error()) log.Errorf("db error :%s", err.Error())
return return
} }
if old != nil {
isUpdate := true newData.Id = old.Id
if refund == nil { newData.CreateTime = old.CreateTime
refund = new(model.ApprovalRefund) err = dbDao.Update(newData)
refund.SpNo = detail.SpNo
isUpdate = false
}
refund.Username = detail.Applyer.Userid
refund.ApplyTime = goutil.TimeToDateTime(detail.ApplyTime)
refund.Status = model.ApprovalRefundStatusCreated
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 { } else {
_, err = dao.NewApprovalRefundDao().Create(refund) _, err = dbDao.Create(newData)
} }
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
}
func (a *Approve) handleCheckin(detail *weixin.ApproveDetail) {
newData := detail.ToCheckin()
dbDao := dao.NewApprovalCheckinDao()
old, err := dbDao.GetBySpNo(detail.SpNo)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
if old != nil {
newData.Id = old.Id
newData.CreateTime = old.CreateTime
err = dbDao.Update(newData)
} else {
_, err = dbDao.Create(newData)
}
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
}
func (a *Approve) handleRefund(detail *weixin.ApproveDetail) {
newData := detail.ToRefund()
dbDao := dao.NewApprovalRefundDao()
old, err := dbDao.GetBySpNo(detail.SpNo)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
if old != nil {
newData.Id = old.Id
newData.CreateTime = old.CreateTime
err = dbDao.Update(newData)
} else {
_, err = dbDao.Create(newData)
}
if err != nil { if err != nil {
log.Errorf("db error :%s", err.Error()) log.Errorf("db error :%s", err.Error())
return return
@ -116,27 +142,27 @@ func (a *Approve) handleRefund(spNo string) {
// 支付费用 // 支付费用
var req weixin.RedMoneyReq var req weixin.RedMoneyReq
req.BillNo = fmt.Sprintf("BX%s%s", refund.SpNo, butil.CutTail(refund.Username, 12)) req.BillNo = fmt.Sprintf("BX%s%s", newData.SpNo, butil.CutTail(newData.Username, 12))
req.Title = fmt.Sprintf("【%s】报销", refund.RefundType) req.Title = fmt.Sprintf("【%s】报销", newData.RefundType)
req.Userid = refund.Username req.Userid = newData.Username
req.TotalAmount = int64(100 * refund.RefundAmount) req.TotalAmount = int64(100 * newData.RefundAmount)
if err := weixin.NewQyPay().PayRedMoney(&req); err != nil { if err := weixin.NewQyPay().PayRedMoney(&req); err != nil {
log.Errorf("pay error :%s", err.Error()) log.Errorf("pay error :%s", err.Error())
return return
} }
message := make([]string, 0) message := make([]string, 0)
message = append(message, fmt.Sprintf("【红包发放】[%s]", refund.RefundType)) message = append(message, fmt.Sprintf("【红包发放】[%s]", newData.RefundType))
message = append(message, fmt.Sprintf("发放金额:%s", fmt.Sprintf("%.2f", refund.RefundAmount))) message = append(message, fmt.Sprintf("发放金额:%s", fmt.Sprintf("%.2f", newData.RefundAmount)))
message = append(message, fmt.Sprintf("员工名称:%s", refund.Username)) message = append(message, fmt.Sprintf("员工名称:%s", newData.Username))
message = append(message, fmt.Sprintf("费用说明:%s", refund.RefundRemark)) message = append(message, fmt.Sprintf("费用说明:%s", newData.RefundRemark))
if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil { if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil {
log.Errorf("send message error :%s", err.Error()) log.Errorf("send message error :%s", err.Error())
} }
refund.Status = model.ApprovalRefundStatusPayed newData.Status = model.ApprovalRefundStatusPayed
if err := dao.NewApprovalRefundDao().Update(refund); err != nil { if err := dao.NewApprovalRefundDao().Update(newData); err != nil {
log.Errorf("db error :%s", err.Error()) log.Errorf("db error :%s", err.Error())
} }
} }

View File

@ -6,17 +6,25 @@ import (
"enterprise/common/model" "enterprise/common/model"
"enterprise/common/weixin" "enterprise/common/weixin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/spf13/cast"
"strings"
"time" "time"
) )
type Approval struct { type Approval struct {
} }
func (s *Approval) SyncCheckinMonth(month string) { func (s *Approval) getTemplateId(tp string) string {
templateId := "C4UCJS891Afmu1rE1Ws6cvph7YHqebWtt7KRFqh8c" if tp == model.ApprovalTypeCheckin {
return "C4UCJS891Afmu1rE1Ws6cvph7YHqebWtt7KRFqh8c"
} else if tp == model.ApprovalTypeRefund {
return "C4UE6NT3ZE7XzER9TBk2ynHEeqA11NE2GGCuBq5yH"
} else if tp == model.ApprovalTypeVacation {
return "3WLJF6naF5jhnXvwisuPmE85wVMYcy1S1ZvYibkw"
}
return ""
}
func (s *Approval) Sync(month, tp string) {
templateId := s.getTemplateId(tp)
cfg := config.GetConfig().QyWeixin cfg := config.GetConfig().QyWeixin
approve := weixin.NewQyWeixinApprove(cfg.Corpid, cfg.ApproveSecret, cfg.ApproveAgent) approve := weixin.NewQyWeixinApprove(cfg.Corpid, cfg.ApproveSecret, cfg.ApproveAgent)
startTime, _ := time.ParseInLocation("200601", month, time.Local) startTime, _ := time.ParseInLocation("200601", month, time.Local)
@ -26,42 +34,81 @@ func (s *Approval) SyncCheckinMonth(month string) {
log.Errorf("approve getlist error :%s", err.Error()) log.Errorf("approve getlist error :%s", err.Error())
return return
} }
//result := make(map[string]float64)
for _, spNo := range spNos { for _, spNo := range spNos {
detail, err := approve.GetDetail(spNo) detail, err := approve.GetDetail(spNo)
if err != nil { if err != nil {
log.Errorf("approve GetDetail error :%s", err.Error()) log.Errorf("approve GetDetail error :%s", err.Error())
continue continue
} }
if tp == model.ApprovalTypeCheckin {
value := strings.SplitN(detail.GetValue("补卡"), ",", 3) s.saveCheckin(detail)
} else if tp == model.ApprovalTypeRefund {
ac, err := dao.NewApprovalCheckinDao().GetBySpNo(spNo) s.saveRefund(detail)
if err != nil { } else if tp == model.ApprovalTypeVacation {
log.Errorf("db error :%s", err.Error()) s.saveVacation(detail)
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 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())
}
}