public int DeleteDept(int id, string accessToken) { int rows = 0; GROUP_INFO groupInfo = _groupRepo.Get(id); if (groupInfo != null) { int childrenCount = _groupRepo.GetCountByParentID(id); if (childrenCount > 0) { rows = -1; //如果有子部门,不能删除 } int empCount = _empRepo.GetCountByGroupID(id); if (empCount > 0) { rows = -2; //组织结构下有员工,不能删除 } DeptResponse response = WXQYClientServiceApi.Create().DeleteDept(accessToken, groupInfo.WX_GROUP_ID); if (response != null && response.ErrorCode == 0) { rows = _groupRepo.Delete(id); if (groupInfo.TYPE == 3) { MDSearch storeInfo = _storeRepo.GetByGroupId(id); if (storeInfo != null) { _storeRepo.Delete(storeInfo.ID); } } } } return(rows); }
public int ModifyEmpDept(string accessToken, int empId, int deptId, ref string errMsg) { EMPLOYEE_EX entity = _empRepo.Get(empId); if (entity != null) { GROUP_INFO_EX groupInfo = _groupRepo.Get(deptId); if (groupInfo != null) { List <int> department = new List <int>() { groupInfo.WX_GROUP_ID }; string gender = entity.GENDER.HasValue ? entity.GENDER.Value.ToString() : null; var response = WXQYClientServiceApi.Create().UpdateUser(accessToken, entity.USERID, entity.NAME, department, entity.POSITION, entity.MOBILE, gender, entity.EMAIL, entity.WECHAT_ID); if (response != null && response.ErrorCode == 0) { REL_EMP_GROUP rel = _empRepo.GetRelByEmpID(empId); rel.GROUP_ID = deptId; rel.FullUpdate = false; return(_empRepo.Update(rel)); } else { errMsg = response.ErrorMessage; } } } return(0); }
public string GetQYAccessToken() { // 读取XML文件中的数据 string filepath = HttpContext.Current.Server.MapPath("~/Configs/AccessTokenData.config"); StreamReader str = new StreamReader(filepath, Encoding.UTF8); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(str); str.Close(); str.Dispose(); var rootNode = xmlDocument.SelectSingleNode("AccessTokenInfo"); string token = rootNode.SelectSingleNode("Token").InnerText; DateTime AccessExpires = Convert.ToDateTime(rootNode.SelectSingleNode("Expires").InnerText); if (DateTime.Now >= AccessExpires) { AccessTokenResponse response = WXQYClientServiceApi.Create().GetAccessToken(); if (response != null) { rootNode.SelectSingleNode("Token").InnerText = response.AccessToken; DateTime _accessExpires = DateTime.Now.AddSeconds(response.ExpiresIn); rootNode.SelectSingleNode("Expires").InnerText = _accessExpires.ToString(); xmlDocument.Save(filepath); token = response.AccessToken; } else { token = ""; throw new Exception("获取AccessToken异常。"); } } return(token); }
public void InitDepartments(string accessToken) { //select a.ID,a.NAME,a.PARENT_ID,a.WX_GROUP_ID,a.wx_parent_id, //b.ID,b.NAME, b.WX_GROUP_ID,b.wx_parent_id, //'update GROUP_INFO set PARENT_ID = ' + str(b.ID) + ' where ID =' + str(a.ID) //from GROUP_INFO a //left //join GROUP_INFO b on a.wx_parent_id = b.WX_GROUP_ID //where b.id is not null //select *, //'update MDSearch set group_id='+STR(g.ID) +' where id = '+str(m.ID), //'update group_info set code =''' + m.Code+''' where id= ' +STR(g.id) //from GROUP_INFO g //inner join MDSearch m on m.Name = g.NAME int rootId = AppConfig.QYDeptRootID; DeptListResponse response = WXQYClientServiceApi.Create().QueryDept(accessToken, rootId); if (response != null && response.Departments != null) { List <GROUP_INFO> groupList = new List <GROUP_INFO>(); List <Department> children = response.Departments.FindAll(m => m.ID != rootId); foreach (var item in children) { GROUP_INFO g = new GROUP_INFO(); g.NAME = item.Name; g.CODE = ""; g.PARENT_ID = 0; if (item.ParentID == rootId) { g.TYPE = 1; } else { var cc = children.FindAll(m => m.ParentID == item.ID); if (cc.Count > 0) { g.TYPE = 2; } else { g.TYPE = 3; } } g.WX_GROUP_ID = item.ID; g.WX_PARENT_ID = item.ParentID; g.WX_ORDER = item.Order; g.CREATE_TIME = DateTime.Now; g.CREATE_USER = "******"; groupList.Add(g); } _storeRepo.BatchInsert(groupList); } }
public void SyncDeptToRemote(string accessToken) { List <GROUP_INFO> groups = this._groupRepo.GetAllByNames(); foreach (var item in groups) { int order = item.WX_ORDER.HasValue ? item.WX_ORDER.Value : 1; DeptResponse response = WXQYClientServiceApi.Create().UpdateDept(accessToken, item.WX_GROUP_ID, item.NAME, item.WX_PARENT_ID.Value, order); if (response != null && response.ErrorCode == 0) { LogService.Info(item.NAME + "-成功"); } else { LogService.Warn(item.NAME + "-失败"); } } }
/// <summary> /// 修改组织架构 /// </summary> /// <param name="accessToken"></param> /// <param name="deptId"></param> /// <param name="parentId"></param> /// <returns></returns> public int UpdateDeptOrg(string accessToken, int deptId, int parentId, ref string errMsg) { int rows = 0, rootId = AppConfig.QYDeptRootID; GROUP_INFO existGroupInfo = _groupRepo.Get(deptId); if (existGroupInfo != null) { int wxParentId = 0; if (parentId == 0) { wxParentId = rootId; } else { GROUP_INFO parentInfo = _groupRepo.Get(parentId); if (parentInfo != null) { wxParentId = parentInfo.WX_GROUP_ID; } } DeptResponse response = WXQYClientServiceApi.Create().UpdateDept(accessToken, existGroupInfo.WX_GROUP_ID, existGroupInfo.NAME, wxParentId, existGroupInfo.WX_ORDER.Value); if (response != null && response.ErrorCode == 0) { GROUP_INFO groupInfo = new GROUP_INFO(); groupInfo.ID = deptId; groupInfo.PARENT_ID = parentId; groupInfo.WX_PARENT_ID = wxParentId; groupInfo.LAST_MODIFY_TIME = DateTime.Now; groupInfo.LAST_MODIFY_USER = "******"; groupInfo.FullUpdate = false; rows = (int)_groupRepo.Update(groupInfo); } else { errMsg = response.ErrorMessage; } } return(rows); }
public int DeleteEmployee(string accessToken, int empId) { EMPLOYEE emp = _empRepo.Get(empId); if (emp != null) { var response = WXQYClientServiceApi.Create().DeleteUser(accessToken, emp.USERID); if (response != null && response.ErrorCode == 0) { using (TransScope scope = new TransScope()) { int rows = _empRepo.Delete(empId); _empRepo.DeleteRelByEmpID(empId); scope.Commit(); return(rows); } } } return(0); }
public int SaveEmployee(string accessToken, EMPLOYEE entity, DeptInfo dept, ref string errMsg) { using (TransScope scope = new TransScope()) { int wxGroupId = 0, deptId = dept.ID; GROUP_INFO groupInfo = null; if (entity.ID > 0) { var rel = _empRepo.GetGroupIdByEmpID(entity.ID); deptId = rel; } groupInfo = _groupRepo.Get(deptId); if (groupInfo == null) { return(0); } wxGroupId = groupInfo.WX_GROUP_ID; List <int> department = new List <int>() { wxGroupId }; string gender = entity.GENDER.HasValue ? entity.GENDER.Value.ToString() : null; int rows = 0; if (entity.ID == 0) { var response = WXQYClientServiceApi.Create().CreateUser(accessToken, entity.USERID, entity.NAME, department, entity.POSITION, entity.MOBILE, gender, entity.EMAIL, entity.WECHAT_ID); if (response != null && response.ErrorCode == 0) { //同步头像 UserInfo user = WXQYClientServiceApi.Create().GetUser(accessToken, entity.USERID); if (user != null) { entity.AVATAR_URL = user.Avatar; } entity.STATUS = 4; entity.CREATE_TIME = DateTime.Now; rows = (int)_empRepo.Insert(entity); entity.ID = rows; //修改关系表 if (rows > 0) { //删除所有员工关系 _empRepo.DeleteRelByEmpID(entity.ID); //新增关系 REL_EMP_GROUP rel = new REL_EMP_GROUP(); rel.TYPE = groupInfo.TYPE; rel.EMP_ID = entity.ID; rel.GROUP_ID = deptId; rel.CREATE_TIME = DateTime.Now; rel.CREATE_USER = "******"; _empRepo.Insert(rel); } } else { errMsg = response.ErrorMessage; } } else { var response = WXQYClientServiceApi.Create().UpdateUser(accessToken, entity.USERID, entity.NAME, department, entity.POSITION, entity.MOBILE, gender, entity.EMAIL, entity.WECHAT_ID); if (response != null && response.ErrorCode == 0) { //同步头像 UserInfo user = WXQYClientServiceApi.Create().GetUser(accessToken, entity.USERID); if (user != null) { entity.AVATAR_URL = user.Avatar; } entity.FullUpdate = false; rows = _empRepo.Update(entity); } else { errMsg = response.ErrorMessage; } } scope.Commit(); return(rows); } }
public void SyncUsersToLocal(string accessToken) { int rootId = AppConfig.QYDeptRootID; UserListResponse response = WXQYClientServiceApi.Create().QueryUser(accessToken, rootId); if (response != null && response.Users != null && response.Users.Count > 0) { _empRepo.DeleteAllRel(); var empList = _empRepo.QueryAll(); var groupList = _groupRepo.GetAll(); foreach (var item in response.Users) { EMPLOYEE emp = empList.FirstOrDefault(m => m.USERID == item.UserId); bool existed = emp != null; if (!existed) { emp = new EMPLOYEE(); } emp.USERID = item.UserId; emp.NAME = item.Name; emp.MOBILE = item.Mobile; emp.EMAIL = item.Email; emp.WECHAT_ID = item.WeiXinId; emp.EMP_NO = item.UserId; emp.GENDER = item.Gender; emp.POSITION = item.Position; emp.AVATAR_URL = item.Avatar; emp.STATUS = item.Status; emp.CREATE_TIME = DateTime.Now; if (existed) { emp.FullUpdate = false; _empRepo.Update(emp); } else { emp.ID = (int)_empRepo.Insert(emp); } if (item.Department != null && item.Department.Length > 0) { var groupId = item.Department[0]; var groupInfo = groupList.FirstOrDefault(m => m.WX_GROUP_ID == groupId); if (groupInfo != null) { REL_EMP_GROUP rel = new REL_EMP_GROUP(); rel.TYPE = groupInfo.TYPE; rel.EMP_ID = emp.ID; rel.GROUP_ID = groupInfo.ID; rel.CREATE_TIME = DateTime.Now; rel.CREATE_USER = "******"; _empRepo.Insert(rel); } else { LogService.Debug(groupId + ","); } } else { LogService.Info(item.UserId + ","); } } } }
public int ModifyDept(DeptInfo deptInfo, string accessToken, ref string errMsg) { int rows = 0, rootId = AppConfig.QYDeptRootID; GROUP_INFO existGroupInfo = _groupRepo.Get(deptInfo.ID); if (existGroupInfo != null) { int wxParentId = 0; if (existGroupInfo.PARENT_ID == 0) { wxParentId = rootId; } else { GROUP_INFO parentInfo = _groupRepo.Get(existGroupInfo.PARENT_ID); if (parentInfo != null) { wxParentId = parentInfo.WX_GROUP_ID; } } DeptResponse response = WXQYClientServiceApi.Create().UpdateDept(accessToken, existGroupInfo.WX_GROUP_ID, deptInfo.DeptName, wxParentId, deptInfo.Order); if (response != null && response.ErrorCode == 0) { GROUP_INFO groupInfo = new GROUP_INFO(); groupInfo.ID = deptInfo.ID; groupInfo.NAME = deptInfo.DeptName; groupInfo.CODE = deptInfo.DeptCode; groupInfo.MAGENTO_GROUP_ID = deptInfo.MagentoGroupID; groupInfo.WX_ORDER = deptInfo.Order; groupInfo.LAST_MODIFY_TIME = DateTime.Now; groupInfo.LAST_MODIFY_USER = "******"; groupInfo.FullUpdate = false; rows = (int)_groupRepo.Update(groupInfo); if (rows > 0 && existGroupInfo.TYPE == 3) { MDSearch storeInfo = _storeRepo.GetByGroupId(deptInfo.ID); if (storeInfo == null) { storeInfo = new MDSearch(); } storeInfo.Name = deptInfo.DeptName; storeInfo.Code = deptInfo.DeptCode; storeInfo.StoreType = deptInfo.StoreType; storeInfo.IS_PICK_UP = deptInfo.IsPickUp; storeInfo.Address = deptInfo.Address; storeInfo.Phone = deptInfo.Telephone; storeInfo.X = deptInfo.Latitude; storeInfo.Y = deptInfo.Longitude; storeInfo.BelongsAreaNo = deptInfo.BelongsAreaNo; storeInfo.Province = deptInfo.Province; storeInfo.City = deptInfo.City; storeInfo.Area = deptInfo.Area; storeInfo.PP = deptInfo.Brand; if (storeInfo.ID > 0) { storeInfo.FullUpdate = false; _storeRepo.Update(storeInfo); } else { storeInfo.GROUP_ID = deptInfo.ID; _groupRepo.Insert(storeInfo); } } } else { errMsg = response.ErrorMessage; } } return(rows); }
public int CreateDept(DeptInfo deptInfo, string accessToken, ref string errMsg) { int groupId = 0, wxParentId = 0, rootId = AppConfig.QYDeptRootID; GROUP_INFO groupInfo = null; if (deptInfo.ParentID == 0) //创建大区 { groupInfo = new GROUP_INFO(); groupInfo.TYPE = 1; wxParentId = rootId;//根目录ID } else { GROUP_INFO parentDept = _groupRepo.Get(deptInfo.ParentID); if (parentDept != null) { groupInfo = new GROUP_INFO(); wxParentId = parentDept.WX_GROUP_ID; if (parentDept.TYPE == 1) //如果父级是大区,则创建区域 { groupInfo.TYPE = 2; } else if (parentDept.TYPE == 2) { groupInfo.TYPE = 3; } } } if (groupInfo != null) { groupInfo.NAME = deptInfo.DeptName; groupInfo.CODE = deptInfo.DeptCode; groupInfo.PARENT_ID = deptInfo.ParentID; groupInfo.MAGENTO_GROUP_ID = deptInfo.MagentoGroupID; groupInfo.WX_ORDER = deptInfo.Order; groupInfo.CREATE_TIME = DateTime.Now; groupInfo.CREATE_USER = "******"; DeptResponse response = WXQYClientServiceApi.Create().CreateDept(accessToken, groupInfo.NAME, wxParentId, groupInfo.WX_ORDER.Value); if (response != null && response.ErrorCode == 0) { groupInfo.WX_GROUP_ID = response.ID; groupInfo.WX_PARENT_ID = wxParentId; groupId = (int)_groupRepo.Insert(groupInfo); if (groupId > 0 && groupInfo.TYPE == 3) //创建MDSearch { MDSearch storeInfo = new MDSearch(); storeInfo.Name = deptInfo.DeptName; storeInfo.Code = deptInfo.DeptCode; storeInfo.StoreType = deptInfo.StoreType; storeInfo.IS_PICK_UP = deptInfo.IsPickUp; storeInfo.Address = deptInfo.Address; storeInfo.Phone = deptInfo.Telephone; storeInfo.X = deptInfo.Latitude; storeInfo.Y = deptInfo.Longitude; storeInfo.BelongsAreaNo = deptInfo.BelongsAreaNo; storeInfo.Province = deptInfo.Province; storeInfo.City = deptInfo.City; storeInfo.Area = deptInfo.Area; storeInfo.PP = deptInfo.Brand; storeInfo.GROUP_ID = groupId; _groupRepo.Insert(storeInfo); } deptInfo.Type = groupInfo.TYPE; } else { errMsg = response.ErrorMessage; } } return(groupId); }