mdm.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package gn
  2. import (
  3. "eta_gn/eta_bridge/global"
  4. "eta_gn/eta_bridge/models/eta"
  5. "eta_gn/eta_bridge/models/gn"
  6. "eta_gn/eta_bridge/utils"
  7. "fmt"
  8. "strconv"
  9. "time"
  10. )
  11. // SyncSysDepartment 同步部门
  12. func SyncSysDepartment(mdmDepart *gn.MDM_COMSTRU) (err error) {
  13. defer func() {
  14. if err != nil {
  15. global.LOG.Info(fmt.Sprintf("同步ETA部门失败, %v", err))
  16. }
  17. }()
  18. if mdmDepart == nil {
  19. err = fmt.Errorf("部门信息有误")
  20. return
  21. }
  22. // 上级编码为空, ETA中为部门
  23. if mdmDepart.MainFieldParentAdminComstruCode == "" {
  24. // 查询ETA中是否已同步
  25. existDepart, e := eta.GetSysDepartmentByOutId(mdmDepart.MainFieldComstruCode)
  26. if e != nil && !utils.IsErrNoRow(e) {
  27. err = fmt.Errorf("获取ETA部门失败, %v", e)
  28. return
  29. }
  30. // 已同步, 名称不一致时更新
  31. if existDepart != nil && existDepart.DepartmentId > 0 {
  32. if existDepart.DepartmentName != mdmDepart.MainFieldComstruNameHR {
  33. existDepart.DepartmentName = mdmDepart.MainFieldComstruNameHR
  34. updateCols := []string{"DepartmentName"}
  35. if e = existDepart.Update(updateCols); e != nil {
  36. err = fmt.Errorf("更新ETA部门失败, %v", e)
  37. }
  38. }
  39. return
  40. }
  41. // 新增部门
  42. newDepart := new(eta.SysDepartment)
  43. newDepart.DepartmentName = mdmDepart.MainFieldComstruNameHR
  44. newDepart.OutID = mdmDepart.MainFieldComstruCode
  45. s, _ := strconv.Atoi(mdmDepart.MainFieldOrderHR)
  46. newDepart.Sort = s
  47. newDepart.CreateTime = time.Now()
  48. if e = newDepart.Create(); e != nil {
  49. err = fmt.Errorf("新增ETA部门失败, %v", e)
  50. }
  51. return
  52. }
  53. // 上级编码不为空, ETA中为分组
  54. if mdmDepart.MainFieldParentAdminComstruCode != "" {
  55. var departmentId, parentId int
  56. var isFind bool
  57. // 查询父级, 先从分组中找
  58. parentGroup, e := eta.GetSysGroupByOutId(mdmDepart.MainFieldParentAdminComstruCode)
  59. if e != nil && !utils.IsErrNoRow(e) {
  60. err = fmt.Errorf("获取ETA父级分组失败, %v", e)
  61. return
  62. }
  63. if parentGroup != nil && parentGroup.GroupId > 0 {
  64. isFind = true
  65. departmentId = parentGroup.DepartmentId
  66. parentId = parentGroup.GroupId
  67. }
  68. if !isFind {
  69. parentDepart, e := eta.GetSysDepartmentByOutId(mdmDepart.MainFieldParentAdminComstruCode)
  70. if e != nil && !utils.IsErrNoRow(e) {
  71. err = fmt.Errorf("获取ETA父级部门失败, %v", e)
  72. return
  73. }
  74. if parentDepart != nil && parentDepart.DepartmentId > 0 {
  75. isFind = true
  76. departmentId = parentDepart.DepartmentId
  77. parentId = 0
  78. }
  79. }
  80. if !isFind {
  81. err = fmt.Errorf("未找到对应父级部门或分组, ParentCode: %s", mdmDepart.MainFieldParentAdminComstruCode)
  82. return
  83. }
  84. // 查询ETA中是否已同步
  85. existGroup, e := eta.GetSysGroupByOutId(mdmDepart.MainFieldComstruCode)
  86. if e != nil && !utils.IsErrNoRow(e) {
  87. err = fmt.Errorf("获取ETA分组失败, %v", e)
  88. return
  89. }
  90. // 已同步则更新
  91. if existGroup != nil && existGroup.GroupId > 0 {
  92. updateCols := []string{"GroupName", "DepartmentId", "ParentId"}
  93. existGroup.GroupName = mdmDepart.MainFieldComstruNameHR
  94. existGroup.DepartmentId = departmentId
  95. existGroup.ParentId = parentId
  96. if e = existGroup.Update(updateCols); e != nil {
  97. err = fmt.Errorf("更新ETA分组失败, %v", e)
  98. }
  99. return
  100. }
  101. // 否则新增分组
  102. newGroup := new(eta.SysGroup)
  103. newGroup.DepartmentId = departmentId
  104. newGroup.GroupName = mdmDepart.MainFieldComstruNameHR
  105. newGroup.ParentId = parentId
  106. newGroup.OutID = mdmDepart.MainFieldComstruCode
  107. s, _ := strconv.Atoi(mdmDepart.MainFieldOrderHR)
  108. newGroup.Sort = s
  109. newGroup.CreateTime = time.Now()
  110. if e = newGroup.Create(); e != nil {
  111. err = fmt.Errorf("新增ETA分组失败, %v", e)
  112. return
  113. }
  114. }
  115. // TODO:父级从分组到部门交叉改变的情况?
  116. return
  117. }
  118. // GetDepartmentGroupIdByMdmCode 根据MDM的部门编码获取ETA的部门和分组ID
  119. func GetDepartmentGroupIdByMdmCode(mdmCode string) (departmentId, groupId int, departmentName, groupName string, err error) {
  120. defer func() {
  121. if departmentId == 0 && groupId == 0 {
  122. global.LOG.Info(fmt.Sprintf("MDM编码: %s, 未找到对应部门或分组", mdmCode))
  123. }
  124. }()
  125. // 校验一下MDM部门是否存在?
  126. //mdmDepartOb := new(gn.MDM_COMSTRU)
  127. //cond := ` AND Main_Field_Comstru_Code = ?`
  128. //pars := make([]interface{}, 0)
  129. //_, e := mdmDepartOb.GetItemByCondition(cond, pars)
  130. //if e != nil {
  131. // if utils.IsErrNoRow(e) {
  132. // err = fmt.Errorf("MDM部门不存在, code: %s", mdmCode)
  133. // return
  134. // }
  135. // err = fmt.Errorf("获取MDM部门失败, %v", e)
  136. // return
  137. //}
  138. // 先查分组, 查得到的话直接返回
  139. groupOb := new(eta.SysGroup)
  140. cond := ` AND out_id = ?`
  141. pars := make([]interface{}, 0)
  142. pars = append(pars, mdmCode)
  143. groupItem, e := groupOb.GetItemByCondition(cond, pars)
  144. if e != nil && !utils.IsErrNoRow(e) {
  145. err = fmt.Errorf("编码获取分组失败, %v", e)
  146. return
  147. }
  148. if groupItem != nil && groupItem.GroupId > 0 {
  149. groupId = groupItem.GroupId
  150. groupName = groupItem.GroupName
  151. // 查询部门
  152. depart, e := eta.GetDepartmentById(groupItem.DepartmentId)
  153. if e != nil && !utils.IsErrNoRow(e) {
  154. err = fmt.Errorf("获取部门失败, %v", e)
  155. return
  156. }
  157. departmentId = depart.DepartmentId
  158. departmentName = depart.DepartmentName
  159. return
  160. }
  161. // 查部门
  162. departOb := new(eta.SysDepartment)
  163. departItem, e := departOb.GetItemByCondition(cond, pars)
  164. if e != nil && !utils.IsErrNoRow(e) {
  165. err = fmt.Errorf("编码获取部门失败, %v", e)
  166. return
  167. }
  168. if departItem != nil && departItem.DepartmentId > 0 {
  169. departmentId = departItem.DepartmentId
  170. departmentName = departItem.DepartmentName
  171. }
  172. return
  173. }
  174. func GetParentDepartmentOrGroup() (err error) {
  175. return
  176. }