作者 tangxvhui

更新

package notify
// 短信通知
// 周期评估短信通知
// 条件:周期自评结束前4个小时,且还未完成评估填写
type NotifySummaryEvaluation struct{}
// 每日自评短信通知
// 条件:每日自评结束前30分钟,且还未完成评估填写
type NotifyStaffAssess struct{}
... ...
package notify
import (
"fmt"
"time"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
)
// 短信通知
// 每日自评短信通知
// 条件:每日自评结束前30分钟,且还未完成评估填写
type NotifyStaffAssess struct {
newSms chan *domain.LogSms
interval time.Duration
}
func (notices *NotifyStaffAssess) init() {
notices.newSms = make(chan *domain.LogSms, 50)
notices.interval = 10 * time.Minute
if constant.Env != "prd" {
notices.interval = 1 * time.Minute
}
}
func (notices *NotifyStaffAssess) From() string {
return "StaffAssess"
}
// AddTask 添加待执行的短信通知任务
func (notices *NotifyStaffAssess) AddTask(index int, phone string, param map[string]string, executeAt time.Time) {
newSms := &domain.LogSms{
Id: 0,
Phone: phone,
TemplateId: 5475050,
Template: "您好,#name#,百忙之中不要忘记填写今天的绩效自评反馈哦",
Value: param,
Result: "",
Status: domain.SmsWait,
From: notices.From(),
Index: index,
ExecuteAt: executeAt,
CreatedAt: time.Now(),
}
notices.newSms <- newSms
}
// RunTask 执行短信通知任务
func (notices *NotifyStaffAssess) RunTask() {
notices.init()
timer := time.NewTimer(notices.interval)
for {
select {
case newSms := <-notices.newSms:
err := notices.addNewSms(newSms)
if err != nil {
e := fmt.Sprintf("添加短信通知任务:%+v %s", newSms, err)
log.Logger.Error(e)
}
case <-timer.C:
err := notices.sendSms()
if err != nil {
log.Logger.Error("发送短信通知任务:" + err.Error())
}
timer.Reset(notices.interval) // 重置定时
}
}
}
func (notices *NotifyStaffAssess) addNewSms(newSms *domain.LogSms) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return err
}
if err := transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
logSmsRepo := factory.CreateLogSmsRepository(map[string]interface{}{"transactionContext": transactionContext})
err = logSmsRepo.Save(newSms)
if err != nil {
return err
}
if err := transactionContext.CommitTransaction(); err != nil {
return err
}
return nil
}
func (notices *NotifyStaffAssess) sendSms() error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return err
}
if err := transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
logSmsRepo := factory.CreateLogSmsRepository(map[string]interface{}{"transactionContext": transactionContext})
_ = logSmsRepo
if err := transactionContext.CommitTransaction(); err != nil {
return err
}
return nil
}
... ...
package notify
import (
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
// 周期评估短信通知
// 条件:周期自评结束前4个小时,且还未完成评估填写
type NotifySummaryEvaluation struct {
}
func (notices *NotifySummaryEvaluation) Init() *NotifySummaryEvaluation {
return &NotifySummaryEvaluation{}
}
func (notices *NotifySummaryEvaluation) From() string {
return "SummaryEvaluation"
}
// AddTask 添加待执行的短信通知任务
func (notices *NotifySummaryEvaluation) AddTask(index string, phone string, param map[string]string) error {
return nil
}
// RunTask 执行短信通知任务
func (notice *NotifySummaryEvaluation) RunTask() error {
return nil
}
func (notice *NotifySummaryEvaluation) addNewSms(newSms *domain.LogSms) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return err
}
if err := transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
logSmsRepo := factory.CreateLogSmsRepository(map[string]interface{}{"transactionContext": transactionContext})
err = logSmsRepo.Save(newSms)
if err != nil {
return err
}
if err := transactionContext.CommitTransaction(); err != nil {
return err
}
return nil
}
... ...
... ... @@ -20,8 +20,8 @@ type EvaluationItemUsed struct {
RuleType int //评估方式(0评级、1评分)
Rule EvaluationRule //评估的选项规则
Weight float64 //"权重"
Required int // 必填项
EvaluatorId int // 项目评估人ID ( 0=无评估人、-1=HRBP、 >0 =员工的id )
Required int //必填项
EvaluatorId int //项目评估人ID ( 0=无评估人、-1=HRBP、 >0 =员工的id )
CreatedAt time.Time //数据创建时间
UpdatedAt time.Time //数据更新时间
}
... ...
... ... @@ -12,6 +12,9 @@ type LogSms struct {
Value map[string]string
Result string
Status SmsStatus
From string //业务来源
Index int //业务数据索引
ExecuteAt time.Time
CreatedAt time.Time
}
... ... @@ -41,4 +44,6 @@ func (sms *LogSms) SummaryEvaluationMessage(phone string, name string) {
type LogSmsRepository interface {
Insert(param *LogSms) error
Save(param *LogSms) error
Find(queryOptions map[string]interface{}) (int, []*LogSms, error)
}
... ...
package models
import "time"
import (
"time"
)
type LogSms struct {
tableName struct{} `comment:"记录短信消息" pg:"log_sms"`
... ... @@ -11,4 +13,8 @@ type LogSms struct {
Value map[string]string `pg:"value"`
CreatedAt time.Time `pg:"created_at"`
Result string `pg:"result"`
Status string `pg:"status"`
From string `pg:"from"` //业务来源
Index int `pg:"index"` //业务数据索引
ExecuteAt time.Time `pg:"execute_at"`
}
... ...
... ... @@ -33,3 +33,76 @@ func (repo *LogSmsRepository) Insert(param *domain.LogSms) error {
}
return nil
}
func (repo *LogSmsRepository) Save(param *domain.LogSms) error {
m := models.LogSms{
Id: param.Id,
Phone: param.Phone,
TemplateId: param.TemplateId,
Template: param.Template,
Value: param.Value,
CreatedAt: param.CreatedAt,
Result: param.Result,
}
tx := repo.transactionContext.PgTx
_, err := tx.Model(&m).Insert()
if err != nil {
return err
}
return nil
}
func (repo *LogSmsRepository) Find(queryOptions map[string]interface{}) (int, []*domain.LogSms, error) {
tx := repo.transactionContext.PgTx
var m []*models.LogSms
query := tx.Model(&m)
if v, ok := queryOptions["limit"].(int); ok {
query.Limit(v)
}
if v, ok := queryOptions["offset"].(int); ok {
query.Offset(v)
}
if v, ok := queryOptions["form"]; ok {
query.Where("from=?", v)
}
if v, ok := queryOptions["index"]; ok {
query.Where("index=?", v)
}
if v, ok := queryOptions["status"]; ok {
query.Where("status=?", v)
}
if v, ok := queryOptions["executeAt"]; ok {
query.Where("execute_at=?", v)
}
count, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
}
var datas []*domain.LogSms
for _, v := range m {
d := repo.TransformToDomain(v)
datas = append(datas, d)
}
return count, datas, nil
}
func (repo *LogSmsRepository) TransformToDomain(d *models.LogSms) *domain.LogSms {
return &domain.LogSms{
Id: d.Id,
Phone: d.Phone,
TemplateId: d.TemplateId,
Template: d.Template,
Value: d.Value,
Result: d.Result,
Status: domain.SmsStatus(d.Status),
From: d.From,
Index: d.Index,
ExecuteAt: d.ExecuteAt,
CreatedAt: d.CreatedAt,
}
}
... ...
... ... @@ -300,6 +300,8 @@ func (c *SummaryEvaluationController) GetTargetUserEvaluationSuper() {
c.Response(nil, e)
return
}
userReq := middlewares.GetUser(c.Ctx)
paramReq.CompanyId = int(userReq.CompanyId)
data, err := srv.GetTargetUserEvaluationSuper(paramReq)
c.Response(data, err)
}
... ...