using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using NUglify.Helpers; using Shentun.Peis.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; using Volo.Abp.Identity; namespace Shentun.Peis.InternalReports { /// /// 内部报表 /// [ApiExplorerSettings(GroupName = "Work")] [Authorize] public class InternalReportAppService : ApplicationService { private readonly IRepository _userRepository; private readonly IRepository _patientRegisterRepository; private readonly IRepository _registerAsbitemRepository; private readonly IRepository _registerCheckRepository; private readonly IRepository _asbitemRepository; private readonly IRepository _itemTypeRepository; private readonly IRepository _customerOrgRepository; public InternalReportAppService( IRepository userRepository, IRepository patientRegisterRepository, IRepository registerAsbitemRepository, IRepository registerCheckRepository, IRepository asbitemRepository, IRepository itemTypeRepository, IRepository customerOrgRepository ) { this._userRepository = userRepository; this._patientRegisterRepository = patientRegisterRepository; this._registerAsbitemRepository = registerAsbitemRepository; this._registerCheckRepository = registerCheckRepository; this._asbitemRepository = asbitemRepository; this._itemTypeRepository = itemTypeRepository; this._customerOrgRepository = customerOrgRepository; } /// /// 登记人员工作量统计 /// /// /// [HttpPost("api/app/internalreport/getregistrationpersonnelworkloadreport")] public async Task> GetRegistrationPersonnelWorkLoadReportAsync(GetRegistrationPersonnelWorkLoadReportRequestDto input) { var query = (from a in await _patientRegisterRepository.GetQueryableAsync() join b in await _userRepository.GetQueryableAsync() on a.CreatorId equals b.Id into bb from ab in bb.DefaultIfEmpty() where (a.CreationTime >= Convert.ToDateTime(input.StartDate) && a.CreationTime < Convert.ToDateTime(input.EndDate).AddDays(1)) select new { a, PersonnelName = ab != null ? ab.UserName : "" }); var sumCount = query.Count(); //总数 if (input.UserIds.Count > 0) { query = query.Where(m => input.UserIds.Contains(m.a.CreatorId.Value)); } var entlistdto = query.GroupBy(g => new { g.a.CreatorId, g.PersonnelName }) .Select(s => new GetRegistrationPersonnelWorkLoadReportDto { PersonnelName = s.Key.PersonnelName, RegisterCount = s.Count(), Percentage = Math.Round(Convert.ToDecimal(s.Count() * 100) / sumCount, 2).ToString() }).ToList(); return entlistdto; } /// /// 医生工作量统计 /// /// /// [HttpPost("api/app/internalreport/getdoctorpersonnelworkloadreport")] public async Task> GetDoctorPersonnelWorkLoadReportAsync(GetDoctorPersonnelWorkLoadReportRequestDto input) { var query = (from a in await _registerAsbitemRepository.GetQueryableAsync() join c in await _asbitemRepository.GetQueryableAsync() on a.AsbitemId equals c.Id into cc from ac in cc.DefaultIfEmpty() join d in await _registerCheckRepository.GetQueryableAsync() on a.RegisterCheckId equals d.Id into dd from ad in dd.DefaultIfEmpty() join b in await _userRepository.GetQueryableAsync() on ad.CheckDoctorId equals b.Id.ToString() into bb from ab in bb.DefaultIfEmpty() where (a.CreationTime >= Convert.ToDateTime(input.StartDate) && a.CreationTime < Convert.ToDateTime(input.EndDate).AddDays(1) && ad.CompleteFlag == '1' ) select new { a, CheckDoctorId = ad.CheckDoctorId, DoctorName = ab != null ? ab.UserName : ad.CheckDoctorId, AsbitemName = ac != null ? ac.DisplayName : "" }); if (input.UserIds.Count > 0) { query = query.Where(m => input.UserIds.Contains(m.CheckDoctorId)); } //var ssd = query.ToQueryString(); var entlistdto = query.GroupBy(g => new { g.a.AsbitemId, g.a.CreatorId, }) .Select(s => new GetDoctorPersonnelWorkLoadReportDto { AsbitemName = s.FirstOrDefault().AsbitemName, CheckCount = s.Count(), DoctorName = s.FirstOrDefault().DoctorName, AvgChargePrice = Math.Round(s.Average(v => v.a.ChargePrice.Value), 2), AvgStandardPrice = Math.Round(s.Average(v => v.a.StandardPrice.Value), 2), SumChargePrice = Math.Round(s.Sum(v => v.a.ChargePrice.Value * v.a.Amount.Value), 2), SumStandardPrice = Math.Round(s.Sum(v => v.a.StandardPrice.Value * v.a.Amount.Value), 2) }).ToList(); return entlistdto; } /// /// 总检医生工作量统计 /// /// /// [HttpPost("api/app/internalreport/getsumcheckdoctorworkloadreport")] public async Task> GetSumCheckDoctorWorkLoadReportAsync(GetSumCheckDoctorWorkLoadReportRequestDto input) { var query = (from a in await _patientRegisterRepository.GetQueryableAsync() join b in await _userRepository.GetQueryableAsync() on a.SummaryDoctor equals b.Id.ToString() into bb from ab in bb.DefaultIfEmpty() where (!string.IsNullOrEmpty(a.SummaryDoctor) && a.SummaryDate != null && a.SummaryDate.Value.ToDateTime(TimeOnly.MinValue) >= Convert.ToDateTime(input.StartDate) && a.SummaryDate.Value.ToDateTime(TimeOnly.MinValue) < Convert.ToDateTime(input.EndDate).AddDays(1)) select new { a, SumCheckDoctorName = ab != null ? ab.UserName : "" }); var sumCount = query.Count(); //总数 if (input.UserIds.Count > 0) { query = query.Where(m => input.UserIds.ToString().Contains(m.a.SummaryDoctor)); } var entlistdto = query.GroupBy(g => new { g.a.SummaryDoctor, g.SumCheckDoctorName }) .Select(s => new GetSumCheckDoctorWorkLoadReportDto { SumCheckDoctorName = s.Key.SumCheckDoctorName, SumCheckCount = s.Count(), Percentage = Math.Round(Convert.ToDecimal(s.Count() * 100) / sumCount, 2).ToString() }).ToList(); return entlistdto; } /// /// 审核医生工作量统计 /// /// /// [HttpPost("api/app/internalreport/getauditdoctorworkloadreport")] public async Task> GetAuditDoctorWorkLoadReportAsync(GetAuditDoctorWorkLoadReportRequestDto input) { var query = (from a in await _patientRegisterRepository.GetQueryableAsync() join b in await _userRepository.GetQueryableAsync() on a.SummaryDoctor equals b.Id.ToString() into bb from ab in bb.DefaultIfEmpty() where (!string.IsNullOrEmpty(a.AuditDoctor) && a.AuditDate != null && a.AuditDate.Value.ToDateTime(TimeOnly.MinValue) >= Convert.ToDateTime(input.StartDate) && a.AuditDate.Value.ToDateTime(TimeOnly.MinValue) < Convert.ToDateTime(input.EndDate).AddDays(1)) select new { a, AuditDoctorName = ab != null ? ab.UserName : "" }); var sumCount = query.Count(); //总数 if (input.UserIds.Count > 0) { query = query.Where(m => input.UserIds.ToString().Contains(m.a.AuditDoctor)); } var entlistdto = query.GroupBy(g => new { g.a.AuditDoctor, g.AuditDoctorName }) .Select(s => new GetAuditDoctorWorkLoadReportDto { AuditDoctorName = s.Key.AuditDoctorName, AuditCount = s.Count(), Percentage = Math.Round(Convert.ToDecimal(s.Count() * 100) / sumCount, 2).ToString() }).ToList(); return entlistdto; } /// /// 科室工作量统计 标准格式 只统计RegisterCheck表标记检查了的数据 /// /// /// [HttpPost("api/app/internalreport/getitemtypeworkloadinstandard")] public async Task> GetItemTypeWorkLoadInStandardAsync(GetItemTypeWorkLoadInStandardRequestDto input) { var qeruy = from a in await _registerAsbitemRepository.GetQueryableAsync() join b in await _asbitemRepository.GetQueryableAsync() on a.AsbitemId equals b.Id join c in await _itemTypeRepository.GetQueryableAsync() on b.ItemTypeId equals c.Id join d in await _registerCheckRepository.GetQueryableAsync() on a.RegisterCheckId equals d.Id into dd from ad in dd.DefaultIfEmpty() where (!string.IsNullOrEmpty(ad.CheckDoctorId) && ad.CheckDate != null) select new { a.AsbitemId, a.StandardPrice, a.ChargePrice, a.Amount, AsbitemName = b.DisplayName, ItemTypeId = b.ItemTypeId, ItemTypeName = c.DisplayName, ad.CheckDate }; if (input.ItemTypeId.Any()) { qeruy = qeruy.Where(m => input.ItemTypeId.Contains(m.ItemTypeId)); } if (!string.IsNullOrEmpty(input.StartDate) && !string.IsNullOrEmpty(input.EndDate)) { qeruy = qeruy.Where(m => m.CheckDate.Value.ToDateTime(TimeOnly.MinValue) >= Convert.ToDateTime(input.StartDate) && m.CheckDate.Value.ToDateTime(TimeOnly.MinValue) < Convert.ToDateTime(input.EndDate).AddDays(1)); } var entlist = qeruy.GroupBy(g => new { g.ItemTypeId, g.AsbitemId }) .Select(s => new GetItemTypeWorkLoadInStandardDto { AsbitemName = s.First().AsbitemName, AsbitemCount = s.Count(), ItemTypeName = s.First().ItemTypeName, AvgChargePrice = Math.Round(s.Average(v => v.ChargePrice.Value), 2), AvgStandardPrice = Math.Round(s.Average(v => v.StandardPrice.Value), 2), SumChargePrice = Math.Round(s.Sum(v => v.ChargePrice.Value * v.Amount.Value), 2), SumStandardPrice = Math.Round(s.Sum(v => v.StandardPrice.Value * v.Amount.Value), 2) }).ToList(); return entlist; } /// /// 科室工作量统计 单位和医生 /// /// /// [HttpPost("api/app/internalreport/getitemtypeworkloadincustomeranddoctor")] public async Task> GetItemTypeWorkLoadInCustomerAndDoctorAsync(GetItemTypeWorkLoadInCustomerAndDoctorRequestDto input) { var qeruy = from a in await _registerAsbitemRepository.GetQueryableAsync() join b in await _asbitemRepository.GetQueryableAsync() on a.AsbitemId equals b.Id join c in await _itemTypeRepository.GetQueryableAsync() on b.ItemTypeId equals c.Id join d in await _registerCheckRepository.GetQueryableAsync() on a.RegisterCheckId equals d.Id into dd from ad in dd.DefaultIfEmpty() join e in await _patientRegisterRepository.GetQueryableAsync() on a.PatientRegisterId equals e.Id join f in await _customerOrgRepository.GetQueryableAsync() on e.CustomerOrgId equals f.Id into ff from af in ff.DefaultIfEmpty() join g in await _userRepository.GetQueryableAsync() on ad.CheckDoctorId equals g.Id.ToString() into gg from ag in gg.DefaultIfEmpty() where (!string.IsNullOrEmpty(ad.CheckDoctorId) && ad.CheckDate != null) select new { a.AsbitemId, AsbitemName = b.DisplayName, ItemTypeId = b.ItemTypeId, ItemTypeName = c.DisplayName, CustomerName = af.DisplayName, CustomerOrgId = e.CustomerOrgId, ad.CheckDoctorId, ad.CheckDate, DoctorName = ag != null ? ag.UserName : "" }; if (input.ItemTypeId.Any()) { qeruy = qeruy.Where(m => input.ItemTypeId.Contains(m.ItemTypeId)); } if (!string.IsNullOrEmpty(input.StartDate) && !string.IsNullOrEmpty(input.EndDate)) { qeruy = qeruy.Where(m => m.CheckDate.Value.ToDateTime(TimeOnly.MinValue) >= Convert.ToDateTime(input.StartDate) && m.CheckDate.Value.ToDateTime(TimeOnly.MinValue) < Convert.ToDateTime(input.EndDate).AddDays(1)); } var entlist = qeruy.GroupBy(g => new { g.CustomerOrgId, g.ItemTypeId, g.AsbitemId, g.CheckDoctorId }) .Select(s => new GetItemTypeWorkLoadInCustomerAndDoctorDto { AsbitemName = s.First().AsbitemName, DoctorCheckCount = s.Count(), ItemTypeName = s.First().ItemTypeName, CustomerName = s.First().CustomerName, DoctorName = s.First().DoctorName }).ToList(); return entlist; } /// /// 科室工作量统计 单位和组合项目 /// /// /// [HttpPost("api/app/internalreport/getitemtypeworkloadincustomerandasbitem")] public async Task> GetItemTypeWorkLoadInCustomerAndAsbitemAsync(GetItemTypeWorkLoadInCustomerAndAsbitemRequestDto input) { var qeruy = from a in await _registerAsbitemRepository.GetQueryableAsync() join b in await _asbitemRepository.GetQueryableAsync() on a.AsbitemId equals b.Id join c in await _itemTypeRepository.GetQueryableAsync() on b.ItemTypeId equals c.Id join d in await _registerCheckRepository.GetQueryableAsync() on a.RegisterCheckId equals d.Id into dd from ad in dd.DefaultIfEmpty() join e in await _patientRegisterRepository.GetQueryableAsync() on a.PatientRegisterId equals e.Id join f in await _customerOrgRepository.GetQueryableAsync() on e.CustomerOrgId equals f.Id into ff from af in ff.DefaultIfEmpty() where (!string.IsNullOrEmpty(ad.CheckDoctorId) && ad.CheckDate != null) select new { a.AsbitemId, AsbitemName = b.DisplayName, ItemTypeId = b.ItemTypeId, ItemTypeName = c.DisplayName, CustomerName = af.DisplayName, CustomerOrgId = e.CustomerOrgId, ad.CheckDate }; if (input.ItemTypeId.Any()) { qeruy = qeruy.Where(m => input.ItemTypeId.Contains(m.ItemTypeId)); } if (!string.IsNullOrEmpty(input.StartDate) && !string.IsNullOrEmpty(input.EndDate)) { qeruy = qeruy.Where(m => m.CheckDate.Value.ToDateTime(TimeOnly.MinValue) >= Convert.ToDateTime(input.StartDate) && m.CheckDate.Value.ToDateTime(TimeOnly.MinValue) < Convert.ToDateTime(input.EndDate).AddDays(1)); } var entlist = qeruy.GroupBy(g => new { g.CustomerOrgId, g.ItemTypeId, g.AsbitemId }) .Select(s => new GetItemTypeWorkLoadInCustomerAndAsbitemDto { AsbitemName = s.First().AsbitemName, DoctorCheckCount = s.Count(), ItemTypeName = s.First().ItemTypeName, CustomerName = s.First().CustomerName }).ToList(); return entlist; } } }