using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models;
using System.Data.SqlClient;
using System.Data;
namespace DAL
{
///
/// 年历数据访问类
///
public class CalendarsService
{
///
/// 查询所有
///
/// sql语句
/// 可变参数数组
/// 返回集合
List excuteSql(string sql, params SqlParameter[] param)
{
return ServiceBase.excuteSql(new Calendars(), "Calendar", sql, CommandType.Text, param);
}
///
/// 获取单个对象
///
/// sql语句
/// 可变参数数组
/// 返回空或者单个对象
Calendars excuteType(string sql, params SqlParameter[] param)
{
//查询结果放入对象集合
List cList = excuteSql(sql, param);
//判断集合是否为空
if (cList == null || cList.Count == 0)
//返回null
return null;
//返回单个对象
return cList[0];
}
///
/// 根据编号查询对象信息
///
/// 对象编号
/// 返回空或者单个对象信息
public Calendars GetCalendarsByID(int id)
{
//调用获取单个对象的方法
return excuteType("select * from Calendar where Id = @id", new SqlParameter("@id", id));
}
///
/// 根据日期查询对象信息
///
///
///
public Calendars GetCalendarsByCalendarDate(string calendarDate)
{
return excuteType("select * from Calendar where CalendarDate = @CalendarDate", new SqlParameter("@CalendarDate", calendarDate));
}
///
/// 根据日期查询集合
///
///
///
public List GetCalendarsByCalendarDates(string yearMonth)
{
string yearMonthStart = Convert.ToDateTime(yearMonth + "-01").AddMonths(-1).ToString("yyyy-MM") + "-28";
string yearMonthEnd = yearMonth + "-27";
SqlParameter[] parameter = new SqlParameter[]{
new SqlParameter("@yearMonthStart",yearMonthStart),
new SqlParameter("@yearMonthEnd",yearMonthEnd)
};
return excuteSql("select * from Calendar where CalendarDate between @yearMonthStart and @yearMonthEnd order by CalendarDate asc", parameter);
}
///
/// 新增
///
/// 对象
public bool AddCalendar(Calendars c)
{
if (SqlHelper.ExecuteNonQuery("insert into Calendar values(@CalendarDate,@CTid)", CommandType.Text, new SqlParameter("@CalendarDate",c.CalendarDate),new SqlParameter("@CTid",c.CTid)) > 0)
return true;
return false;
}
///
/// 删除
///
///
///
public bool DelCalendar(string calendarDate)
{
if (SqlHelper.ExecuteNonQuery("delete Calendar where CalendarDate = @CalendarDate", CommandType.Text, new SqlParameter("@CalendarDate", calendarDate)) > 0)
return true;
return false;
}
///
/// 获取工作天数
///
///
///
///
public int GetCalendarWeekCount(string starDate,string endDate)
{
string sql = "select count(distinct calendarDate) from Calendar where ctid = 0 and (calendardate between '"+starDate+"' and '"+endDate+"')";
return (int)SqlHelper.ExecuteScalar(sql, CommandType.Text);
}
///
/// LiuChengYi 2014/5/5
/// 根据月份获取天数
///
///
///
public List GetCalendarMonths(string startDate, string endDate)
{
string sql = "SELECT * FROM dbo.Calendar WHERE CalendarDate BETWEEN '"+startDate+"' AND '"+endDate+"' ORDER BY CalendarDate asc";
return excuteSql(sql);
}
///
/// 获取指定日期内的日历
///
/// 开始日期
/// 结束日期
/// 倒序排列的日历表
public List GetCalendar(string startDate, string endDate)
{
string sql = "SELECT * FROM dbo.Calendar WHERE CalendarDate BETWEEN '" + startDate + "' AND '" + endDate + "' ORDER BY CalendarDate Desc";
return excuteSql(sql);
}
}
}