Files
zmkgC/third/todo/todo.go
2025-07-07 20:11:59 +08:00

247 lines
7.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package todo
import (
"context"
"fmt"
"strings"
"github.com/google/uuid"
"github.com/samber/lo"
"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
"github.com/tiger1103/gfast/v3/third/reminders"
"github.com/gogf/gf/v2/frame/g"
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
"github.com/tiger1103/gfast/v3/library/liberr"
)
type TaskGroup struct {
Role int // 0 管理员 1 施工人员
TsakType int // 补卡提醒
ProjectID int // 项目 ID
UserID string // sys.user.id 或 wx.openid
MissingCardTime string // 缺卡时间
}
// 考勤审批 struct
type ReissueReminder struct {
UserID string // sys.user.id 或 wx.openid
Role int // 0 管理员 1 施工人员
ProjectID int // 项目 ID
CreatorID string // 创建人 ID
TargetID int // 对应跳转的主键ID
}
func CreateCardReminderTasks(todoList []TaskGroup) error {
// 获取 OpenID 对应的 sys_user.id
openIDToUserIDMap := reminders.GetUserIDByOpenIDs( // 2. 获取所有的 openid 对应的 sys_user.id
lo.FilterMap(todoList, func(item TaskGroup, _ int) (string, bool) { // 1. 筛选出所有的 openid
if reminders.HasLetter(item.UserID) {
return item.UserID, true
}
return item.UserID, false
}),
)
// 替换 todoList 中的 userID
for i := range todoList {
if newUserID, ok := openIDToUserIDMap[todoList[i].UserID]; ok {
todoList[i].UserID = newUserID
}
}
// 同一组的消息拥有相同的 UUID
groupUUID := uuid.New().String()
todoTasks := lo.Map(todoList, func(item TaskGroup, _ int) do.TodoTasks {
return do.TodoTasks{
TaskType: item.TsakType, // 补卡提醒
ProjectId: item.ProjectID, // 项目 ID
UserId: item.UserID, // 消息接收者的 sys_User.Id
Role: item.Role,
MissingCardTime: item.MissingCardTime, // 缺卡时间 示例:`03月14日 08:30上班卡18:00下班卡`
Uuid: groupUUID,
}
})
_, err := dao.TodoTasks.Ctx(context.Background()).Insert(todoTasks)
return err
}
// CreateMissingCardReminder 创建并发布一个补卡提醒
// userID: 消息接收者的 sys_user.id
// role: 0:普通员工 1:管理员
// projectID: 项目 ID
// missingCardTime: 缺卡时间见设计图
func CreateMissingCardReminder(userID string, role int, projectID int, missingCardTime string, orderId int64) error {
if reminders.HasLetter(userID) {
userID = reminders.GetUserIDByOpenID(userID)
if userID == "" {
return fmt.Errorf("未找到对应的用户")
}
}
if strings.Contains(userID, "_") {
return fmt.Errorf("未找到对应的用户")
}
// 构建消息模板
task := do.TodoTasks{
TaskType: Reissue, // 补卡提醒
ProjectId: projectID, // 项目 ID
UserId: userID, // 消息接收者的 sys_User.Id
Role: role,
OrderId: orderId,
MissingCardTime: missingCardTime, // 缺卡时间 示例:`03月14日 08:30上班卡18:00下班卡`
}
// 发布提醒
_, err := dao.TodoTasks.Ctx(context.Background()).Insert(&task)
return err
}
// CreateAttendanceApprovalReminder 发布一条考勤审批提醒
// projectID: 项目 ID
// userID: 消息接收者的 sys_user.id
// creatorID创建人 ID
// targetID: 对应跳转的主键ID
func CreateAttendanceApprovalReminder(userID string, projectID int, creatorID string, targetID int) error {
if reminders.HasLetter(creatorID) || reminders.HasLetter(userID) {
if creatorID == userID {
creatorID = reminders.GetUserIDByOpenID(creatorID)
userID = creatorID
} else {
if reminders.HasLetter(creatorID) {
creatorID = reminders.GetUserIDByOpenID(creatorID)
}
if reminders.HasLetter(userID) {
userID = reminders.GetUserIDByOpenID(userID)
}
}
if creatorID == "" || userID == "" {
return fmt.Errorf("未找到对应的用户")
}
}
// 构建消息模板
task := do.TodoTasks{
TaskType: AttendanceApproval, // 考勤审批提醒
ProjectId: projectID, // 项目 ID
UserId: userID, // 消息接收者的 sys_User.Id
Applicant: creatorID, // 创建人ID
OrderId: targetID, // 对应跳转的主键ID
Status: 0,
}
// 发布提醒
_, err := dao.TodoTasks.Ctx(context.Background()).Insert(&task)
return err
}
// CreateAskForLeaveReminder 发布一条请假审批提醒
// userID: 消息接收者的 sys_user.id
// projectID: 项目 ID
// creatorID创建人 ID
// targetID: 对应跳转的主键ID
func CreateAskForLeaveReminder(userID string, projectID int, creatorID string, targetID int) error {
if reminders.HasLetter(creatorID) || reminders.HasLetter(userID) {
if creatorID == userID {
creatorID = reminders.GetUserIDByOpenID(creatorID)
userID = creatorID
} else {
if reminders.HasLetter(creatorID) {
creatorID = reminders.GetUserIDByOpenID(creatorID)
}
if reminders.HasLetter(userID) {
userID = reminders.GetUserIDByOpenID(userID)
}
}
if creatorID == "" || userID == "" {
return fmt.Errorf("未找到对应的用户")
}
}
// 构建消息模板
task := do.TodoTasks{
TaskType: ApprovalReminder, // 请假审批提醒
UserId: userID, // 消息接收者的 sys_User.Id
ProjectId: projectID, // 项目 ID
Applicant: creatorID, // 创建人ID
OrderId: targetID, // 对应跳转的主键ID
Status: 0, // 未处理
Role: "1", // 发送给施工人员
}
// 发布提醒
_, err := dao.TodoTasks.Ctx(context.Background()).Insert(&task)
return err
}
func CreateAttendanceApprovalGroup(list []ReissueReminder, num int) error {
UserIDs := lo.FilterMap(list, func(item ReissueReminder, _ int) (string, bool) {
if reminders.HasLetter(item.UserID) {
return item.UserID, true
}
return item.UserID, false
})
CreatorIDs := lo.FilterMap(list, func(item ReissueReminder, _ int) (string, bool) {
if reminders.HasLetter(item.CreatorID) {
return item.CreatorID, true
}
return item.CreatorID, false
})
// 获取 OpenID 对应的 sys_user.id
openIDToUserIDMap := reminders.GetUserIDByOpenIDs(append(UserIDs, CreatorIDs...))
// 替换 todoList 中的 userID
for i := range list {
if newUserID, ok := openIDToUserIDMap[list[i].UserID]; ok {
list[i].UserID = newUserID
}
if newUserID, ok := openIDToUserIDMap[list[i].CreatorID]; ok {
list[i].CreatorID = newUserID
}
}
// 同一组的消息拥有相同的 UUID
groupUUID := uuid.New().String()
todoTasks := lo.Map(list, func(item ReissueReminder, _ int) do.TodoTasks {
return do.TodoTasks{
TaskType: num, // 考勤审批提醒
ProjectId: item.ProjectID, // 项目 ID
UserId: item.UserID, // 消息接收者的 sys_User.Id
Applicant: item.CreatorID, // 创建人ID
OrderId: item.TargetID, // 对应跳转的主键ID
Role: item.Role,
Uuid: groupUUID,
}
})
_, err := dao.TodoTasks.Ctx(context.Background()).Insert(todoTasks)
return err
}
// MarkTaskAsProcessed 标记特定的待办任务为已处理
// 每个待办任务都与一个 OrderID 关联,可以通过 OrderID 查找任务详情
// 用户完成补卡或审批操作后,应将相应的待办任务标记为已处理
func MarkTaskAsProcessed(taskID int) error {
return g.Try(context.Background(), func(ctx context.Context) {
_, err := dao.TodoTasks.Ctx(ctx).Where("order_id", taskID).Update(g.Map{"status": 1})
liberr.ErrIsNil(ctx, err, "标记代办任务为已处理失败")
})
}
// 根据 uuid 将一组待办任务标记为已处理
func MarkTasksAsProcessedByUUID(id int64) error {
return g.Try(context.Background(), func(ctx context.Context) {
value, err2 := dao.TodoTasks.Ctx(ctx).Where("order_id", id).Where("status", 0).Limit(1).Fields("uuid").Value()
liberr.ErrIsNil(ctx, err2)
_, err := dao.TodoTasks.Ctx(ctx).Where("uuid", value.String()).Update(g.Map{"status": 1})
liberr.ErrIsNil(ctx, err, "标记代办任务为已处理失败")
})
}