AttendanceDataService.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. using System.Data.Common;
  9. namespace DAL
  10. {
  11. /// <summary>
  12. /// 考勤数据数据访问类
  13. /// </summary>
  14. public class AttendanceDataService
  15. {
  16. /// <summary>
  17. /// 查询所有
  18. /// </summary>
  19. /// <param name="sql">sql语句</param>
  20. /// <param name="param">可变参数数组</param>
  21. /// <returns>返回集合</returns>
  22. List<AttendanceData> excuteSql(string sql, params SqlParameter[] param)
  23. {
  24. return ServiceBase<AttendanceData>.excuteSql(new AttendanceData(), "AttendanceData", sql, CommandType.Text, param);
  25. }
  26. /// <summary>
  27. /// 获取单个对象
  28. /// </summary>
  29. /// <param name="sql">sql语句</param>
  30. /// <param name="param">可变参数数组</param>
  31. /// <returns>返回空或者单个对象</returns>
  32. AttendanceData excuteType(string sql, params SqlParameter[] param)
  33. {
  34. //查询结果放入对象集合
  35. List<AttendanceData> adList = excuteSql(sql, param);
  36. //判断集合是否为空
  37. if (adList == null || adList.Count == 0)
  38. //返回null
  39. return null;
  40. //返回单个对象
  41. return adList[0];
  42. }
  43. /// <summary>
  44. /// 根据编号查询对象信息
  45. /// </summary>
  46. /// <param name="id">对象编号</param>
  47. /// <returns>返回空或者单个对象信息</returns>
  48. public AttendanceData GetAttendanceDataByID(int id)
  49. {
  50. //调用获取单个对象的方法
  51. return excuteType("select * from AttendanceData where Id = @id", new SqlParameter("@id", id));
  52. }
  53. /// <summary>
  54. /// 根据员工号和日期获取数据集合
  55. /// </summary>
  56. /// <param name="userNumber"></param>
  57. /// <param name="swipeDate"></param>
  58. /// <returns></returns>
  59. public List<AttendanceData> GetByUserNumberAndSwipeDate(string userNumber, string swipeDate)
  60. {
  61. string sql = "select * from AttendanceData where UserNumber = @UserNumber and SwipeDate = @SwipeDate order by SwipeTime";
  62. SqlParameter[] parameter = new SqlParameter[]{
  63. new SqlParameter("@UserNumber",userNumber),
  64. new SqlParameter("@SwipeDate",swipeDate)
  65. };
  66. return excuteSql(sql, parameter);
  67. }
  68. /// <summary>
  69. /// 根据员工号和日期获取数据
  70. /// </summary>
  71. /// <param name="userNumber"></param>
  72. /// <param name="swipeDate"></param>
  73. /// <returns></returns>
  74. public AttendanceData GetByUserNumberAndSwipeDateEntity(string userNumber, string swipeDate, string swipeTime)
  75. {
  76. string sql = "select * from AttendanceData where UserNumber = @UserNumber and SwipeDate = @SwipeDate and InitSwipeTime = @SwipeTime";
  77. SqlParameter[] parameter = new SqlParameter[]{
  78. new SqlParameter("@UserNumber",userNumber),
  79. new SqlParameter("@SwipeDate",swipeDate),
  80. new SqlParameter("@SwipeTime",swipeTime)
  81. };
  82. return excuteType(sql, parameter);
  83. }
  84. /// <summary>
  85. /// 根据条件获取单个数据
  86. /// </summary>
  87. /// <param name="userNumber"></param>
  88. /// <param name="swipeDate"></param>
  89. /// <param name="startTime"></param>
  90. /// <param name="endTime"></param>
  91. /// <returns></returns>
  92. public AttendanceData GetByUserNumberAndSwipeDateEntity(string userNumber, string swipeDate, string startTime, string endTime)
  93. {
  94. string sql = "select * from AttendanceData where UserNumber = @UserNumber and SwipeDate = @SwipeDate and swipeTime between @StartTime and @EndTime";
  95. SqlParameter[] parameter = new SqlParameter[]{
  96. new SqlParameter("@UserNumber",userNumber),
  97. new SqlParameter("@SwipeDate",swipeDate),
  98. new SqlParameter("@StartTime",startTime),
  99. new SqlParameter("@EndTime",endTime)
  100. };
  101. return excuteType(sql, parameter);
  102. }
  103. /// <summary>
  104. /// 多条数据添加
  105. /// </summary>
  106. /// <param name="ad"></param>
  107. public bool AddAttendanceData(List<AttendanceData> adList)
  108. {
  109. SqlCommand cmd = SqlHelper.createCon().CreateCommand();
  110. cmd.Connection.Open();
  111. SqlTransaction trans = cmd.Connection.BeginTransaction();
  112. try
  113. {
  114. foreach (AttendanceData ad in adList)
  115. {
  116. string sql = string.Format("insert into AttendanceData values('" + ad.UserNumber + "','" + ad.SwipeDate + "','" + ad.SwipeTime + "','" + ad.SwipeTime + "')");
  117. cmd.CommandText = sql;
  118. cmd.ExecuteNonQuery();
  119. }
  120. trans.Commit();
  121. cmd.Connection.Close();
  122. return true;
  123. }
  124. catch
  125. {
  126. trans.Rollback();
  127. cmd.Connection.Close();
  128. return false;
  129. }
  130. }
  131. /// <summary>
  132. /// 更改打卡时间
  133. /// </summary>
  134. /// <param name="id"></param>
  135. /// <returns></returns>
  136. public bool UpdateSwipeDate(int id, string swipeTime)
  137. {
  138. string sql = "update AttendanceData set SwipeTime = '" + swipeTime + "' where id = " + id + "";
  139. if (SqlHelper.ExecuteNonQuery(sql, CommandType.Text) > 0)
  140. return true;
  141. return false;
  142. }
  143. /// <summary>
  144. /// 添加
  145. /// </summary>
  146. /// <param name="id"></param>
  147. /// <returns></returns>
  148. public bool AddAttendanceData(AttendanceData ad)
  149. {
  150. string sql = "insert into AttendanceData values('" + ad.UserNumber + "','" + ad.SwipeDate + "','" + ad.SwipeTime + "','')";
  151. if (SqlHelper.ExecuteNonQuery(sql, CommandType.Text) > 0)
  152. return true;
  153. return false;
  154. }
  155. /// <summary>
  156. /// 更改打卡时间
  157. /// </summary>
  158. /// <param name="id"></param>
  159. /// <returns></returns>
  160. public bool DelAttendanceData(int id)
  161. {
  162. string sql = "Delete AttendanceData where Id = @Id";
  163. if (SqlHelper.ExecuteNonQuery(sql, CommandType.Text, new SqlParameter("@Id", id)) > 0)
  164. return true;
  165. return false;
  166. }
  167. /// <summary>
  168. /// 获取所有请假列表
  169. /// </summary>
  170. /// <param name="uid"></param>
  171. /// <param name="yearMonth"></param>
  172. /// <param name="UpCardType"></param>
  173. /// <returns></returns>
  174. public DataTable GetLeaveList(int uid, string yearMonth, int UpCardType, int pageIndex, int pageSize, out int sumPage, out int totalRecord)
  175. {
  176. SqlParameter[] parameters = new SqlParameter[] {
  177. new SqlParameter("@pageIndex", pageIndex),
  178. new SqlParameter("@pageSize", pageSize),
  179. new SqlParameter("@uid",uid),
  180. new SqlParameter("@yearMonth",yearMonth),
  181. new SqlParameter("@UpCardType",UpCardType)
  182. };
  183. DataTable dt = SqlHelper.TransferProcedure("[dbo].[UpCard_Hourse]", CommandType.StoredProcedure, parameters);
  184. parameters = null;
  185. //查询总条数
  186. SqlParameter[] para = new SqlParameter[] {
  187. new SqlParameter("@pageIndex", pageIndex),
  188. new SqlParameter("@pageSize", pageSize),
  189. new SqlParameter("@uid",uid),
  190. new SqlParameter("@yearMonth",yearMonth),
  191. new SqlParameter("@UpCardType",UpCardType)
  192. };
  193. DataTable counts = SqlHelper.TransferProcedure("[dbo].[UpCard_Hourse_count]", CommandType.StoredProcedure, para);
  194. if (counts != null && counts.Rows.Count > 0)
  195. totalRecord = Convert.ToInt32(counts.Rows[0]["c"]);
  196. else
  197. totalRecord = 0;
  198. //计算出总页数
  199. sumPage = totalRecord % 15 == 0 ? totalRecord / 15 : totalRecord / 15 + 1;
  200. return dt;
  201. }
  202. /// <summary>
  203. /// 查询该年度请假次数
  204. /// </summary>
  205. /// <param name="uid"></param>
  206. /// <param name="yearMonth"></param>
  207. /// <param name="UpCardType"></param>
  208. /// <returns></returns>
  209. public string GetLeaveCountY(int uid, string yearMonth, int UpCardType)
  210. {
  211. SqlParameter[] parameters = new SqlParameter[] {
  212. new SqlParameter("@uid",uid),
  213. new SqlParameter("@yearMonth",yearMonth),
  214. new SqlParameter("@UpCardType",UpCardType)
  215. };
  216. DataTable dt = SqlHelper.TransferProcedure("[dbo].[UpCard_Years_count]", CommandType.StoredProcedure, parameters);
  217. if (dt != null && dt.Rows.Count > 0)
  218. return dt.Rows[0]["c"].ToString();
  219. else
  220. return "0";
  221. }
  222. /// <summary>
  223. /// 查询考勤日
  224. /// </summary>
  225. /// <param name="userNumber"></param>
  226. /// <param name="start"></param>
  227. /// <param name="end"></param>
  228. /// <returns></returns>
  229. public List<AttendanceData> GetByZB(string userNumber, string start, string end)
  230. {
  231. string sql = "select * from AttendanceData where UserNumber=@UserNumber and (SwipeDate between @start and @end) order by SwipeDate asc";
  232. SqlParameter[] parameter = new SqlParameter[]{
  233. new SqlParameter("@UserNumber",userNumber),
  234. new SqlParameter("@start",start),
  235. new SqlParameter("@end",end)
  236. };
  237. return excuteSql(sql, parameter);
  238. }
  239. }
  240. }