123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- package HTtask
- import (
- "errors"
- "eta/eta_bridge/global"
- "eta/eta_bridge/models/eta"
- "eta/eta_bridge/models/ht/oa"
- "eta/eta_bridge/models/ht/wx_crm"
- "fmt"
- "github.com/robfig/cron/v3"
- )
- func StartCronJob() {
- // 创建一个新的 cron 实例
- c := cron.New(cron.WithSeconds())
- //constr := "0 17 * * * *"
- constr := "*/5 * * * * *"
- // 添加定时任务
- _, err := c.AddFunc(constr, func() {
- go syncCrmOAInfo()
- go SyncETA()
- })
- if err != nil {
- fmt.Println("添加定时任务失败:", err)
- return
- }
- // 启动定时任务
- c.Start()
- // 在程序结束时停止定时任务
- go func() {
- <-make(chan struct{})
- c.Stop()
- }()
- }
- func SyncETA() {
- roleId := global.CONFIG.HTFutures.SyncTask.SyncRoleId
- role, err := eta.GetSysRoleById(roleId)
- if err != nil {
- global.FILE_LOG.Error("获取角色信息失败:", err.Error())
- return
- }
- //先同步分公司列表
- subCompanylist, err := oa.GetSubCompanyList()
- if err != nil {
- global.FILE_LOG.Error("获取海通OA子公司数据失败:", err.Error())
- }
- var sysSubCompanyList []eta.SysDepartment
- for _, item := range subCompanylist {
- sysDepartment := eta.SysDepartment{
- DepartmentId: item.ID,
- DepartmentName: item.SUBCOMPANYNAME,
- Sort: item.SHOWORDEROFTREE,
- }
- sysSubCompanyList = append(sysSubCompanyList, sysDepartment)
- }
- err = eta.BatchInsertOrUpdate(sysSubCompanyList)
- if err != nil {
- global.FILE_LOG.Error("同步海通OA子公司数据失败:", err.Error())
- return
- }
- //同步部门信息
- DepartmentList, err := oa.GetDepartMentList()
- if err != nil {
- global.FILE_LOG.Error("获取海通部门数据失败:", err.Error())
- }
- var sysDepartmentList []eta.SysGroup
- for _, item := range DepartmentList {
- sysDepartment := eta.SysGroup{
- GroupId: item.ID,
- DepartmentId: item.SUBCOMPANYID1,
- GroupName: item.DEPARTMENTNAME,
- ParentId: item.SUPDEPID,
- Sort: item.SHOWORDEROFTREE,
- }
- sysDepartmentList = append(sysDepartmentList, sysDepartment)
- }
- err = eta.BatchInsertOrUpdateGroup(sysDepartmentList)
- if err != nil {
- global.FILE_LOG.Error("同步海通部门数据数据失败:", err.Error())
- return
- }
- //同步员工信息
- staffList, err := oa.GetEmployeeList()
- if err != nil {
- global.FILE_LOG.Error("获取海通OA员工数据失败:", err.Error())
- }
- var Employee []eta.Admin
- for _, item := range staffList {
- var department *eta.SysDepartment
- department, err = eta.GetDepartmentById(item.SUBCOMPANYID1)
- if err != nil {
- global.FILE_LOG.Error("获取员工子公司信息失败:", err.Error(), "跳过同步员工", item.WORKCODE)
- continue
- }
- var group *eta.SysGroup
- group, err = eta.GetSysGroupByGroupId(item.DEPARTMENTID)
- if err != nil {
- global.FILE_LOG.Error("获取员工部门组织架信息失败:", err.Error(), "跳过同步员工", item.WORKCODE)
- continue
- }
- sysUser := eta.Admin{
- AdminName: item.LOGINID,
- RealName: item.LASTNAME,
- Enabled: 1,
- Email: item.EMAIL,
- //Role: role.RoleName,
- Mobile: item.MOBILE,
- RoleType: 0,
- RoleId: role.RoleId,
- RoleName: role.RoleName,
- RoleTypeCode: role.RoleTypeCode,
- DepartmentId: item.SUBCOMPANYID1,
- DepartmentName: department.DepartmentName,
- GroupId: item.DEPARTMENTID,
- GroupName: group.GroupName,
- EmployeeId: item.WORKCODE,
- }
- Employee = append(Employee, sysUser)
- }
- err = eta.BatchInsertOrUpdateAdmin(Employee)
- if err != nil {
- global.FILE_LOG.Error("同步海通OA员工数据失败:", err.Error())
- return
- }
- }
- func syncCrmOAInfo() {
- roleId := global.CONFIG.HTFutures.SyncTask.SyncRoleId
- role, err := wx_crm.GetSysRoleById(roleId)
- if err != nil {
- global.FILE_LOG.Error("获取角色信息失败:", err.Error())
- return
- }
- //先同步分公司列表
- subCompanylist, err := oa.GetSubCompanyList()
- if err != nil {
- global.FILE_LOG.Error("获取海通OA子公司数据失败:", err.Error())
- }
- var sysSubCompanyList []wx_crm.SysDepartment
- for _, item := range subCompanylist {
- sysDepartment := wx_crm.SysDepartment{
- SysDepartmentName: item.SUBCOMPANYNAME,
- Sort: item.SHOWORDEROFTREE,
- Level: item.TLEVEL,
- OutId: item.ID,
- Type: wx_crm.SubCompany,
- ParentId: item.SUPSUBCOMID,
- }
- sysSubCompanyList = append(sysSubCompanyList, sysDepartment)
- }
- err = wx_crm.BatchInsertOrUpdate(sysSubCompanyList)
- if err != nil {
- global.FILE_LOG.Error("同步海通OA子公司数据失败:", err.Error())
- return
- }
- //同步部门信息
- DepartmentList, err := oa.GetDepartMentList()
- if err != nil {
- global.FILE_LOG.Error("获取海通部门数据失败:", err.Error())
- }
- var sysDepartmentList []wx_crm.SysDepartment
- for _, item := range DepartmentList {
- sysDepartment := wx_crm.SysDepartment{
- SysDepartmentName: item.DEPARTMENTNAME,
- Sort: item.SHOWORDEROFTREE,
- Level: item.TLEVEL,
- OutId: item.ID,
- Type: wx_crm.Department,
- ParentId: item.SUPDEPID,
- }
- if sysDepartment.Level == 2 && sysDepartment.ParentId == 0 {
- sysDepartment.ParentId = item.SUBCOMPANYID1
- }
- sysDepartmentList = append(sysDepartmentList, sysDepartment)
- }
- err = wx_crm.BatchInsertOrUpdate(sysDepartmentList)
- if err != nil {
- global.FILE_LOG.Error("同步海通部门数据失败:", err.Error())
- return
- }
- //同步员工信息
- staffList, err := oa.GetEmployeeList()
- if err != nil {
- global.FILE_LOG.Error("获取海通OA员工数据失败:", err.Error())
- }
- var Employee []wx_crm.SysUser
- for _, item := range staffList {
- idList, departmentErr := GetEmployeeDepartmentList(item.DEPARTMENTID)
- if departmentErr != nil {
- global.FILE_LOG.Error("获取员工部门组织架构失败:", departmentErr.Error(), "跳过同步员工", item.WORKCODE)
- continue
- }
- fillSize := 4 - len(idList)
- var departments []int
- departments = append(departments, idList...)
- if fillSize > 0 {
- for i := 0; i < fillSize; i++ {
- departments = append([]int{0}, departments...)
- }
- }
- sysUser := wx_crm.SysUser{
- SysUserName: item.LOGINID,
- SysRealName: item.LASTNAME,
- Email: item.EMAIL,
- Phone: item.MOBILE,
- AreaCode: "86",
- SysRoleId: role.SysRoleId,
- SysRoleName: role.SysRoleName,
- SysDepartmentId: item.DEPARTMENTID,
- SysDepartmentId1: departments[3],
- SysDepartmentId2: departments[2],
- SysDepartmentId3: departments[1],
- SysDepartmentId4: departments[0],
- PositionCode: item.SECLEVEL,
- IsEnabled: true,
- }
- Employee = append(Employee, sysUser)
- }
- err = wx_crm.BatchInsertOrUpdateUser(Employee)
- if err != nil {
- global.FILE_LOG.Error("同步海通OA员工数据失败:", err.Error())
- return
- }
- }
- func GetEmployeeDepartmentList(departmentId int) (departmentIdList []int, err error) {
- departmentList, err := wx_crm.GetDepartmentList()
- if err != nil {
- global.FILE_LOG.Error("获取部门信息失败", err.Error())
- return
- }
- var currentDepartment wx_crm.SysDepartment
- found := false
- for _, item := range departmentList {
- if item.OutId == departmentId {
- currentDepartment = item
- found = true
- break
- }
- }
- if found {
- departmentIdList = append(departmentIdList, currentDepartment.OutId)
- subList := TraceDepartmentList(currentDepartment.ParentId, departmentList, currentDepartment.Level-1)
- departmentIdList = append(departmentIdList, subList...)
- } else {
- err = errors.New("当前部门id不存在")
- }
- return
- }
- func TraceDepartmentList(departmentId int, departmentList []wx_crm.SysDepartment, level int) (departmentIdList []int) {
- if level == 0 || departmentId == 0 {
- return
- }
- for _, item := range departmentList {
- if item.OutId == departmentId {
- if level > 1 && item.Type == wx_crm.SubCompany {
- continue
- }
- if level == 1 && item.Type == wx_crm.Department {
- continue
- }
- departmentIdList = append(departmentIdList, item.OutId)
- subSubDepartments := TraceDepartmentList(item.ParentId, departmentList, item.Level-1)
- departmentIdList = append(departmentIdList, subSubDepartments...)
- }
- }
- return departmentIdList
- }
|