package services
import (
"context"
"encoding/json"
"eta/eta_forum_task/models"
"eta/eta_forum_task/models/system"
"eta/eta_forum_task/services/alarm_msg"
"eta/eta_forum_task/services/eta_forum"
"eta/eta_forum_task/utils"
"fmt"
"time"
)
type AdminBatchSaveReq struct {
List []*models.Admin
}
func AdminBatchSaveTask(cont context.Context) (err error) {
deleteCache := true
cacheKey := "eta_forum_task:EtaForumAdminUpdate"
defer func() {
if deleteCache {
_ = utils.Rc.Delete(cacheKey)
}
if err != nil {
go alarm_msg.SendAlarmMsg(utils.APP_NAME_CN+"同步管理员数据失败"+time.Now().Format("2006-01-02 15:04:05")+"
"+err.Error(), 3)
}
}()
if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
deleteCache = false
err = fmt.Errorf("系统处理中,请稍后重试!")
return
}
// 查询需要更新的图表信息总数
adminList, err := models.GetAllAdminList()
if err != nil {
return
}
if len(adminList) == 0 {
return
}
// 更新管理员数据
//查询用户信息
var req AdminBatchSaveReq
req.List = adminList
reqJson, err := json.Marshal(req)
if err != nil {
err = fmt.Errorf("参数解析异常,Err:" + err.Error())
return
}
respItem, err := eta_forum.AdminBatchSaveLib(string(reqJson))
if err != nil {
err = fmt.Errorf("上传管理员信息失败,Err:" + err.Error())
return
}
if respItem.Ret != 200 {
err = fmt.Errorf("上传管理员信息失败,Err:%v,errMsg:%v", respItem.Msg, respItem.ErrMsg)
return
}
fmt.Println("同步管理员数据完成, 同步管理员数据完成:", len(adminList), "条")
go GroupSyncTask()
go RoleSyncTask()
go DepartmentSyncTask()
return
}
type SysGroupSyncReq struct {
List []*system.SysGroup
}
// 同步分组信息
func GroupSyncTask() (err error) {
defer func() {
if err != nil {
go alarm_msg.SendAlarmMsg(utils.APP_NAME_CN+"同步部门分组数据失败"+time.Now().Format("2006-01-02 15:04:05")+"
"+err.Error(), 3)
}
}()
//查询用户信息
list, err := system.GetAllSysGroup()
if err != nil {
err = fmt.Errorf("获取分组信息失败,Err:" + err.Error())
return
}
var req SysGroupSyncReq
req.List = list
reqJson, err := json.Marshal(req)
if err != nil {
err = fmt.Errorf("参数解析异常,Err:" + err.Error())
return
}
respItem, err := eta_forum.PostLib("/v1/system/group/sync", string(reqJson))
if err != nil {
err = fmt.Errorf("上传角色信息失败,Err:" + err.Error())
return
}
if respItem.Ret != 200 {
err = fmt.Errorf("上传角色信息失败,Err:%v,errMsg:%v", respItem.Msg, respItem.ErrMsg)
return
}
fmt.Println("同步部门分组数据成功:", len(list), "条")
return
}
type DepartmentSyncReq struct {
List []*system.SysDepartment
}
// 同步部门信息
func DepartmentSyncTask() (err error) {
defer func() {
if err != nil {
go alarm_msg.SendAlarmMsg(utils.APP_NAME_CN+"同步部门数据失败"+time.Now().Format("2006-01-02 15:04:05")+"
"+err.Error(), 3)
}
}()
//查询用户信息
list, err := system.GetAllSysDepartment()
if err != nil {
err = fmt.Errorf("获取部门信息失败,Err:" + err.Error())
return
}
var req DepartmentSyncReq
req.List = list
reqJson, err := json.Marshal(req)
if err != nil {
err = fmt.Errorf("参数解析异常,Err:" + err.Error())
return
}
respItem, err := eta_forum.PostLib("/v1/system/department/sync", string(reqJson))
if err != nil {
err = fmt.Errorf("同步部门信息失败,Err:" + err.Error())
return
}
if respItem.Ret != 200 {
err = fmt.Errorf("同步部门信息失败,Err:%v,errMsg:%v", respItem.Msg, respItem.ErrMsg)
return
}
fmt.Println("同步部门数据成功:", len(list), "条")
return
}
type SysRoleSyncReq struct {
List []*system.SysRole
}
// 同步角色信息
func RoleSyncTask() (err error) {
defer func() {
if err != nil {
go alarm_msg.SendAlarmMsg(utils.APP_NAME_CN+"同步角色信息失败"+time.Now().Format("2006-01-02 15:04:05")+"
"+err.Error(), 3)
}
}()
//查询用户信息
list, err := system.GetAllSysRole()
if err != nil {
err = fmt.Errorf("获取角色信息失败,Err:" + err.Error())
return
}
var req SysRoleSyncReq
req.List = list
reqJson, err := json.Marshal(req)
if err != nil {
err = fmt.Errorf("参数解析异常,Err:" + err.Error())
return
}
respItem, err := eta_forum.PostLib("/v1/system/role/sync", string(reqJson))
if err != nil {
err = fmt.Errorf("同步角色信息失败,Err:" + err.Error())
return
}
if respItem.Ret != 200 {
err = fmt.Errorf("同步角色信息失败,Err:%v,errMsg:%v", respItem.Msg, respItem.ErrMsg)
return
}
fmt.Println("同步角色数据成功:", len(list), "条")
return
}