user.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. package xiangyu
  2. import (
  3. "errors"
  4. "eta/eta_bridge/global"
  5. "eta/eta_bridge/models/eta"
  6. "eta/eta_bridge/services/xiangyu"
  7. "fmt"
  8. "strings"
  9. "time"
  10. )
  11. // SyncUser
  12. // @Description: 全量同步用户
  13. // @author: Roc
  14. // @datetime 2024-01-22 10:28:39
  15. // @return err error
  16. func SyncUser() (err error) {
  17. if global.CONFIG.Xiangyu.DefaultRoleId <= 0 {
  18. err = errors.New("默认角色ID不能为空")
  19. return
  20. }
  21. // 指定的角色
  22. roleInfo, err := eta.GetSysRoleById(global.CONFIG.Xiangyu.DefaultRoleId)
  23. if err != nil {
  24. return
  25. }
  26. // 获取所有部门和分组
  27. departmentMap, departmentNameMap, groupMap, groupNameMap, userMap, err := getAllDepartmentAndGroup()
  28. if err != nil {
  29. return
  30. }
  31. // 获取token
  32. tokenId, err := xiangyu.Login()
  33. if err != nil {
  34. return
  35. }
  36. //tokenId := `228f7024-1966-47bf-86b5-9ec61ac149ed`
  37. defer func() {
  38. // 获取用户列表
  39. tmpErr := xiangyu.Logout(tokenId)
  40. if tmpErr != nil {
  41. fmt.Println("退出失败:", tmpErr.Error())
  42. }
  43. }()
  44. for {
  45. // 获取用户列表
  46. userResp, tmpErr := xiangyu.SyncTask(tokenId)
  47. if tmpErr != nil {
  48. err = tmpErr
  49. return
  50. }
  51. // 没有数据了,那么结束同步了
  52. if userResp == nil || userResp.Out.ObjectCode == `` {
  53. return
  54. }
  55. // 是否同步成功
  56. var isSync bool
  57. var etaUserId, message string
  58. // 处理用户信息,将象屿用户与eta用户做关联
  59. tmpErr = handleUser(userResp.Out.Data, userResp.Out.Id, departmentMap, departmentNameMap, groupMap, groupNameMap, userMap, roleInfo)
  60. if tmpErr == nil {
  61. isSync = true
  62. if etUserInfo, ok := userMap[userResp.Out.Data.Username]; ok {
  63. etaUserId = fmt.Sprint(etUserInfo.AdminId)
  64. }
  65. } else {
  66. message = tmpErr.Error()
  67. }
  68. // 结束单次同步
  69. err = xiangyu.SyncFinish(tokenId, userResp.Out.ObjectCode, userResp.Out.ObjectType, userResp.Out.Id, etaUserId, message, isSync)
  70. if err != nil {
  71. return
  72. }
  73. }
  74. return
  75. }
  76. // PullUser
  77. // @Description: 增量同步用户
  78. // @author: Roc
  79. // @datetime 2024-01-23 18:01:27
  80. // @return err error
  81. func PullUser() (err error) {
  82. if global.CONFIG.Xiangyu.DefaultRoleId <= 0 {
  83. err = errors.New("默认角色ID不能为空")
  84. return
  85. }
  86. defer func() {
  87. if err != nil {
  88. global.FILE_LOG.Info("同步增量用户失败:" + err.Error())
  89. }
  90. }()
  91. // 指定的角色
  92. roleInfo, err := eta.GetSysRoleById(global.CONFIG.Xiangyu.DefaultRoleId)
  93. if err != nil {
  94. return
  95. }
  96. // 获取所有部门和分组
  97. departmentMap, departmentNameMap, groupMap, groupNameMap, userMap, err := getAllDepartmentAndGroup()
  98. if err != nil {
  99. return
  100. }
  101. // 获取token
  102. tokenId, err := xiangyu.Login()
  103. if err != nil {
  104. return
  105. }
  106. defer func() {
  107. // 获取用户列表
  108. tmpErr := xiangyu.Logout(tokenId)
  109. if tmpErr != nil {
  110. fmt.Println("退出失败:", tmpErr.Error())
  111. }
  112. }()
  113. for {
  114. // 获取增量用户列表
  115. userResp, tmpErr := xiangyu.PullTask(tokenId)
  116. if tmpErr != nil {
  117. err = tmpErr
  118. return
  119. }
  120. // 没有数据了,那么结束同步了
  121. if userResp == nil || userResp.Out.ObjectCode == `` {
  122. return
  123. }
  124. // 是否同步成功
  125. var isSync bool
  126. var etaUserId, message string
  127. // 处理用户信息,将象屿用户与eta用户做关联
  128. tmpErr = handleUser(userResp.Out.Data, userResp.Out.Id, departmentMap, departmentNameMap, groupMap, groupNameMap, userMap, roleInfo)
  129. if tmpErr == nil {
  130. isSync = true
  131. if etUserInfo, ok := userMap[userResp.Out.Data.Username]; ok {
  132. etaUserId = fmt.Sprint(etUserInfo.AdminId)
  133. }
  134. } else {
  135. message = tmpErr.Error()
  136. }
  137. // 结束单次增量同步
  138. err = xiangyu.PullFinish(tokenId, userResp.Out.TaskId, etaUserId, message, isSync)
  139. if err != nil {
  140. return
  141. }
  142. }
  143. return
  144. }
  145. // TaskPullUser
  146. // @Description: 定时增量同步用户
  147. // @author: Roc
  148. // @datetime 2024-01-23 18:03:04
  149. func TaskPullUser() {
  150. PullUser()
  151. return
  152. }
  153. // getAllDepartmentAndGroup
  154. // @Description: 获取所有的部门和分组
  155. // @author: Roc
  156. // @datetime 2024-01-18 17:39:14
  157. // @return departmentMap map[string]*eta.SysDepartment
  158. // @return groupMap map[string]*eta.SysGroup
  159. // @return err error
  160. func getAllDepartmentAndGroup() (departmentMap, departmentNameMap map[string]*eta.SysDepartment, groupMap, groupNameMap map[string]*eta.SysGroup, userMap map[string]*eta.Admin, err error) {
  161. // 获取所有的部门
  162. departmentList, err := eta.GetAllDepartment()
  163. if err != nil {
  164. return
  165. }
  166. // 部门信息
  167. departmentMap = make(map[string]*eta.SysDepartment)
  168. departmentNameMap = make(map[string]*eta.SysDepartment)
  169. for _, department := range departmentList {
  170. if department.OutID == `` {
  171. departmentNameMap[department.DepartmentName] = department
  172. } else {
  173. departmentMap[department.OutID] = department
  174. }
  175. }
  176. // 获取所有的分组
  177. groupList, err := eta.GetAllSysGroup()
  178. if err != nil {
  179. return
  180. }
  181. // 部门信息
  182. groupMap = make(map[string]*eta.SysGroup)
  183. groupNameMap = make(map[string]*eta.SysGroup)
  184. for _, group := range groupList {
  185. if group.OutID == `` {
  186. groupNameMap[fmt.Sprint(group.DepartmentId, "_", group.GroupName)] = group
  187. } else {
  188. groupMap[group.OutID] = group
  189. }
  190. }
  191. // 获取所有的用户
  192. adminList, err := eta.GetAllSysUser()
  193. if err != nil {
  194. return
  195. }
  196. // 部门信息
  197. userMap = make(map[string]*eta.Admin)
  198. for _, admin := range adminList {
  199. userMap[admin.AdminName] = admin
  200. }
  201. return
  202. }
  203. // handleUser
  204. // @Description: 用户信息处理
  205. // @author: Roc
  206. // @datetime 2024-01-18 18:00:04
  207. // @param xyUserInfo xiangyu.UserData
  208. // @param departmentMap map[string]*eta.SysDepartment
  209. // @param groupMap map[string]*eta.SysGroup
  210. // @param userMap map[string]*eta.Admin
  211. // @return err error
  212. 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) {
  213. // 判断公司id是否存在,如果不存在,那么就去创建
  214. departmentInfo, ok := departmentMap[xyUserInfo.CompanyId]
  215. if !ok {
  216. // 没有外部id的部门,那么用名称去匹配
  217. departmentInfo, ok = departmentNameMap[xyUserInfo.CompanyName]
  218. if !ok {
  219. // 如果没有的话,那么就创建该部门
  220. departmentInfo = &eta.SysDepartment{
  221. DepartmentId: 0,
  222. DepartmentName: xyUserInfo.CompanyName,
  223. OutID: xyUserInfo.CompanyId,
  224. CreateTime: time.Now(),
  225. Sort: 0,
  226. }
  227. err = departmentInfo.Create()
  228. if err != nil {
  229. return
  230. }
  231. } else {
  232. // 如果通过名称找到了,那么就更新一下外部id
  233. departmentInfo.OutID = xyUserInfo.CompanyId
  234. err = departmentInfo.Update([]string{"OutID"})
  235. if err != nil {
  236. return
  237. }
  238. delete(departmentNameMap, xyUserInfo.CompanyName)
  239. }
  240. departmentMap[xyUserInfo.CompanyId] = departmentInfo
  241. }
  242. // 判断部门id是否存在,如果不存在,那么就去创建
  243. groupInfo, ok := groupMap[xyUserInfo.DepartId]
  244. if !ok {
  245. groupInfo, ok = groupNameMap[fmt.Sprint(departmentInfo.DepartmentId, "_", xyUserInfo.DepartmentName)]
  246. if !ok {
  247. // 如果通过名称还是没有找到,那么就创建一个分组
  248. groupInfo = &eta.SysGroup{
  249. GroupId: 0,
  250. DepartmentId: departmentInfo.DepartmentId,
  251. GroupName: xyUserInfo.DepartmentName,
  252. CreateTime: time.Now(),
  253. ParentId: 0,
  254. Sort: 0,
  255. OutID: xyUserInfo.DepartId,
  256. }
  257. // 如果没有的话,那么就创建该分组
  258. err = groupInfo.Create()
  259. if err != nil {
  260. return
  261. }
  262. } else {
  263. // 如果通过名称找到了,那么就更新一下外部id
  264. groupInfo.OutID = xyUserInfo.DepartId
  265. err = groupInfo.Update([]string{"OutID"})
  266. if err != nil {
  267. return
  268. }
  269. delete(groupNameMap, xyUserInfo.DepartmentName)
  270. }
  271. groupMap[xyUserInfo.DepartId] = groupInfo
  272. }
  273. enabled := 1
  274. if xyUserInfo.IsDisabled {
  275. enabled = 0
  276. }
  277. // 判断用户是否存在,如果不存在,那么就去创建
  278. userInfo, ok := userMap[xyUserInfo.Username]
  279. if !ok {
  280. userInfo = &eta.Admin{
  281. AdminId: 0,
  282. AdminName: xyUserInfo.Username,
  283. AdminAvatar: "",
  284. RealName: xyUserInfo.FullName,
  285. Password: xyUserInfo.Password,
  286. //LastUpdatedPasswordTime: time.Time{},
  287. Enabled: enabled,
  288. Email: "",
  289. //LastLoginTime: time.Time{},
  290. CreatedTime: time.Now(),
  291. LastUpdatedTime: time.Now(),
  292. //Role: roleInfo.,
  293. Mobile: xyUserInfo.Mobile,
  294. RoleType: 0,
  295. RoleId: roleInfo.RoleId,
  296. RoleName: roleInfo.RoleName,
  297. RoleTypeCode: roleInfo.RoleTypeCode,
  298. DepartmentId: departmentInfo.DepartmentId,
  299. DepartmentName: departmentInfo.DepartmentName,
  300. GroupId: groupInfo.GroupId,
  301. GroupName: groupInfo.GroupName,
  302. Authority: 0,
  303. Position: xyUserInfo.PositionName,
  304. //DisableTime: time.Time{},
  305. ChartPermission: 0,
  306. EdbPermission: 0,
  307. MysteelChemicalPermission: 0,
  308. OpenId: "",
  309. UnionId: "",
  310. PredictEdbPermission: 0,
  311. Province: "",
  312. ProvinceCode: "",
  313. City: "",
  314. CityCode: "",
  315. EmployeeId: xyUserInfo.EmployeeNo,
  316. TelAreaCode: "",
  317. OutID: xyUserId,
  318. }
  319. if strings.Contains(roleInfo.RoleTypeCode, "researcher") {
  320. userInfo.Role = "researcher"
  321. } else if strings.Contains(userInfo.RoleTypeCode, "seller") {
  322. userInfo.Role = "sales"
  323. } else {
  324. userInfo.Role = "admin"
  325. }
  326. // 如果没有的话,那么就创建该用户
  327. err = userInfo.Create()
  328. if err != nil {
  329. return
  330. }
  331. } else {
  332. updateColList := make([]string, 0)
  333. //状态
  334. if userInfo.Enabled != enabled {
  335. updateColList = append(updateColList, "Enabled")
  336. userInfo.Enabled = enabled
  337. }
  338. // 部门
  339. if userInfo.DepartmentId != departmentInfo.DepartmentId {
  340. updateColList = append(updateColList, "DepartmentId", "DepartmentName")
  341. userInfo.DepartmentId = departmentInfo.DepartmentId
  342. userInfo.DepartmentName = departmentInfo.DepartmentName
  343. }
  344. // 分组
  345. if userInfo.GroupId != groupInfo.GroupId {
  346. updateColList = append(updateColList, "GroupId", "GroupName")
  347. userInfo.GroupId = groupInfo.GroupId
  348. userInfo.GroupName = groupInfo.GroupName
  349. }
  350. // 职位
  351. if userInfo.Position != xyUserInfo.PositionName {
  352. updateColList = append(updateColList, "Position")
  353. userInfo.Position = xyUserInfo.PositionName
  354. }
  355. // 姓名
  356. if userInfo.RealName != xyUserInfo.FullName {
  357. updateColList = append(updateColList, "RealName")
  358. userInfo.RealName = xyUserInfo.FullName
  359. }
  360. // 手机号
  361. if userInfo.Mobile != xyUserInfo.Mobile {
  362. updateColList = append(updateColList, "Mobile")
  363. userInfo.Mobile = xyUserInfo.Mobile
  364. }
  365. // 外部id
  366. if userInfo.OutID == `` {
  367. updateColList = append(updateColList, "OutID")
  368. userInfo.OutID = xyUserId
  369. }
  370. // 工号
  371. if userInfo.EmployeeId != xyUserInfo.EmployeeNo {
  372. updateColList = append(updateColList, "EmployeeId")
  373. userInfo.OutID = xyUserInfo.EmployeeNo
  374. }
  375. if len(updateColList) > 0 {
  376. updateColList = append(updateColList, "LastUpdatedTime")
  377. userInfo.LastUpdatedTime = time.Now()
  378. err = userInfo.Update(updateColList)
  379. if err != nil {
  380. return
  381. }
  382. }
  383. }
  384. userMap[xyUserInfo.Username] = userInfo
  385. return
  386. }