CalendarsService.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Models;
  6. using System.Data.SqlClient;
  7. using System.Data;
  8. namespace DAL
  9. {
  10. /// <summary>
  11. /// 年历数据访问类
  12. /// </summary>
  13. public class CalendarsService
  14. {
  15. /// <summary>
  16. /// 查询所有
  17. /// </summary>
  18. /// <param name="sql">sql语句</param>
  19. /// <param name="param">可变参数数组</param>
  20. /// <returns>返回集合</returns>
  21. List<Calendars> excuteSql(string sql, params SqlParameter[] param)
  22. {
  23. return ServiceBase<Calendars>.excuteSql(new Calendars(), "Calendar", sql, CommandType.Text, param);
  24. }
  25. /// <summary>
  26. /// 获取单个对象
  27. /// </summary>
  28. /// <param name="sql">sql语句</param>
  29. /// <param name="param">可变参数数组</param>
  30. /// <returns>返回空或者单个对象</returns>
  31. Calendars excuteType(string sql, params SqlParameter[] param)
  32. {
  33. //查询结果放入对象集合
  34. List<Calendars> cList = excuteSql(sql, param);
  35. //判断集合是否为空
  36. if (cList == null || cList.Count == 0)
  37. //返回null
  38. return null;
  39. //返回单个对象
  40. return cList[0];
  41. }
  42. /// <summary>
  43. /// 根据编号查询对象信息
  44. /// </summary>
  45. /// <param name="id">对象编号</param>
  46. /// <returns>返回空或者单个对象信息</returns>
  47. public Calendars GetCalendarsByID(int id)
  48. {
  49. //调用获取单个对象的方法
  50. return excuteType("select * from Calendar where Id = @id", new SqlParameter("@id", id));
  51. }
  52. /// <summary>
  53. /// 根据日期查询对象信息
  54. /// </summary>
  55. /// <param name="calendarDate"></param>
  56. /// <returns></returns>
  57. public Calendars GetCalendarsByCalendarDate(string calendarDate)
  58. {
  59. return excuteType("select * from Calendar where CalendarDate = @CalendarDate", new SqlParameter("@CalendarDate", calendarDate));
  60. }
  61. /// <summary>
  62. /// 根据日期查询集合
  63. /// </summary>
  64. /// <param name="yearMonth"></param>
  65. /// <returns></returns>
  66. public List<Calendars> GetCalendarsByCalendarDates(string yearMonth)
  67. {
  68. string yearMonthStart = Convert.ToDateTime(yearMonth + "-01").AddMonths(-1).ToString("yyyy-MM") + "-28";
  69. string yearMonthEnd = yearMonth + "-27";
  70. SqlParameter[] parameter = new SqlParameter[]{
  71. new SqlParameter("@yearMonthStart",yearMonthStart),
  72. new SqlParameter("@yearMonthEnd",yearMonthEnd)
  73. };
  74. return excuteSql("select * from Calendar where CalendarDate between @yearMonthStart and @yearMonthEnd order by CalendarDate asc", parameter);
  75. }
  76. /// <summary>
  77. /// 新增
  78. /// </summary>
  79. /// <param name="sdt">对象</param>
  80. public bool AddCalendar(Calendars c)
  81. {
  82. if (SqlHelper.ExecuteNonQuery("insert into Calendar values(@CalendarDate,@CTid)", CommandType.Text, new SqlParameter("@CalendarDate",c.CalendarDate),new SqlParameter("@CTid",c.CTid)) > 0)
  83. return true;
  84. return false;
  85. }
  86. /// <summary>
  87. /// 删除
  88. /// </summary>
  89. /// <param name="calendarDate"></param>
  90. /// <returns></returns>
  91. public bool DelCalendar(string calendarDate)
  92. {
  93. if (SqlHelper.ExecuteNonQuery("delete Calendar where CalendarDate = @CalendarDate", CommandType.Text, new SqlParameter("@CalendarDate", calendarDate)) > 0)
  94. return true;
  95. return false;
  96. }
  97. /// <summary>
  98. /// 获取工作天数
  99. /// </summary>
  100. /// <param name="starDate"></param>
  101. /// <param name="endDate"></param>
  102. /// <returns></returns>
  103. public int GetCalendarWeekCount(string starDate,string endDate)
  104. {
  105. string sql = "select count(distinct calendarDate) from Calendar where ctid = 0 and (calendardate between '"+starDate+"' and '"+endDate+"')";
  106. return (int)SqlHelper.ExecuteScalar(sql, CommandType.Text);
  107. }
  108. /// <summary>
  109. /// LiuChengYi 2014/5/5
  110. /// 根据月份获取天数
  111. /// </summary>
  112. /// <param name="months"></param>
  113. /// <returns></returns>
  114. public List<Calendars> GetCalendarMonths(string startDate, string endDate)
  115. {
  116. string sql = "SELECT * FROM dbo.Calendar WHERE CalendarDate BETWEEN '"+startDate+"' AND '"+endDate+"' ORDER BY CalendarDate asc";
  117. return excuteSql(sql);
  118. }
  119. /// <summary>
  120. /// 获取指定日期内的日历
  121. /// </summary>
  122. /// <param name="startDate">开始日期</param>
  123. /// <param name="endDate">结束日期</param>
  124. /// <returns>倒序排列的日历表</returns>
  125. public List<Calendars> GetCalendar(string startDate, string endDate)
  126. {
  127. string sql = "SELECT * FROM dbo.Calendar WHERE CalendarDate BETWEEN '" + startDate + "' AND '" + endDate + "' ORDER BY CalendarDate Desc";
  128. return excuteSql(sql);
  129. }
  130. }
  131. }