using Dapper;
using Learun.Application.Base.AuthorizeModule;
using Learun.Application.Organization;
using Learun.Application.TwoDevelopment.LR_Desktop;
using Learun.DataBase.Repository;
using Learun.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Learun.Application.Base.SystemModule;
namespace Learun.Application.TwoDevelopment.EducationalAdministration
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创 建:超级管理员
/// 日 期:2020-04-16 15:52
/// 描 述:班级自诊打卡
///
public class ThermographyService : RepositoryFactory
{
#region 获取数据
///
/// 获取页面显示列表分页数据
///
/// 分页参数
/// 查询参数
///
public IEnumerable GetPageList(Pagination pagination, string queryJson)
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT t.* ");
strSql.Append(" FROM Thermography 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.MeasureDate >= @startTime AND t.MeasureDate <= @endTime ) ");
}
if (!queryParam["DeptNo"].IsEmpty())
{
dp.Add("DeptNo", queryParam["DeptNo"].ToString(), DbType.String);
strSql.Append(" AND t.DeptNo = @DeptNo ");
}
if (!queryParam["MajorNo"].IsEmpty())
{
dp.Add("MajorNo", queryParam["MajorNo"].ToString(), DbType.String);
strSql.Append(" AND t.MajorNo = @MajorNo ");
}
if (!queryParam["ClassNo"].IsEmpty())
{
dp.Add("ClassNo", queryParam["ClassNo"].ToString(), DbType.String);
strSql.Append(" AND t.ClassNo = @ClassNo ");
}
if (!queryParam["MeasureTime"].IsEmpty())
{
dp.Add("MeasureTime", queryParam["MeasureTime"].ToString(), DbType.String);
strSql.Append(" AND t.MeasureTime = @MeasureTime ");
}
if (!queryParam["PersonBeingMeasured"].IsEmpty())
{
dp.Add("PersonBeingMeasured", queryParam["PersonBeingMeasured"].ToString(), DbType.String);
strSql.Append(" AND t.PersonBeingMeasured = @PersonBeingMeasured ");
}
if (!queryParam["Status"].IsEmpty())
{
dp.Add("Status", queryParam["Status"].ToString(), DbType.String);
strSql.Append(" AND t.Status = @Status ");
}
if (!queryParam["MeasurerID"].IsEmpty())
{
dp.Add("MeasurerID", queryParam["MeasurerID"].ToString(), DbType.String);
strSql.Append(" AND t.MeasurerID = @MeasurerID ");
}
return this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp, pagination);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取页面显示列表数据
///
/// 查询参数
///
public IEnumerable GetList(string queryJson)
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append(@"
t.ID,
t.ClassNo,
t.MeasurerID,
t.PersonBeingMeasured,
t.Status,
t.Temperature,
t.ProcessingResult,
t.Remark,
t.MeasureTime,
t.DeptNo,
t.MajorNo,
t.MeasureDate
");
strSql.Append(" FROM Thermography 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.MeasureDate >= @startTime AND t.MeasureDate <= @endTime ) ");
}
if (!queryParam["ClassNo"].IsEmpty())
{
dp.Add("ClassNo", queryParam["ClassNo"].ToString(), DbType.String);
strSql.Append(" AND t.ClassNo = @ClassNo ");
}
if (!queryParam["PersonBeingMeasured"].IsEmpty())
{
dp.Add("PersonBeingMeasured", queryParam["PersonBeingMeasured"].ToString(), DbType.String);
strSql.Append(" AND t.PersonBeingMeasured = @PersonBeingMeasured ");
}
if (!queryParam["Status"].IsEmpty())
{
dp.Add("Status", queryParam["Status"].ToString(), DbType.String);
strSql.Append(" AND t.Status = @Status ");
}
return this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取Thermography表实体数据
/// 主键
///
///
public ThermographyEntity GetThermographyEntity(string keyValue)
{
try
{
return this.BaseRepository("CollegeMIS").FindEntity(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取Thermography表实体数据
/// 主键
///
///
public ThermographyEntity GetThermographyEntitiesByClass(string classNo, DateTime measureDate, string measureTime)
{
try
{
var nextday = measureDate.AddDays(1).Date;
return this.BaseRepository("CollegeMIS").FindEntity(x => x.ClassNo == classNo && x.MeasureDate >= measureDate && x.MeasureDate < nextday && x.MeasureTime == measureTime);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 开始测温的学生列表
///
/// 分页参数
/// 查询参数
///
public IEnumerable GetPageListOfStudent(string queryJson)
{
try
{
var userInfo = LoginUserInfo.Get();
var strSql = new StringBuilder();
strSql.Append("SELECT t.ClassNo,t.DeptNo,t.MajorNo,t.StuNo as PersonBeingMeasured,t.StuName as PersonBeingMeasuredName,'" + userInfo.account + "' as MeasurerID,'1' as Status,c.ClassName ");
strSql.Append(" FROM StuInfoBasic t left join StuInfoBasic tt on t.ClassNo=tt.ClassNo left join ClassInfo c on t.ClassNo=c.ClassNo ");
strSql.Append(" WHERE 1=1 ");
var queryParam = queryJson.ToJObject();
// 虚拟参数
var dp = new DynamicParameters(new { });
if (!queryParam["MeasurerID"].IsEmpty())
{
dp.Add("MeasurerID", queryParam["MeasurerID"].ToString(), DbType.String);
strSql.Append(" AND tt.StuNo = @MeasurerID ");
}
return this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// APP中开始测温的学生列表
///
/// 分页参数
/// 查询参数
///
public IEnumerable GetPageListOfStudentInApp(string queryJson)
{
try
{
var userInfo = LoginUserInfo.Get();
var strSql = new StringBuilder();
var queryParam = queryJson.ToJObject();
// 虚拟参数
var dp = new DynamicParameters(new { });
var nowDate = DateTime.Now.Date;
var nextDate = DateTime.Now.AddDays(1).Date;
strSql.Append("SELECT t.ClassNo,t.DeptNo,t.MajorNo,t.StuNo as PersonBeingMeasured,t.StuName as PersonBeingMeasuredName,'" + userInfo.account + "' as MeasurerID,'1' as Status,c.ClassName ");
if (!queryParam["MeasureTime"].IsEmpty() && queryParam["MeasureTime"].ToString() != "-1")
{
strSql.Append(" ,g.ID,case when g.Status is null then '1' else g.Status end as Status,g.Temperature,g.ProcessingResult,g.Remark,g.MeasureTime ");
}
strSql.Append(" FROM StuInfoBasic t left join StuInfoBasic tt on t.ClassNo=tt.ClassNo left join ClassInfo c on t.ClassNo=c.ClassNo ");
if (!queryParam["MeasureTime"].IsEmpty() && queryParam["MeasureTime"].ToString() != "-1")
{
strSql.Append(" left join Thermography g on t.StuNo=g.PersonBeingMeasured and t.ClassNo=g.ClassNo and t.DeptNo=g.DeptNo and t.MajorNo=g.DeptNo ");
strSql.Append(" and g.MeasureDate >='" + nowDate + "' and g.MeasureDate<'" + nextDate + "' ");
dp.Add("MeasureTime", queryParam["MeasureTime"].ToString(), DbType.String);
strSql.Append(" and g.MeasureTime=@MeasureTime ");
}
strSql.Append(" WHERE 1=1 ");
if (!queryParam["MeasurerID"].IsEmpty())
{
dp.Add("MeasurerID", queryParam["MeasurerID"].ToString(), DbType.String);
strSql.Append(" AND tt.StuNo = @MeasurerID ");
}
return this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 班级自诊打卡统计
///
/// 查询参数
///
public IEnumerable GetListOfStatistic(string queryJson)
{
try
{
var queryParam = queryJson.ToJObject();
// 虚拟参数
var dp = new DynamicParameters(new { });
var strSql = new StringBuilder();
strSql.Append("select t.DeptNo,CONVERT(varchar(100), t.MeasureDate, 23) as MeasureDate,t.MeasureTime,sum(case when t.Status='0' then 1 else 0 end) as statusNum0,sum(case when t.Status='1' then 1 else 0 end) as statusNum1,sum(case when t.Status='2' then 1 else 0 end) as statusNum2,sum(case when t.Status!='0' then 1 else 0 end) as statusNum12,(select count(s.StuId) from StuInfoBasic s where s.DeptNo=t.DeptNo and s.CheckMark='1') as totalNum ");
strSql.Append(" FROM Thermography t ");
strSql.Append(" WHERE 1=1 ");
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.MeasureDate >= @startTime AND t.MeasureDate <= @endTime ) ");
}
if (!queryParam["DeptNo"].IsEmpty())
{
dp.Add("DeptNo", queryParam["DeptNo"].ToString(), DbType.String);
strSql.Append(" AND t.DeptNo = @DeptNo ");
}
if (!queryParam["MajorNo"].IsEmpty())
{
dp.Add("MajorNo", queryParam["MajorNo"].ToString(), DbType.String);
strSql.Append(" AND t.MajorNo = @MajorNo ");
}
if (!queryParam["ClassNo"].IsEmpty())
{
dp.Add("ClassNo", queryParam["ClassNo"].ToString(), DbType.String);
strSql.Append(" AND t.ClassNo = @ClassNo ");
}
strSql.Append(" group by t.DeptNo,CONVERT(varchar(100), t.MeasureDate, 23),t.MeasureTime ");
return this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#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(UserInfo userInfo, string keyValue, ThermographyEntity entity)
{
try
{
//限制测温时间(上午、中午、晚上)时:判断学生是否已测温,是则更新,否则新增;不限制测温时间时:新增;
if (entity.MeasureTime == "-1")
{
entity.Create(userInfo);
this.BaseRepository("CollegeMIS").Insert(entity);
}
else
{
if (!string.IsNullOrEmpty(keyValue))
{
entity.Modify(keyValue, userInfo);
this.BaseRepository("CollegeMIS").Update(entity);
}
else
{
entity.Create(userInfo);
this.BaseRepository("CollegeMIS").Insert(entity);
}
}
LogEntity logEntity = new LogEntity();
logEntity.F_LogId = Guid.NewGuid().ToString();
logEntity.F_Module = "Thermography";
logEntity.F_ExecuteResultJson = "Thermography Status=" + entity.Status;
logEntity.WriteLog();
//体温异常
if (entity.Status == "2")
{
try
{
var stuModel = this.BaseRepository("CollegeMIS").FindEntity(x => x.StuNo == entity.PersonBeingMeasured);
if (stuModel != null)
{
logEntity = new LogEntity();
logEntity.F_LogId = Guid.NewGuid().ToString();
logEntity.F_Module = "Thermography";
logEntity.F_ExecuteResultJson = "Thermography stuModelStuNo=" + stuModel.StuNo;
logEntity.WriteLog();
var title = string.Format("{0}({1})", stuModel.StuName, stuModel.StuNo);
//读取信息推送管理-班级自诊打卡体温异常学生推送(05)的配置
var informationPushEntity = this.BaseRepository().FindEntity(x => x.PushItem == "05");
if (informationPushEntity != null && informationPushEntity.Status == true)
{
//微信推送
PushWeixin(title);
}
}
}
catch (Exception e)
{
}
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 生成对应时间段的测量体温的学生数据
/// 时间段类型
///
///
public void CreateMorningStudents(string timeType)
{
try
{
//获取当前登录人
var loginUser = LoginUserInfo.Get();
//根据当前登录人获取所属班级
//var classNo = this.BaseRepository("CollegeMIS")
// .FindEntity(a => a.StuNo == loginUser.account)?.ClassNo;
var classNo = "201801301";
if (classNo != null)
{
var latestTheEntity = this.BaseRepository("CollegeMIS")
.FindList(a => a.CreateTime > DateTime.Today && a.MeasureTime == timeType).ToList();
if (latestTheEntity.Count == 0)
{
//根据班号获取所有学生
var students = this.BaseRepository("CollegeMIS")
.FindList(a => a.ClassNo == classNo);
foreach (var student in students)
{
var thermographyEntity = new ThermographyEntity()
{
ClassNo = student.ClassNo,
MeasureTime = timeType,
PersonBeingMeasured = student.StuNo,
CreateTime = DateTime.Now,
MeasurerID = loginUser.account,
MeasureDate = DateTime.Now,
DeptNo = student.DeptNo,
MajorNo = student.MajorNo,
Status = "0"
};
thermographyEntity.Create(loginUser);
this.BaseRepository("CollegeMIS").Insert(thermographyEntity);
}
}
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存实体数据(新增、修改)
/// 主键
///
///
public void SaveEntityList(string measureTime, List entities)
{
var db = this.BaseRepository("CollegeMIS");
db.BeginTrans();
try
{
var userInfo = LoginUserInfo.Get();
var now = DateTime.Now;
var nowDate = DateTime.Now.Date;
var nextDate = DateTime.Now.AddDays(1).Date;
var pushTitle = string.Empty;//微信推送标题
foreach (var entity in entities)
{
entity.MeasurerID = userInfo.account;
entity.MeasureDate = now;
entity.MeasureTime = measureTime;
entity.CreateTime = now;
//限制测温时间(上午、中午、晚上)时:判断学生是否已测温,是则更新,否则新增;不限制测温时间时:新增;
if (measureTime != "-1")
{
var model = db.FindEntity(x => x.PersonBeingMeasured == entity.PersonBeingMeasured && x.MeasureTime == measureTime && x.MeasureDate >= nowDate && x.MeasureDate < nextDate);
if (model != null)
{
db.Delete(model);
}
}
entity.Create(userInfo);
db.Insert(entity);
//体温异常
if (entity.Status == "2")
{
if (string.IsNullOrEmpty(pushTitle))
{
var classModel = db.FindEntity(x => x.ClassNo == entity.ClassNo);
pushTitle = string.Format("{0}班级({1})发现异常学生:{2}({3})", classModel?.ClassName, entity.ClassNo, entity.PersonBeingMeasuredName, entity.PersonBeingMeasured);
}
else
{
pushTitle += string.Format(",{0}({1})", entity.PersonBeingMeasuredName, entity.PersonBeingMeasured);
}
}
}
db.Commit();
if (entities.Any(x => x.Status == "2"))
{
//读取信息推送管理-班级自诊打卡体温异常学生推送(05)的配置
var informationPushEntity = this.BaseRepository().FindEntity(x => x.PushItem == "05");
if (informationPushEntity != null && informationPushEntity.Status == true)
{
try
{
//微信推送
PushWeixin(pushTitle);
}
catch (Exception)
{
throw;
}
}
}
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 微信推送
///
///
public void PushWeixin(string title)
{
var EpidemicControlTeamRoleId = Config.GetValue("EpidemicControlTeamRoleId");
if (!string.IsNullOrEmpty(EpidemicControlTeamRoleId))
{
//疫情防控组成员
var userRelationList = this.BaseRepository().FindList(x => x.F_ObjectId == EpidemicControlTeamRoleId);
//微信配置
var WeChatConfigentity = BaseRepository().FindEntity(m => m.IsEnable == true);
string appid = WeChatConfigentity.APPId;
string secret = WeChatConfigentity.secret;
var wechatemplete = BaseRepository().FindEntity(m => m.WeID == WeChatConfigentity.ID && m.TCode == "task");
string weixintaskurl = wechatemplete.TUrl;
string weixintasktempid = wechatemplete.TempId;
var responsejson = Util.HttpMethods.HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret);
LogEntity logEntity = new LogEntity();
logEntity.F_LogId = Guid.NewGuid().ToString();
logEntity.F_Module = "Thermography";
logEntity.F_ExecuteResultJson = "Thermography responsejson=" + responsejson;
logEntity.WriteLog();
foreach (var userRelationItem in userRelationList)
{
var userEntity = this.BaseRepository().FindEntity(userRelationItem.F_UserId);
if (userEntity != null && !string.IsNullOrEmpty(userEntity.OpenIdForWeixin))
{
//执行推送任务
if (!string.IsNullOrEmpty(appid) && !string.IsNullOrEmpty(secret) && !string.IsNullOrEmpty(weixintaskurl) && !string.IsNullOrEmpty(weixintasktempid))
{
if (!string.IsNullOrEmpty(responsejson))
{
var weixintokenobj = JsonConvert.DeserializeObject(responsejson);
if (string.IsNullOrEmpty(weixintokenobj.errcode))
{
string access_token = weixintokenobj.access_token;
string jsondata = "{\"touser\":\"" + userEntity.OpenIdForWeixin + "\"," +
"\"template_id\":\"" + weixintasktempid + "\"," +
"\"url\":\"" + weixintaskurl + "\"," +
"\"data\":{" +
"\"first\": {\"value\":\"发现体温异常学生\",\"color\":\"#173177\"}," +
"\"keyword1\":{\"value\":\"体温异常学生\",\"color\":\"#173177\"}," +
"\"keyword2\": {\"value\":\"" + title + "\",\"color\":\"#173177\"}," +
"\"keyword3\": {\"value\":\"待查看\",\"color\":\"#173177\"}," +
"\"keyword4\": {\"value\":\"发现体温异常学生【" + title + "】\",\"color\":\"#173177\"}" +
"}" +
"}";
string pushresult = Util.HttpMethods.HttpPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token, jsondata);
logEntity = new LogEntity();
logEntity.F_LogId = Guid.NewGuid().ToString();
logEntity.F_Module = "Thermography";
logEntity.F_ExecuteResultJson = "Thermography pushresult=" + pushresult;
logEntity.WriteLog();
}
}
}
}
}
}
}
#endregion
}
}