123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- package xiangyu
- import (
- "errors"
- "eta/eta_bridge/global"
- "eta/eta_bridge/models/eta"
- "eta/eta_bridge/services/xiangyu"
- "fmt"
- "strings"
- "time"
- )
- // SyncUser
- // @Description: 全量同步用户
- // @author: Roc
- // @datetime 2024-01-22 10:28:39
- // @return err error
- func SyncUser() (err error) {
- if global.CONFIG.Xiangyu.DefaultRoleId <= 0 {
- err = errors.New("默认角色ID不能为空")
- return
- }
- // 指定的角色
- roleInfo, err := eta.GetSysRoleById(global.CONFIG.Xiangyu.DefaultRoleId)
- if err != nil {
- return
- }
- // 获取所有部门和分组
- departmentMap, departmentNameMap, groupMap, groupNameMap, userMap, err := getAllDepartmentAndGroup()
- if err != nil {
- return
- }
- // 获取token
- tokenId, err := xiangyu.Login()
- if err != nil {
- return
- }
- //tokenId := `228f7024-1966-47bf-86b5-9ec61ac149ed`
- defer func() {
- // 获取用户列表
- tmpErr := xiangyu.Logout(tokenId)
- if tmpErr != nil {
- fmt.Println("退出失败:", tmpErr.Error())
- }
- }()
- for {
- // 获取用户列表
- userResp, tmpErr := xiangyu.SyncTask(tokenId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 没有数据了,那么结束同步了
- if userResp == nil || userResp.Out.ObjectCode == `` {
- return
- }
- // 是否同步成功
- var isSync bool
- var etaUserId, message string
- // 处理用户信息,将象屿用户与eta用户做关联
- tmpErr = handleUser(userResp.Out.Data, userResp.Out.Id, departmentMap, departmentNameMap, groupMap, groupNameMap, userMap, roleInfo)
- if tmpErr == nil {
- isSync = true
- if etUserInfo, ok := userMap[userResp.Out.Data.Username]; ok {
- etaUserId = fmt.Sprint(etUserInfo.AdminId)
- }
- } else {
- message = tmpErr.Error()
- }
- // 结束单次同步
- err = xiangyu.SyncFinish(tokenId, userResp.Out.ObjectCode, userResp.Out.ObjectType, userResp.Out.Id, etaUserId, message, isSync)
- if err != nil {
- return
- }
- }
- return
- }
- // PullUser
- // @Description: 增量同步用户
- // @author: Roc
- // @datetime 2024-01-23 18:01:27
- // @return err error
- func PullUser() (err error) {
- if global.CONFIG.Xiangyu.DefaultRoleId <= 0 {
- err = errors.New("默认角色ID不能为空")
- return
- }
- defer func() {
- if err != nil {
- global.FILE_LOG.Info("同步增量用户失败:" + err.Error())
- }
- }()
- // 指定的角色
- roleInfo, err := eta.GetSysRoleById(global.CONFIG.Xiangyu.DefaultRoleId)
- if err != nil {
- return
- }
- // 获取所有部门和分组
- departmentMap, departmentNameMap, groupMap, groupNameMap, userMap, err := getAllDepartmentAndGroup()
- if err != nil {
- return
- }
- // 获取token
- tokenId, err := xiangyu.Login()
- if err != nil {
- return
- }
- defer func() {
- // 获取用户列表
- tmpErr := xiangyu.Logout(tokenId)
- if tmpErr != nil {
- fmt.Println("退出失败:", tmpErr.Error())
- }
- }()
- for {
- // 获取增量用户列表
- userResp, tmpErr := xiangyu.PullTask(tokenId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 没有数据了,那么结束同步了
- if userResp == nil || userResp.Out.ObjectCode == `` {
- return
- }
- // 是否同步成功
- var isSync bool
- var etaUserId, message string
- // 处理用户信息,将象屿用户与eta用户做关联
- tmpErr = handleUser(userResp.Out.Data, userResp.Out.Id, departmentMap, departmentNameMap, groupMap, groupNameMap, userMap, roleInfo)
- if tmpErr == nil {
- isSync = true
- if etUserInfo, ok := userMap[userResp.Out.Data.Username]; ok {
- etaUserId = fmt.Sprint(etUserInfo.AdminId)
- }
- } else {
- message = tmpErr.Error()
- }
- // 结束单次增量同步
- err = xiangyu.PullFinish(tokenId, userResp.Out.TaskId, etaUserId, message, isSync)
- if err != nil {
- return
- }
- }
- return
- }
- // TaskPullUser
- // @Description: 定时增量同步用户
- // @author: Roc
- // @datetime 2024-01-23 18:03:04
- func TaskPullUser() {
- PullUser()
- return
- }
- // getAllDepartmentAndGroup
- // @Description: 获取所有的部门和分组
- // @author: Roc
- // @datetime 2024-01-18 17:39:14
- // @return departmentMap map[string]*eta.SysDepartment
- // @return groupMap map[string]*eta.SysGroup
- // @return err error
- func getAllDepartmentAndGroup() (departmentMap, departmentNameMap map[string]*eta.SysDepartment, groupMap, groupNameMap map[string]*eta.SysGroup, userMap map[string]*eta.Admin, err error) {
- // 获取所有的部门
- departmentList, err := eta.GetAllDepartment()
- if err != nil {
- return
- }
- // 部门信息
- departmentMap = make(map[string]*eta.SysDepartment)
- departmentNameMap = make(map[string]*eta.SysDepartment)
- for _, department := range departmentList {
- if department.OutID == `` {
- departmentNameMap[department.DepartmentName] = department
- } else {
- departmentMap[department.OutID] = department
- }
- }
- // 获取所有的分组
- groupList, err := eta.GetAllSysGroup()
- if err != nil {
- return
- }
- // 部门信息
- groupMap = make(map[string]*eta.SysGroup)
- groupNameMap = make(map[string]*eta.SysGroup)
- for _, group := range groupList {
- if group.OutID == `` {
- groupNameMap[fmt.Sprint(group.DepartmentId, "_", group.GroupName)] = group
- } else {
- groupMap[group.OutID] = group
- }
- }
- // 获取所有的用户
- adminList, err := eta.GetAllSysUser()
- if err != nil {
- return
- }
- // 部门信息
- userMap = make(map[string]*eta.Admin)
- for _, admin := range adminList {
- userMap[admin.AdminName] = admin
- }
- return
- }
- // handleUser
- // @Description: 用户信息处理
- // @author: Roc
- // @datetime 2024-01-18 18:00:04
- // @param xyUserInfo xiangyu.UserData
- // @param departmentMap map[string]*eta.SysDepartment
- // @param groupMap map[string]*eta.SysGroup
- // @param userMap map[string]*eta.Admin
- // @return err error
- func handleUser(xyUserInfo xiangyu.UserData, xyUserId string, departmentMap, departmentNameMap map[string]*eta.SysDepartment, groupMap, groupNameMap map[string]*eta.SysGroup, userMap map[string]*eta.Admin, roleInfo *eta.SysRole) (err error) {
- // 判断公司id是否存在,如果不存在,那么就去创建
- departmentInfo, ok := departmentMap[xyUserInfo.CompanyId]
- if !ok {
- // 没有外部id的部门,那么用名称去匹配
- departmentInfo, ok = departmentNameMap[xyUserInfo.CompanyName]
- if !ok {
- // 如果没有的话,那么就创建该部门
- departmentInfo = &eta.SysDepartment{
- DepartmentId: 0,
- DepartmentName: xyUserInfo.CompanyName,
- OutID: xyUserInfo.CompanyId,
- CreateTime: time.Now(),
- Sort: 0,
- }
- err = departmentInfo.Create()
- if err != nil {
- return
- }
- } else {
- // 如果通过名称找到了,那么就更新一下外部id
- departmentInfo.OutID = xyUserInfo.CompanyId
- err = departmentInfo.Update([]string{"OutID"})
- if err != nil {
- return
- }
- delete(departmentNameMap, xyUserInfo.CompanyName)
- }
- departmentMap[xyUserInfo.CompanyId] = departmentInfo
- }
- // 判断部门id是否存在,如果不存在,那么就去创建
- groupInfo, ok := groupMap[xyUserInfo.DepartId]
- if !ok {
- groupInfo, ok = groupNameMap[fmt.Sprint(departmentInfo.DepartmentId, "_", xyUserInfo.DepartmentName)]
- if !ok {
- // 如果通过名称还是没有找到,那么就创建一个分组
- groupInfo = &eta.SysGroup{
- GroupId: 0,
- DepartmentId: departmentInfo.DepartmentId,
- GroupName: xyUserInfo.DepartmentName,
- CreateTime: time.Now(),
- ParentId: 0,
- Sort: 0,
- OutID: xyUserInfo.DepartId,
- }
- // 如果没有的话,那么就创建该分组
- err = groupInfo.Create()
- if err != nil {
- return
- }
- } else {
- // 如果通过名称找到了,那么就更新一下外部id
- groupInfo.OutID = xyUserInfo.DepartId
- err = groupInfo.Update([]string{"OutID"})
- if err != nil {
- return
- }
- delete(groupNameMap, xyUserInfo.DepartmentName)
- }
- groupMap[xyUserInfo.DepartId] = groupInfo
- }
- enabled := 1
- if xyUserInfo.IsDisabled {
- enabled = 0
- }
- // 判断用户是否存在,如果不存在,那么就去创建
- userInfo, ok := userMap[xyUserInfo.Username]
- if !ok {
- userInfo = &eta.Admin{
- AdminId: 0,
- AdminName: xyUserInfo.Username,
- AdminAvatar: "",
- RealName: xyUserInfo.FullName,
- Password: xyUserInfo.Password,
- //LastUpdatedPasswordTime: time.Time{},
- Enabled: enabled,
- Email: "",
- //LastLoginTime: time.Time{},
- CreatedTime: time.Now(),
- LastUpdatedTime: time.Now(),
- //Role: roleInfo.,
- Mobile: xyUserInfo.Mobile,
- RoleType: 0,
- RoleId: roleInfo.RoleId,
- RoleName: roleInfo.RoleName,
- RoleTypeCode: roleInfo.RoleTypeCode,
- DepartmentId: departmentInfo.DepartmentId,
- DepartmentName: departmentInfo.DepartmentName,
- GroupId: groupInfo.GroupId,
- GroupName: groupInfo.GroupName,
- Authority: 0,
- Position: xyUserInfo.PositionName,
- //DisableTime: time.Time{},
- ChartPermission: 0,
- EdbPermission: 0,
- MysteelChemicalPermission: 0,
- OpenId: "",
- UnionId: "",
- PredictEdbPermission: 0,
- Province: "",
- ProvinceCode: "",
- City: "",
- CityCode: "",
- EmployeeId: xyUserInfo.EmployeeNo,
- TelAreaCode: "",
- OutID: xyUserId,
- }
- if strings.Contains(roleInfo.RoleTypeCode, "researcher") {
- userInfo.Role = "researcher"
- } else if strings.Contains(userInfo.RoleTypeCode, "seller") {
- userInfo.Role = "sales"
- } else {
- userInfo.Role = "admin"
- }
- // 如果没有的话,那么就创建该用户
- err = userInfo.Create()
- if err != nil {
- return
- }
- } else {
- updateColList := make([]string, 0)
- //状态
- if userInfo.Enabled != enabled {
- updateColList = append(updateColList, "Enabled")
- userInfo.Enabled = enabled
- }
- // 部门
- if userInfo.DepartmentId != departmentInfo.DepartmentId {
- updateColList = append(updateColList, "DepartmentId", "DepartmentName")
- userInfo.DepartmentId = departmentInfo.DepartmentId
- userInfo.DepartmentName = departmentInfo.DepartmentName
- }
- // 分组
- if userInfo.GroupId != groupInfo.GroupId {
- updateColList = append(updateColList, "GroupId", "GroupName")
- userInfo.GroupId = groupInfo.GroupId
- userInfo.GroupName = groupInfo.GroupName
- }
- // 职位
- if userInfo.Position != xyUserInfo.PositionName {
- updateColList = append(updateColList, "Position")
- userInfo.Position = xyUserInfo.PositionName
- }
- // 姓名
- if userInfo.RealName != xyUserInfo.FullName {
- updateColList = append(updateColList, "RealName")
- userInfo.RealName = xyUserInfo.FullName
- }
- // 手机号
- if userInfo.Mobile != xyUserInfo.Mobile {
- updateColList = append(updateColList, "Mobile")
- userInfo.Mobile = xyUserInfo.Mobile
- }
- // 外部id
- if userInfo.OutID == `` {
- updateColList = append(updateColList, "OutID")
- userInfo.OutID = xyUserId
- }
- // 工号
- if userInfo.EmployeeId != xyUserInfo.EmployeeNo {
- updateColList = append(updateColList, "EmployeeId")
- userInfo.OutID = xyUserInfo.EmployeeNo
- }
- if len(updateColList) > 0 {
- updateColList = append(updateColList, "LastUpdatedTime")
- userInfo.LastUpdatedTime = time.Now()
- err = userInfo.Update(updateColList)
- if err != nil {
- return
- }
- }
- }
- userMap[xyUserInfo.Username] = userInfo
- return
- }
|