sys_user_sync.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_bridge/models/crm"
  5. "eta/eta_bridge/models/eta"
  6. "eta/eta_bridge/services/alarm_msg"
  7. "eta/eta_bridge/utils"
  8. "fmt"
  9. "time"
  10. )
  11. // SyncRoleData 同步角色数据
  12. type SyncRoleData struct {
  13. Source int `description:"来源: 1-CRM; 2-ETA"`
  14. RoleId int `description:"角色ID"`
  15. }
  16. // HandleSyncRole 同步系统角色
  17. func HandleSyncRole(data SyncRoleData) (err error) {
  18. if data.RoleId <= 0 {
  19. return
  20. }
  21. if data.Source != utils.SOURCE_CRM_FLAG && data.Source != utils.SOURCE_ETA_FLAG {
  22. return
  23. }
  24. defer func() {
  25. if err != nil {
  26. j, _ := json.Marshal(data)
  27. tips := fmt.Sprintf("CRM-ETA同步系统角色失败, errMsg: %s\ndata: %s", err.Error(), string(j))
  28. go alarm_msg.SendAlarmMsg(tips, 3)
  29. }
  30. }()
  31. // CRM
  32. if data.Source == utils.SOURCE_CRM_FLAG {
  33. // 获取角色不存在, 则表示CRM删除了该角色, 需删除对应ETA角色
  34. crmRole, e := crm.GetSysRoleById(data.RoleId)
  35. if e != nil {
  36. if e != utils.ErrNoRow {
  37. err = fmt.Errorf("获取CRM角色信息失败, Err: " + e.Error())
  38. return
  39. }
  40. _ = eta.DeleteRoleById(data.RoleId)
  41. return
  42. }
  43. // 获取ETA对应角色, 存在则更新, 否则新增
  44. etaRole, e := eta.GetSysRoleById(data.RoleId)
  45. if e != nil {
  46. if e != utils.ErrNoRow {
  47. err = fmt.Errorf("获取ETA对应角色信息失败, Err: " + e.Error())
  48. return
  49. }
  50. // 新增
  51. newRole := new(eta.SysRole)
  52. newRole.RoleId = crmRole.RoleId
  53. newRole.RoleName = crmRole.RoleName
  54. newRole.RoleType = crmRole.RoleType
  55. newRole.RoleTypeCode = crmRole.RoleTypeCode
  56. newRole.RoleLevel = crmRole.RoleLevel
  57. newRole.CreateTime = time.Now().Local()
  58. newRole.ModifyTime = time.Now().Local()
  59. e = newRole.Create()
  60. if e != nil {
  61. err = fmt.Errorf("新增ETA对应角色失败, Err: " + e.Error())
  62. }
  63. return
  64. }
  65. // 更新
  66. etaRole.RoleName = crmRole.RoleName
  67. etaRole.RoleType = crmRole.RoleType
  68. etaRole.RoleTypeCode = crmRole.RoleTypeCode
  69. etaRole.RoleLevel = crmRole.RoleLevel
  70. etaRole.ModifyTime = time.Now().Local()
  71. cols := []string{"RoleName", "RoleType", "RoleTypeCode", "RoleLevel", "ModifyTime"}
  72. e = etaRole.Update(cols)
  73. if e != nil {
  74. err = fmt.Errorf("更新ETA对应角色失败, Err: " + e.Error())
  75. }
  76. return
  77. }
  78. // ETA
  79. etaRole, e := eta.GetSysRoleById(data.RoleId)
  80. if e != nil {
  81. if e != utils.ErrNoRow {
  82. err = fmt.Errorf("获取ETA角色信息失败, Err: " + e.Error())
  83. return
  84. }
  85. _ = crm.DeleteRoleById(data.RoleId)
  86. return
  87. }
  88. crmRole, e := crm.GetSysRoleById(data.RoleId)
  89. if e != nil {
  90. if e != utils.ErrNoRow {
  91. err = fmt.Errorf("获取CRM对应角色信息失败, Err: " + e.Error())
  92. return
  93. }
  94. // 新增
  95. newRole := new(crm.SysRole)
  96. newRole.RoleId = etaRole.RoleId
  97. newRole.RoleName = etaRole.RoleName
  98. newRole.RoleType = etaRole.RoleType
  99. newRole.RoleTypeCode = etaRole.RoleTypeCode
  100. newRole.RoleLevel = etaRole.RoleLevel
  101. newRole.CreateTime = time.Now().Local()
  102. newRole.ModifyTime = time.Now().Local()
  103. e = newRole.Create()
  104. if e != nil {
  105. err = fmt.Errorf("新增CRM对应角色失败, Err: " + e.Error())
  106. }
  107. return
  108. }
  109. // 更新
  110. crmRole.RoleName = etaRole.RoleName
  111. crmRole.RoleType = etaRole.RoleType
  112. crmRole.RoleTypeCode = etaRole.RoleTypeCode
  113. crmRole.RoleLevel = etaRole.RoleLevel
  114. crmRole.ModifyTime = time.Now().Local()
  115. cols := []string{"RoleName", "RoleType", "RoleTypeCode", "RoleLevel", "ModifyTime"}
  116. e = crmRole.Update(cols)
  117. if e != nil {
  118. err = fmt.Errorf("更新CRM对应角色失败, Err: " + e.Error())
  119. }
  120. return
  121. }
  122. // SyncAdminData 同步用户数据
  123. type SyncAdminData struct {
  124. Source int `description:"来源: 1-CRM; 2-ETA"`
  125. AdminName string `description:"用户名"`
  126. }
  127. // HandleSyncAdmin 同步系统用户
  128. func HandleSyncAdmin(data SyncAdminData) (err error) {
  129. if data.AdminName == "" {
  130. return
  131. }
  132. if data.Source != utils.SOURCE_CRM_FLAG && data.Source != utils.SOURCE_ETA_FLAG {
  133. return
  134. }
  135. defer func() {
  136. if err != nil {
  137. j, _ := json.Marshal(data)
  138. tips := fmt.Sprintf("CRM-ETA同步系统用户失败, errMsg: %s\ndata: %s", err.Error(), string(j))
  139. go alarm_msg.SendAlarmMsg(tips, 3)
  140. }
  141. }()
  142. // CRM
  143. if data.Source == utils.SOURCE_CRM_FLAG {
  144. // 获取用户不存在, 则表示CRM删除了该用户, 需删除对应ETA用户
  145. crmUser, e := crm.GetSysUserByAdminName(data.AdminName)
  146. if e != nil {
  147. if e != utils.ErrNoRow {
  148. err = fmt.Errorf("获取CRM用户信息失败, Err: " + e.Error())
  149. return
  150. }
  151. _ = eta.DeleteSysUserByName(data.AdminName)
  152. return
  153. }
  154. // 获取ETA对应用户, 存在则更新, 否则新增
  155. etaUser, e := eta.GetSysUserByAdminName(data.AdminName)
  156. if e != nil {
  157. if e != utils.ErrNoRow {
  158. err = fmt.Errorf("获取ETA对应用户信息失败, Err: " + e.Error())
  159. return
  160. }
  161. // 新增
  162. newUser := new(eta.Admin)
  163. newUser.AdminName = crmUser.AdminName
  164. newUser.AdminAvatar = crmUser.AdminAvatar
  165. newUser.RealName = crmUser.RealName
  166. newUser.Password = crmUser.Password
  167. newUser.LastUpdatedPasswordTime = crmUser.LastUpdatedPasswordTime
  168. newUser.Enabled = crmUser.Enabled
  169. newUser.Email = crmUser.Email
  170. newUser.LastLoginTime = crmUser.LastLoginTime
  171. newUser.CreatedTime = time.Now().Local()
  172. newUser.LastUpdatedTime = time.Now().Local()
  173. newUser.Role = crmUser.Role
  174. newUser.Mobile = crmUser.Mobile
  175. newUser.RoleType = crmUser.RoleType
  176. newUser.RoleId = crmUser.RoleId // 理论上角色也会同步ID应该也是一样
  177. newUser.RoleName = crmUser.RoleName
  178. newUser.RoleTypeCode = crmUser.RoleTypeCode
  179. newUser.DepartmentId = crmUser.DepartmentId
  180. newUser.DepartmentName = crmUser.DepartmentName
  181. newUser.GroupId = crmUser.GroupId
  182. newUser.GroupName = crmUser.GroupName
  183. newUser.Authority = crmUser.Authority
  184. newUser.Position = crmUser.Position
  185. newUser.DisableTime = crmUser.DisableTime
  186. newUser.ChartPermission = crmUser.ChartPermission
  187. newUser.EdbPermission = crmUser.EdbPermission
  188. newUser.MysteelChemicalPermission = crmUser.MysteelChemicalPermission
  189. newUser.OpenId = crmUser.OpenId
  190. newUser.UnionId = crmUser.UnionId
  191. newUser.PredictEdbPermission = crmUser.PredictEdbPermission
  192. newUser.Province = crmUser.Province
  193. newUser.ProvinceCode = crmUser.ProvinceCode
  194. newUser.City = crmUser.City
  195. newUser.CityCode = crmUser.CityCode
  196. newUser.EmployeeId = crmUser.EmployeeId
  197. newUser.TelAreaCode = crmUser.TelAreaCode
  198. e = newUser.Create()
  199. if e != nil {
  200. err = fmt.Errorf("新增ETA对应用户失败, Err: " + e.Error())
  201. }
  202. return
  203. }
  204. // 更新
  205. etaUser.AdminName = crmUser.AdminName
  206. etaUser.AdminAvatar = crmUser.AdminAvatar
  207. etaUser.RealName = crmUser.RealName
  208. etaUser.Password = crmUser.Password
  209. etaUser.LastUpdatedPasswordTime = crmUser.LastUpdatedPasswordTime
  210. etaUser.Enabled = crmUser.Enabled
  211. etaUser.Email = crmUser.Email
  212. etaUser.LastUpdatedTime = time.Now().Local()
  213. etaUser.Role = crmUser.Role
  214. etaUser.Mobile = crmUser.Mobile
  215. etaUser.RoleType = crmUser.RoleType
  216. etaUser.RoleId = crmUser.RoleId
  217. etaUser.RoleName = crmUser.RoleName
  218. etaUser.RoleTypeCode = crmUser.RoleTypeCode
  219. etaUser.DepartmentId = crmUser.DepartmentId
  220. etaUser.DepartmentName = crmUser.DepartmentName
  221. etaUser.GroupId = crmUser.GroupId
  222. etaUser.GroupName = crmUser.GroupName
  223. etaUser.Authority = crmUser.Authority
  224. etaUser.Position = crmUser.Position
  225. etaUser.DisableTime = crmUser.DisableTime
  226. etaUser.ChartPermission = crmUser.ChartPermission
  227. etaUser.EdbPermission = crmUser.EdbPermission
  228. etaUser.MysteelChemicalPermission = crmUser.MysteelChemicalPermission
  229. etaUser.OpenId = crmUser.OpenId
  230. etaUser.UnionId = crmUser.UnionId
  231. etaUser.PredictEdbPermission = crmUser.PredictEdbPermission
  232. etaUser.Province = crmUser.Province
  233. etaUser.ProvinceCode = crmUser.ProvinceCode
  234. etaUser.City = crmUser.City
  235. etaUser.CityCode = crmUser.CityCode
  236. etaUser.EmployeeId = crmUser.EmployeeId
  237. etaUser.TelAreaCode = crmUser.TelAreaCode
  238. cols := []string{
  239. "AdminName", "AdminAvatar", "RealName", "Password", "LastUpdatedPasswordTime", "Enabled", "Email", "LastUpdatedTime",
  240. "Role", "Mobile", "RoleType", "RoleId", "RoleName", "RoleTypeCode", "DepartmentId", "DepartmentName", "GroupId", "GroupName",
  241. "Authority", "Position", "DirectorId", "DisableTime", "ChartPermission", "EdbPermission", "MysteelChemicalPermission", "OpenId",
  242. "UnionId", "PredictEdbPermission", "Province", "ProvinceCode", "City", "CityCode", "EmployeeId", "TelAreaCode",
  243. }
  244. e = etaUser.Update(cols)
  245. if e != nil {
  246. err = fmt.Errorf("更新ETA对应用户失败, Err: " + e.Error())
  247. }
  248. return
  249. }
  250. // ETA
  251. etaUser, e := eta.GetSysUserByAdminName(data.AdminName)
  252. if e != nil {
  253. if e != utils.ErrNoRow {
  254. err = fmt.Errorf("获取ETA用户信息失败, Err: " + e.Error())
  255. return
  256. }
  257. _ = crm.DeleteSysUserByName(data.AdminName)
  258. return
  259. }
  260. crmUser, e := crm.GetSysUserByAdminName(data.AdminName)
  261. if e != nil {
  262. if e != utils.ErrNoRow {
  263. err = fmt.Errorf("获取ETA对应用户信息失败, Err: " + e.Error())
  264. return
  265. }
  266. // 新增
  267. newUser := new(crm.Admin)
  268. newUser.AdminName = etaUser.AdminName
  269. newUser.AdminAvatar = etaUser.AdminAvatar
  270. newUser.RealName = etaUser.RealName
  271. newUser.Password = etaUser.Password
  272. newUser.LastUpdatedPasswordTime = etaUser.LastUpdatedPasswordTime
  273. newUser.Enabled = etaUser.Enabled
  274. newUser.Email = etaUser.Email
  275. newUser.LastLoginTime = etaUser.LastLoginTime
  276. newUser.CreatedTime = time.Now().Local()
  277. newUser.LastUpdatedTime = time.Now().Local()
  278. newUser.Role = etaUser.Role
  279. newUser.Mobile = etaUser.Mobile
  280. newUser.RoleType = etaUser.RoleType
  281. newUser.RoleId = etaUser.RoleId
  282. newUser.RoleName = etaUser.RoleName
  283. newUser.RoleTypeCode = etaUser.RoleTypeCode
  284. newUser.DepartmentId = etaUser.DepartmentId
  285. newUser.DepartmentName = etaUser.DepartmentName
  286. newUser.GroupId = etaUser.GroupId
  287. newUser.GroupName = etaUser.GroupName
  288. newUser.Authority = etaUser.Authority
  289. newUser.Position = etaUser.Position
  290. newUser.DisableTime = etaUser.DisableTime
  291. newUser.ChartPermission = etaUser.ChartPermission
  292. newUser.EdbPermission = etaUser.EdbPermission
  293. newUser.MysteelChemicalPermission = etaUser.MysteelChemicalPermission
  294. newUser.OpenId = etaUser.OpenId
  295. newUser.UnionId = etaUser.UnionId
  296. newUser.PredictEdbPermission = etaUser.PredictEdbPermission
  297. newUser.Province = etaUser.Province
  298. newUser.ProvinceCode = etaUser.ProvinceCode
  299. newUser.City = etaUser.City
  300. newUser.CityCode = etaUser.CityCode
  301. newUser.EmployeeId = etaUser.EmployeeId
  302. newUser.TelAreaCode = etaUser.TelAreaCode
  303. e = newUser.Create()
  304. if e != nil {
  305. err = fmt.Errorf("新增ETA对应用户失败, Err: " + e.Error())
  306. }
  307. return
  308. }
  309. // 更新
  310. crmUser.AdminName = etaUser.AdminName
  311. crmUser.AdminAvatar = etaUser.AdminAvatar
  312. crmUser.RealName = etaUser.RealName
  313. crmUser.Password = etaUser.Password
  314. crmUser.LastUpdatedPasswordTime = etaUser.LastUpdatedPasswordTime
  315. crmUser.Enabled = etaUser.Enabled
  316. crmUser.Email = etaUser.Email
  317. crmUser.LastUpdatedTime = time.Now().Local()
  318. crmUser.Role = etaUser.Role
  319. crmUser.Mobile = etaUser.Mobile
  320. crmUser.RoleType = etaUser.RoleType
  321. crmUser.RoleId = etaUser.RoleId
  322. crmUser.RoleName = etaUser.RoleName
  323. crmUser.RoleTypeCode = etaUser.RoleTypeCode
  324. crmUser.DepartmentId = etaUser.DepartmentId
  325. crmUser.DepartmentName = etaUser.DepartmentName
  326. crmUser.GroupId = etaUser.GroupId
  327. crmUser.GroupName = etaUser.GroupName
  328. crmUser.Authority = etaUser.Authority
  329. crmUser.Position = etaUser.Position
  330. crmUser.DisableTime = etaUser.DisableTime
  331. crmUser.ChartPermission = etaUser.ChartPermission
  332. crmUser.EdbPermission = etaUser.EdbPermission
  333. crmUser.MysteelChemicalPermission = etaUser.MysteelChemicalPermission
  334. crmUser.OpenId = etaUser.OpenId
  335. crmUser.UnionId = etaUser.UnionId
  336. crmUser.PredictEdbPermission = etaUser.PredictEdbPermission
  337. crmUser.Province = etaUser.Province
  338. crmUser.ProvinceCode = etaUser.ProvinceCode
  339. crmUser.City = etaUser.City
  340. crmUser.CityCode = etaUser.CityCode
  341. crmUser.EmployeeId = etaUser.EmployeeId
  342. crmUser.TelAreaCode = etaUser.TelAreaCode
  343. cols := []string{
  344. "AdminName", "AdminAvatar", "RealName", "Password", "LastUpdatedPasswordTime", "Enabled", "Email", "LastUpdatedTime",
  345. "Role", "Mobile", "RoleType", "RoleId", "RoleName", "RoleTypeCode", "DepartmentId", "DepartmentName", "GroupId", "GroupName",
  346. "Authority", "Position", "DirectorId", "DisableTime", "ChartPermission", "EdbPermission", "MysteelChemicalPermission", "OpenId",
  347. "UnionId", "PredictEdbPermission", "Province", "ProvinceCode", "City", "CityCode", "EmployeeId", "TelAreaCode",
  348. }
  349. e = crmUser.Update(cols)
  350. if e != nil {
  351. err = fmt.Errorf("更新CRM对应用户失败, Err: " + e.Error())
  352. }
  353. return
  354. }
  355. // SyncDepartmentData 同步部门数据
  356. type SyncDepartmentData struct {
  357. Source int `description:"来源: 1-CRM; 2-ETA"`
  358. DepartmentId int `description:"部门ID"`
  359. }
  360. // HandleSyncDepartment 同步部门
  361. func HandleSyncDepartment(data SyncDepartmentData) (err error) {
  362. if data.DepartmentId <= 0 {
  363. return
  364. }
  365. if data.Source != utils.SOURCE_CRM_FLAG && data.Source != utils.SOURCE_ETA_FLAG {
  366. return
  367. }
  368. defer func() {
  369. if err != nil {
  370. j, _ := json.Marshal(data)
  371. tips := fmt.Sprintf("CRM-ETA同步部门失败, errMsg: %s\ndata: %s", err.Error(), string(j))
  372. go alarm_msg.SendAlarmMsg(tips, 3)
  373. }
  374. }()
  375. // CRM
  376. if data.Source == utils.SOURCE_CRM_FLAG {
  377. // 获取部门不存在, 则表示CRM删除了该部门, 需删除对应ETA部门
  378. crmDepartment, e := crm.GetDepartmentById(data.DepartmentId)
  379. if e != nil {
  380. if e != utils.ErrNoRow {
  381. err = fmt.Errorf("获取CRM部门信息失败, Err: " + e.Error())
  382. return
  383. }
  384. _ = eta.DeleteDepartmentById(data.DepartmentId)
  385. return
  386. }
  387. // 获取ETA对应部门, 存在则更新, 否则新增
  388. etaDepartment, e := eta.GetDepartmentById(data.DepartmentId)
  389. if e != nil {
  390. if e != utils.ErrNoRow {
  391. err = fmt.Errorf("获取ETA对应部门信息失败, Err: " + e.Error())
  392. return
  393. }
  394. // 新增
  395. newDepartment := new(eta.SysDepartment)
  396. newDepartment.DepartmentId = crmDepartment.DepartmentId
  397. newDepartment.DepartmentName = crmDepartment.DepartmentName
  398. newDepartment.Sort = crmDepartment.Sort
  399. newDepartment.CreateTime = time.Now().Local()
  400. e = newDepartment.Create()
  401. if e != nil {
  402. err = fmt.Errorf("新增ETA对应部门失败, Err: " + e.Error())
  403. }
  404. return
  405. }
  406. // 更新
  407. etaDepartment.DepartmentName = crmDepartment.DepartmentName
  408. etaDepartment.Sort = crmDepartment.Sort
  409. cols := []string{"DepartmentName", "Sort"}
  410. e = etaDepartment.Update(cols)
  411. if e != nil {
  412. err = fmt.Errorf("更新ETA对应部门失败, Err: " + e.Error())
  413. }
  414. return
  415. }
  416. // ETA
  417. etaDepartment, e := eta.GetDepartmentById(data.DepartmentId)
  418. if e != nil {
  419. if e != utils.ErrNoRow {
  420. err = fmt.Errorf("获取ETA部门信息失败, Err: " + e.Error())
  421. return
  422. }
  423. _ = crm.DeleteDepartmentById(data.DepartmentId)
  424. return
  425. }
  426. crmDepartment, e := crm.GetDepartmentById(data.DepartmentId)
  427. if e != nil {
  428. if e != utils.ErrNoRow {
  429. err = fmt.Errorf("获取CRM对应部门信息失败, Err: " + e.Error())
  430. return
  431. }
  432. // 新增
  433. newDepartment := new(crm.SysDepartment)
  434. newDepartment.DepartmentId = etaDepartment.DepartmentId
  435. newDepartment.DepartmentName = etaDepartment.DepartmentName
  436. newDepartment.Sort = etaDepartment.Sort
  437. newDepartment.CreateTime = time.Now().Local()
  438. e = newDepartment.Create()
  439. if e != nil {
  440. err = fmt.Errorf("新增CRM对应部门失败, Err: " + e.Error())
  441. }
  442. return
  443. }
  444. // 更新
  445. crmDepartment.DepartmentName = etaDepartment.DepartmentName
  446. crmDepartment.Sort = etaDepartment.Sort
  447. cols := []string{"DepartmentName", "Sort"}
  448. e = crmDepartment.Update(cols)
  449. if e != nil {
  450. err = fmt.Errorf("更新CRM对应部门失败, Err: " + e.Error())
  451. }
  452. return
  453. }
  454. // SyncGroupData 同步分组数据
  455. type SyncGroupData struct {
  456. Source int `description:"来源: 1-CRM; 2-ETA"`
  457. GroupId int `description:"分组ID"`
  458. }
  459. // HandleSyncGroup 同步分组
  460. func HandleSyncGroup(data SyncGroupData) (err error) {
  461. if data.GroupId <= 0 {
  462. return
  463. }
  464. if data.Source != utils.SOURCE_CRM_FLAG && data.Source != utils.SOURCE_ETA_FLAG {
  465. return
  466. }
  467. defer func() {
  468. if err != nil {
  469. j, _ := json.Marshal(data)
  470. tips := fmt.Sprintf("CRM-ETA同步分组失败, errMsg: %s\ndata: %s", err.Error(), string(j))
  471. go alarm_msg.SendAlarmMsg(tips, 3)
  472. }
  473. }()
  474. // CRM
  475. if data.Source == utils.SOURCE_CRM_FLAG {
  476. // 获取分组不存在, 则表示CRM删除了该分组, 需删除对应ETA分组
  477. crmGroup, e := crm.GetSysGroupByGroupId(data.GroupId)
  478. if e != nil {
  479. if e != utils.ErrNoRow {
  480. err = fmt.Errorf("获取CRM分组信息失败, Err: " + e.Error())
  481. return
  482. }
  483. _ = eta.DeleteGroupByGroupId(data.GroupId)
  484. return
  485. }
  486. // 获取ETA对应分组, 存在则更新, 否则新增
  487. etaGroup, e := eta.GetSysGroupByGroupId(data.GroupId)
  488. if e != nil {
  489. if e != utils.ErrNoRow {
  490. err = fmt.Errorf("获取ETA对应分组信息失败, Err: " + e.Error())
  491. return
  492. }
  493. // 新增
  494. newGroup := new(eta.SysGroup)
  495. newGroup.GroupId = crmGroup.GroupId
  496. newGroup.DepartmentId = crmGroup.DepartmentId
  497. newGroup.GroupName = crmGroup.GroupName
  498. newGroup.ParentId = crmGroup.ParentId
  499. newGroup.Sort = crmGroup.Sort
  500. newGroup.CreateTime = time.Now().Local()
  501. e = newGroup.Create()
  502. if e != nil {
  503. err = fmt.Errorf("新增ETA对应分组失败, Err: " + e.Error())
  504. }
  505. return
  506. }
  507. // 更新
  508. etaGroup.DepartmentId = crmGroup.DepartmentId
  509. etaGroup.GroupName = crmGroup.GroupName
  510. etaGroup.ParentId = crmGroup.ParentId
  511. etaGroup.Sort = crmGroup.Sort
  512. cols := []string{"DepartmentId", "GroupName", "ParentId", "Sort"}
  513. e = etaGroup.Update(cols)
  514. if e != nil {
  515. err = fmt.Errorf("更新ETA对应分组失败, Err: " + e.Error())
  516. }
  517. return
  518. }
  519. // ETA
  520. etaGroup, e := eta.GetSysGroupByGroupId(data.GroupId)
  521. if e != nil {
  522. if e != utils.ErrNoRow {
  523. err = fmt.Errorf("获取ETA分组信息失败, Err: " + e.Error())
  524. return
  525. }
  526. _ = crm.DeleteGroupByGroupId(data.GroupId)
  527. return
  528. }
  529. crmGroup, e := crm.GetSysGroupByGroupId(data.GroupId)
  530. if e != nil {
  531. if e != utils.ErrNoRow {
  532. err = fmt.Errorf("获取CRM对应分组信息失败, Err: " + e.Error())
  533. return
  534. }
  535. // 新增
  536. newGroup := new(crm.SysGroup)
  537. newGroup.GroupId = etaGroup.GroupId
  538. newGroup.DepartmentId = etaGroup.DepartmentId
  539. newGroup.GroupName = etaGroup.GroupName
  540. newGroup.ParentId = etaGroup.ParentId
  541. newGroup.Sort = etaGroup.Sort
  542. newGroup.CreateTime = time.Now().Local()
  543. e = newGroup.Create()
  544. if e != nil {
  545. err = fmt.Errorf("新增CRM对应分组失败, Err: " + e.Error())
  546. }
  547. return
  548. }
  549. // 更新
  550. crmGroup.DepartmentId = etaGroup.DepartmentId
  551. crmGroup.GroupName = etaGroup.GroupName
  552. crmGroup.ParentId = etaGroup.ParentId
  553. crmGroup.Sort = etaGroup.Sort
  554. cols := []string{"DepartmentId", "GroupName", "ParentId", "Sort"}
  555. e = crmGroup.Update(cols)
  556. if e != nil {
  557. err = fmt.Errorf("更新CRM对应分组失败, Err: " + e.Error())
  558. }
  559. return
  560. }
  561. // SyncAdminEnRole 同步用户英文权限角色
  562. type SyncAdminEnRole struct {
  563. AdminName string `description:"用户名"`
  564. RoleCodes []string `description:"角色Codes"`
  565. }
  566. // HandleSyncUserEnRole 同步用户英文权限角色
  567. func HandleSyncUserEnRole(data SyncAdminEnRole) (err error) {
  568. if data.AdminName == "" {
  569. return
  570. }
  571. defer func() {
  572. if err != nil {
  573. j, _ := json.Marshal(data)
  574. tips := fmt.Sprintf("ETA同步至CRM英文权限角色失败, errMsg: %s\ndata: %s", err.Error(), string(j))
  575. go alarm_msg.SendAlarmMsg(tips, 3)
  576. }
  577. }()
  578. crmAdmin, e := crm.GetSysUserByAdminName(data.AdminName)
  579. if e != nil {
  580. return
  581. }
  582. // 获取英文权限角色配置
  583. confRoles := make([]eta.ConfEnAuthRole, 0)
  584. conf, e := eta.GetCrmConfByCode(eta.ConfEnAuthRoleKey)
  585. if e != nil {
  586. err = fmt.Errorf("获取英文权限角色配置失败, Err: %s", e.Error())
  587. return
  588. }
  589. if conf.ConfigValue == "" {
  590. err = fmt.Errorf("英文权限角色配置为空")
  591. return
  592. }
  593. if e := json.Unmarshal([]byte(conf.ConfigValue), &confRoles); e != nil {
  594. err = fmt.Errorf("英文权限角色配置有误, Err: %s", e.Error())
  595. return
  596. }
  597. roleCodes := make([]string, 0)
  598. for _, v := range confRoles {
  599. roleCodes = append(roleCodes, v.RoleCode)
  600. }
  601. // 获取CRM英文权限角色
  602. roles := make([]*crm.SysRole, 0)
  603. {
  604. ob := new(crm.SysRole)
  605. cond := ` role_type_code IN (?)`
  606. pars := make([]interface{}, 0)
  607. pars = append(pars, roleCodes)
  608. rs, e := ob.GetItemsByCondition(cond, pars)
  609. if e != nil {
  610. err = fmt.Errorf("获取英文权限角色失败, Err: %s", e.Error())
  611. return
  612. }
  613. roles = rs
  614. }
  615. if len(roles) == 0 {
  616. err = fmt.Errorf("英文权限角色为空")
  617. return
  618. }
  619. roleIds := make([]int, 0)
  620. roleCodeId := make(map[string]int)
  621. for _, v := range roles {
  622. roleIds = append(roleIds, v.RoleId)
  623. roleCodeId[v.RoleTypeCode] = v.RoleId
  624. }
  625. // 清空并新增
  626. items := make([]*crm.SysRoleAdmin, 0)
  627. for _, v := range data.RoleCodes {
  628. if roleCodeId[v] > 0 {
  629. items = append(items, &crm.SysRoleAdmin{
  630. AdminId: crmAdmin.AdminId,
  631. RoleId: roleCodeId[v],
  632. CreateTime: time.Now().Local(),
  633. })
  634. }
  635. }
  636. relateOb := new(crm.SysRoleAdmin)
  637. if e = relateOb.DeleteAndCreate(crmAdmin.AdminId, roleIds, items); e != nil {
  638. err = fmt.Errorf("新增用户英文权限角色失败, Err: %s", e.Error())
  639. return
  640. }
  641. return
  642. }