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 LinksCom : ILinks { #region 友情链接项 public void LinksAdd(Links entity) { Song.Entities.Organization org = Business.Do().OrganCurrent(); //添加对象,并设置排序号 object obj = Gateway.Default.Max(Links._.Lk_Tax, Links._.Ls_Id == entity.Ls_Id && Links._.Org_ID == org.Org_ID); int tax = obj is int ? (int)obj : 0; entity.Lk_Tax = tax + 1; LinksSort ls = Gateway.Default.From().Where(LinksSort._.Ls_Id == entity.Ls_Id).ToFirst(); if (ls != null) { entity.Ls_Name = ls.Ls_Name; } entity.Lk_IsApply = false; entity.Lk_IsVerify = true; //Song.Entities.Organization org = Business.Do().OrganCurrent(); if (org != null) { entity.Org_ID = org.Org_ID; entity.Org_Name = org.Org_Name; } Gateway.Default.Save(entity); } public void LinksApply(Links entity) { entity.Lk_IsApply = true; entity.Lk_IsVerify = false; Gateway.Default.Save(entity); } public void LinksVerify(Links entity) { Song.Entities.Organization org = Business.Do().OrganCurrent(); entity.Lk_IsVerify = true; entity.Lk_Tax = LinksMaxTaxis((int)entity.Ls_Id, org.Org_ID) + 1; LinksSort ls = Gateway.Default.From().Where(LinksSort._.Ls_Id == entity.Ls_Id).ToFirst(); if (ls != null) { entity.Ls_Name = ls.Ls_Name; } Gateway.Default.Save(entity); } public void LinksSave(Links entity) { LinksSort ls = Gateway.Default.From().Where(LinksSort._.Ls_Id == entity.Ls_Id).ToFirst(); if (ls != null) { entity.Ls_Name = ls.Ls_Name; } Gateway.Default.Save(entity); } public void LinksDelete(Links entity) { _LinksDelete(entity); } public void LinksDelete(int identify) { _LinksDelete(this.LinksSingle(identify)); } public void LinksDelete(int orgid, string name) { _LinksDelete(this.LinksSingle(orgid, name)); } public Links LinksSingle(int identify) { return Gateway.Default.From().Where(Links._.Lk_Id == identify).ToFirst(); } public Links LinksSingle(int orgid, string ttl) { return Gateway.Default.From().Where(Links._.Org_ID == orgid && Links._.Lk_Name == ttl).ToFirst(); } public int LinksMaxTaxis(int orgid, int sortId) { //添加对象,并设置排序号 object obj = Gateway.Default.Max(Links._.Lk_Tax, Links._.Ls_Id == sortId && Links._.Org_ID == orgid); int tax = 0; if (obj is int) { tax = (int)obj; } return tax; } public Links[] GetLinksAll(int orgid, bool? isShow) { WhereClip wc = Links._.Org_ID == orgid; if (isShow != null) wc.And(Links._.Lk_IsShow == isShow); return Gateway.Default.From().Where(wc).OrderBy(Links._.Lk_Tax.Asc).ToArray(); } /// /// 取友情链接 /// /// 分类Id,如果为空则取所有 /// /// /// 取多少条记录,如果小于等于0,则取所有 /// public Links[] GetLinks(int orgid, int sortId, bool? isShow, bool? isUse, int count) { WhereClip wc = Links._.Org_ID == orgid; if (sortId > 0) wc.And(Links._.Ls_Id == sortId); if (isShow != null) wc.And(Links._.Lk_IsShow == isShow); if (isUse != null) wc.And(Links._.Lk_IsUse == isUse); return Gateway.Default.From().Where(wc).OrderBy(Links._.Lk_Tax.Asc && Links._.Lk_Id.Asc).ToArray(count); } public Links[] GetLinks(int orgid, string sortName, bool? isShow, bool? isUse, int count) { if (string.IsNullOrWhiteSpace(sortName) || sortName.Trim() == "") return null; Song.Entities.LinksSort sort = Gateway.Default.From().Where(LinksSort._.Org_ID == orgid && LinksSort._.Ls_Name == sortName.Trim()).ToFirst(); if (sort == null) return null; if (isShow != null && (bool)isShow && !sort.Ls_IsShow && !sort.Ls_IsUse) return null; return GetLinks(orgid, sort.Ls_Id, isShow, isUse, count); } public Links[] GetLinksPager(int orgid, int size, int index, out int countSum) { WhereClip wc = Links._.Org_ID == orgid; countSum = Gateway.Default.Count(wc); return Gateway.Default.From().Where(wc).OrderBy(Links._.Lk_Tax.Asc).ToArray(size, (index - 1) * size); } public Links[] GetLinksPager(int orgid, int sortId, int size, int index, out int countSum) { WhereClip wc = Links._.Org_ID == orgid; if (sortId > 0) wc.And(Links._.Ls_Id == sortId); countSum = Gateway.Default.Count(wc); return Gateway.Default.From().Where(wc).OrderBy(Links._.Lk_Tax.Asc).ToArray(size, (index - 1) * size); } /// /// 分页获取所有链接项 /// /// 分类id /// 是否使用 /// 是否显示 /// 检索字符 /// /// /// /// public Links[] GetLinksPager(int orgid, int sortId, bool? isUse, bool? isShow, string searTxt, int size, int index, out int countSum) { WhereClip wc = Links._.Org_ID == orgid; if (sortId > -1) wc.And(Links._.Ls_Id == sortId); if (isUse != null) wc.And(Links._.Lk_IsUse == (bool)isUse); if (isShow != null) wc.And(Links._.Lk_IsShow == (bool)isShow); if (searTxt != null || searTxt.Trim() != "") wc.And(Links._.Lk_Name.Like("%" + searTxt.Trim() + "%")); countSum = Gateway.Default.Count(wc); return Gateway.Default.From().Where(wc).OrderBy(Links._.Lk_Tax.Asc).ToArray(size, (index - 1) * size); } public Links[] GetLinksPager(int orgid, int sortId, bool? isUse, bool? isShow, bool? isVeri, bool? isApply, string searTxt, int size, int index, out int countSum) { WhereClip wc = Links._.Org_ID == orgid; if (sortId > -1) wc.And(Links._.Ls_Id == sortId); if (isUse != null) wc.And(Links._.Lk_IsUse == (bool)isUse); if (isShow != null) wc.And(Links._.Lk_IsShow == (bool)isShow); if (isVeri != null) wc.And(Links._.Lk_IsVerify == (bool)isVeri); if (isApply != null) wc.And(Links._.Lk_IsApply == (bool)isApply); if (searTxt != null || searTxt.Trim() != "") wc.And(Links._.Lk_Name.Like("%" + searTxt.Trim() + "%")); countSum = Gateway.Default.Count(wc); return Gateway.Default.From().Where(wc).OrderBy(Links._.Lk_Tax.Asc).ToArray(size, (index - 1) * size); } public Links[] GetLinksPager(int orgid, bool? isShow, int size, int index, out int countSum) { WhereClip wc = Links._.Org_ID == orgid; if (isShow != null) wc.And(Links._.Lk_IsShow == (bool)isShow); countSum = Gateway.Default.Count(wc); return Gateway.Default.From().Where(wc).OrderBy(Links._.Lk_Tax.Asc).ToArray(size, (index - 1) * size); } public bool LinksRemoveUp(int id) { //当前对象 Links current = Gateway.Default.From().Where(Links._.Lk_Id == id).ToFirst(); //当前对象父节点id; int lsId = (int)current.Ls_Id; //当前对象排序号 int orderValue = (int)current.Lk_Tax; //上一个对象,即兄长对象; Links up = Gateway.Default.From().Where(Links._.Org_ID == current.Org_ID && Links._.Ls_Id == lsId && Links._.Lk_Tax < orderValue).OrderBy(Links._.Lk_Tax.Desc).ToFirst(); if (up == null) { //如果兄长对象不存在,则表示当前节点在兄弟中是老大;即是最顶点; return false; } //交换排序号 current.Lk_Tax = up.Lk_Tax; up.Lk_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; } public bool LinksRemoveDown(int id) { //当前对象 Links current = Gateway.Default.From().Where(Links._.Lk_Id == id).ToFirst(); //当前对象父节点id; int lsId = (int)current.Ls_Id; //当前对象排序号 int orderValue = (int)current.Lk_Tax; //下一个对象,即弟弟对象;弟弟不存则直接返回false; Links next = Gateway.Default.From() .Where(Links._.Lk_Tax > orderValue && Links._.Org_ID == current.Org_ID && Links._.Ls_Id == lsId) .OrderBy(Links._.Lk_Tax.Asc).ToFirst(); if (next == null) { //如果弟对象不存在,则表示当前节点在兄弟中是老幺;即是最底端; return false; } //交换排序号 current.Lk_Tax = next.Lk_Tax; next.Lk_Tax = orderValue; using (DbTrans tran = Gateway.Default.BeginTrans()) { try { tran.Save(current); tran.Save(next); tran.Commit(); } catch { tran.Rollback(); throw; } finally { tran.Close(); } } return true; } #endregion #region 私有方法 /// /// 私有对象,用于删除对象的子级,以及相关信息 /// /// private void _LinksDelete(Links entity) { if (entity == null) { return; } if (!string.IsNullOrWhiteSpace(entity.Lk_Logo)) { //删除图片 WeiSha.WebControl.FileUpload.Delete("Links", entity.Lk_Logo); } //删除自身 Gateway.Default.Delete(entity); } #endregion #region 友情链接的分类 /// /// 添加 /// /// 业务实体 public int SortAdd(LinksSort entity) { if (entity.Ls_PatId < 0) { //如果父节点小于0,违反逻辑 return -1; } if (!(entity.Ls_Name != null && entity.Ls_Name.Trim() != "")) { //如果名称为空 return -1; } if (entity.Ls_PatId > 0) { LinksSort parent = Gateway.Default.From().Where(LinksSort._.Ls_PatId == entity.Ls_Id).ToFirst(); if (parent == null) { //如果父节点不存在 return -1; } //新增对象的属性,遵循上级; entity.Ls_IsUse = parent.Ls_IsUse; } //添加对象,并设置排序号 object obj = Gateway.Default.Max(LinksSort._.Ls_Tax, LinksSort._.Ls_PatId == entity.Ls_PatId); int tax = 0; if (obj is int) { tax = (int)obj; } entity.Ls_Tax = tax + 1; Song.Entities.Organization org = Business.Do().OrganCurrent(); if (org != null) { entity.Org_ID = org.Org_ID; entity.Org_Name = org.Org_Name; } int id = Gateway.Default.Save(entity); return id; } /// /// 修改 /// /// 业务实体 public void SortSave(LinksSort entity) { using (DbTrans tran = Gateway.Default.BeginTrans()) { try { tran.Save(entity); tran.Update(new Field[] { Links._.Ls_Name }, new object[] { entity.Ls_Name }, Links._.Ls_Id == entity.Ls_Id); tran.Commit(); } catch { tran.Rollback(); throw; } finally { tran.Close(); } } } /// /// 删除 /// /// 业务实体 public void SortDelete(LinksSort entity) { using (DbTrans tran = Gateway.Default.BeginTrans()) { try { tran.Delete(LinksSort._.Ls_Id == entity.Ls_Id); tran.Delete(Links._.Ls_Id == entity.Ls_Id); tran.Commit(); } catch { tran.Rollback(); throw; } finally { tran.Close(); } } } /// /// 删除,按主键ID; /// /// 实体的主键 public void SortDelete(int identify) { Gateway.Default.Delete(LinksSort._.Ls_Id == identify); } /// /// 删除,按栏目名称 /// /// 栏目名称 public void SortDelete(string name) { LinksSort entity = Gateway.Default.From().Where(LinksSort._.Ls_Name == name).ToFirst(); if (entity == null) return; using (DbTrans tran = Gateway.Default.BeginTrans()) { try { tran.Delete(LinksSort._.Ls_Id == entity.Ls_Id); tran.Delete(Links._.Ls_Id == entity.Ls_Id); tran.Commit(); } catch { tran.Rollback(); throw; } finally { tran.Close(); } } } /// /// 获取单一实体对象,按主键ID; /// /// 实体的主键 /// public LinksSort SortSingle(int identify) { return Gateway.Default.From().Where(LinksSort._.Ls_Id == identify).ToFirst(); } /// /// 获取单一实体对象,按栏目名称 /// /// 栏目名称 /// public LinksSort SortSingle(string name) { return Gateway.Default.From().Where(LinksSort._.Ls_Name == name).ToFirst(); } /// /// 获取同一父级下的最大排序号; /// /// 父Id /// public int SortMaxTaxis(int orgid, int parentId) { //添加对象,并设置排序号 object obj = Gateway.Default.Max(LinksSort._.Ls_Tax, LinksSort._.Ls_PatId == parentId && LinksSort._.Org_ID == orgid); int tax = 0; if (obj is int) { tax = (int)obj; } return tax; } /// /// 获取对象;即所有栏目; /// /// public LinksSort[] GetSortAll(int orgid, bool? isUse, bool? isShow) { WhereClip wc = LinksSort._.Org_ID == orgid; if (isUse != null) wc.And(LinksSort._.Ls_IsUse == isUse); if (isShow != null) wc.And(LinksSort._.Ls_IsShow == isShow); return Gateway.Default.From().Where(wc).OrderBy(LinksSort._.Ls_Tax.Asc).ToArray(); } public LinksSort[] GetSortCount(int orgid, bool? isUse, bool? isShow, int count) { WhereClip wc = LinksSort._.Org_ID == orgid; if (isUse != null) wc.And(LinksSort._.Ls_IsUse == isUse); if (isShow != null) wc.And(LinksSort._.Ls_IsShow == isShow); return Gateway.Default.From().Where(wc).OrderBy(LinksSort._.Ls_Tax.Asc).ToArray(count); } /// /// 分页获取 /// /// /// /// /// /// /// /// public LinksSort[] GetSortPager(int orgid, bool? isUse, bool? isShow, string searTxt, int size, int index, out int countSum) { WhereClip wc = LinksSort._.Org_ID == orgid; if (isUse != null) wc.And(LinksSort._.Ls_IsUse == isUse); if (isShow != null) wc.And(LinksSort._.Ls_IsShow == isShow); if (!string.IsNullOrWhiteSpace(searTxt)) wc.And(LinksSort._.Ls_Name.Like("%" + searTxt.Trim() + "%")); countSum = Gateway.Default.Count(wc); return Gateway.Default.From().Where(wc).OrderBy(LinksSort._.Ls_Tax.Asc).ToArray(size, (index - 1) * size); } /// /// 当前对象名称是否重名 /// /// 业务实体 /// 重名返回true,否则返回false public bool SortIsExist(int orgid, LinksSort entity) { LinksSort Ps = null; //如果是一个新对象 if (entity.Ls_Id == 0) { Ps = Gateway.Default.From().Where(LinksSort._.Ls_Name == entity.Ls_Name && LinksSort._.Org_ID == orgid).ToFirst(); } else { //如果是一个已经存在的对象,则不匹配自己 Ps = Gateway.Default.From().Where(LinksSort._.Org_ID == orgid && LinksSort._.Ls_Name == entity.Ls_Name && LinksSort._.Ls_Id != entity.Ls_Id).ToFirst(); } return Ps != null; } /// /// 将当前栏目向上移动;仅在当前对象的同层移动,即同一父节点下的对象之间移动; /// /// /// 如果已经处于顶端,则返回false;移动成功,返回true public bool SortRemoveUp(int id) { //当前对象 LinksSort current = Gateway.Default.From().Where(LinksSort._.Ls_Id == id).ToFirst(); //当前对象排序号 int orderValue = (int)current.Ls_Tax; //上一个对象,即兄长对象; LinksSort up = Gateway.Default.From().Where(LinksSort._.Org_ID == current.Org_ID && LinksSort._.Ls_Tax < orderValue).OrderBy(LinksSort._.Ls_Tax.Desc).ToFirst(); if (up == null) { //如果兄长对象不存在,则表示当前节点在兄弟中是老大;即是最顶点; return false; } //交换排序号 current.Ls_Tax = up.Ls_Tax; up.Ls_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 SortRemoveDown(int id) { //当前对象 LinksSort current = Gateway.Default.From().Where(LinksSort._.Ls_Id == id).ToFirst(); //当前对象排序号 int orderValue = (int)current.Ls_Tax; //下一个对象,即弟弟对象; LinksSort down = Gateway.Default.From().Where(LinksSort._.Org_ID == current.Org_ID && LinksSort._.Ls_Tax > orderValue).OrderBy(LinksSort._.Ls_Tax.Asc).ToFirst(); if (down == null) { //如果弟对象不存在,则表示当前节点在兄弟中是老幺;即是最底端; return false; } //交换排序号 current.Ls_Tax = down.Ls_Tax; down.Ls_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; } #endregion } }