StableVersion4.3/HL_FristAidPlatform_DataBase/Service/T_Service_EmergencyStatisti...

363 lines
16 KiB
C#

using HL_FristAidPlatform_Help;
using HL_FristAidPlatform_IDataBase;
using HL_FristAidPlatform_Models;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HL_FristAidPlatform_DataBase
{
public class T_Service_EmergencyStatisticsDB : BaseDB, IT_Service_EmergencyStatistics
{
public SqlSugarClient db = GetClient();
/// <summary>
/// 分诊统计
/// </summary>
/// <param name="hospitalGuid"></param>
/// <param name="starTime"></param>
/// <param name="endTime"></param>
/// <param name="type">1.分诊人数统计 2 三无人员统计 3 死亡人数统计</param>
/// <returns></returns>
public List<DataSetModel> GetTriageStatistics(string hospitalGuid, string starTime, string endTime, int type)
{
List<DataSetModel> list = new List<DataSetModel>();
if (!string.IsNullOrEmpty(starTime))
starTime = starTime + "-01";
if (!string.IsNullOrEmpty(endTime))
endTime = endTime + "-01";
var oneClass = db.Queryable<T_Service_Patient, T_Service_FirstAid_PatientInfo, T_Service_FirstAid_AssistantExamination>
((a, b, c) => new JoinQueryInfos(JoinType.Inner, a.GUID == b.PatientGUID, JoinType.Left, a.GUID == c.PatientGUID))
.Where((a, b) => a.HospitalGuid == hospitalGuid && a.DeleteFlag == 0)
.WhereIF(!SqlFunc.IsNullOrEmpty(starTime), (a, b) => a.CreationDate >= Convert.ToDateTime(starTime))
.WhereIF(!SqlFunc.IsNullOrEmpty(endTime), (a, b) => a.CreationDate <= Convert.ToDateTime(endTime).AddMonths(1))
.WhereIF(type == 1, (a, b) => !SqlFunc.IsNullOrEmpty(b.TriageDepartment))
.WhereIF(type == 2, (a, b) => a.Category == 1)
.WhereIF(type == 3, (a, b, c) => !SqlFunc.IsNullOrEmpty(c.DeathTime))
.Select((a, b, c) => new
{
b.PatientGUID,
a.RegisterTime,
c.DeathTime
}).ToList();
DateTime dt1;
DateTime dt2;
if (string.IsNullOrEmpty(starTime))
{
dt2 = Convert.ToDateTime(oneClass.Max(t => t.RegisterTime));
}
else
{
dt2 = Convert.ToDateTime(starTime);
}
if (string.IsNullOrEmpty(endTime))
{
dt1 = Convert.ToDateTime(oneClass.Min(t => t.RegisterTime));
}
else
{
dt1 = Convert.ToDateTime(endTime);
}
List<DateTime> monthList = new List<DateTime>();
int Month = (dt1.Year - dt2.Year) * 12 + (dt1.Month - dt2.Month);
for (int i = 0; i < Month + 1; i++)
{
string res = dt2.AddMonths(i).Year + "-" + GetDateZero(dt2.AddMonths(i).Month) + "-01";
DateTime dt = Convert.ToDateTime(res);
monthList.Add(dt.AddDays(1 - dt.Day));
}
if (oneClass != null && oneClass.Count > 0)
{
if (monthList.Count > 0)
{
DataSetModel dataSet = null;
for (int i = 0; i < monthList.Count; i++)
{
dataSet = new DataSetModel();
dataSet.TimeDate = monthList[i].ToString("yyyy年MM月");
if (type == 3)
{
dataSet.Number = oneClass.Where(j => j.DeathTime >= SqlFunc.ToDate(monthList[i]) && j.DeathTime <= SqlFunc.ToDate(monthList[i].AddMonths(1))).Count();
}
else
{
dataSet.Number = oneClass.Where(j => j.RegisterTime >= SqlFunc.ToDate(monthList[i]) && j.RegisterTime <= SqlFunc.ToDate(monthList[i].AddMonths(1))).Count();
}
list.Add(dataSet);
}
}
}
else
{
if (monthList.Count > 0)
{
DataSetModel dataSet = null;
for (int i = 0; i < monthList.Count; i++)
{
dataSet = new DataSetModel();
dataSet.TimeDate = monthList[i].ToString("yyyy年MM月");
list.Add(dataSet);
}
}
}
return list;
}
/// <summary>
/// 分诊人数总数统计
/// </summary>
/// <param name="hospitalGuid"></param>
/// <param name="starTime"></param>
/// <param name="endTime"></param>
/// <returns></returns>
public List<QuantityModel> GetQuantityModel(string hospitalGuid, string starTime, string endTime)
{
if (!string.IsNullOrEmpty(starTime))
starTime = starTime + "-01";
if (!string.IsNullOrEmpty(endTime))
endTime = endTime + "-01";
var oneClass = db.Queryable<T_Service_Patient, T_Service_FirstAid_PatientInfo, T_Service_FirstAid_AssistantExamination>
((a, b, c) => new JoinQueryInfos(JoinType.Inner, a.GUID == b.PatientGUID, JoinType.Left, a.GUID == c.PatientGUID))
.Where((a, b) => a.HospitalGuid == hospitalGuid && a.DeleteFlag == 0)
.WhereIF(!SqlFunc.IsNullOrEmpty(starTime), (a, b) => a.CreationDate >= Convert.ToDateTime(starTime))
.WhereIF(!SqlFunc.IsNullOrEmpty(endTime), (a, b) => a.CreationDate <= Convert.ToDateTime(endTime).AddMonths(1))
.Select((a, b, c) => new
{
b.TriageDepartment,
c.DeathTime,
a.Category
}).ToList();
List<QuantityModel> list = new List<QuantityModel>();
QuantityModel quantity1 = new QuantityModel();
quantity1.Name = "分诊总人数";
quantity1.Quantity = oneClass.Where(i => !SqlFunc.IsNullOrEmpty(i.TriageDepartment)).Count();
list.Add(quantity1);
QuantityModel quantity2 = new QuantityModel();
quantity2.Name = "三无总人数";
quantity2.Quantity = oneClass.Where(i => i.Category == 1).Count();
list.Add(quantity2);
QuantityModel quantity3 = new QuantityModel();
quantity3.Name = "死亡总人数";
quantity3.Quantity = oneClass.Where(i => !SqlFunc.IsNullOrEmpty(i.DeathTime)).Count();
list.Add(quantity3);
return list;
}
/// <summary>
/// 计算月份
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public string GetDateZero(int date)
{
if (date < 10)
{
return "0" + date;
}
else
{
return date.ToString();
}
}
/// <summary>
/// 急诊分诊统计 三无人员占比统计 病情等级统计 死亡人数统计
/// </summary>
/// <param name="starTime"></param>
/// <param name="endTime"></param>
/// <returns></returns>
public DataSet GetTriageProportion(string starTime, string endTime)
{
DataSet dataSet = new DataSet();
DataTable dtCopy = new DataTable();
if (!string.IsNullOrEmpty(starTime) && !string.IsNullOrEmpty(endTime))
{
DateTime dateTime = Convert.ToDateTime(starTime);
DateTime dateTime1 = Convert.ToDateTime(endTime);
string sql = @"select CONVERT(varchar(100), a.RegisterTime, 23) as 日期, count(a.GUID) as 人数 from T_Service_Patient a join T_Service_FirstAid_PatientInfo b on a.GUID=b.PatientGUID where a.DeleteFlag=0 and b.DeleteFlag=0 and a.RegisterTime Between '{0}' and '{1}' group by CONVERT(varchar(100), a.RegisterTime, 23)";
sql = string.Format(sql, dateTime, dateTime1);
DataTable dt = db.Ado.GetDataTable(sql);
dtCopy = dt.Copy();
dtCopy.TableName = "table1";
}
//string items = "";
string sql1 = @"select *, t.人数*100/t.急诊分诊总人数 as 占比 from(select LEFT (CONVERT(varchar,a.RegisterTime,112),6) as 日期, count(a.GUID) as 人数,(select count(a.GUID) as 人数 from T_Service_Patient a join T_Service_FirstAid_PatientInfo b on a.GUID=b.PatientGUID where a.DeleteFlag=0 and b.DeleteFlag=0) as 急诊分诊总人数 from T_Service_Patient a join T_Service_FirstAid_PatientInfo b on a.GUID=b.PatientGUID where a.Category=1 and a.DeleteFlag=0 and b.DeleteFlag=0 group by LEFT (CONVERT(varchar,a.RegisterTime,112),6),a.Category) as t";
DataTable dt1Copy = new DataTable();
DataTable dt1 = db.Ado.GetDataTable(sql1);
dt1Copy = dt1.Copy();
dt1Copy.TableName = "table2";
string sql2 = @"select b.Killip,count(a.GUID) as 人数 from T_Service_Patient a join T_Service_FirstAid_PatientInfo b on a.GUID=b.PatientGUID where b.Killip is not null and a.DeleteFlag=0 and b.DeleteFlag=0 group by b.Killip";
DataTable dt2Copy = new DataTable();
DataTable dt2 = db.Ado.GetDataTable(sql2);
dt2Copy = dt2.Copy();
dt2Copy.TableName = "table3";
string sql3 = @"select LEFT (CONVERT(varchar,a.RegisterTime,112),6) as 日期, count(a.GUID) as 人数 from T_Service_Patient a join T_Service_FirstAid_PatientInfo b on a.GUID=b.PatientGUID where b.DeleteFlag=0 and a.DeleteFlag=0 and b.DeathTime is not null group by LEFT (CONVERT(varchar,a.RegisterTime,112),6)";
DataTable dt3Copy = new DataTable();
DataTable dt3 = db.Ado.GetDataTable(sql3);
dt3Copy = dt3.Copy();
dt3Copy.TableName = "table4";
//if (dt1.Rows.Count > 0)
//{
// foreach (DataRow item in dt1.Rows)
// {
// string row = "";
// for (int i = 0; i < dt1.Columns.Count; i++)
// {
// row += JsonConvert.SerializeObject(dt1.Columns[i].ColumnName) + ":";
// row += JsonConvert.SerializeObject(item[i]) + ",";
// }
// row = row.Remove(row.Length - 1);
// items += "{" + row + "}" + ",";
// }
// items = items.Remove(items.Length - 1);
//}
//items = "[" + items + "]";
if (dtCopy != null && dtCopy.Rows.Count != 0)
{
dataSet.Tables.Add(dtCopy);
}
if (dt1Copy != null && dt1Copy.Rows.Count != 0)
{
dataSet.Tables.Add(dt1Copy);
}
if (dt2Copy != null && dt2Copy.Rows.Count != 0)
{
dataSet.Tables.Add(dt2Copy);
}
if (dt3Copy != null && dt3Copy.Rows.Count != 0)
{
dataSet.Tables.Add(dt3Copy);
}
return dataSet;
}
/// <summary>
/// 病因例数统计
/// </summary>
/// <param name="hospitalGuid"></param>
/// <param name="starTime"></param>
/// <param name="endTime"></param>
/// <returns></returns>
public List<EtiologicalModel> GetEtiologicalStatistics(string hospitalGuid, string starTime, string endTime)
{
if (!string.IsNullOrEmpty(starTime))
starTime = starTime + "-01";
if (!string.IsNullOrEmpty(endTime))
endTime = endTime + "-01";
var oneClass = db.Queryable<T_Service_Patient, T_Service_FirstAid_PatientInfo>
((a, b) => new JoinQueryInfos(JoinType.Inner, a.GUID == b.PatientGUID))
.Where((a, b) => a.HospitalGuid == hospitalGuid && a.DeleteFlag == 0)
.WhereIF(!SqlFunc.IsNullOrEmpty(starTime), (a, b) => a.CreationDate >= Convert.ToDateTime(starTime))
.WhereIF(!SqlFunc.IsNullOrEmpty(endTime), (a, b) => a.CreationDate <= Convert.ToDateTime(endTime).AddMonths(1))
.Select((a, b) => new
{
a.RegisterTime,
b.PathogenyGUID
}).ToList();
List<EtiologicalModel> list = new List<EtiologicalModel>();
var pathogenyList = db.Queryable<T_Base_Pathogeny>().ToList();
if (pathogenyList != null && pathogenyList.Count > 0)
{
for (int i = 0; i < pathogenyList.Count; i++)
{
EtiologicalModel quantity = new EtiologicalModel();
quantity.Name = pathogenyList[i].Name;
quantity.PathogenyID = pathogenyList[i].ID;
quantity.Quantity = oneClass.Where(j => j.PathogenyGUID == pathogenyList[i].GUID).Count();
list.Add(quantity);
}
}
return list;
}
public List<PathogenyModel> GetPathogenyStatistics(string hospitalGuid,int pathogenyID, string startTime,string endTime)
{
List<PathogenyModel> list = new List<PathogenyModel>();
startTime = startTime + "-01";
endTime = endTime + "-01";
int Month = (Convert.ToDateTime(endTime).Year - Convert.ToDateTime(startTime).Year) * 12 + (Convert.ToDateTime(endTime).Month - Convert.ToDateTime(startTime).Month);
List<DateTime> monthList = new List<DateTime>();
for (int i = 0; i < Month + 1; i++)
{
string res = Convert.ToDateTime(startTime).AddMonths(i).Year + "-" + GetMonthZero(Convert.ToDateTime(startTime).AddMonths(i).Month) + "-01";
DateTime dt = Convert.ToDateTime(res);
monthList.Add(dt.AddDays(1 - dt.Day));
}
var oneClass = db.Queryable<T_Service_Patient, T_Service_FirstAid_PatientInfo, T_Base_Pathogeny>((a, b, c) => new JoinQueryInfos(
JoinType.Inner, a.GUID == b.PatientGUID,
JoinType.Inner, b.PathogenyGUID == c.GUID))
.Where((a, b, c) => a.HospitalGuid == hospitalGuid && a.DeleteFlag == 0)
//.Where((a, b, c) => a.CreationDate >= SqlFunc.ToDate(startTime) && a.CreationDate <= SqlFunc.ToDate(endTime))
.WhereIF(!SqlFunc.IsNullOrEmpty(startTime), (a, b, c) => a.CreationDate >= Convert.ToDateTime(startTime))
.WhereIF(!SqlFunc.IsNullOrEmpty(endTime), (a, b, c) => a.CreationDate <= Convert.ToDateTime(endTime).AddMonths(1))
.Select((a,b,c)=>new
{
CreationDate=a.CreationDate,
PathogenyGUID =b.PathogenyGUID,
PathogenyID =c.ID
})
.ToList();
if (oneClass != null)
{
for (int i = 0; i < monthList.Count; i++)
{
PathogenyModel model = new PathogenyModel();
model.Month = monthList[i].ToString("yyyy-MM");
int count = 0;
count = oneClass.Where(j => j.CreationDate >= SqlFunc.ToDate(monthList[i].ToString()) && j.CreationDate <= Convert.ToDateTime(SqlFunc.ToDate(monthList[i].ToString()).AddMonths(1).ToString("yyyy-MM-01")))
.Where((j) => j.PathogenyID == pathogenyID).Count();
model.Count = count + "";
list.Add(model);
}
}
return list;
}
/// <summary>
/// 计算月份
/// </summary>
/// <param name="month"></param>
/// <returns></returns>
private string GetMonthZero(int month)
{
if (month < 10)
{
return "0" + month;
}
else
{
return month.ToString();
}
}
}
}