using System; using System.Collections.Generic; using System.Linq; using System.Text; using Models; using System.Data.SqlClient; using System.Data; using System.Data.Common; namespace DAL { /// /// 考勤数据数据访问类 /// public class AttendanceDataService { /// /// 查询所有 /// /// sql语句 /// 可变参数数组 /// 返回集合 List excuteSql(string sql, params SqlParameter[] param) { return ServiceBase.excuteSql(new AttendanceData(), "AttendanceData", sql, CommandType.Text, param); } /// /// 获取单个对象 /// /// sql语句 /// 可变参数数组 /// 返回空或者单个对象 AttendanceData excuteType(string sql, params SqlParameter[] param) { //查询结果放入对象集合 List adList = excuteSql(sql, param); //判断集合是否为空 if (adList == null || adList.Count == 0) //返回null return null; //返回单个对象 return adList[0]; } /// /// 根据编号查询对象信息 /// /// 对象编号 /// 返回空或者单个对象信息 public AttendanceData GetAttendanceDataByID(int id) { //调用获取单个对象的方法 return excuteType("select * from AttendanceData where Id = @id", new SqlParameter("@id", id)); } /// /// 根据员工号和日期获取数据集合 /// /// /// /// public List GetByUserNumberAndSwipeDate(string userNumber, string swipeDate) { string sql = "select * from AttendanceData where UserNumber = @UserNumber and SwipeDate = @SwipeDate order by SwipeTime"; SqlParameter[] parameter = new SqlParameter[]{ new SqlParameter("@UserNumber",userNumber), new SqlParameter("@SwipeDate",swipeDate) }; return excuteSql(sql, parameter); } /// /// 根据员工号和日期获取数据 /// /// /// /// public AttendanceData GetByUserNumberAndSwipeDateEntity(string userNumber, string swipeDate, string swipeTime) { string sql = "select * from AttendanceData where UserNumber = @UserNumber and SwipeDate = @SwipeDate and InitSwipeTime = @SwipeTime"; SqlParameter[] parameter = new SqlParameter[]{ new SqlParameter("@UserNumber",userNumber), new SqlParameter("@SwipeDate",swipeDate), new SqlParameter("@SwipeTime",swipeTime) }; return excuteType(sql, parameter); } /// /// 根据条件获取单个数据 /// /// /// /// /// /// public AttendanceData GetByUserNumberAndSwipeDateEntity(string userNumber, string swipeDate, string startTime, string endTime) { string sql = "select * from AttendanceData where UserNumber = @UserNumber and SwipeDate = @SwipeDate and swipeTime between @StartTime and @EndTime"; SqlParameter[] parameter = new SqlParameter[]{ new SqlParameter("@UserNumber",userNumber), new SqlParameter("@SwipeDate",swipeDate), new SqlParameter("@StartTime",startTime), new SqlParameter("@EndTime",endTime) }; return excuteType(sql, parameter); } /// /// 多条数据添加 /// /// public bool AddAttendanceData(List adList) { SqlCommand cmd = SqlHelper.createCon().CreateCommand(); cmd.Connection.Open(); SqlTransaction trans = cmd.Connection.BeginTransaction(); try { foreach (AttendanceData ad in adList) { string sql = string.Format("insert into AttendanceData values('" + ad.UserNumber + "','" + ad.SwipeDate + "','" + ad.SwipeTime + "','" + ad.SwipeTime + "')"); cmd.CommandText = sql; cmd.ExecuteNonQuery(); } trans.Commit(); cmd.Connection.Close(); return true; } catch { trans.Rollback(); cmd.Connection.Close(); return false; } } /// /// 更改打卡时间 /// /// /// public bool UpdateSwipeDate(int id, string swipeTime) { string sql = "update AttendanceData set SwipeTime = '" + swipeTime + "' where id = " + id + ""; if (SqlHelper.ExecuteNonQuery(sql, CommandType.Text) > 0) return true; return false; } /// /// 添加 /// /// /// public bool AddAttendanceData(AttendanceData ad) { string sql = "insert into AttendanceData values('" + ad.UserNumber + "','" + ad.SwipeDate + "','" + ad.SwipeTime + "','')"; if (SqlHelper.ExecuteNonQuery(sql, CommandType.Text) > 0) return true; return false; } /// /// 更改打卡时间 /// /// /// public bool DelAttendanceData(int id) { string sql = "Delete AttendanceData where Id = @Id"; if (SqlHelper.ExecuteNonQuery(sql, CommandType.Text, new SqlParameter("@Id", id)) > 0) return true; return false; } /// /// 获取所有请假列表 /// /// /// /// /// public DataTable GetLeaveList(int uid, string yearMonth, int UpCardType, int pageIndex, int pageSize, out int sumPage, out int totalRecord) { SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@pageIndex", pageIndex), new SqlParameter("@pageSize", pageSize), new SqlParameter("@uid",uid), new SqlParameter("@yearMonth",yearMonth), new SqlParameter("@UpCardType",UpCardType) }; DataTable dt = SqlHelper.TransferProcedure("[dbo].[UpCard_Hourse]", CommandType.StoredProcedure, parameters); parameters = null; //查询总条数 SqlParameter[] para = new SqlParameter[] { new SqlParameter("@pageIndex", pageIndex), new SqlParameter("@pageSize", pageSize), new SqlParameter("@uid",uid), new SqlParameter("@yearMonth",yearMonth), new SqlParameter("@UpCardType",UpCardType) }; DataTable counts = SqlHelper.TransferProcedure("[dbo].[UpCard_Hourse_count]", CommandType.StoredProcedure, para); if (counts != null && counts.Rows.Count > 0) totalRecord = Convert.ToInt32(counts.Rows[0]["c"]); else totalRecord = 0; //计算出总页数 sumPage = totalRecord % 15 == 0 ? totalRecord / 15 : totalRecord / 15 + 1; return dt; } /// /// 查询该年度请假次数 /// /// /// /// /// public string GetLeaveCountY(int uid, string yearMonth, int UpCardType) { SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@uid",uid), new SqlParameter("@yearMonth",yearMonth), new SqlParameter("@UpCardType",UpCardType) }; DataTable dt = SqlHelper.TransferProcedure("[dbo].[UpCard_Years_count]", CommandType.StoredProcedure, parameters); if (dt != null && dt.Rows.Count > 0) return dt.Rows[0]["c"].ToString(); else return "0"; } /// /// 查询考勤日 /// /// /// /// /// public List GetByZB(string userNumber, string start, string end) { string sql = "select * from AttendanceData where UserNumber=@UserNumber and (SwipeDate between @start and @end) order by SwipeDate asc"; SqlParameter[] parameter = new SqlParameter[]{ new SqlParameter("@UserNumber",userNumber), new SqlParameter("@start",start), new SqlParameter("@end",end) }; return excuteSql(sql, parameter); } } }