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 GuideCom :IGuide
{
private string _artUppath = "Guide";
#region 考试指南
///
/// 添加考试指南
///
/// 业务实体
public void GuideAdd(Guide entity)
{
//创建时间
entity.Gu_CrtTime = DateTime.Now;
if (entity.Gu_PushTime < DateTime.Now.AddYears(-100))
entity.Gu_PushTime = entity.Gu_CrtTime;
//所在机构
Song.Entities.Organization org = Business.Do().OrganCurrent();
if (org != null)
{
entity.Org_ID = org.Org_ID;
entity.Org_Name = org.Org_Name;
}
//所属栏目
Song.Entities.GuideColumns nc = this.ColumnsSingle((int)entity.Gc_ID);
if (nc != null) entity.Gc_Title = nc.Gc_Title;
Gateway.Default.Save(entity);
}
///
/// 修改
///
/// 业务实体
public void GuideSave(Guide entity)
{
//最后编辑时间
entity.Gu_LastTime = DateTime.Now;
if (entity.Gu_PushTime < DateTime.Now.AddYears(-100))
entity.Gu_PushTime = entity.Gu_CrtTime;
//所在机构
Song.Entities.Organization org = Business.Do().OrganCurrent();
if (org != null)
{
entity.Org_ID = org.Org_ID;
entity.Org_Name = org.Org_Name;
}
//所属栏目
Song.Entities.GuideColumns nc = this.ColumnsSingle((int)entity.Gc_ID);
if (nc != null) entity.Gc_Title = nc.Gc_Title;
Gateway.Default.Save(entity);
}
///
/// 删除
///
/// 业务实体
public void GuideDelete(Guide entity)
{
using (DbTrans tran = Gateway.Default.BeginTrans())
{
try
{
if (!string.IsNullOrWhiteSpace(entity.Gu_Uid))
{
//删除附件
Business.Do().Delete(entity.Gu_Uid);
//删除图片文件
string img = WeiSha.Common.Upload.Get[_artUppath].Physics + entity.Gu_Logo;
if (System.IO.File.Exists(img))
System.IO.File.Delete(img);
}
tran.Delete(Guide._.Gu_Id == entity.Gu_Id);
tran.Commit();
}
catch (Exception ex)
{
tran.Rollback();
throw ex;
}
finally
{
tran.Close();
}
}
}
///
/// 删除,按主键ID;
///
/// 实体的主键
public void GuideDelete(int identify)
{
Song.Entities.Guide guide = this.GuideSingle(identify);
GuideDelete(guide);
}
///
/// 当前新闻的上一条新闻
///
///
///
public Guide GuidePrev(Guide entity)
{
WhereClip wc = new WhereClip();
wc &= Guide._.Gu_IsShow == true;
wc &= Guide._.Cou_ID == entity.Cou_ID;
wc &= Guide._.Gu_CrtTime > entity.Gu_CrtTime;
return Gateway.Default.From().OrderBy(Guide._.Gu_CrtTime.Asc)
.Where(wc).ToFirst();
}
///
/// 当前新闻的下一条新闻
///
///
///
public Guide GuideNext(Guide entity)
{
WhereClip wc = new WhereClip();
wc &= Guide._.Gu_IsShow == true;
wc &= Guide._.Cou_ID == entity.Cou_ID;
wc &= Guide._.Gu_CrtTime < entity.Gu_CrtTime;
return Gateway.Default.From().OrderBy(Guide._.Gu_CrtTime.Desc)
.Where(wc).ToFirst();
}
///
/// 获取单一实体对象,按主键ID;
///
/// 实体的主键
///
public Guide GuideSingle(int identify)
{
return Gateway.Default.From().Where(Guide._.Gu_Id == identify).ToFirst();
}
public Guide[] GuideCount(int orgid, int couid, int gcid, int count)
{
WhereClip wc = new WhereClip();
wc &= Guide._.Gu_IsShow == true;
if (orgid > 0) wc &= Guide._.Org_ID == orgid;
if (couid > 0) wc &= Guide._.Cou_ID == couid;
if (gcid > 0) wc &= Guide._.Gc_ID == gcid;
return Gateway.Default.From().Where(wc).ToArray();
}
///
/// 分页获取
///
///
/// 课程id
/// 考试指南分类
/// 是否显示
///
///
///
///
public Guide[] GetGuidePager(int orgid, int couid, int gcid, string searTxt, bool? isShow, int size, int index, out int countSum)
{
WhereClip wc = new WhereClip();
if (orgid > 0) wc.And(Guide._.Org_ID == orgid);
if (couid > 0) wc.And(Guide._.Cou_ID == couid);
if (gcid >=0) wc.And(Guide._.Gc_ID == gcid);
if (isShow != null) wc.And(Guide._.Gu_IsShow == (bool)isShow);
if (searTxt != null && searTxt.Trim() != "")
wc.And(Guide._.Gu_Title.Like("%" + searTxt + "%"));
countSum = Gateway.Default.Count(wc);
return Gateway.Default.From().Where(wc).OrderBy(Guide._.Gu_PushTime.Desc).ToArray(size, (index - 1) * size);
}
///
/// 分页获取
///
///
///
/// 考试指南分类,多个id,逗号分隔
///
///
///
///
///
///
public Guide[] GetGuidePager(int orgid, int couid, string gcids, string searTxt, bool? isShow, int size, int index, out int countSum)
{
WhereClip wc = new WhereClip();
if (orgid > 0) wc.And(Guide._.Org_ID == orgid);
if (couid > 0) wc.And(Guide._.Cou_ID == couid);
if (!string.IsNullOrWhiteSpace(gcids))
{
WhereClip wcSbjid = new WhereClip();
foreach (string tm in gcids.Split(','))
{
if (string.IsNullOrWhiteSpace(tm)) continue;
int sbj = 0;
int.TryParse(tm, out sbj);
if (sbj == 0) continue;
wcSbjid.Or(Guide._.Gc_ID == sbj);
}
wc.And(wcSbjid);
}
if (isShow != null) wc.And(Guide._.Gu_IsShow == (bool)isShow);
if (searTxt != null && searTxt.Trim() != "")
wc.And(Guide._.Gu_Title.Like("%" + searTxt + "%"));
countSum = Gateway.Default.Count(wc);
return Gateway.Default.From().Where(wc).OrderBy(Guide._.Gu_PushTime.Desc).ToArray(size, (index - 1) * size);
}
#endregion
#region 考试指南分类
///
/// 添加
///
/// 业务实体
public int ColumnsAdd(GuideColumns entity)
{
entity.Gc_CrtTime = DateTime.Now;
//如果没有排序号,则自动计算
if (entity.Gc_Tax < 1)
{
object obj = Gateway.Default.Max(GuideColumns._.Gc_Tax, GuideColumns._.Cou_ID == entity.Cou_ID && GuideColumns._.Gc_PID == entity.Gc_PID);
entity.Gc_Tax = obj is int ? (int)obj + 1 : 0;
}
Song.Entities.Organization org = Business.Do().OrganCurrent();
if (org != null)
{
entity.Org_ID = org.Org_ID;
entity.Org_Name = org.Org_Name;
}
return Gateway.Default.Save(entity);
}
///
/// 修改
///
/// 业务实体
public void ColumnsSave(GuideColumns entity)
{
Song.Entities.GuideColumns old = this.ColumnsSingle(entity.Gc_ID);
if (old.Gc_PID != entity.Gc_PID)
{
object obj = Gateway.Default.Max(GuideColumns._.Gc_Tax, GuideColumns._.Cou_ID == entity.Cou_ID && GuideColumns._.Gc_PID == entity.Gc_PID);
entity.Gc_Tax = obj is int ? (int)obj + 1 : 0;
}
using (DbTrans trans = Gateway.Default.BeginTrans())
{
try
{
trans.Save(entity);
trans.Update(new Field[] { Guide._.Gc_Title }, new object[] { entity.Gc_Title, }, GuideColumns._.Gc_ID == entity.Gc_ID);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
trans.Close();
}
}
}
///
/// 删除
///
/// 业务实体
public void ColumnsDelete(GuideColumns entity)
{
Song.Entities.GuideColumns[] cols = GetColumnsChild(entity.Cou_ID, entity.Gc_ID, null);
foreach (Song.Entities.GuideColumns cl in cols)
ColumnsDelete(cl);
Gateway.Default.Delete(Guide._.Gc_ID == entity.Gc_ID);
Gateway.Default.Delete(GuideColumns._.Gc_ID == entity.Gc_ID);
}
///
/// 删除,按主键ID;
///
/// 实体的主键
public void ColumnsDelete(int identify)
{
Song.Entities.GuideColumns col = ColumnsSingle(identify);
ColumnsDelete(col);
}
///
/// 获取单一实体对象,按主键ID;
///
/// 实体的主键
///
public GuideColumns ColumnsSingle(int identify)
{
return Gateway.Default.From().Where(GuideColumns._.Gc_ID == identify).ToFirst();
}
///
/// 获取同一父级下的最大排序号;
///
///课程id
///学科id
///
public int ColumnsMaxTaxis(int couid, int pid)
{
object obj = Gateway.Default.Max(GuideColumns._.Gc_Tax, GuideColumns._.Cou_ID == couid && GuideColumns._.Gc_PID == pid);
int tax = obj is int ? (int)obj + 1 : 0;
return tax;
}
///
/// 获取对象;即所有分类;
///
///
public GuideColumns[] GetColumnsAll(int couid, bool? isUse)
{
WhereClip wc = new WhereClip();
if (couid > 0) wc.And(GuideColumns._.Cou_ID == couid);
if (isUse != null) wc.And(GuideColumns._.Gc_IsUse == (bool)isUse);
return Gateway.Default.From().Where(wc).OrderBy(GuideColumns._.Gc_Tax.Asc).ToArray();
}
///
/// 获取当前分类下的子分类
///
///
///
///
public GuideColumns[] GetColumnsChild(int couid, int pid, bool? isUse)
{
WhereClip wc = GuideColumns._.Cou_ID == couid;
if (pid >= 0) wc.And(GuideColumns._.Gc_PID == pid);
if (isUse != null) wc.And(GuideColumns._.Gc_IsUse == (bool)isUse);
return Gateway.Default.From().Where(wc).OrderBy(GuideColumns._.Gc_Tax.Asc).ToArray();
}
public bool ColumnsIsChildren(int couid, int pid, bool? isUse)
{
WhereClip wc = GuideColumns._.Cou_ID == couid;
if (pid >= 0) wc.And(GuideColumns._.Gc_PID == pid);
if (isUse != null) wc.And(GuideColumns._.Gc_IsUse == (bool)isUse);
int count = Gateway.Default.Count(wc);
return count > 0;
}
///
/// 当前对象名称是否重名
///
/// 业务实体
///
public bool ColumnsIsExist(int couid, int pid, GuideColumns entity)
{
WhereClip wc = GuideColumns._.Gc_Title == entity.Gc_Title && GuideColumns._.Cou_ID == couid && GuideColumns._.Gc_PID == pid;
//如果是一个已经存在的对象
if (entity.Gc_ID == 0) wc = wc.And(GuideColumns._.Gc_ID == entity.Gc_ID);
GuideColumns mm = Gateway.Default.From().Where(wc).ToFirst();
return mm != null;
}
///
/// 将当前项目向上移动;仅在当前对象的同层移动,即同一父节点下的对象这前移动;
///
///
/// 如果已经处于顶端,则返回false;移动成功,返回true
public bool ColumnsRemoveUp(int id)
{
//当前对象
GuideColumns current = Gateway.Default.From().Where(GuideColumns._.Gc_ID == id).ToFirst();
int tax = (int)current.Gc_Tax;
//上一个对象,即兄长对象;兄长不存则直接返回false;
GuideColumns prev = Gateway.Default.From()
.Where(GuideColumns._.Gc_Tax < tax && GuideColumns._.Gc_PID == current.Gc_PID && GuideColumns._.Cou_ID == current.Cou_ID)
.OrderBy(GuideColumns._.Gc_Tax.Desc).ToFirst();
if (prev == null) return false;
//交换排序号
current.Gc_Tax = prev.Gc_Tax;
prev.Gc_Tax = tax;
using (DbTrans tran = Gateway.Default.BeginTrans())
{
try
{
tran.Save(current);
tran.Save(prev);
tran.Commit();
return true;
}
catch
{
tran.Rollback();
throw;
}
finally
{
tran.Close();
}
}
}
///
/// 将当前项目向下移动;仅在当前对象的同层移动,即同一父节点下的对象这前移动;
///
///
/// 如果已经处于顶端,则返回false;移动成功,返回true
public bool ColumnsRemoveDown(int id)
{
//当前对象
GuideColumns current = Gateway.Default.From().Where(GuideColumns._.Gc_ID == id).ToFirst();
int tax = (int)current.Gc_Tax;
//下一个对象,即弟弟对象;弟弟不存则直接返回false;
GuideColumns next = Gateway.Default.From()
.Where(GuideColumns._.Gc_Tax > tax && GuideColumns._.Gc_PID == current.Gc_PID && GuideColumns._.Cou_ID == current.Cou_ID)
.OrderBy(GuideColumns._.Gc_Tax.Asc).ToFirst();
if (next == null) return false;
//交换排序号
current.Gc_Tax = next.Gc_Tax;
next.Gc_Tax = tax;
using (DbTrans tran = Gateway.Default.BeginTrans())
{
try
{
tran.Save(current);
tran.Save(next);
tran.Commit();
return true;
}
catch
{
tran.Rollback();
throw;
}
finally
{
tran.Close();
}
}
}
#endregion
}
}