using Dapper;
using Learun.DataBase.Repository;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace Learun.Application.TwoDevelopment.EducationalAdministration
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创 建:超级管理员
/// 日 期:2019-06-14 10:24
/// 描 述:教师考勤
///
public class TeachAttendanceService : RepositoryFactory
{
#region 获取数据
///
/// 获取页面显示列表数据
///
/// 查询参数
///
public IEnumerable GetPageList(Pagination pagination, string queryJson)
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT t.* ");
strSql.Append(" FROM Teach_attendance t ");
strSql.Append(" WHERE 1=1 ");
var queryParam = queryJson.ToJObject();
// 虚拟参数
var dp = new DynamicParameters(new { });
if (!queryParam["StartTime"].IsEmpty() && !queryParam["EndTime"].IsEmpty())
{
dp.Add("startTime", queryParam["StartTime"].ToDate(), DbType.DateTime);
dp.Add("endTime", queryParam["EndTime"].ToDate(), DbType.DateTime);
strSql.Append(" AND ( t.ClockTime >= @startTime AND t.ClockTime <= @endTime ) ");
}
return this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp, pagination);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
internal object GetAttendanceByEmpNo(string empNo)
{
try
{
var entityList = this.BaseRepository("CollegeMIS").FindList(a => a.EmpNo == empNo).ToList();
var dataK = entityList.GroupBy(a => a.ClockTime?.ToString("yyyyMMdd"))
.Select(a => new
{
key = a.Key,
y = a.Key.Substring(0, 4),
m = a.Key.Substring(4, 2),
d = a.Key.Substring(6),
day = $"{entityList.FirstOrDefault(b => b.ClockTime?.ToString("yyyyMMdd") == a.Key)?.ClockTime?.ToString("yyyy年MM月dd日")} {System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(Convert.ToDateTime(entityList.FirstOrDefault(b => b.ClockTime?.ToString("yyyyMMdd") == a.Key)?.ClockTime).DayOfWeek)}",
list = entityList.Where(b => b.ClockTime?.ToString("yyyyMMdd") == a.Key).OrderBy(b => b.ClockTime).Select(b => new { time = b.ClockTime?.ToString("HH:mm"), location = b.ClockPlace, status = "1" }),
state = "1"
});
return dataK;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取Teach_attendance表实体数据
/// 主键
///
///
public Teach_attendanceEntity GetTeach_attendanceEntity(string keyValue)
{
try
{
return this.BaseRepository("CollegeMIS").FindEntity(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 授课考勤明细统计
///
/// 查询参数
///
public IEnumerable GetStatisticDetailList(Pagination pagination, string queryJson)
{
try
{
var queryParam = queryJson.ToJObject();
var datest = DateTime.Now.ToDateString();
var dateet = datest;
if (!queryParam["startTime"].IsEmpty())
{
datest = queryParam["startTime"].ToDate().ToDateString();
}
if (!queryParam["endTime"].IsEmpty())
{
dateet = queryParam["endTime"].ToDate().ToDateString();
}
var strSql1 = $@"select a.ALTId as ALTIdInArrange,a.LessonDate,a.AcademicYearNo,a.Semester, a.LessonNo, a.LessonName, a.EmpNo as EmpNoInArrange, a.EmpName as EmpNameInArrange,a.ClassroomNo,a.LessonTime,
a.StartTime, a.EndTime,a.LessonSortNo as LessonSortNoInArrange,e.F_DepartmentId,t.ID,t.EmpNo,t.EmpName,t.ALTId,t.ALTOEId,t.LessonSortNo,t.ADType,t.ADTime,t.ClockTime,t.ClockStatus,t.ClockPlace,t.ADPhoto,t.ALon,t.ALat,t.AIsOut,t.ARemark,e.photo,t.IsFit,t.Img
from ArrangeLessonTerm a
left join Teach_Attendance t on a.ALTId=t.ALTId and t.LessonSortNo='1'
left join EmpInfo e on a.EmpNo=e.EmpNo
where a.LessonDate >='{datest}' and a.LessonDate <= '{dateet}'";
var strSql2 = $@"select a.Id as ALTIdInArrange,a.LessonDate,a.AcademicYearNo, a.Semester, a.LessonNo, a.LessonName,a.EmpNo as EmpNoInArrange, a.EmpName as EmpNameInArrange,a.ClassRoomNo as ClassroomNo,a.LessonSection as LessonTime,
case when LEN(a.LessonTime)>0 then SUBSTRING(a.LessonTime,1,CHARINDEX('-',a.LessonTime)-1) else '' end as StartTime,
case when LEN(a.LessonTime)>0 then SUBSTRING(a.LessonTime,CHARINDEX('-',a.LessonTime)+1,LEN(a.LessonTime)-CHARINDEX('-',a.LessonTime)) else '' end as EndTime,
a.LessonSortNo as LessonSortNoInArrange,e.F_DepartmentId,t.ID,t.EmpNo,t.EmpName,t.ALTId,t.ALTOEId,t.LessonSortNo,t.ADType,t.ADTime,t.ClockTime,t.ClockStatus,t.ClockPlace,t.ADPhoto,t.ALon,t.ALat,t.AIsOut,t.ARemark,e.photo,t.IsFit,t.Img from ArrangeLessonTermOfElective a
left join Teach_Attendance t on a.Id=t.ALTId and t.LessonSortNo='2'
left join EmpInfo e on a.EmpNo=e.EmpNo
where a.LessonDate >='{datest}' and a.LessonDate <= '{dateet}'";
// 虚拟参数
var dp = new DynamicParameters(new { });
if (!queryParam["EmpNo"].IsEmpty())
{
dp.Add("EmpNo", queryParam["EmpNo"].ToString(), DbType.String);
strSql1 += " AND a.EmpNo = @EmpNo";
strSql2 += " AND a.EmpNo = @EmpNo";
}
if (!queryParam["EmpName"].IsEmpty())
{
dp.Add("EmpName", "%" + queryParam["EmpName"].ToString() + "%", DbType.String);
strSql1 += " AND a.EmpName Like @EmpName";
strSql2 += " AND a.EmpName Like @EmpName";
}
if (!queryParam["F_DepartmentId"].IsEmpty())
{
dp.Add("F_DepartmentId", queryParam["F_DepartmentId"].ToString(), DbType.String);
strSql1 += " AND e.F_DepartmentId = @F_DepartmentId";
strSql2 += " AND e.F_DepartmentId = @F_DepartmentId";
}
var strSql = new StringBuilder();
strSql.Append(strSql1);
strSql.Append(" union ");
strSql.Append(strSql2);
//某天记录列表
var data = this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp)
.GroupBy(a => new
{
a.ALTIdInArrange,
a.LessonDate,
a.AcademicYearNo,
a.Semester,
a.LessonNo,
a.LessonName,
a.EmpNoInArrange,
a.EmpNameInArrange,
a.ClassroomNo,
a.LessonTime,
a.StartTime,
a.EndTime,
a.LessonSortNoInArrange,
a.F_DepartmentId,a.Photo
}).Select(x => new Teach_attendanceEntity
{
ALTIdInArrange = x.Key.ALTIdInArrange,
LessonDate = x.Key.LessonDate.ToDate().ToDateString(),
AcademicYearNo = x.Key.AcademicYearNo,
Semester = x.Key.Semester,
LessonNo = x.Key.LessonNo,
LessonName = x.Key.LessonName,
EmpNoInArrange = x.Key.EmpNoInArrange,
EmpNameInArrange = x.Key.EmpNameInArrange,
ClassroomNo = x.Key.ClassroomNo,
LessonTime = x.Key.LessonTime,
StartTime = x.Key.StartTime,
EndTime = x.Key.EndTime,
LessonSortNoInArrange = x.Key.LessonSortNoInArrange,
F_DepartmentId = x.Key.F_DepartmentId,
Photo=x.Key.Photo,
Group = x.Select(y => new Teach_attendanceEntity()
{
Photo=y.Photo,
ADType = y.ADType,
ClockTime = y.ClockTime,
ClockStatus = y.ClockStatus,
IsFit = y.IsFit,
Img=y.Img
}).ToList()
}).OrderBy(x => x.LessonDate).ThenBy(x => Convert.ToInt32(x.LessonTime.Substring(1)));
//结果列表
var result = new List();
foreach (var item in data)
{
string photo = item.Photo;
//上课时间
var WorkTimeTemp = string.Format("{0} {1}", item.LessonDate, item.StartTime).ToDate();
//下课时间
var CloseTimeTemp = string.Format("{0} {1}", item.LessonDate, item.EndTime).ToDate();
//课程应工作分钟数
var WholeMinutes = (CloseTimeTemp - WorkTimeTemp).TotalMinutes.ToInt();
//上课
var adtype1 = item.Group.FirstOrDefault(x => x.ADType == "1");
//下课
var adtype2 = item.Group.FirstOrDefault(x => x.ADType == "2");
//补充结果集
item.TimePeriod = string.Format("{0}-{1}", item.StartTime, item.EndTime);
item.ADStatusWork = adtype1 != null ? adtype1.ClockStatus : "6";
item.ADStatusClose = adtype2 != null ? adtype2.ClockStatus : "6";
item.ClockTimeWork = adtype1 != null ? adtype1.ClockTime.ToTimeString() : "-";
item.ClockTimeClose = adtype2 != null ? adtype2.ClockTime.ToTimeString() : "-";
item.ChidaoMinutes = adtype1 != null ? adtype1.ClockStatus == "2" ? (adtype1.ClockTime.Value - WorkTimeTemp).TotalMinutes.ToInt() : 0 : 0;
item.ZaoTuiMinutes = adtype2 != null ? adtype2.ClockStatus == "3" ? (CloseTimeTemp - adtype2.ClockTime.Value).TotalMinutes.ToInt() : 0 : 0;
item.ChuQinMinutes = adtype1 != null && adtype2 != null ? (adtype2.ClockTime.Value - adtype1.ClockTime.Value).TotalMinutes.ToInt() : 0;
item.QueQinMinutes = adtype1 != null && adtype2 != null ? 0 : WholeMinutes;
item.RestMinutes = 0;
item.WorkMinutes = WholeMinutes == item.QueQinMinutes ? 0 : WholeMinutes - item.ChidaoMinutes - item.ZaoTuiMinutes - item.QueQinMinutes;
item.Photo = photo;
item.IsFitstr = (adtype1 != null ? adtype1.IsFit.ToString() : "") + "," + (adtype2 != null ? adtype2.IsFit.ToString() : "");
item.Img = (adtype1 != null ? adtype1.Img : "") + "," + (adtype2 != null ? adtype2.Img : "");
result.Add(item);
}
if (!queryParam["ClockStatus"].IsEmpty())
{
if (queryParam["ClockStatus"].ToString() == "0")//旷工
{
result = result.Where(x => x.ADStatusWork == "6" || x.ADStatusClose == "6").ToList();
}
else
{
result = result.Where(x => x.ADStatusWork == queryParam["ClockStatus"].ToString() || x.ADStatusClose == queryParam["ClockStatus"].ToString()).ToList();
}
}
return result;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
public string GetStatisticDetailimg(string teachno)
{
var strSql = new StringBuilder();
strSql.Append("SELECT t.img ");
strSql.Append(" FROM Teach_attendance t ");
strSql.Append(" WHERE 1=1 and empno='"+teachno+"' and isfit=1 and img is not null");
return this.BaseRepository("CollegeMIS").FindObject(strSql.ToString())?.ToString();
}
#endregion
#region 提交数据
///
/// 删除实体数据
/// 主键
///
///
public void DeleteEntity(string keyValue)
{
try
{
this.BaseRepository("CollegeMIS").Delete(t => t.ID == keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存实体数据(新增、修改)
/// 主键
///
///
public void SaveEntity(string keyValue, Teach_attendanceEntity entity)
{
try
{
if (!string.IsNullOrEmpty(keyValue))
{
entity.Modify(keyValue);
this.BaseRepository("CollegeMIS").Update(entity);
}
else
{
entity.Create();
this.BaseRepository("CollegeMIS").Insert(entity);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
}
}