using System; using System.Collections.Generic; using System.Text; using System.Data; using WeiSha.Common; using Song.Entities; using WeiSha.Data; using Song.ServiceInterfaces; using System.Data.Common; namespace Song.ServiceImpls { public class EmpGroupCom :IEmpGroup { /// /// 添加 /// /// 业务实体 public void Add(EmpGroup entity) { //添加对象,并设置排序号 object obj = Gateway.Default.Max(EmpGroup._.EGrp_Tax, EmpGroup._.EGrp_Tax > -1 && EmpGroup._.Org_ID == entity.Org_ID); entity.EGrp_Tax = obj is int ? (int)obj+1 : 1; Gateway.Default.Save(entity); } /// /// 增加员工与组的关联 /// /// /// public void AddRelation(int empId, int grpId) { Song.Entities.EmpAcc_Group eg = new EmpAcc_Group(); eg.Acc_Id = empId; eg.EGrp_Id = grpId; Gateway.Default.Save(eg); } /// /// 根据员工Id,删除关联 /// /// public void DelRelation4Emplyee(int empId) { Gateway.Default.Delete(EmpAcc_Group._.Acc_Id == empId); } /// /// 根据组Id,删除关联 /// /// public void DelRelation4Group(int grpId) { Gateway.Default.Delete(EmpAcc_Group._.EGrp_Id == grpId); } /// /// 修改 /// /// 业务实体 public void Save(EmpGroup entity) { Gateway.Default.Save(entity); } /// /// 删除 /// /// 业务实体 public void Delete(EmpGroup entity) { if (entity == null) return; //如果是系统组,则不允许删除 if (entity.EGrp_IsSystem) return; using (DbTrans tran = Gateway.Default.BeginTrans()) { try { tran.Delete(EmpAcc_Group._.EGrp_Id == entity.EGrp_Id); tran.Delete(Purview._.EGrp_Id == entity.EGrp_Id); tran.Delete(entity); tran.Commit(); } catch { tran.Rollback(); throw; } finally { tran.Close(); } } } /// /// 删除,按主键ID; /// /// 实体的主键 public void Delete(int identify) { EmpGroup entity = this.GetSingle(identify); this.Delete(entity); } /// /// 获取单一实体对象,按主键ID; /// /// 实体的主键 /// public EmpGroup GetSingle(int identify) { EmpGroup group= Gateway.Default.From().Where(EmpGroup._.EGrp_Id==identify).ToFirst(); return group; } /// /// 获取对象;即所有组; /// /// public EmpGroup[] GetAll(int orgid) { return Gateway.Default.From().Where(EmpGroup._.Org_ID==orgid).OrderBy(EmpGroup._.EGrp_Tax.Asc).ToArray(); } public EmpGroup[] GetAll(int orgid, bool? isUse) { if (isUse == null) { return this.GetAll(orgid); } return Gateway.Default.From() .Where(EmpGroup._.Org_ID == orgid && EmpGroup._.EGrp_IsUse == isUse) .OrderBy(EmpGroup._.EGrp_Tax.Asc).ToArray(); } /// /// 获取某员工所属的所有组; /// /// 员工id /// public EmpGroup[] GetAll4Emp(int EmpAccountId) { return Gateway.Default.From().InnerJoin(EmpGroup._.EGrp_Id == EmpAcc_Group._.EGrp_Id) .Where(EmpAcc_Group._.Acc_Id == EmpAccountId) .OrderBy(EmpGroup._.EGrp_Tax.Asc).ToArray(); } /// /// 获取某个组的所有员工 /// /// 组id /// public EmpAccount[] GetAll4Group(int grpId) { return Gateway.Default.From().InnerJoin(EmpAcc_Group._.Acc_Id == EmpAccount._.Acc_Id) .Where(EmpAcc_Group._.EGrp_Id == grpId) .OrderBy(EmpAccount._.Acc_RegTime.Asc).ToArray(); } /// /// 获取某个组的所有在职员工 /// /// /// /// public EmpAccount[] GetAll4Group(int grpId, bool use) { return Gateway.Default.From().InnerJoin(EmpAcc_Group._.Acc_Id == EmpAccount._.Acc_Id) .Where(EmpAcc_Group._.EGrp_Id == grpId && EmpAccount._.Acc_IsUse == use) .OrderBy(EmpAccount._.Acc_RegTime.Asc).ToArray(); } /// /// 当前对象名称是否重名 /// /// 业务实体 /// 重名返回true,否则返回false public bool IsExist(EmpGroup entity) { EmpGroup mm = null; //如果是一个新对象 if (entity.EGrp_Id == 0) { mm = Gateway.Default.From().Where(EmpGroup._.EGrp_Name == entity.EGrp_Name).ToFirst(); } else { //如果是一个已经存在的对象,则不匹配自己 mm = Gateway.Default.From().Where(EmpGroup._.EGrp_Name == entity.EGrp_Name && EmpGroup._.EGrp_Id != entity.EGrp_Id).ToFirst(); } return mm != null; } /// /// 将当前栏目向上移动;仅在当前对象的同层移动,即同一父节点下的对象之间移动; /// /// /// 如果已经处于顶端,则返回false;移动成功,返回true public bool RemoveUp(int orgid,int id) { //当前对象 EmpGroup current = Gateway.Default.From().Where(EmpGroup._.EGrp_Id == id).ToFirst(); //当前对象排序号 int orderValue = (int)current.EGrp_Tax; ; //上一个对象,即兄长对象; EmpGroup up = Gateway.Default.From() .Where(EmpGroup._.Org_ID == orgid && EmpGroup._.EGrp_Tax < orderValue) .OrderBy(EmpGroup._.EGrp_Tax.Desc).ToFirst(); if (up == null) return false; //交换排序号 current.EGrp_Tax = up.EGrp_Tax; up.EGrp_Tax = orderValue; using (DbTrans tran = Gateway.Default.BeginTrans()) { try { tran.Save(current); tran.Save(up); tran.Commit(); } catch { tran.Rollback(); throw; } finally { tran.Close(); } } return true; } /// /// 将当前栏目向下移动;仅在当前对象的同层移动,即同一父节点下的对象之间移动; /// /// /// 如果已经处于最底端,则返回false;移动成功,返回true public bool RemoveDown(int orgid,int id) { //当前对象 EmpGroup current = Gateway.Default.From().Where(EmpGroup._.EGrp_Id == id).ToFirst(); //当前对象排序号 int orderValue = (int)current.EGrp_Tax; //下一个对象,即弟弟对象; EmpGroup down = Gateway.Default.From() .Where(EmpGroup._.Org_ID == orgid && EmpGroup._.EGrp_Tax > orderValue) .OrderBy(EmpGroup._.EGrp_Tax.Asc).ToFirst(); if (down == null) return false; //交换排序号 current.EGrp_Tax = down.EGrp_Tax; down.EGrp_Tax = orderValue; using (DbTrans tran = Gateway.Default.BeginTrans()) { try { tran.Save(current); tran.Save(down); tran.Commit(); } catch { tran.Rollback(); throw; } finally { tran.Close(); } } return true; } } }