123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673 |
- 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
- }
|