package services import ( "encoding/json" "eta/eta_bridge/models/crm" "eta/eta_bridge/models/eta" "eta/eta_bridge/services/alarm_msg" "eta/eta_bridge/utils" "fmt" "time" ) // SyncRoleData 同步角色数据 type SyncRoleData struct { Source int `description:"来源: 1-CRM; 2-ETA"` RoleId int `description:"角色ID"` } // HandleSyncRole 同步系统角色 func HandleSyncRole(data SyncRoleData) (err error) { if data.RoleId <= 0 { return } if data.Source != utils.SOURCE_CRM_FLAG && data.Source != utils.SOURCE_ETA_FLAG { return } defer func() { if err != nil { j, _ := json.Marshal(data) tips := fmt.Sprintf("CRM-ETA同步系统角色失败, errMsg: %s\ndata: %s", err.Error(), string(j)) go alarm_msg.SendAlarmMsg(tips, 3) } }() // CRM if data.Source == utils.SOURCE_CRM_FLAG { // 获取角色不存在, 则表示CRM删除了该角色, 需删除对应ETA角色 crmRole, e := crm.GetSysRoleById(data.RoleId) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取CRM角色信息失败, Err: " + e.Error()) return } _ = eta.DeleteRoleById(data.RoleId) return } // 获取ETA对应角色, 存在则更新, 否则新增 etaRole, e := eta.GetSysRoleById(data.RoleId) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取ETA对应角色信息失败, Err: " + e.Error()) return } // 新增 newRole := new(eta.SysRole) newRole.RoleId = crmRole.RoleId newRole.RoleName = crmRole.RoleName newRole.RoleType = crmRole.RoleType newRole.RoleTypeCode = crmRole.RoleTypeCode newRole.RoleLevel = crmRole.RoleLevel newRole.CreateTime = time.Now().Local() newRole.ModifyTime = time.Now().Local() e = newRole.Create() if e != nil { err = fmt.Errorf("新增ETA对应角色失败, Err: " + e.Error()) } return } // 更新 etaRole.RoleName = crmRole.RoleName etaRole.RoleType = crmRole.RoleType etaRole.RoleTypeCode = crmRole.RoleTypeCode etaRole.RoleLevel = crmRole.RoleLevel etaRole.ModifyTime = time.Now().Local() cols := []string{"RoleName", "RoleType", "RoleTypeCode", "RoleLevel", "ModifyTime"} e = etaRole.Update(cols) if e != nil { err = fmt.Errorf("更新ETA对应角色失败, Err: " + e.Error()) } return } // ETA etaRole, e := eta.GetSysRoleById(data.RoleId) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取ETA角色信息失败, Err: " + e.Error()) return } _ = crm.DeleteRoleById(data.RoleId) return } crmRole, e := crm.GetSysRoleById(data.RoleId) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取CRM对应角色信息失败, Err: " + e.Error()) return } // 新增 newRole := new(crm.SysRole) newRole.RoleId = etaRole.RoleId newRole.RoleName = etaRole.RoleName newRole.RoleType = etaRole.RoleType newRole.RoleTypeCode = etaRole.RoleTypeCode newRole.RoleLevel = etaRole.RoleLevel newRole.CreateTime = time.Now().Local() newRole.ModifyTime = time.Now().Local() e = newRole.Create() if e != nil { err = fmt.Errorf("新增CRM对应角色失败, Err: " + e.Error()) } return } // 更新 crmRole.RoleName = etaRole.RoleName crmRole.RoleType = etaRole.RoleType crmRole.RoleTypeCode = etaRole.RoleTypeCode crmRole.RoleLevel = etaRole.RoleLevel crmRole.ModifyTime = time.Now().Local() cols := []string{"RoleName", "RoleType", "RoleTypeCode", "RoleLevel", "ModifyTime"} e = crmRole.Update(cols) if e != nil { err = fmt.Errorf("更新CRM对应角色失败, Err: " + e.Error()) } return } // SyncAdminData 同步用户数据 type SyncAdminData struct { Source int `description:"来源: 1-CRM; 2-ETA"` AdminName string `description:"用户名"` } // HandleSyncAdmin 同步系统用户 func HandleSyncAdmin(data SyncAdminData) (err error) { if data.AdminName == "" { return } if data.Source != utils.SOURCE_CRM_FLAG && data.Source != utils.SOURCE_ETA_FLAG { return } defer func() { if err != nil { j, _ := json.Marshal(data) tips := fmt.Sprintf("CRM-ETA同步系统用户失败, errMsg: %s\ndata: %s", err.Error(), string(j)) go alarm_msg.SendAlarmMsg(tips, 3) } }() // CRM if data.Source == utils.SOURCE_CRM_FLAG { // 获取用户不存在, 则表示CRM删除了该用户, 需删除对应ETA用户 crmUser, e := crm.GetSysUserByAdminName(data.AdminName) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取CRM用户信息失败, Err: " + e.Error()) return } _ = eta.DeleteSysUserByName(data.AdminName) return } // 获取ETA对应用户, 存在则更新, 否则新增 etaUser, e := eta.GetSysUserByAdminName(data.AdminName) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取ETA对应用户信息失败, Err: " + e.Error()) return } // 新增 newUser := new(eta.Admin) newUser.AdminName = crmUser.AdminName newUser.AdminAvatar = crmUser.AdminAvatar newUser.RealName = crmUser.RealName newUser.Password = crmUser.Password newUser.LastUpdatedPasswordTime = crmUser.LastUpdatedPasswordTime newUser.Enabled = crmUser.Enabled newUser.Email = crmUser.Email newUser.LastLoginTime = crmUser.LastLoginTime newUser.CreatedTime = time.Now().Local() newUser.LastUpdatedTime = time.Now().Local() newUser.Role = crmUser.Role newUser.Mobile = crmUser.Mobile newUser.RoleType = crmUser.RoleType newUser.RoleId = crmUser.RoleId // 理论上角色也会同步ID应该也是一样 newUser.RoleName = crmUser.RoleName newUser.RoleTypeCode = crmUser.RoleTypeCode newUser.DepartmentId = crmUser.DepartmentId newUser.DepartmentName = crmUser.DepartmentName newUser.GroupId = crmUser.GroupId newUser.GroupName = crmUser.GroupName newUser.Authority = crmUser.Authority newUser.Position = crmUser.Position newUser.DisableTime = crmUser.DisableTime newUser.ChartPermission = crmUser.ChartPermission newUser.EdbPermission = crmUser.EdbPermission newUser.MysteelChemicalPermission = crmUser.MysteelChemicalPermission newUser.OpenId = crmUser.OpenId newUser.UnionId = crmUser.UnionId newUser.PredictEdbPermission = crmUser.PredictEdbPermission newUser.Province = crmUser.Province newUser.ProvinceCode = crmUser.ProvinceCode newUser.City = crmUser.City newUser.CityCode = crmUser.CityCode newUser.EmployeeId = crmUser.EmployeeId newUser.TelAreaCode = crmUser.TelAreaCode e = newUser.Create() if e != nil { err = fmt.Errorf("新增ETA对应用户失败, Err: " + e.Error()) } return } // 更新 etaUser.AdminName = crmUser.AdminName etaUser.AdminAvatar = crmUser.AdminAvatar etaUser.RealName = crmUser.RealName etaUser.Password = crmUser.Password etaUser.LastUpdatedPasswordTime = crmUser.LastUpdatedPasswordTime etaUser.Enabled = crmUser.Enabled etaUser.Email = crmUser.Email etaUser.LastUpdatedTime = time.Now().Local() etaUser.Role = crmUser.Role etaUser.Mobile = crmUser.Mobile etaUser.RoleType = crmUser.RoleType etaUser.RoleId = crmUser.RoleId etaUser.RoleName = crmUser.RoleName etaUser.RoleTypeCode = crmUser.RoleTypeCode etaUser.DepartmentId = crmUser.DepartmentId etaUser.DepartmentName = crmUser.DepartmentName etaUser.GroupId = crmUser.GroupId etaUser.GroupName = crmUser.GroupName etaUser.Authority = crmUser.Authority etaUser.Position = crmUser.Position etaUser.DisableTime = crmUser.DisableTime etaUser.ChartPermission = crmUser.ChartPermission etaUser.EdbPermission = crmUser.EdbPermission etaUser.MysteelChemicalPermission = crmUser.MysteelChemicalPermission etaUser.OpenId = crmUser.OpenId etaUser.UnionId = crmUser.UnionId etaUser.PredictEdbPermission = crmUser.PredictEdbPermission etaUser.Province = crmUser.Province etaUser.ProvinceCode = crmUser.ProvinceCode etaUser.City = crmUser.City etaUser.CityCode = crmUser.CityCode etaUser.EmployeeId = crmUser.EmployeeId etaUser.TelAreaCode = crmUser.TelAreaCode cols := []string{ "AdminName", "AdminAvatar", "RealName", "Password", "LastUpdatedPasswordTime", "Enabled", "Email", "LastUpdatedTime", "Role", "Mobile", "RoleType", "RoleId", "RoleName", "RoleTypeCode", "DepartmentId", "DepartmentName", "GroupId", "GroupName", "Authority", "Position", "DirectorId", "DisableTime", "ChartPermission", "EdbPermission", "MysteelChemicalPermission", "OpenId", "UnionId", "PredictEdbPermission", "Province", "ProvinceCode", "City", "CityCode", "EmployeeId", "TelAreaCode", } e = etaUser.Update(cols) if e != nil { err = fmt.Errorf("更新ETA对应用户失败, Err: " + e.Error()) } return } // ETA etaUser, e := eta.GetSysUserByAdminName(data.AdminName) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取ETA用户信息失败, Err: " + e.Error()) return } _ = crm.DeleteSysUserByName(data.AdminName) return } crmUser, e := crm.GetSysUserByAdminName(data.AdminName) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取ETA对应用户信息失败, Err: " + e.Error()) return } // 新增 newUser := new(crm.Admin) newUser.AdminName = etaUser.AdminName newUser.AdminAvatar = etaUser.AdminAvatar newUser.RealName = etaUser.RealName newUser.Password = etaUser.Password newUser.LastUpdatedPasswordTime = etaUser.LastUpdatedPasswordTime newUser.Enabled = etaUser.Enabled newUser.Email = etaUser.Email newUser.LastLoginTime = etaUser.LastLoginTime newUser.CreatedTime = time.Now().Local() newUser.LastUpdatedTime = time.Now().Local() newUser.Role = etaUser.Role newUser.Mobile = etaUser.Mobile newUser.RoleType = etaUser.RoleType newUser.RoleId = etaUser.RoleId newUser.RoleName = etaUser.RoleName newUser.RoleTypeCode = etaUser.RoleTypeCode newUser.DepartmentId = etaUser.DepartmentId newUser.DepartmentName = etaUser.DepartmentName newUser.GroupId = etaUser.GroupId newUser.GroupName = etaUser.GroupName newUser.Authority = etaUser.Authority newUser.Position = etaUser.Position newUser.DisableTime = etaUser.DisableTime newUser.ChartPermission = etaUser.ChartPermission newUser.EdbPermission = etaUser.EdbPermission newUser.MysteelChemicalPermission = etaUser.MysteelChemicalPermission newUser.OpenId = etaUser.OpenId newUser.UnionId = etaUser.UnionId newUser.PredictEdbPermission = etaUser.PredictEdbPermission newUser.Province = etaUser.Province newUser.ProvinceCode = etaUser.ProvinceCode newUser.City = etaUser.City newUser.CityCode = etaUser.CityCode newUser.EmployeeId = etaUser.EmployeeId newUser.TelAreaCode = etaUser.TelAreaCode e = newUser.Create() if e != nil { err = fmt.Errorf("新增ETA对应用户失败, Err: " + e.Error()) } return } // 更新 crmUser.AdminName = etaUser.AdminName crmUser.AdminAvatar = etaUser.AdminAvatar crmUser.RealName = etaUser.RealName crmUser.Password = etaUser.Password crmUser.LastUpdatedPasswordTime = etaUser.LastUpdatedPasswordTime crmUser.Enabled = etaUser.Enabled crmUser.Email = etaUser.Email crmUser.LastUpdatedTime = time.Now().Local() crmUser.Role = etaUser.Role crmUser.Mobile = etaUser.Mobile crmUser.RoleType = etaUser.RoleType crmUser.RoleId = etaUser.RoleId crmUser.RoleName = etaUser.RoleName crmUser.RoleTypeCode = etaUser.RoleTypeCode crmUser.DepartmentId = etaUser.DepartmentId crmUser.DepartmentName = etaUser.DepartmentName crmUser.GroupId = etaUser.GroupId crmUser.GroupName = etaUser.GroupName crmUser.Authority = etaUser.Authority crmUser.Position = etaUser.Position crmUser.DisableTime = etaUser.DisableTime crmUser.ChartPermission = etaUser.ChartPermission crmUser.EdbPermission = etaUser.EdbPermission crmUser.MysteelChemicalPermission = etaUser.MysteelChemicalPermission crmUser.OpenId = etaUser.OpenId crmUser.UnionId = etaUser.UnionId crmUser.PredictEdbPermission = etaUser.PredictEdbPermission crmUser.Province = etaUser.Province crmUser.ProvinceCode = etaUser.ProvinceCode crmUser.City = etaUser.City crmUser.CityCode = etaUser.CityCode crmUser.EmployeeId = etaUser.EmployeeId crmUser.TelAreaCode = etaUser.TelAreaCode cols := []string{ "AdminName", "AdminAvatar", "RealName", "Password", "LastUpdatedPasswordTime", "Enabled", "Email", "LastUpdatedTime", "Role", "Mobile", "RoleType", "RoleId", "RoleName", "RoleTypeCode", "DepartmentId", "DepartmentName", "GroupId", "GroupName", "Authority", "Position", "DirectorId", "DisableTime", "ChartPermission", "EdbPermission", "MysteelChemicalPermission", "OpenId", "UnionId", "PredictEdbPermission", "Province", "ProvinceCode", "City", "CityCode", "EmployeeId", "TelAreaCode", } e = crmUser.Update(cols) if e != nil { err = fmt.Errorf("更新CRM对应用户失败, Err: " + e.Error()) } return } // SyncDepartmentData 同步部门数据 type SyncDepartmentData struct { Source int `description:"来源: 1-CRM; 2-ETA"` DepartmentId int `description:"部门ID"` } // HandleSyncDepartment 同步部门 func HandleSyncDepartment(data SyncDepartmentData) (err error) { if data.DepartmentId <= 0 { return } if data.Source != utils.SOURCE_CRM_FLAG && data.Source != utils.SOURCE_ETA_FLAG { return } defer func() { if err != nil { j, _ := json.Marshal(data) tips := fmt.Sprintf("CRM-ETA同步部门失败, errMsg: %s\ndata: %s", err.Error(), string(j)) go alarm_msg.SendAlarmMsg(tips, 3) } }() // CRM if data.Source == utils.SOURCE_CRM_FLAG { // 获取部门不存在, 则表示CRM删除了该部门, 需删除对应ETA部门 crmDepartment, e := crm.GetDepartmentById(data.DepartmentId) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取CRM部门信息失败, Err: " + e.Error()) return } _ = eta.DeleteDepartmentById(data.DepartmentId) return } // 获取ETA对应部门, 存在则更新, 否则新增 etaDepartment, e := eta.GetDepartmentById(data.DepartmentId) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取ETA对应部门信息失败, Err: " + e.Error()) return } // 新增 newDepartment := new(eta.SysDepartment) newDepartment.DepartmentId = crmDepartment.DepartmentId newDepartment.DepartmentName = crmDepartment.DepartmentName newDepartment.Sort = crmDepartment.Sort newDepartment.CreateTime = time.Now().Local() e = newDepartment.Create() if e != nil { err = fmt.Errorf("新增ETA对应部门失败, Err: " + e.Error()) } return } // 更新 etaDepartment.DepartmentName = crmDepartment.DepartmentName etaDepartment.Sort = crmDepartment.Sort cols := []string{"DepartmentName", "Sort"} e = etaDepartment.Update(cols) if e != nil { err = fmt.Errorf("更新ETA对应部门失败, Err: " + e.Error()) } return } // ETA etaDepartment, e := eta.GetDepartmentById(data.DepartmentId) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取ETA部门信息失败, Err: " + e.Error()) return } _ = crm.DeleteDepartmentById(data.DepartmentId) return } crmDepartment, e := crm.GetDepartmentById(data.DepartmentId) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取CRM对应部门信息失败, Err: " + e.Error()) return } // 新增 newDepartment := new(crm.SysDepartment) newDepartment.DepartmentId = etaDepartment.DepartmentId newDepartment.DepartmentName = etaDepartment.DepartmentName newDepartment.Sort = etaDepartment.Sort newDepartment.CreateTime = time.Now().Local() e = newDepartment.Create() if e != nil { err = fmt.Errorf("新增CRM对应部门失败, Err: " + e.Error()) } return } // 更新 crmDepartment.DepartmentName = etaDepartment.DepartmentName crmDepartment.Sort = etaDepartment.Sort cols := []string{"DepartmentName", "Sort"} e = crmDepartment.Update(cols) if e != nil { err = fmt.Errorf("更新CRM对应部门失败, Err: " + e.Error()) } return } // SyncGroupData 同步分组数据 type SyncGroupData struct { Source int `description:"来源: 1-CRM; 2-ETA"` GroupId int `description:"分组ID"` } // HandleSyncGroup 同步分组 func HandleSyncGroup(data SyncGroupData) (err error) { if data.GroupId <= 0 { return } if data.Source != utils.SOURCE_CRM_FLAG && data.Source != utils.SOURCE_ETA_FLAG { return } defer func() { if err != nil { j, _ := json.Marshal(data) tips := fmt.Sprintf("CRM-ETA同步分组失败, errMsg: %s\ndata: %s", err.Error(), string(j)) go alarm_msg.SendAlarmMsg(tips, 3) } }() // CRM if data.Source == utils.SOURCE_CRM_FLAG { // 获取分组不存在, 则表示CRM删除了该分组, 需删除对应ETA分组 crmGroup, e := crm.GetSysGroupByGroupId(data.GroupId) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取CRM分组信息失败, Err: " + e.Error()) return } _ = eta.DeleteGroupByGroupId(data.GroupId) return } // 获取ETA对应分组, 存在则更新, 否则新增 etaGroup, e := eta.GetSysGroupByGroupId(data.GroupId) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取ETA对应分组信息失败, Err: " + e.Error()) return } // 新增 newGroup := new(eta.SysGroup) newGroup.GroupId = crmGroup.GroupId newGroup.DepartmentId = crmGroup.DepartmentId newGroup.GroupName = crmGroup.GroupName newGroup.ParentId = crmGroup.ParentId newGroup.Sort = crmGroup.Sort newGroup.CreateTime = time.Now().Local() e = newGroup.Create() if e != nil { err = fmt.Errorf("新增ETA对应分组失败, Err: " + e.Error()) } return } // 更新 etaGroup.DepartmentId = crmGroup.DepartmentId etaGroup.GroupName = crmGroup.GroupName etaGroup.ParentId = crmGroup.ParentId etaGroup.Sort = crmGroup.Sort cols := []string{"DepartmentId", "GroupName", "ParentId", "Sort"} e = etaGroup.Update(cols) if e != nil { err = fmt.Errorf("更新ETA对应分组失败, Err: " + e.Error()) } return } // ETA etaGroup, e := eta.GetSysGroupByGroupId(data.GroupId) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取ETA分组信息失败, Err: " + e.Error()) return } _ = crm.DeleteGroupByGroupId(data.GroupId) return } crmGroup, e := crm.GetSysGroupByGroupId(data.GroupId) if e != nil { if e != utils.ErrNoRow { err = fmt.Errorf("获取CRM对应分组信息失败, Err: " + e.Error()) return } // 新增 newGroup := new(crm.SysGroup) newGroup.GroupId = etaGroup.GroupId newGroup.DepartmentId = etaGroup.DepartmentId newGroup.GroupName = etaGroup.GroupName newGroup.ParentId = etaGroup.ParentId newGroup.Sort = etaGroup.Sort newGroup.CreateTime = time.Now().Local() e = newGroup.Create() if e != nil { err = fmt.Errorf("新增CRM对应分组失败, Err: " + e.Error()) } return } // 更新 crmGroup.DepartmentId = etaGroup.DepartmentId crmGroup.GroupName = etaGroup.GroupName crmGroup.ParentId = etaGroup.ParentId crmGroup.Sort = etaGroup.Sort cols := []string{"DepartmentId", "GroupName", "ParentId", "Sort"} e = crmGroup.Update(cols) if e != nil { err = fmt.Errorf("更新CRM对应分组失败, Err: " + e.Error()) } return } // SyncAdminEnRole 同步用户英文权限角色 type SyncAdminEnRole struct { AdminName string `description:"用户名"` RoleCodes []string `description:"角色Codes"` } // HandleSyncUserEnRole 同步用户英文权限角色 func HandleSyncUserEnRole(data SyncAdminEnRole) (err error) { if data.AdminName == "" { return } defer func() { if err != nil { j, _ := json.Marshal(data) tips := fmt.Sprintf("ETA同步至CRM英文权限角色失败, errMsg: %s\ndata: %s", err.Error(), string(j)) go alarm_msg.SendAlarmMsg(tips, 3) } }() crmAdmin, e := crm.GetSysUserByAdminName(data.AdminName) if e != nil { return } // 获取英文权限角色配置 confRoles := make([]eta.ConfEnAuthRole, 0) conf, e := eta.GetCrmConfByCode(eta.ConfEnAuthRoleKey) if e != nil { err = fmt.Errorf("获取英文权限角色配置失败, Err: %s", e.Error()) return } if conf.ConfigValue == "" { err = fmt.Errorf("英文权限角色配置为空") return } if e := json.Unmarshal([]byte(conf.ConfigValue), &confRoles); e != nil { err = fmt.Errorf("英文权限角色配置有误, Err: %s", e.Error()) return } roleCodes := make([]string, 0) for _, v := range confRoles { roleCodes = append(roleCodes, v.RoleCode) } // 获取CRM英文权限角色 roles := make([]*crm.SysRole, 0) { ob := new(crm.SysRole) cond := ` role_type_code IN (?)` pars := make([]interface{}, 0) pars = append(pars, roleCodes) rs, e := ob.GetItemsByCondition(cond, pars) if e != nil { err = fmt.Errorf("获取英文权限角色失败, Err: %s", e.Error()) return } roles = rs } if len(roles) == 0 { err = fmt.Errorf("英文权限角色为空") return } roleIds := make([]int, 0) roleCodeId := make(map[string]int) for _, v := range roles { roleIds = append(roleIds, v.RoleId) roleCodeId[v.RoleTypeCode] = v.RoleId } // 清空并新增 items := make([]*crm.SysRoleAdmin, 0) for _, v := range data.RoleCodes { if roleCodeId[v] > 0 { items = append(items, &crm.SysRoleAdmin{ AdminId: crmAdmin.AdminId, RoleId: roleCodeId[v], CreateTime: time.Now().Local(), }) } } relateOb := new(crm.SysRoleAdmin) if e = relateOb.DeleteAndCreate(crmAdmin.AdminId, roleIds, items); e != nil { err = fmt.Errorf("新增用户英文权限角色失败, Err: %s", e.Error()) return } return }