dddd
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
9.7 KiB

package commonus
import (
"encoding/json"
"strconv"
"time"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/testpage"
"github.com/flipped-aurora/gin-vue-admin/server/model/wechat"
"github.com/flipped-aurora/gin-vue-admin/server/utils/redishandel"
)
//遍历所有父类
type ErgodicStruct struct {
ParentId int64
Date []int64
}
//遍历组织架构
func (e *ErgodicStruct) ErgodicParentClassGroup(id int64, groupStruct []wechat.GroupForm) {
for _, v := range groupStruct {
if e.ParentId == 1 {
if v.Id == id {
e.Date = append(e.Date, v.Parentid)
e.ErgodicParentClassGroup(v.Parentid, groupStruct)
}
} else {
if v.Id == id {
if v.Parentid != 1 {
e.Date = append(e.Date, v.Parentid)
}
e.ErgodicParentClassGroup(v.Parentid, groupStruct)
}
}
}
}
//冒泡排序法
func BubbleSort(slice []int64) []int64 {
for n := 0; n <= len(slice); n++ {
for i := 1; i < len(slice)-n; i++ {
if slice[i] < slice[i-1] {
slice[i], slice[i-1] = slice[i-1], slice[i]
}
}
}
return slice
}
//获取公司,分厂,工段
func GetGroupInfo(id int64) (isTrue bool, groupStruct wechat.GroupForm) {
isTrue = false
var groupStructs wechat.GroupForm //集团表结构
err := global.GVA_DB_WatchDate.Where("g_id = ?", id).First(&groupStruct).Error //获取集团信息
if err != nil {
groupStruct, isTrue = AddWechatGroup(id, groupStructs)
return
}
isTrue = true
// groupInfo["name"] = groupStruct.Name
return
}
//获主数据库员工信息(微信版本)
func GetMainDataBaseUserInfo(weChatId string) (isTrue bool, myInfo testpage.WorkMan) {
isTrue = false
err := global.GVA_DB_Master.Where("qywx_key = ? or wx_key = ?", weChatId, weChatId).First(&myInfo).Error //获取集团信息
if err != nil {
userWechat := GetWechatUserInfo(weChatId)
isTrues, myDepartment := GetGroupInfo(int64(userWechat.MainDepartment))
if isTrues == false {
return
}
myInfo.WorkWechatId = userWechat.UserId
var arrAy []int64
errdd := json.Unmarshal([]byte(myDepartment.Group), &arrAy)
if errdd == nil {
if len(arrAy) >= 2 {
if arrAy[0] == 1 {
myInfo.Group = arrAy[1]
} else {
myInfo.Group = arrAy[1]
}
}
}
var weChatInfo wechat.WechatUsers
weChatInfo.Uid = GetFileNumber()
weChatInfo.Userid = userWechat.UserId
weChatInfo.Name = userWechat.Name
jsonStr, jsonErr := json.Marshal(userWechat.Department)
if jsonErr == nil {
weChatInfo.Department = string(jsonStr)
} else {
weChatInfo.Department = "[]"
}
weChatInfo.Position = userWechat.Position
weChatInfo.Mobile = userWechat.Mobile
GenderInt, gendererr := strconv.Atoi(userWechat.Gender)
if gendererr == nil {
weChatInfo.Gender = GenderInt
} else {
weChatInfo.Gender = 1
}
weChatInfo.Email = userWechat.Email
weChatInfo.Avatar = userWechat.Avatar
weChatInfo.Status = userWechat.Status
extattrStr, extattrErr := json.Marshal(userWechat.Extattr)
if extattrErr == nil {
weChatInfo.Extattr = string(extattrStr)
} else {
weChatInfo.Extattr = "{\"attrs\":[]}"
}
weChatInfo.MainDepartment = int64(userWechat.MainDepartment)
weChatInfo.QrCode = userWechat.QrCode
isLeaderInDeptStr, isLeaderInDeptrErr := json.Marshal(userWechat.IsLeaderInDept)
if isLeaderInDeptrErr == nil {
weChatInfo.IsLeaderInDept = string(isLeaderInDeptStr)
} else {
weChatInfo.IsLeaderInDept = "[0]"
}
weChatInfo.ThumbAvatar = userWechat.ThumbAvatar
weChatInfo.Time = time.Now().Unix()
addErr := global.GVA_DB_WatchDate.Create(&weChatInfo).Error
if addErr == nil {
isTrue = true
}
return
}
isTrue = true
return
}
//获取成员信息
func GetWechatUserInfo(weChatId string) (us UserListStruct) {
if weChatId == "" {
return
}
redisClient := redishandel.RunRedis()
isTrue, tokenInfo := redisClient.HashGet("deUserAry:wechat_"+global.GVA_CONFIG.RedisPrefix.Alias, "userinfo_"+"_"+weChatId)
if isTrue != true {
userInfo, _, _ := GetOneAddressBookMember(weChatId)
// var callBackData WechatUserInfo
// json.Unmarshal(userInfo, &callBackData)
// var us UserListStruct
json.Unmarshal(userInfo, &us)
userMap := MapOut()
json.Unmarshal(userInfo, &userMap)
jsonUser, _ := json.Marshal(us)
redisClient.SetRedisTime(2678400)
redisClient.HashSet("deUserAry:wechat_"+global.GVA_CONFIG.RedisPrefix.Alias, "userinfo_"+"_"+weChatId, string(jsonUser))
// fmt.Printf("未经过redis=====>%v\n", weChatId)
} else {
// var us UserListStruct
json.Unmarshal([]byte(tokenInfo), &us)
// fmt.Printf("经过redis=====>%v\n", weChatId)
}
return
}
//添加企业微信组织
func AddWechatGroup(id int64, groupStruct wechat.GroupForm) (groupInfo wechat.GroupForm, isTrue bool) {
isTrue = false
groupFrameWork, isTrueErr := GetWechatFramework(id)
if isTrueErr == false {
return
}
var callBackData dePartMent
err := json.Unmarshal(groupFrameWork, &callBackData)
if err != nil {
return
}
var sunGroup []int64
sunGroup = append(sunGroup, id)
if callBackData.Errcode == 0 {
redisClient := redishandel.RunRedis()
redisClient.SetRedisTime(604800)
for _, val := range callBackData.DePartMent {
// fmt.Printf("Key ==> %v ------ Val ==> %v\n", key, val)
writeRedisData := map[string]interface{}{
"id": val.Id,
"name": val.Name,
"name_en": val.NameEN,
"parentid": val.Parentid,
"order": val.Order,
}
departId := strconv.FormatInt(val.Id, 10)
if id == val.Id {
groupInfo.Id = val.Id
groupInfo.Name = val.Name
groupInfo.Parentid = val.Parentid
groupInfo.Orderes = val.Order
groupInfo.Time = time.Now().Unix()
groupInfo.Sate = 1
jsonStr, jsonErr := json.Marshal(sunGroup)
if jsonErr == nil {
groupInfo.GroupSun = string(jsonStr)
}
var group []wechat.GroupForm
listErr := global.GVA_DB_WatchDate.Find(&group).Error
if listErr == nil {
var parentGroup ErgodicStruct
parentGroup.ParentId = val.Parentid
parentGroup.ErgodicParentClassGroup(val.Parentid, group)
parentGroup.Date = append(parentGroup.Date, val.Parentid)
parentGroup.Date = append(parentGroup.Date, val.Id)
parentGroup.Date = BubbleSort(parentGroup.Date)
jsonStrFather, jsonErrFather := json.Marshal(parentGroup.Date)
if jsonErrFather == nil {
groupInfo.Group = string(jsonStrFather)
}
}
addErr := global.GVA_DB_WatchDate.Create(&groupInfo).Error
if addErr == nil {
isTrue = true
}
} else {
sunGroup = append(sunGroup, val.Id)
}
redisClient.HashMsetAdd("dePartMentAry:ment_"+global.GVA_CONFIG.RedisPrefix.Alias+"_"+departId, writeRedisData)
}
dePartMentJson, _ := json.Marshal(callBackData.DePartMent)
dePartMentIdStr := strconv.FormatInt(id, 10)
redisClient.Set("dePartMentList:list_"+global.GVA_CONFIG.RedisPrefix.Alias+"_"+dePartMentIdStr, string(dePartMentJson))
}
//
return
}
//获取成员信息列表
func GetMemberList() (userStruct []wechat.WechatUsers, isTrue bool) {
isTrue = false
var userAry []wechat.WechatUsers
redisClient := redishandel.RunRedis()
isTrues, tokenInfo := redisClient.Get("deUserAry:wechatUser_" + global.GVA_CONFIG.RedisPrefix.Alias + "_all")
if isTrues != true {
userErr := global.GVA_DB_WatchDate.Where("status = 1").Find(&userAry).Error
if userErr != nil {
return
}
for _, userCont := range userAry {
var userInfo wechat.WechatUsers
userInfo.Uid = userCont.Uid
userInfo.Userid = userCont.Userid
userInfo.Name = userCont.Name
userInfo.Department = userCont.Department
userInfo.Position = userCont.Position
userInfo.Mobile = userCont.Mobile
userInfo.Gender = userCont.Gender
userInfo.Email = userCont.Email
userInfo.Avatar = userCont.Avatar
userInfo.Status = userCont.Status
userInfo.Extattr = userCont.Extattr
userInfo.MainDepartment = userCont.MainDepartment
userInfo.QrCode = userCont.QrCode
userInfo.IsLeaderInDept = userCont.IsLeaderInDept
userInfo.ThumbAvatar = userCont.ThumbAvatar
userInfo.UserNumber = userCont.UserNumber
userInfo.SysBf = userCont.SysBf
userInfo.SysWs = userCont.SysWs
userInfo.SysPs = userCont.SysPs
userInfo.WmTema = userCont.WmTema
userInfo.IsAdmin = userCont.IsAdmin
userInfo.IsRole = userCont.IsRole
userInfo.Pwd = userCont.Pwd
userInfo.Time = userCont.Time
userStruct = append(userStruct, userInfo)
}
isTrue = true
jsonUserList, jsonErr := json.Marshal(userAry)
if jsonErr != nil {
return
}
redisClient.SetRedisTime(259200)
redisClient.Set("deUserAry:wechatUser_"+global.GVA_CONFIG.RedisPrefix.Alias+"_all", string(jsonUserList))
} else {
jsonUserList := json.Unmarshal([]byte(tokenInfo), &userAry)
if jsonUserList != nil {
return
}
for _, userCont := range userAry {
var userInfo wechat.WechatUsers
userInfo.Uid = userCont.Uid
userInfo.Userid = userCont.Userid
userInfo.Name = userCont.Name
userInfo.Department = userCont.Department
userInfo.Position = userCont.Position
userInfo.Mobile = userCont.Mobile
userInfo.Gender = userCont.Gender
userInfo.Email = userCont.Email
userInfo.Avatar = userCont.Avatar
userInfo.Status = userCont.Status
userInfo.Extattr = userCont.Extattr
userInfo.MainDepartment = userCont.MainDepartment
userInfo.QrCode = userCont.QrCode
userInfo.IsLeaderInDept = userCont.IsLeaderInDept
userInfo.ThumbAvatar = userCont.ThumbAvatar
userInfo.UserNumber = userCont.UserNumber
userInfo.SysBf = userCont.SysBf
userInfo.SysWs = userCont.SysWs
userInfo.SysPs = userCont.SysPs
userInfo.WmTema = userCont.WmTema
userInfo.IsAdmin = userCont.IsAdmin
userInfo.IsRole = userCont.IsRole
userInfo.Pwd = userCont.Pwd
userInfo.Time = userCont.Time
userStruct = append(userStruct, userInfo)
}
isTrue = true
}
return
}