sync_staff_task.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package HTtask
  2. import (
  3. "errors"
  4. "eta/eta_bridge/global"
  5. "eta/eta_bridge/models/eta"
  6. "eta/eta_bridge/models/ht/oa"
  7. "eta/eta_bridge/models/ht/wx_crm"
  8. "fmt"
  9. "github.com/robfig/cron/v3"
  10. )
  11. func StartCronJob() {
  12. // 创建一个新的 cron 实例
  13. c := cron.New(cron.WithSeconds())
  14. //constr := "0 17 * * * *"
  15. constr := "*/5 * * * * *"
  16. // 添加定时任务
  17. _, err := c.AddFunc(constr, func() {
  18. go syncCrmOAInfo()
  19. go SyncETA()
  20. })
  21. if err != nil {
  22. fmt.Println("添加定时任务失败:", err)
  23. return
  24. }
  25. // 启动定时任务
  26. c.Start()
  27. // 在程序结束时停止定时任务
  28. go func() {
  29. <-make(chan struct{})
  30. c.Stop()
  31. }()
  32. }
  33. func SyncETA() {
  34. roleId := global.CONFIG.HTFutures.SyncTask.SyncRoleId
  35. role, err := eta.GetSysRoleById(roleId)
  36. if err != nil {
  37. global.FILE_LOG.Error("获取角色信息失败:", err.Error())
  38. return
  39. }
  40. //先同步分公司列表
  41. subCompanylist, err := oa.GetSubCompanyList()
  42. if err != nil {
  43. global.FILE_LOG.Error("获取海通OA子公司数据失败:", err.Error())
  44. }
  45. var sysSubCompanyList []eta.SysDepartment
  46. for _, item := range subCompanylist {
  47. sysDepartment := eta.SysDepartment{
  48. DepartmentId: item.ID,
  49. DepartmentName: item.SUBCOMPANYNAME,
  50. Sort: item.SHOWORDEROFTREE,
  51. }
  52. sysSubCompanyList = append(sysSubCompanyList, sysDepartment)
  53. }
  54. err = eta.BatchInsertOrUpdate(sysSubCompanyList)
  55. if err != nil {
  56. global.FILE_LOG.Error("同步海通OA子公司数据失败:", err.Error())
  57. return
  58. }
  59. //同步部门信息
  60. DepartmentList, err := oa.GetDepartMentList()
  61. if err != nil {
  62. global.FILE_LOG.Error("获取海通部门数据失败:", err.Error())
  63. }
  64. var sysDepartmentList []eta.SysGroup
  65. for _, item := range DepartmentList {
  66. sysDepartment := eta.SysGroup{
  67. GroupId: item.ID,
  68. DepartmentId: item.SUBCOMPANYID1,
  69. GroupName: item.DEPARTMENTNAME,
  70. ParentId: item.SUPDEPID,
  71. Sort: item.SHOWORDEROFTREE,
  72. }
  73. sysDepartmentList = append(sysDepartmentList, sysDepartment)
  74. }
  75. err = eta.BatchInsertOrUpdateGroup(sysDepartmentList)
  76. if err != nil {
  77. global.FILE_LOG.Error("同步海通部门数据数据失败:", err.Error())
  78. return
  79. }
  80. //同步员工信息
  81. staffList, err := oa.GetEmployeeList()
  82. if err != nil {
  83. global.FILE_LOG.Error("获取海通OA员工数据失败:", err.Error())
  84. }
  85. var Employee []eta.Admin
  86. for _, item := range staffList {
  87. var department *eta.SysDepartment
  88. department, err = eta.GetDepartmentById(item.SUBCOMPANYID1)
  89. if err != nil {
  90. global.FILE_LOG.Error("获取员工子公司信息失败:", err.Error(), "跳过同步员工", item.WORKCODE)
  91. continue
  92. }
  93. var group *eta.SysGroup
  94. group, err = eta.GetSysGroupByGroupId(item.DEPARTMENTID)
  95. if err != nil {
  96. global.FILE_LOG.Error("获取员工部门组织架信息失败:", err.Error(), "跳过同步员工", item.WORKCODE)
  97. continue
  98. }
  99. sysUser := eta.Admin{
  100. AdminName: item.LOGINID,
  101. RealName: item.LASTNAME,
  102. Enabled: 1,
  103. Email: item.EMAIL,
  104. //Role: role.RoleName,
  105. Mobile: item.MOBILE,
  106. RoleType: 0,
  107. RoleId: role.RoleId,
  108. RoleName: role.RoleName,
  109. RoleTypeCode: role.RoleTypeCode,
  110. DepartmentId: item.SUBCOMPANYID1,
  111. DepartmentName: department.DepartmentName,
  112. GroupId: item.DEPARTMENTID,
  113. GroupName: group.GroupName,
  114. EmployeeId: item.WORKCODE,
  115. }
  116. Employee = append(Employee, sysUser)
  117. }
  118. err = eta.BatchInsertOrUpdateAdmin(Employee)
  119. if err != nil {
  120. global.FILE_LOG.Error("同步海通OA员工数据失败:", err.Error())
  121. return
  122. }
  123. }
  124. func syncCrmOAInfo() {
  125. roleId := global.CONFIG.HTFutures.SyncTask.SyncRoleId
  126. role, err := wx_crm.GetSysRoleById(roleId)
  127. if err != nil {
  128. global.FILE_LOG.Error("获取角色信息失败:", err.Error())
  129. return
  130. }
  131. //先同步分公司列表
  132. subCompanylist, err := oa.GetSubCompanyList()
  133. if err != nil {
  134. global.FILE_LOG.Error("获取海通OA子公司数据失败:", err.Error())
  135. }
  136. var sysSubCompanyList []wx_crm.SysDepartment
  137. for _, item := range subCompanylist {
  138. sysDepartment := wx_crm.SysDepartment{
  139. SysDepartmentName: item.SUBCOMPANYNAME,
  140. Sort: item.SHOWORDEROFTREE,
  141. Level: item.TLEVEL,
  142. OutId: item.ID,
  143. Type: wx_crm.SubCompany,
  144. ParentId: item.SUPSUBCOMID,
  145. }
  146. sysSubCompanyList = append(sysSubCompanyList, sysDepartment)
  147. }
  148. err = wx_crm.BatchInsertOrUpdate(sysSubCompanyList)
  149. if err != nil {
  150. global.FILE_LOG.Error("同步海通OA子公司数据失败:", err.Error())
  151. return
  152. }
  153. //同步部门信息
  154. DepartmentList, err := oa.GetDepartMentList()
  155. if err != nil {
  156. global.FILE_LOG.Error("获取海通部门数据失败:", err.Error())
  157. }
  158. var sysDepartmentList []wx_crm.SysDepartment
  159. for _, item := range DepartmentList {
  160. sysDepartment := wx_crm.SysDepartment{
  161. SysDepartmentName: item.DEPARTMENTNAME,
  162. Sort: item.SHOWORDEROFTREE,
  163. Level: item.TLEVEL,
  164. OutId: item.ID,
  165. Type: wx_crm.Department,
  166. ParentId: item.SUPDEPID,
  167. }
  168. if sysDepartment.Level == 2 && sysDepartment.ParentId == 0 {
  169. sysDepartment.ParentId = item.SUBCOMPANYID1
  170. }
  171. sysDepartmentList = append(sysDepartmentList, sysDepartment)
  172. }
  173. err = wx_crm.BatchInsertOrUpdate(sysDepartmentList)
  174. if err != nil {
  175. global.FILE_LOG.Error("同步海通部门数据失败:", err.Error())
  176. return
  177. }
  178. //同步员工信息
  179. staffList, err := oa.GetEmployeeList()
  180. if err != nil {
  181. global.FILE_LOG.Error("获取海通OA员工数据失败:", err.Error())
  182. }
  183. var Employee []wx_crm.SysUser
  184. for _, item := range staffList {
  185. idList, departmentErr := GetEmployeeDepartmentList(item.DEPARTMENTID)
  186. if departmentErr != nil {
  187. global.FILE_LOG.Error("获取员工部门组织架构失败:", departmentErr.Error(), "跳过同步员工", item.WORKCODE)
  188. continue
  189. }
  190. fillSize := 4 - len(idList)
  191. var departments []int
  192. departments = append(departments, idList...)
  193. if fillSize > 0 {
  194. for i := 0; i < fillSize; i++ {
  195. departments = append([]int{0}, departments...)
  196. }
  197. }
  198. sysUser := wx_crm.SysUser{
  199. SysUserName: item.LOGINID,
  200. SysRealName: item.LASTNAME,
  201. Email: item.EMAIL,
  202. Phone: item.MOBILE,
  203. AreaCode: "86",
  204. SysRoleId: role.SysRoleId,
  205. SysRoleName: role.SysRoleName,
  206. SysDepartmentId: item.DEPARTMENTID,
  207. SysDepartmentId1: departments[3],
  208. SysDepartmentId2: departments[2],
  209. SysDepartmentId3: departments[1],
  210. SysDepartmentId4: departments[0],
  211. PositionCode: item.SECLEVEL,
  212. IsEnabled: true,
  213. }
  214. Employee = append(Employee, sysUser)
  215. }
  216. err = wx_crm.BatchInsertOrUpdateUser(Employee)
  217. if err != nil {
  218. global.FILE_LOG.Error("同步海通OA员工数据失败:", err.Error())
  219. return
  220. }
  221. }
  222. func GetEmployeeDepartmentList(departmentId int) (departmentIdList []int, err error) {
  223. departmentList, err := wx_crm.GetDepartmentList()
  224. if err != nil {
  225. global.FILE_LOG.Error("获取部门信息失败", err.Error())
  226. return
  227. }
  228. var currentDepartment wx_crm.SysDepartment
  229. found := false
  230. for _, item := range departmentList {
  231. if item.OutId == departmentId {
  232. currentDepartment = item
  233. found = true
  234. break
  235. }
  236. }
  237. if found {
  238. departmentIdList = append(departmentIdList, currentDepartment.OutId)
  239. subList := TraceDepartmentList(currentDepartment.ParentId, departmentList, currentDepartment.Level-1)
  240. departmentIdList = append(departmentIdList, subList...)
  241. } else {
  242. err = errors.New("当前部门id不存在")
  243. }
  244. return
  245. }
  246. func TraceDepartmentList(departmentId int, departmentList []wx_crm.SysDepartment, level int) (departmentIdList []int) {
  247. if level == 0 || departmentId == 0 {
  248. return
  249. }
  250. for _, item := range departmentList {
  251. if item.OutId == departmentId {
  252. if level > 1 && item.Type == wx_crm.SubCompany {
  253. continue
  254. }
  255. if level == 1 && item.Type == wx_crm.Department {
  256. continue
  257. }
  258. departmentIdList = append(departmentIdList, item.OutId)
  259. subSubDepartments := TraceDepartmentList(item.ParentId, departmentList, item.Level-1)
  260. departmentIdList = append(departmentIdList, subSubDepartments...)
  261. }
  262. }
  263. return departmentIdList
  264. }