package gn import ( "eta_gn/eta_bridge/global" "eta_gn/eta_bridge/models/eta" "eta_gn/eta_bridge/models/gn" "eta_gn/eta_bridge/utils" "fmt" "strconv" "time" ) // SyncSysDepartment 同步部门 func SyncSysDepartment(mdmDepart *gn.MDM_COMSTRU) (err error) { defer func() { if err != nil { global.LOG.Info(fmt.Sprintf("同步ETA部门失败, %v", err)) } }() if mdmDepart == nil { err = fmt.Errorf("部门信息有误") return } // 上级编码为空, ETA中为部门 if mdmDepart.MainFieldParentAdminComstruCode == "" { // 查询ETA中是否已同步 existDepart, e := eta.GetSysDepartmentByOutId(mdmDepart.MainFieldComstruCode) if e != nil && !utils.IsErrNoRow(e) { err = fmt.Errorf("获取ETA部门失败, %v", e) return } // 已同步, 名称不一致时更新 if existDepart != nil && existDepart.DepartmentId > 0 { if existDepart.DepartmentName != mdmDepart.MainFieldComstruNameHR { existDepart.DepartmentName = mdmDepart.MainFieldComstruNameHR updateCols := []string{"DepartmentName"} if e = existDepart.Update(updateCols); e != nil { err = fmt.Errorf("更新ETA部门失败, %v", e) } } return } // 新增部门 newDepart := new(eta.SysDepartment) newDepart.DepartmentName = mdmDepart.MainFieldComstruNameHR newDepart.OutID = mdmDepart.MainFieldComstruCode s, _ := strconv.Atoi(mdmDepart.MainFieldOrderHR) newDepart.Sort = s newDepart.CreateTime = time.Now() if e = newDepart.Create(); e != nil { err = fmt.Errorf("新增ETA部门失败, %v", e) } return } // 上级编码不为空, ETA中为分组 if mdmDepart.MainFieldParentAdminComstruCode != "" { var departmentId, parentId int var isFind bool // 查询父级, 先从分组中找 parentGroup, e := eta.GetSysGroupByOutId(mdmDepart.MainFieldParentAdminComstruCode) if e != nil && !utils.IsErrNoRow(e) { err = fmt.Errorf("获取ETA父级分组失败, %v", e) return } if parentGroup != nil && parentGroup.GroupId > 0 { isFind = true departmentId = parentGroup.DepartmentId parentId = parentGroup.GroupId } if !isFind { parentDepart, e := eta.GetSysDepartmentByOutId(mdmDepart.MainFieldParentAdminComstruCode) if e != nil && !utils.IsErrNoRow(e) { err = fmt.Errorf("获取ETA父级部门失败, %v", e) return } if parentDepart != nil && parentDepart.DepartmentId > 0 { isFind = true departmentId = parentDepart.DepartmentId parentId = 0 } } if !isFind { err = fmt.Errorf("未找到对应父级部门或分组, ParentCode: %s", mdmDepart.MainFieldParentAdminComstruCode) return } // 查询ETA中是否已同步 existGroup, e := eta.GetSysGroupByOutId(mdmDepart.MainFieldComstruCode) if e != nil && !utils.IsErrNoRow(e) { err = fmt.Errorf("获取ETA分组失败, %v", e) return } // 已同步则更新 if existGroup != nil && existGroup.GroupId > 0 { updateCols := []string{"GroupName", "DepartmentId", "ParentId"} existGroup.GroupName = mdmDepart.MainFieldComstruNameHR existGroup.DepartmentId = departmentId existGroup.ParentId = parentId if e = existGroup.Update(updateCols); e != nil { err = fmt.Errorf("更新ETA分组失败, %v", e) } return } // 否则新增分组 newGroup := new(eta.SysGroup) newGroup.DepartmentId = departmentId newGroup.GroupName = mdmDepart.MainFieldComstruNameHR newGroup.ParentId = parentId newGroup.OutID = mdmDepart.MainFieldComstruCode s, _ := strconv.Atoi(mdmDepart.MainFieldOrderHR) newGroup.Sort = s newGroup.CreateTime = time.Now() if e = newGroup.Create(); e != nil { err = fmt.Errorf("新增ETA分组失败, %v", e) return } } // TODO:父级从分组到部门交叉改变的情况? return } // GetDepartmentGroupIdByMdmCode 根据MDM的部门编码获取ETA的部门和分组ID func GetDepartmentGroupIdByMdmCode(mdmCode string) (departmentId, groupId int, departmentName, groupName string, err error) { defer func() { if departmentId == 0 && groupId == 0 { global.LOG.Info(fmt.Sprintf("MDM编码: %s, 未找到对应部门或分组", mdmCode)) } }() // 校验一下MDM部门是否存在? //mdmDepartOb := new(gn.MDM_COMSTRU) //cond := ` AND Main_Field_Comstru_Code = ?` //pars := make([]interface{}, 0) //_, e := mdmDepartOb.GetItemByCondition(cond, pars) //if e != nil { // if utils.IsErrNoRow(e) { // err = fmt.Errorf("MDM部门不存在, code: %s", mdmCode) // return // } // err = fmt.Errorf("获取MDM部门失败, %v", e) // return //} // 先查分组, 查得到的话直接返回 groupOb := new(eta.SysGroup) cond := ` AND out_id = ?` pars := make([]interface{}, 0) pars = append(pars, mdmCode) groupItem, e := groupOb.GetItemByCondition(cond, pars) if e != nil && !utils.IsErrNoRow(e) { err = fmt.Errorf("编码获取分组失败, %v", e) return } if groupItem != nil && groupItem.GroupId > 0 { groupId = groupItem.GroupId groupName = groupItem.GroupName // 查询部门 depart, e := eta.GetDepartmentById(groupItem.DepartmentId) if e != nil && !utils.IsErrNoRow(e) { err = fmt.Errorf("获取部门失败, %v", e) return } departmentId = depart.DepartmentId departmentName = depart.DepartmentName return } // 查部门 departOb := new(eta.SysDepartment) departItem, e := departOb.GetItemByCondition(cond, pars) if e != nil && !utils.IsErrNoRow(e) { err = fmt.Errorf("编码获取部门失败, %v", e) return } if departItem != nil && departItem.DepartmentId > 0 { departmentId = departItem.DepartmentId departmentName = departItem.DepartmentName } return } func GetParentDepartmentOrGroup() (err error) { return }