Files
zmkgC/third/reminders/reminder.go

111 lines
3.2 KiB
Go
Raw Permalink Normal View History

2025-07-07 20:11:59 +08:00
package reminders
import (
"context"
"fmt"
"github.com/jinzhu/copier"
"github.com/samber/lo"
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
)
type Reminder struct {
Content string `copier:"ViolationType"` // 内容
Title string // 标题
ReceiverID string `copier:"UserId"` // 消息接收者的 User_id
Type int `copier:"ReminderType"` // 类型
Status int // 状态
ProjectID int `copier:"ProjectId"` // 项目 ID
TargetID int `copier:"OrderId"` // 对应跳转的主键ID
//TargetID int `copier:"OrderId"` // 是否需要转换
}
// UserIDOrderIDDel 根据userID与orderID 删除提醒
func UserIDOrderIDDel(userId string, orderID int64) error {
columns := dao.Reminders.Columns()
_, err := dao.Reminders.Ctx(context.Background()).Where(columns.UserId, userId).Where(columns.OrderId, orderID).Delete()
return err
}
// PublishReminder 发布提醒
func PublishReminder(message Reminder, convertOpenID bool) error {
if convertOpenID && HasLetter(message.ReceiverID) {
message.ReceiverID = GetUserIDByOpenID(message.ReceiverID)
if message.ReceiverID == "" {
return fmt.Errorf("未找到对应的用户")
}
}
var reminder do.Reminders
copier.Copy(&reminder, &message)
// 保存到数据库
_, err := dao.Reminders.Ctx(context.Background()).Insert(&reminder)
return err
}
// CheckInReminder 打卡提醒
// Reminders.CheckIn 表示上班打卡
// Reminders.CheckOut 表示下班打卡
func CheckInReminder(checkType ReminderType, projectID int, userID, time string) error {
// 1. 构建消息模板
message := Reminder{
Type: checkType, // 打卡类型
ProjectID: projectID, // 项目 ID
ReceiverID: userID, // 消息接收者的 sys_user.Id
Title: time, // 时间,只需要多少分钟,如 "10""20"
}
return PublishReminder(message, false)
}
// hasLetter 判断字符串中是否包含字母
func HasLetter(s string) bool {
for _, c := range s {
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') {
return true
}
}
return false
}
// GetUserIDByOpenID 通过 OpenID 获取用户对应的 sys_user.Id
func GetUserIDByOpenID(openID string) string {
openid, err := dao.BusConstructionUser.Ctx(context.Background()).As("bu").
Fields("su.id").
InnerJoin("sys_user AS su", "bu.phone = su.mobile").
Where("bu.openid", openID).Value()
if err != nil {
return ""
}
return openid.String()
}
type userOpenID struct {
OpenID string
ID int
}
// GetUserIDByOpenIDs 传入多个 OpenID返回对应的 User.ID
func GetUserIDByOpenIDs(openIDs []string) map[string]string {
// SELECT su.id,bu.openid
// FROM bus_construction_user AS bu
// INNER JOIN sys_user AS su ON bu.phone = su.mobile
// WHERE bu.openid in ("oLYsI4x-L6ZVKCinQ4Rw1D9XtFZ0","oLYsI49aMJHTYorhmvVuRrasPTbU")
var userOpenIDList []userOpenID
err := dao.BusConstructionUser.Ctx(context.Background()).As("bu").
Fields("su.id", "bu.openid").
InnerJoin("sys_user AS su", "bu.phone = su.mobile").
WhereIn("bu.openid", openIDs).Scan(&userOpenIDList)
if err != nil {
return nil
}
return lo.SliceToMap(userOpenIDList, func(item userOpenID) (string, string) {
return item.OpenID, fmt.Sprintf("%d", item.ID)
})
}