package worker import ( butil "enterprise/base/util" "enterprise/common/config" "enterprise/common/dao" "enterprise/common/global" "enterprise/common/model" "enterprise/common/weixin" "fmt" log "github.com/sirupsen/logrus" "github.com/smbrave/goutil" "github.com/spf13/cast" "strings" "time" ) var ( checkOndutyName = "上班打卡" checkOffdutyName = "下班打卡" ) func NotifyCheckinOnDuty(checkin *model.Checkin) { message := make([]string, 0) message = append(message, "【上班提醒】") message = append(message, fmt.Sprintf("员工名称:%s", checkin.Username)) message = append(message, fmt.Sprintf("考勤日期:%s", checkin.Day)) message = append(message, fmt.Sprintf("开始时间:%s", goutil.TimeToDateTime(checkin.StartTime))) if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil { log.Errorf("send message error :%s", err.Error()) } userConfig, err := dao.NewUserConfigDao().GetByUsername(checkin.Username) if err != nil { log.Errorf("db error :%s", err.Error()) return } if userConfig == nil { return } if isOndutyPay(checkin, userConfig) && checkin.Exception == "" { payMoney := cast.ToInt64(userConfig.Get(model.CheckinOndutyMoney)) if payMoney == 0 { payMoney = 2000 } autoPayMoney(checkin, checkOndutyName, payMoney) } } func NotifyCheckinOffDuty(checkin *model.Checkin) { if checkin.Exception != "" { log.Infof("execption[%s] %s", checkin.Exception, goutil.EncodeJSON(checkin)) return } thresold := config.GetConfig().QyWeixin.CheckinPayThresold duration := checkin.EndTime - checkin.StartTime if duration < int64(3600*thresold) { return } userConfig, err := dao.NewUserConfigDao().GetByUsername(checkin.Username) if err != nil { log.Errorf("db error :%s", err.Error()) return } if userConfig == nil { return } payMoney := cast.ToInt64(userConfig.Get(model.CheckinOffdutyMoney)) if payMoney == 0 { payMoney = 2000 } autoPayMoney(checkin, checkOffdutyName, payMoney) } func isOndutyPay(checkin *model.Checkin, userConfig *model.UserConfig) bool { hour := time.Unix(checkin.StartTime, 0).Hour() if hour >= 10 { return false } // 配置了上班打卡的直接发放 if cast.ToBool(userConfig.Get(model.CheckinOndutyMoneyEnable)) == true { return true } // 周六加班也直接发放 if time.Now().Weekday() == time.Saturday { return true } return false } func autoPayMoney(checkin *model.Checkin, checkinType string, payMoney int64) error { //cfg := config.GetConfig() checkinMoneyDao := dao.NewCheckinMoneyDao() checkinMoney, err := checkinMoneyDao.GetByDay(checkin.Username, checkin.Day, checkinType) if err != nil { log.Errorf("db error :%s", err.Error()) return err } if checkinMoney != nil { return nil } var req weixin.RedMoneyReq req.TotalAmount = payMoney req.Title = checkinType req.BillNo = fmt.Sprintf("DK%s%s", time.Now().Format("20060102150405"), butil.CutTail(checkin.Username, 12)) req.Userid = checkin.Username if err := weixin.NewQyPay().PayRedMoney(&req); err != nil { log.Errorf("pay red money error :%s", err.Error()) return err } message := make([]string, 0) duration := checkin.EndTime - checkin.StartTime message = append(message, fmt.Sprintf("【红包发放】[%s]", checkinType)) message = append(message, fmt.Sprintf("发放金额:%s", fmt.Sprintf("%.2f", float64(payMoney)/100.00))) message = append(message, fmt.Sprintf("员工名称:%s", checkin.Username)) message = append(message, fmt.Sprintf("考勤日期:%s", checkin.Day)) message = append(message, fmt.Sprintf("开始时间:%s", goutil.TimeToDateTime(checkin.StartTime))) if checkinType == checkOffdutyName { message = append(message, fmt.Sprintf("结束时间:%s", goutil.TimeToDateTime(checkin.EndTime))) message = append(message, fmt.Sprintf("工作时长:%s", (time.Duration(duration)*time.Second).String())) } if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil { log.Errorf("send message error :%s", err.Error()) } checkinMoney = new(model.CheckinMoney) checkinMoney.Username = checkin.Username checkinMoney.CheckinId = checkin.Id checkinMoney.BillNo = req.BillNo checkinMoney.CheckinType = checkinType checkinMoney.Day = checkin.Day checkinMoney.BillAmount = payMoney if _, err := checkinMoneyDao.Create(checkinMoney); err != nil { log.Errorf("create checkinMoney model error :%s", err.Error()) return err } return nil }