BiSnapshotApplication.cs 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. using Hotline.Application.Snapshot.Contracts;
  2. using Hotline.Configurations;
  3. using Hotline.Orders;
  4. using Hotline.Settings;
  5. using Hotline.Settings.Hotspots;
  6. using Hotline.Share.Attributes;
  7. using Hotline.Share.Dtos.Snapshot;
  8. using Hotline.Share.Enums.CallCenter;
  9. using Hotline.Share.Enums.FlowEngine;
  10. using Hotline.Share.Enums.Order;
  11. using Hotline.Share.Enums.Snapshot;
  12. using Hotline.Share.Tools;
  13. using Hotline.Snapshot;
  14. using Hotline.Snapshot.IRepository;
  15. using Hotline.Tools;
  16. using Mapster;
  17. using Microsoft.AspNetCore.Mvc;
  18. using Microsoft.Extensions.Options;
  19. using SqlSugar;
  20. using XF.Domain.Authentications;
  21. using XF.Domain.Dependency;
  22. using XF.Domain.Exceptions;
  23. using XF.Domain.Repository;
  24. using static NPOI.SS.Format.CellNumberFormatter;
  25. namespace Hotline.Application.Snapshot;
  26. public class BiSnapshotApplication : IBiSnapshotApplication, IScopeDependency
  27. {
  28. private readonly IOrderSnapshotRepository _orderSnapshotRepository;
  29. private readonly IRedPackRecordRepository _redPackRecordRepository;
  30. private readonly IIndustryRepository _industryRepository;
  31. private readonly IIndustryCaseRepository _industryCaseRepository;
  32. private readonly IRedPackAuditRepository _redPackAuditRepository;
  33. private readonly IRepository<Hotspot> _hotspotTypeRepository;
  34. private readonly ISessionContext _sessionContext;
  35. private readonly IOrderRepository _orderRepository;
  36. private readonly ICommunityInfoRepository _communityInfoRepository;
  37. private readonly IRepository<SystemArea> _systemAreaRepository;
  38. private readonly IOptionsSnapshot<AppConfiguration> _appOptions;
  39. private readonly ISnapshotPointsRecordRepository _snapshotPointsRecordRepository;
  40. public BiSnapshotApplication(IOrderSnapshotRepository orderSnapshotRepository, IRedPackRecordRepository redPackRecordRepository, IIndustryRepository industryRepository, IIndustryCaseRepository industryCaseRepository, IRedPackAuditRepository redPackAuditRepository, IRepository<Hotspot> hotspotTypeRepository, ISessionContext sessionContext, IOrderRepository orderRepository, ICommunityInfoRepository communityInfoRepository, IRepository<SystemArea> systemAreaRepository, IOptionsSnapshot<AppConfiguration> appOptions, ISnapshotPointsRecordRepository snapshotPointsRecordRepository)
  41. {
  42. _orderSnapshotRepository = orderSnapshotRepository;
  43. _redPackRecordRepository = redPackRecordRepository;
  44. _industryRepository = industryRepository;
  45. _industryCaseRepository = industryCaseRepository;
  46. _redPackAuditRepository = redPackAuditRepository;
  47. _hotspotTypeRepository = hotspotTypeRepository;
  48. _sessionContext = sessionContext;
  49. _orderRepository = orderRepository;
  50. _communityInfoRepository = communityInfoRepository;
  51. _systemAreaRepository = systemAreaRepository;
  52. _appOptions = appOptions;
  53. _snapshotPointsRecordRepository = snapshotPointsRecordRepository;
  54. }
  55. public ISugarQueryable<HotspotStatisticsOutDto> GetHotspotStatistics(HotspotStatisticsInDto dto)
  56. {
  57. var IsCenter = _sessionContext.OrgIsCenter;
  58. string count = "2";
  59. string countx = string.Empty;
  60. if (dto.HotspotCode.NotNullOrEmpty())
  61. {
  62. count = (dto.HotspotCode.Length + 2).ToString();
  63. countx = dto.HotspotCode.Length.ToString();
  64. }
  65. return _hotspotTypeRepository.Queryable()
  66. .LeftJoin<Order>((it, o) => it.Id == o.HotspotId)
  67. .LeftJoin<OrderSnapshot>((it, o, s) => o.Id == s.Id)
  68. .Where((it, o, s) => o.CreationTime >= dto.StartTime && o.CreationTime <= dto.EndTime && s.Id != null)
  69. .WhereIF(dto.HotspotCode.IsNullOrEmpty(), (it, o) => o.Id != null)
  70. .WhereIF(dto.HotspotCode.NotNullOrEmpty(), (it, o) => it.ParentId.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>(countx)) == dto.HotspotCode)
  71. .WhereIF(IsCenter == false, (it, o) => o.ActualHandleOrgCode.StartsWith(_sessionContext.RequiredOrgId))
  72. .GroupBy((it, o) => new { Id = it.Id.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>(count)) })
  73. .Select((it, o) => new
  74. {
  75. HotspotCode = it.Id.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>(count)),
  76. SumCount = SqlFunc.AggregateCount(it.HotSpotName)
  77. })
  78. .MergeTable()
  79. .LeftJoin<Hotspot>((x, q) => x.HotspotCode == q.Id)
  80. .Select((x, q) => new HotspotStatisticsOutDto
  81. {
  82. HotspotCode = x.HotspotCode,
  83. SumCount = x.SumCount,
  84. HotspotName = q.HotSpotName,
  85. HasChild = SqlFunc.Subqueryable<Hotspot>().Where(d => d.ParentId == x.HotspotCode).Any()
  86. });
  87. }
  88. /// <summary>
  89. /// 热点类型小类统计明细
  90. /// </summary>
  91. /// <param name="dto"></param>
  92. /// <returns></returns>
  93. public ISugarQueryable<HotspotStatisticsDetailsOutDto> HotspotStatisticsDetail([FromQuery] HotspotStatisticsDetailsInDto dto)
  94. {
  95. var IsCenter = _sessionContext.OrgIsCenter;
  96. IsCenter = true;
  97. var query = _orderRepository.Queryable()
  98. .Includes(d => d.OrderVisits)
  99. .Where(d => d.CreationTime >= dto.StartTime && d.CreationTime <= dto.EndTime)
  100. .WhereIF(dto.HotspotCode.NotNullOrEmpty(), d => d.HotspotId.StartsWith(dto.HotspotCode))
  101. .WhereIF(IsCenter == false, d => d.ActualHandleOrgCode.StartsWith(_sessionContext.RequiredOrgId))
  102. .Select(d => new HotspotStatisticsDetailsOutDto
  103. {
  104. OrderScreenStatus = SqlFunc.Subqueryable<OrderScreen>().Where(q => q.OrderId == d.Id).OrderByDesc(q => q.CreationTime).Select(q => q.Status), //x.OrderScreens.FirstOrDefault().Status,
  105. }, true);
  106. return query;
  107. }
  108. /// <summary>
  109. /// 市民红包审批统计
  110. /// </summary>
  111. /// <param name="dto"></param>
  112. /// <param name="requestAborted"></param>
  113. /// <returns></returns>
  114. /// <exception cref="NotImplementedException"></exception>
  115. [ExportExcel("市民红包审核统计")]
  116. public IList<RedPackStatisticsOutDto> GetRedPackAuditStatistics(RedPackStatisticsInDto dto)
  117. {
  118. var industries = _industryRepository.Queryable(includeDeleted: true)
  119. .LeftJoin<IndustryCase>((industry, industryCase) => industry.Id == industryCase.IndustryId && industryCase.IsEnable == true)
  120. .Select((industry, industryCase) => new RedPackStatisticsOutDto
  121. {
  122. Id = industry.Id,
  123. Name = industry.Name,
  124. CaseId = industryCase.Id,
  125. CaseName = industryCase.Name,
  126. ShouldAmount = industryCase.CitizenReadPackAmount == null ? industry.CitizenReadPackAmount : industryCase.CitizenReadPackAmount,
  127. }).ToList();
  128. var query = _redPackAuditRepository.Queryable(includeDeleted: true)
  129. .LeftJoin<OrderSnapshot>((audit, snapshot) => audit.OrderId == snapshot.Id)
  130. .LeftJoin<RedPackRecord>((audit, snapshot, record) => record.RedPackAuditId == audit.Id)
  131. .LeftJoin<SupplementRecord>((audit, snapshot, record, supplement) => supplement.OrderId == snapshot.Id)
  132. .Where((audit, snapshot) => audit.CreationTime >= dto.StartTime && audit.CreationTime <= dto.EndTime)
  133. .GroupBy((audit, snapshot) => new { snapshot.IndustryCase, snapshot.IndustryId, snapshot.IndustryName })
  134. .Select((audit, snapshot, record, supplement) => new RedPackStatisticsOutDto
  135. {
  136. Id = snapshot.IndustryId,
  137. Name = snapshot.IndustryName,
  138. CaseId = snapshot.IndustryCase,
  139. ApprovalAmount = SqlFunc.AggregateSum(SqlFunc.IIF(audit.Status == ERedPackAuditStatus.Agree, audit.ApprovedAmount, 0)), //审批同意总金额
  140. ApprovalCount = SqlFunc.AggregateSum(SqlFunc.IIF(audit.Status == ERedPackAuditStatus.Agree, 1, 0)), // 审批同意总个数
  141. SentAmount = SqlFunc.AggregateSum(SqlFunc.IIF(audit.IsSend == true, audit.AcutalAmount, 0)), // 发送成功金额
  142. SentCount = SqlFunc.AggregateSum(SqlFunc.IIF(audit.IsSend == true, 1, 0)), // 发送成功个数
  143. SendFailAmount = SqlFunc.AggregateSum(SqlFunc.IIF(record.DistributionState == EReadPackSendStatus.Fail, record.Amount, 0)), //发送失败金额
  144. SendFailCount = SqlFunc.AggregateSum(SqlFunc.IIF(record.DistributionState == EReadPackSendStatus.Fail, 1, 0)), // 发送失败个数
  145. PendingAmount = SqlFunc.AggregateSum(SqlFunc.IIF(record.DistributionState == EReadPackSendStatus.Unsend, audit.ApprovedAmount, 0)), // 待发金额
  146. PendingCount = SqlFunc.AggregateSum(SqlFunc.IIF(record.DistributionState == EReadPackSendStatus.Unsend, 1, 0)), // 待发个数
  147. SupplementAmount = SqlFunc.AggregateSum(supplement.ReplenishAmount), // 补充红包金额
  148. SupplementCount = SqlFunc.AggregateCount(supplement.Id), // 补充红包数
  149. });
  150. #if DEBUG
  151. var sql = query.ToSqlString();
  152. #endif
  153. var redPackOutDto = query.ToList();
  154. foreach (var industry in industries)
  155. {
  156. var item = redPackOutDto
  157. .WhereIF(industry.CaseId != null, m => m.CaseId == industry.CaseId)
  158. .WhereIF(industry.CaseId == null, m => m.Id == industry.Id)
  159. .FirstOrDefault();
  160. var config = new TypeAdapterConfig();
  161. config.ForType<RedPackStatisticsOutDto, RedPackStatisticsOutDto>()
  162. .Ignore(dest => dest.CaseId)
  163. .Ignore(dest => dest.CaseName)
  164. .Ignore(dest => dest.ShouldAmount);
  165. item?.Adapt(industry, config);
  166. if (industry.CaseId == null)
  167. {
  168. industry.IndustryName = $"{industry.Name}({industry.ShouldAmount?.ToString("f2")})";
  169. industry.IndustryType = 1;
  170. industry.IndustryId = industry.Id;
  171. }
  172. else
  173. {
  174. industry.IndustryType = 2;
  175. industry.IndustryId = industry.CaseId;
  176. if (industry.CaseName == industry.Name)
  177. industry.IndustryName = $"{industry.Name}({industry.ShouldAmount?.ToString("f2")})";
  178. else
  179. {
  180. industry.IndustryName = $"{industry.Name}-{industry.CaseName}({industry.ShouldAmount?.ToString("f2")})";
  181. }
  182. }
  183. }
  184. return industries;
  185. }
  186. public ISugarQueryable<RedPackStatisticsDetailsOutDto> GetRedPackAuditStatisticsDetails(RedPackStatisticsDetailsInDto dto)
  187. {
  188. dto.ValidateObject();
  189. dto.FieldName = dto.FieldName.ToLower();
  190. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  191. .LeftJoin<Order>((snapshot, order) => order.Id == snapshot.Id)
  192. .LeftJoin<RedPackAudit>((snapshot, order, audit) => snapshot.Id == audit.OrderId)
  193. .LeftJoin<RedPackRecord>((snapshot, order, audit, record) => audit.Id == record.RedPackAuditId)
  194. .LeftJoin<SupplementRecord>((snapshot, order, audit, record, supplement) => audit.Id == supplement.RedPackAuditId)
  195. .Where((snapshot, order, audit) => audit.CreationTime >= dto.StartTime && audit.CreationTime <= dto.EndTime)
  196. .WhereIF(dto.IndustryType == 1, (snapshot, order, audit) => snapshot.IndustryId == dto.IndustryId)
  197. .WhereIF(dto.IndustryType == 2, (snapshot, order, audit) => snapshot.IndustryCase == dto.IndustryId);
  198. query = dto.FieldName switch
  199. {
  200. "approvalamount" => query.Where((snapshot, order, audit) => audit.Status == ERedPackAuditStatus.Agree),
  201. "approvalcount" => query.Where((snapshot, order, audit) => audit.Status == ERedPackAuditStatus.Agree),
  202. "sentamount" => query.Where((snapshot, order, audit) => audit.IsSend == true),
  203. "sentcount" => query.Where((snapshot, order, audit) => audit.IsSend == true),
  204. "sendfailamount" => query.Where((snapshot, order, audit, record) => record.DistributionState == EReadPackSendStatus.Fail),
  205. "sendfailcount" => query.Where((snapshot, order, audit, record) => record.DistributionState == EReadPackSendStatus.Fail),
  206. "pendingamount" => query.Where((snapshot, order, audit, record) => record.DistributionState == EReadPackSendStatus.Unsend),
  207. "pendingcount" => query.Where((snapshot, order, audit, record) => record.DistributionState == EReadPackSendStatus.Unsend),
  208. "supplementamount" => query.Where((snapshot, order, audit, record, supplement) => supplement.Id != null),
  209. "supplementcount" => query.Where((snapshot, order, audit, record, supplement) => supplement.Id != null),
  210. _ => throw new UserFriendlyException($"不支持输入字段: {dto.FieldName}")
  211. };
  212. return query.Select((snapshot, order, audit, record, supplement) => new RedPackStatisticsDetailsOutDto
  213. {
  214. CreationTime = order.CreationTime
  215. }, true);
  216. }
  217. public async Task<SnapshotStatisticsOutDto> GetSnapshotStatisticsAsync(SnapshotStatisticsInDto dto, CancellationToken token)
  218. {
  219. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  220. .LeftJoin<Order>((snapshot, order) => order.Id == snapshot.Id)
  221. .LeftJoin<RedPackAudit>((snapshot, order, redPackAudit) => snapshot.Id == redPackAudit.OrderId)
  222. .LeftJoin<SpecialRedPackAudit>((snapshot, order, redPackAudit, special) => snapshot.Id == special.OrderId)
  223. .LeftJoin<RedPackGuiderAudit>((snapshot, order, redPackAudit, special, guiderAudit) => snapshot.Id == guiderAudit.OrderId)
  224. .LeftJoin<RedPackRecord>((snapshot, order, redPackAudit, special, guiderAudit, record) => redPackAudit.Id == record.RedPackAuditId)
  225. .LeftJoin<OrderSecondaryHandling>((snapshot, order, redPackAudit, special, guiderAudit, record, second) => snapshot.Id == second.OrderId)
  226. .LeftJoin<OrderSpecial>((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial) => snapshot.Id == orderSpecial.OrderId)
  227. .LeftJoin<SystemOrganize>((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => order.ActualHandleOrgCode == organize.Id)
  228. .Where((snapshot) => snapshot.CreationTime >= dto.StartTime && snapshot.CreationTime <= dto.EndTime)
  229. .Select((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => new SnapshotStatisticsOutDto
  230. {
  231. WZSLFWNJS = SqlFunc.AggregateSum(SqlFunc.IIF(order.HotspotId.StartsWith("18"), 1, 0)), // 未在受理范围内件数,
  232. SSPZ12345JS = SqlFunc.AggregateSum(SqlFunc.IIF(order.SourceChannelCode == "SJP12345", 1, 0)), // 随手拍转12345件数,
  233. SLFWNZJS = SqlFunc.AggregateSum(SqlFunc.IIF(order.HotspotId.StartsWith("18") == false && order.SourceChannelCode == "SJP12345", 1, 0)), // 受理范围内总件数,
  234. SLFWNPGWGYSXSNHFJS = SqlFunc.AggregateSum(SqlFunc.IIF(snapshot.IsGuidSystemCallBack == true && snapshot.GuidSystemCallBackTime.Value.AddHours(-4) > snapshot.SendGuidSystemTime, 1, 0)), // 受理范围内派给网格员四小时内回复件数,
  235. SLFWNPGWGYCGSXSHFJS = SqlFunc.AggregateSum(SqlFunc.IIF(snapshot.IsGuidSystemCallBack == true && snapshot.GuidSystemCallBackTime.Value.AddHours(-4) <= snapshot.SendGuidSystemTime, 1, 0)), // 受理范围内派给网格员超过四小时回复件数
  236. SLFWNPGWGYWHFJS = SqlFunc.AggregateSum(SqlFunc.IIF(snapshot.IsGuidSystemCallBack == false, 1, 0)), // 受理范围内派给网格员未回复件数
  237. SLFWNA12345ZPGGBMJS = SqlFunc.AggregateSum(SqlFunc.IIF(organize.OrgType == EOrgType.City && snapshot.SendGuidSystemTime == null, 1, 0)), //受理范围内按12345直派给各部门件数,
  238. SLFWNA12345ZPGGQXJS = SqlFunc.AggregateSum(SqlFunc.IIF(organize.OrgType == EOrgType.County && snapshot.SendGuidSystemTime == null, 1, 0)), //受理范围内按12345直派给各区县件数
  239. ZXYB = SqlFunc.AggregateSum(SqlFunc.IIF(order.FileOrgIsCenter == true, 1, 0)), // 中心已办
  240. BMYB = SqlFunc.AggregateSum(SqlFunc.IIF(order.FileOrgIsCenter == false, 1, 0)), // 部门已办
  241. //SLFWMYD = 0, //受理范围满意度
  242. MYL = SqlFunc.AggregateSum(SqlFunc.IIF(SqlFunc.JsonListObjectAny(order.OrgProcessingResults, "key", "1") || SqlFunc.JsonListObjectAny(order.OrgProcessingResults, "key", "2"), 0, 1)), // 满意量
  243. BMYL = SqlFunc.AggregateSum(SqlFunc.IIF(SqlFunc.JsonListObjectAny(order.OrgProcessingResults, "key", "1") || SqlFunc.JsonListObjectAny(order.OrgProcessingResults, "key", "2"), 1, 0)), // 不满意量
  244. SPBTYHBGS = SqlFunc.AggregateSum(SqlFunc.IIF(redPackAudit.Status == ERedPackAuditStatus.Refuse, 1, 0)), // 审批不同意红包个数
  245. SPTYHBGS = SqlFunc.AggregateSum(SqlFunc.IIF(redPackAudit.Status == ERedPackAuditStatus.Agree, 1, 0)), // 审批同意红包个数
  246. TSHBSP = 0, //特殊红包审批统计
  247. SPTYGS = SqlFunc.AggregateSum(SqlFunc.IIF(special.Status == ERedPackAuditStatus.Agree, 1, 0)), //审批同意个数
  248. YFJE = SqlFunc.AggregateSum(SqlFunc.IIF(special.IsSend == true, special.AcutalAmount, 0)), // 已发金额
  249. JSHFFWGJLGS = 0, //局审核发放网格员奖励个数
  250. SPTYWGYHBGS = SqlFunc.AggregateSum(SqlFunc.IIF(guiderAudit.LevelTwoStatus == ERedPackAuditStatus.Agree, 1, 0)), //审批同意(网格员)红包个数
  251. SPBTYWGYHBGS = SqlFunc.AggregateSum(SqlFunc.IIF(guiderAudit.LevelOneStatus == ERedPackAuditStatus.Refuse || guiderAudit.LevelTwoStatus == ERedPackAuditStatus.Refuse, 1, 0)), //审批不同意(网格员)红包个数
  252. SMJLZE = SqlFunc.AggregateSum(redPackAudit.AcutalAmount), // 市民奖励总额
  253. SMYFFJLZE = SqlFunc.AggregateSum(SqlFunc.IIF(redPackAudit.IsSend == true, redPackAudit.AcutalAmount, 0)), // 市民已发放奖励总额
  254. SMDFFJLZE = SqlFunc.AggregateSum(SqlFunc.IIF(redPackAudit.IsSend == false, redPackAudit.ApprovedAmount, 0)), // 市民待发奖励总额
  255. YFG = SqlFunc.AggregateSum(SqlFunc.IIF(redPackAudit.IsSend == true, 1, 0)), //已发(个)
  256. WFLXG = SqlFunc.AggregateSum(SqlFunc.IIF(record.FailCase == ERedPackPickupFailCase.NotContact, 1, 0)), // 无法联系(个)
  257. WJHBG = SqlFunc.AggregateSum(SqlFunc.IIF(record.FailCase == ERedPackPickupFailCase.Excuse, 1, 0)), // 婉拒红包(个)
  258. WGYYFJLJE = SqlFunc.AggregateSum(SqlFunc.IIF(guiderAudit.LevelTwoStatus == ERedPackAuditStatus.Agree, guiderAudit.AcutalAmount, 0)), //网格员应发奖励金额
  259. WGYYFFJLZE = SqlFunc.AggregateSum(SqlFunc.IIF(guiderAudit.IsSend == true, guiderAudit.AcutalAmount, 0)), // 网格员已发放奖励总额
  260. WGYDFFJLZE = SqlFunc.AggregateSum(SqlFunc.IIF(guiderAudit.LevelTwoStatus == ERedPackAuditStatus.Agree && guiderAudit.IsSend == false, guiderAudit.ApprovedAmount, 0)), // 网格员待发放奖励总额
  261. WGYKKZEYF = 0, // 网格员扣款总额(已发)
  262. WGYKKZEDF = 0, // 网格员扣款总额(待发)
  263. SLFWNDBMHQJJS = SqlFunc.AggregateSum(SqlFunc.IIF(order.CounterSignType != null, 1, 0)), // 受理范围内多部门会签件件数
  264. SLFWNRXZXGDJS = SqlFunc.AggregateSum(SqlFunc.IIF(order.FileOrgIsCenter == true, 1, 0)), // 受理范围内热线中心归档件数
  265. RXZXFQHQJJS = SqlFunc.AggregateSum(SqlFunc.IIF(order.CounterSignType != null && order.CounterSignType == ECounterSignType.Center, 1, 0)), // 热线中心发起会签件件数
  266. AQYH = SqlFunc.AggregateSum(SqlFunc.IIF(snapshot.IsDangerDepartment == true, 1, 0)), // 安全隐患
  267. YWCAQYHZG = SqlFunc.AggregateSum(SqlFunc.IIF(snapshot.IsRectifyDepartment == true, 1, 0)), // 已完成安全隐患整改
  268. SQYQGDJS = SqlFunc.AggregateSum(SqlFunc.IIF(order.OrderDelays.Count(m => m.DelayState == EDelayState.Pass) > 0, 1, 0)), // 申请延期工单件数
  269. SQYQGDCS = SqlFunc.AggregateSum(SqlFunc.IIF(order.OrderDelays.Count() > 0, order.OrderDelays.Count(m => m.DelayState == EDelayState.Pass), 0)), // 申请延期工单次数
  270. CQJ = SqlFunc.AggregateSum(SqlFunc.IIF(order.Status >= EOrderStatus.Filed && order.ExpiredTime < order.FiledTime, 1, 0)), // 超期件
  271. ECBLJSTHBM = SqlFunc.AggregateSum(SqlFunc.IIF(second.State == ESecondaryHandlingState.Handled && second.ApplyOrgId != "001", 1, 0)), // 二次办理件数 - 退回部门
  272. ECBLJSHFBMYCB = SqlFunc.AggregateSum(SqlFunc.IIF(second.State == ESecondaryHandlingState.Handled, 1, 0)), // 二次办理件数-回访不满意重办
  273. ECBLJSTTDYYJBM = SqlFunc.AggregateSum(SqlFunc.IIF(second.State == ESecondaryHandlingState.Handled, 1, 0)), // 二次办理件数-特提到原一级部门
  274. ECBLJSHFMY = SqlFunc.AggregateSum(SqlFunc.IIF(second.State == ESecondaryHandlingState.Handled && (SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "5" || SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "4" || SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "-1" || SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "0"), 1, 0)), // 二次办理件数-回访满意
  275. // ECBLGDMYL = , // 二次办理工单满意率
  276. ECBLGDJSTMBMHFMYD = SqlFunc.AggregateSum(SqlFunc.IIF(second.State == ESecondaryHandlingState.Handled && second.ApplyOrgId != "001" && (SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "5" || SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "4" || SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "-1" || SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "0"), 1, 0)), // 二次办理工单件数-退回部门回访满意
  277. // ECBLGDMYLTHBM = , // 二次办理工单满意率-退回部门
  278. // ECBLGDMYLHFBMYCB = 0, // 二次办理工单满意率-回访不满意重办
  279. TTDYYJBMJS = SqlFunc.AggregateSum(SqlFunc.IIF(orderSpecial.State == 1 && orderSpecial.SpecialType == ESpecialType.Special && orderSpecial.NextStepName == "一级部门", 1, 0)), // 特提到原一级部门件数
  280. TTDPDZJS = SqlFunc.AggregateSum(SqlFunc.IIF(orderSpecial.State == 1 && orderSpecial.SpecialType == ESpecialType.Special && orderSpecial.NextStepName == "派单组", 1, 0)), // 特提到派单组件数
  281. QTTTJS = SqlFunc.AggregateSum(SqlFunc.IIF(orderSpecial.State == 1 && orderSpecial.SpecialType == ESpecialType.Special && (orderSpecial.NextStepName != "一级部门" || orderSpecial.NextStepName != "派单组"), 1, 0)), // 其他特提件数
  282. });
  283. return await query.FirstAsync();
  284. }
  285. public ISugarQueryable<SnapshotStatisticsDetailOutDto> GetSnapshotStatisticsDetail(SnapshotStatisticsDetailInDto dto)
  286. {
  287. dto.FieldName = dto.FieldName.ToUpper();
  288. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  289. .LeftJoin<Order>((snapshot, order) => order.Id == snapshot.Id)
  290. .LeftJoin<RedPackAudit>((snapshot, order, redPackAudit) => snapshot.Id == redPackAudit.OrderId)
  291. .LeftJoin<SpecialRedPackAudit>((snapshot, order, redPackAudit, special) => snapshot.Id == special.OrderId)
  292. .LeftJoin<RedPackGuiderAudit>((snapshot, order, redPackAudit, special, guiderAudit) => snapshot.Id == guiderAudit.OrderId)
  293. .LeftJoin<RedPackRecord>((snapshot, order, redPackAudit, special, guiderAudit, record) => redPackAudit.Id == record.RedPackAuditId)
  294. .LeftJoin<OrderSecondaryHandling>((snapshot, order, redPackAudit, special, guiderAudit, record, second) => snapshot.Id == second.OrderId)
  295. .LeftJoin<OrderSpecial>((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial) => snapshot.Id == orderSpecial.OrderId)
  296. .LeftJoin<SystemOrganize>((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => order.ActualHandleOrgCode == organize.Id)
  297. .Where((snapshot, order) => order.CreationTime >= dto.StartTime && order.CreationTime <= dto.EndTime);
  298. query = dto.FieldName switch
  299. {
  300. "WZSLFWNJS" => query.Where((snapshot, order) => order.SourceChannelCode != "SSP"),
  301. "SSPZ12345JS" => query.Where((snapshot, order) => order.SourceChannelCode != "SSP"),
  302. "SLFWNZJS" => query.Where((snapshot, order) => order.HotspotName != "非受理范围" && order.SourceChannelCode == "SJP12345"),
  303. "SLFWNPGWGYSXSNHFJS" => query.Where((snapshot, order) => snapshot.IsGuidSystemCallBack == true && snapshot.GuidSystemCallBackTime!.Value.AddHours(-4) <= snapshot.SendGuidSystemTime),
  304. "SLFWNPGWGYCGSXSHFJS" => query.Where((snapshot, order) => snapshot.IsGuidSystemCallBack == true && snapshot.GuidSystemCallBackTime!.Value.AddHours(-4) <= snapshot.SendGuidSystemTime),
  305. "SLFWNPGWGYWHFJS" => query.Where((snapshot, order) => snapshot.IsGuidSystemCallBack == false),
  306. "SLFWNA12345ZPGGBMJS" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => organize.OrgType == EOrgType.County && snapshot.SendGuidSystemTime == null),
  307. "SLFWNA12345ZPGGQXJS" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => organize.OrgType == EOrgType.County && snapshot.SendGuidSystemTime == null),
  308. "ZXYB" => query.Where((snapshop, order) => order.FileOrgIsCenter == true),
  309. "BMYB" => query.Where((snapshop, order) => order.FileOrgIsCenter == false),
  310. "MYL" => query.Where((snapshot, order) => SqlFunc.JsonListObjectAny(order.OrgProcessingResults, "key", "-1") || SqlFunc.JsonListObjectAny(order.OrgProcessingResults, "key", "3") || SqlFunc.JsonListObjectAny(order.OrgProcessingResults, "key", "4") || SqlFunc.JsonListObjectAny(order.OrgProcessingResults, "key", "5")),
  311. "BMYL" => query.Where((snapshot, order) => SqlFunc.JsonListObjectAny(order.OrgProcessingResults, "key", "1") || SqlFunc.JsonListObjectAny(order.OrgProcessingResults, "key", "2")),
  312. "SPBTYHBGS" => query.Where((snapshot, order, redPackAudit) => redPackAudit.Status == ERedPackAuditStatus.Refuse),
  313. "SPTYHBGS" => query.Where((snapshot, order, redPackAudit) => redPackAudit.Status == ERedPackAuditStatus.Agree),
  314. "TSHBSP" => query,
  315. "SPTYGS" => query.Where((snapshot, order, redPackAudit, special) => special.Status == ERedPackAuditStatus.Agree),
  316. "YFJE" => query.Where((snapshot, order, redPackAudit, special) => special.IsSend == true),
  317. "SPTYWGYHBGS" => query.Where((snapshot, order, redPackAudit, special, guiderAudit) => guiderAudit.LevelTwoStatus == ERedPackAuditStatus.Agree),
  318. "SPBTYWGYHBGS" => query.Where((snapshot, order, redPackAudit, special, guiderAudit) => guiderAudit.LevelOneStatus == ERedPackAuditStatus.Refuse || guiderAudit.LevelTwoStatus == ERedPackAuditStatus.Refuse),
  319. "SMYFFJLZE" => query.Where((snapshot, order, redPackAudit, special, guiderAudit) => redPackAudit.IsSend == true),
  320. "SMDFFJLZE" => query.Where((snapshot, order, redPackAudit, special, guiderAudit) => redPackAudit.IsSend == false),
  321. "YFG" => query.Where((snapshot, order, redPackAudit, special, guiderAudit) => redPackAudit.IsSend == true),
  322. "WFLXG" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => record.FailCase == ERedPackPickupFailCase.NotContact),
  323. "WJHBG" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => record.FailCase == ERedPackPickupFailCase.Excuse),
  324. "WGYYFJLJE" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => guiderAudit.LevelTwoStatus == ERedPackAuditStatus.Agree),
  325. "WGYYFFJLZE" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => guiderAudit.IsSend == true),
  326. "WGYDFFJLZE" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => guiderAudit.LevelTwoStatus == ERedPackAuditStatus.Agree && guiderAudit.IsSend == false),
  327. "WGYKKZEYF" => query,
  328. "WGYKKZEDF" => query,
  329. "SLFWNDBMHQJJS" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => order.CounterSignType != null),
  330. "SLFWNRXZXGDJS" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => order.FileOrgIsCenter == true),
  331. "RXZXFQHQJJS" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => order.CounterSignType != null && order.CounterSignType == ECounterSignType.Center),
  332. "AQYH" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => snapshot.IsDangerDepartment == true),
  333. "YWCAQYHZG" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => snapshot.IsRectifyDepartment == true),
  334. "SQYQGDJS" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => order.OrderDelays.Count(m => m.DelayState == EDelayState.Pass) > 0),
  335. "SQYQGDCS" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => order.OrderDelays.Count() > 0),
  336. "CQJ" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => order.Status >= EOrderStatus.Filed && order.ExpiredTime < order.FiledTime),
  337. "ECBLJSTHBM" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => second.State == ESecondaryHandlingState.Handled && second.ApplyOrgId != "001"),
  338. "ECBLJSHFBMYCB" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => second.State == ESecondaryHandlingState.Handled),
  339. "ECBLJSTTDYYJBM" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => second.State == ESecondaryHandlingState.Handled),
  340. "ECBLJSHFMY" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => second.State == ESecondaryHandlingState.Handled && (SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "5" || SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "4" || SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "-1" || SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "0")),
  341. "ECBLGDJSTMBMHFMYD" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => second.State == ESecondaryHandlingState.Handled && second.ApplyOrgId != "001" && (SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "5" || SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "4" || SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "-1" || SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "0")),
  342. "TTDYYJBMJS" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => orderSpecial.State == 1 && orderSpecial.SpecialType == ESpecialType.Special && orderSpecial.NextStepName == "一级部门"),
  343. "TTDPDZJS" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => orderSpecial.State == 1 && orderSpecial.SpecialType == ESpecialType.Special && orderSpecial.NextStepName == "派单组"),
  344. "QTTTJS" => query.Where((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => orderSpecial.State == 1 && orderSpecial.SpecialType == ESpecialType.Special && (orderSpecial.NextStepName != "一级部门" || orderSpecial.NextStepName != "派单组")),
  345. _ => throw new UserFriendlyException("入参错误:" + dto.FieldName + " 未实现")
  346. };
  347. return query.Select((snapshot, order, redPackAudit, special, guiderAudit, record, second, orderSpecial, organize) => new SnapshotStatisticsDetailOutDto
  348. {
  349. CreationTime = order.CreationTime
  350. }, true);
  351. }
  352. /// <summary>
  353. /// 办件统计-随手拍
  354. /// </summary>
  355. /// <param name="dto"></param>
  356. /// <returns></returns>
  357. /// <exception cref="NotImplementedException"></exception>
  358. public ISugarQueryable<SnapshotProcessingStatisticsOutDto> GetSnapshotProcessingStatistics(SnapshotProcessingStatisticsInDto dto)
  359. {
  360. bool IsCenter = _sessionContext.OrgIsCenter;
  361. var orgLevel = _sessionContext.OrgLevel;
  362. string orgLevelStr = (_sessionContext.RequiredOrgId.Length + 3).ToString();
  363. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  364. .LeftJoin<Order>((snapshot, order) => snapshot.Id == order.Id)
  365. .LeftJoin<OrderSendBackAudit>((snapshot, order, back) => snapshot.Id == back.OrderId && back.State == ESendBackAuditState.End)
  366. .LeftJoin<OrderVisit>((snapshot, order, back, visit) => snapshot.Id == visit.OrderId && visit.VisitState == EVisitState.Visited)
  367. .LeftJoin<OrderSecondaryHandling>((snapshot, order, back, visit, second) => snapshot.Id == second.OrderId && second.State == ESecondaryHandlingState.End)
  368. .Where(snapshot => snapshot.CreationTime >= dto.StartTime && snapshot.CreationTime <= dto.EndTime)
  369. .WhereIF(dto.IndustryId.NotNullOrEmpty(), snapshot => snapshot.IndustryId == dto.IndustryId)
  370. .GroupBy((snapshot, order) => new
  371. {
  372. OrgCode = order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6"))
  373. })
  374. .Select((snapshot, order, back, visit, second) => new SnapshotProcessingStatisticsOutDto()
  375. {
  376. OrgCode = order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6")),
  377. YbOrderCountNum = SqlFunc.AggregateCount(SqlFunc.IIF(order.Status >= EOrderStatus.Filed, 1, 0)),
  378. ZbOrderCountNum = SqlFunc.AggregateCount(SqlFunc.IIF(order.Status < EOrderStatus.Filed, 1, 0)),
  379. ReceiveIn20Minutes = SqlFunc.AggregateCount(SqlFunc.IIF(order.ActualHandleStepCreateTime != null && order.ActualHandleStepCreateTime.Value.AddMinutes(-20) <= order.CreationTime, 1, 0)),
  380. ReceiveOut20Minutes = SqlFunc.AggregateCount(SqlFunc.IIF(order.ActualHandleStepCreateTime == null || order.ActualHandleStepCreateTime.Value.AddMinutes(-20) > order.CreationTime, 1, 0)),
  381. BackNum = SqlFunc.AggregateCount(SqlFunc.IIF(back.OrderId != null && back.OrderId != "", 1, 0)),
  382. TotalHandleDuration = SqlFunc.AggregateSum(order.HandleDuration),
  383. End3Day = SqlFunc.AggregateSum(SqlFunc.IIF(order.FiledTime != null && order.FiledTime.Value.AddDays(-3) < order.CreationTime, 1, 0)),
  384. End3To5Day = SqlFunc.AggregateSum(SqlFunc.IIF(order.FiledTime != null && order.FiledTime.Value.AddDays(-3) > order.CreationTime && order.FiledTime.Value.AddDays(-5) < order.CreationTime, 1, 0)),
  385. End5To7Day = SqlFunc.AggregateSum(SqlFunc.IIF(order.FiledTime != null && order.FiledTime.Value.AddDays(-5) > order.CreationTime && order.FiledTime.Value.AddDays(-7) < order.CreationTime, 1, 0)),
  386. End7Day = SqlFunc.AggregateSum(SqlFunc.IIF(order.FiledTime != null && order.FiledTime.Value.AddDays(-7) < order.CreationTime, 1, 0)),
  387. OnTimeCount = SqlFunc.AggregateSum(SqlFunc.IIF(order.FiledTime <= order.ExpiredTime, 1, 0)),
  388. SatisfiedCount = SqlFunc.AggregateSum(SqlFunc.IIF(SqlFunc.JsonListObjectAny(visit.NowEvaluate, "key", "1") || SqlFunc.JsonListObjectAny(visit.NowEvaluate, "key", "2"), 1, 0)),
  389. NoSatisfiedCount = SqlFunc.AggregateSum(SqlFunc.IIF(SqlFunc.JsonListObjectAny(visit.NowEvaluate, "key", "1") || SqlFunc.JsonListObjectAny(visit.NowEvaluate, "key", "2"), 0, 1)),
  390. SecondNum = SqlFunc.AggregateCount(second.Id)
  391. })
  392. .MergeTable()
  393. .LeftJoin<SystemOrganize>((it, o) => it.OrgCode == o.Id)
  394. .Select((it, o) => new SnapshotProcessingStatisticsOutDto()
  395. {
  396. OrgName = o.Name,
  397. OrgCode = it.OrgCode,
  398. YbOrderCountNum = it.YbOrderCountNum,
  399. ZbOrderCountNum = it.ZbOrderCountNum,
  400. ReceiveIn20Minutes = it.ReceiveIn20Minutes,
  401. ReceiveOut20Minutes = it.ReceiveOut20Minutes,
  402. BackNum = it.BackNum,
  403. TotalHandleDuration = it.TotalHandleDuration,
  404. End3Day = it.End3Day,
  405. End3To5Day = it.End3To5Day,
  406. End5To7Day = it.End5To7Day,
  407. End7Day = it.End7Day,
  408. OnTimeCount = it.OnTimeCount,
  409. SatisfiedCount = it.SatisfiedCount,
  410. NoSatisfiedCount = it.NoSatisfiedCount,
  411. SecondNum = it.SecondNum
  412. });
  413. return query;
  414. }
  415. public ISugarQueryable<SnapshotProcessingStatisticsDetailsOutDto> GetSnapshotProcessingStatisticsDetails(SnapshotProcessingStatisticsDetailsInDto dto)
  416. {
  417. dto.FieldName = dto.FieldName.ToLower();
  418. var IsCenter = _sessionContext.OrgIsCenter;
  419. IsCenter = true;
  420. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  421. .LeftJoin<Order>((snapshot, order) => snapshot.Id == order.Id)
  422. .LeftJoin<OrderSendBackAudit>((snapshot, order, back) => snapshot.Id == back.OrderId && back.State == ESendBackAuditState.End)
  423. .LeftJoin<OrderVisit>((snapshot, order, back, visit) => snapshot.Id == visit.OrderId && visit.VisitState == EVisitState.Visited)
  424. .LeftJoin<OrderSecondaryHandling>((snapshot, order, back, visit, second) => snapshot.Id == second.OrderId && second.State == ESecondaryHandlingState.End)
  425. .Where((snapshot, order, back) => order.CreationTime >= dto.StartTime && order.CreationTime <= dto.EndTime && order.ActualHandleOrgCode.StartsWith(dto.OrgId))
  426. .WhereIF(IsCenter == false, (snapshot, order, back) => order.ActualHandleOrgCode.StartsWith(_sessionContext.RequiredOrgId))
  427. .WhereIF(dto.IndustryId.NotNullOrEmpty(), (snapshot, order, back) => snapshot.IndustryId == dto.IndustryId);
  428. query = dto.FieldName switch
  429. {
  430. "ybordercountnum" => query.Where((snapshot, order, back) => order.Status >= EOrderStatus.Filed),
  431. "zbordercountnum" => query.Where((snapshot, order, back) => order.Status < EOrderStatus.Filed),
  432. "receivein20minutes" => query.Where((snapshot, order, back) => order.ActualHandleStepCreateTime != null && order.ActualHandleStepCreateTime.Value.AddMinutes(-20) <= order.CreationTime),
  433. "receiveout20minutes" => query.Where((snapshot, order, back) => order.ActualHandleStepCreateTime == null || order.ActualHandleStepCreateTime.Value.AddMinutes(-20) > order.CreationTime),
  434. "backnum" => query.Where((snapshot, order, back) => back.OrderId != null && back.OrderId != ""),
  435. "end3day" => query.Where((snapshot, order, back) => order.FiledTime != null && order.FiledTime.Value.AddDays(-3) < order.CreationTime),
  436. "end3to5day" => query.Where((snapshot, order, back) => order.FiledTime != null && order.FiledTime.Value.AddDays(-3) > order.CreationTime && order.FiledTime.Value.AddDays(-5) < order.CreationTime),
  437. "end5to7day" => query.Where((snapshot, order, back) => order.FiledTime != null && order.FiledTime.Value.AddDays(-5) > order.CreationTime && order.FiledTime.Value.AddDays(-7) < order.CreationTime),
  438. "end7day" => query.Where((snapshot, order, back) => order.FiledTime != null && order.FiledTime.Value.AddDays(-7) < order.CreationTime),
  439. "satisfiedcount" => query.Where((snapshot, order, back, visit) => SqlFunc.JsonListObjectAny(visit.NowEvaluate, "key", "1") || SqlFunc.JsonListObjectAny(visit.NowEvaluate, "key", "2")),
  440. "nosatisfiedcount" => query.Where((snapshot, order, back, visit) => SqlFunc.JsonListObjectAny(visit.NowEvaluate, "key", "1") || SqlFunc.JsonListObjectAny(visit.NowEvaluate, "key", "2")),
  441. "secondnum" => query.Where((snapshot, order, back, visit, second) => second.Id != null || second.Id != ""),
  442. _ => throw new UserFriendlyException($"入参: {dto.FieldName} 异常")
  443. };
  444. return query.Select((snapshot, order, back, visit, second) => new SnapshotProcessingStatisticsDetailsOutDto
  445. {
  446. OrderScreenStatus = SqlFunc.Subqueryable<OrderScreen>().Where(q => q.OrderId == order.Id).OrderByDesc(q => q.CreationTime).Select(q => q.Status),
  447. }, true);
  448. }
  449. public ISugarQueryable<GuiderWorkStatisticsOutDto> GetGuiderWorkStatisticsAsync(GuiderWorkStatisticsInDto dto)
  450. {
  451. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  452. .Where(snapshot => snapshot.CreationTime >= dto.StartTime && snapshot.CreationTime <= dto.EndTime && snapshot.MemberName != null)
  453. .GroupBy(snapshot => new { snapshot.MemberName, snapshot.MemberMobile })
  454. .Select(snapshot => new GuiderWorkStatisticsOutDto
  455. {
  456. MemberMobile = snapshot.MemberMobile,
  457. MemberName = snapshot.MemberName,
  458. ReplyIn4HourCount = SqlFunc.AggregateSum(SqlFunc.IIF(snapshot.ReplyResultType == EGuiderSystemReplyType.Field && snapshot.GuidSystemCallBackTime.Value.AddHours(-4) <= snapshot.SendGuidSystemTime, 1, 0)),
  459. ReplyOut4HourCount = SqlFunc.AggregateSum(SqlFunc.IIF(snapshot.ReplyResultType == EGuiderSystemReplyType.Field && snapshot.GuidSystemCallBackTime.Value.AddHours(-4) > snapshot.SendGuidSystemTime, 1, 0)),
  460. UnReplyCount = SqlFunc.AggregateSum(SqlFunc.IIF(snapshot.ReplyResultType != EGuiderSystemReplyType.Field, 1, 0)),
  461. });
  462. return query;
  463. }
  464. public ISugarQueryable<GuiderWorkStatisticsDetailsOutDto> GetGuiderWorkStatisticsDetails(GuiderWorkStatisticsDetailsInDto dto)
  465. {
  466. dto.FieldName = dto.FieldName.ToLower();
  467. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  468. .LeftJoin<Order>((snapshot, order) => order.Id == snapshot.Id)
  469. .Where(snapshot => snapshot.CreationTime >= dto.StartTime && snapshot.CreationTime <= dto.EndTime && snapshot.MemberMobile == dto.MemberMobile);
  470. query = dto.FieldName switch
  471. {
  472. "replyin4hourcount" => query.Where(snapshot => snapshot.ReplyResultType == EGuiderSystemReplyType.Field && snapshot.GuidSystemCallBackTime.Value.AddHours(-4) <= snapshot.SendGuidSystemTime),
  473. "replyout4hourcount" => query.Where(snapshot => snapshot.ReplyResultType == EGuiderSystemReplyType.Field && snapshot.GuidSystemCallBackTime.Value.AddHours(-4) > snapshot.SendGuidSystemTime),
  474. "unreplycount" => query.Where(snapshot => snapshot.ReplyResultType != EGuiderSystemReplyType.Field),
  475. _ => throw new UserFriendlyException($"入参: {dto.FieldName} 异常")
  476. };
  477. return query.Select((snapshot, order) => new GuiderWorkStatisticsDetailsOutDto
  478. {
  479. }, true);
  480. }
  481. public ISugarQueryable<HotspotDataStatisticsOutDto> GetHotspotDataStatisticsAsync(HotspotDataStatisticsInDto dto)
  482. {
  483. var isCenter = _sessionContext.OrgIsCenter;
  484. var query = _hotspotTypeRepository.Queryable(includeDeleted: true)
  485. .LeftJoin<Order>((hotspot, order) => order.HotspotSpliceName != null && (hotspot.HotSpotName == order.HotspotSpliceName || order.HotspotSpliceName.Contains(hotspot.HotSpotName)) && order.IsDeleted == false && order.CreationTime >= dto.StartTime && order.CreationTime <= dto.EndTime)
  486. .WhereIF(isCenter == false, (hotspot, order) => order.ActualHandleOrgCode == _sessionContext.RequiredOrgId)
  487. .Where(hotspot => hotspot.ParentId == null)
  488. .GroupBy((hotspot, order) => new { hotspot.Id, hotspot.HotSpotName })
  489. .Select((hotspot, order) => new HotspotDataStatisticsOutDto
  490. {
  491. Name = hotspot.HotSpotName,
  492. OrderCount = SqlFunc.AggregateSum(SqlFunc.IIF(order.Id != null, 1, 0)),
  493. });
  494. return query;
  495. }
  496. public ISugarQueryable<GuiderWorkLogsOutDto> GetGuiderWorkLogs(GuiderWorkLogsInDto dto)
  497. {
  498. var query = _orderSnapshotRepository.Queryable()
  499. .LeftJoin<Order>((snapshot, order) => order.Id == snapshot.Id)
  500. .Where((snapshot, order) => snapshot.IsGuidSystemCallBack == true)
  501. .WhereIF(dto.MemberMobile.NotNullOrEmpty(), snapshot => snapshot.MemberMobile.Contains(dto.MemberMobile))
  502. .WhereIF(dto.BeginCreationTime != null && dto.EndCreationTime != null, snapshot => snapshot.CreationTime >= dto.BeginCreationTime && snapshot.CreationTime <= dto.EndCreationTime)
  503. .WhereIF(dto.MemberName.NotNullOrEmpty(), snapshot => snapshot.MemberName.Contains(dto.MemberName))
  504. .WhereIF(dto.No.NotNullOrEmpty(), (snapshot, order) => order.No.Contains(dto.No))
  505. .WhereIF(dto.Title.NotNullOrEmpty(), (snapshot, order) => order.Title.Contains(dto.Title))
  506. .WhereIF(dto.NetworkENumber.NotNullOrEmpty(), (snapshot, order) => snapshot.NetworkENumber.Contains(dto.NetworkENumber))
  507. .WhereIF(dto.Status != null, (snapshot, order) => order.Status == dto.Status)
  508. .OrderByDescending(snapshot => snapshot.CreationTime)
  509. .Select(snapshot => new GuiderWorkLogsOutDto(), true);
  510. return query;
  511. }
  512. public ISugarQueryable<DuplicateItemsOutDto> GetDuplicateItems(DuplicateItemsInDto dto)
  513. {
  514. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  515. .LeftJoin<Order>((snapshot, order) => order.Id == snapshot.Id)
  516. .WhereIF(dto.No.NotNullOrEmpty(), (snapshot, order) => order.No.Contains(dto.No))
  517. .WhereIF(dto.Title.NotNullOrEmpty(), (snapshot, order) => order.Title.Contains(dto.Title))
  518. .WhereIF(dto.FromName.NotNullOrEmpty(), (snapshot, order) => order.FromName.Contains(dto.FromName))
  519. .WhereIF(dto.FromPhone.NotNullOrEmpty(), (snapshot, order) => order.FromPhone.Contains(dto.FromPhone))
  520. .WhereIF(dto.BeginCreationTime != null && dto.EndCreationTime != null, (snapshot, order) => order.CreationTime >= dto.BeginCreationTime && order.CreationTime <= dto.EndCreationTime)
  521. .WhereIF(dto.BeginExpiredTime != null && dto.EndExpiredTime != null, (snapshot, order) => order.ExpiredTime >= dto.BeginExpiredTime && order.ExpiredTime <= dto.EndExpiredTime)
  522. .WhereIF(dto.ActualHandleOrgName.NotNullOrEmpty(), (snapshot, order) => order.ActualHandleOrgName == dto.ActualHandleOrgName)
  523. .WhereIF(dto.AcceptType.NotNullOrEmpty(), (snapshot, order) => order.AcceptType == dto.AcceptType)
  524. .WhereIF(dto.HotspotName.NotNullOrEmpty(), (snapshot, order) => order.HotspotName == dto.HotspotName)
  525. .WhereIF(dto.AcceptorName.NotNullOrEmpty(), (snapshot, order) => order.AcceptorName == dto.AcceptorName)
  526. .WhereIF(dto.IndustryId.NotNullOrEmpty(), (snapshot, order) => snapshot.IndustryId == dto.IndustryId)
  527. .Select((snapshot, order) => new DuplicateItemsOutDto(), true);
  528. return query;
  529. }
  530. public ISugarQueryable<CommunityStatisticsOutDto> GetCommunityStatistics(CommunityStatisticsInDto dto)
  531. {
  532. var query = _communityInfoRepository.Queryable(includeDeleted: true)
  533. .LeftJoin<OrderSnapshot>((it, s) => s.CommunityFullName.Contains(it.FullName) && s.CreationTime >= dto.StartTime && s.CreationTime <= dto.EndTime)
  534. .WhereIF(dto.CommunityCode.IsNullOrEmpty(), (it, s) => it.ParentCode == null)
  535. .WhereIF(dto.CommunityCode.NotNullOrEmpty(), (it, s) => it.ParentCode == dto.CommunityCode)
  536. .WhereIF(dto.IndustryId.NotNullOrEmpty(), (it, s) => s.IndustryId == dto.IndustryId)
  537. .GroupBy((it, s) => new { it.Name, it.Id })
  538. .Select((it, s) => new CommunityStatisticsOutDto
  539. {
  540. CommunityCode = it.Id,
  541. CommunityName = it.Name,
  542. CommunityFullName = it.FullName,
  543. SumCount = SqlFunc.Subqueryable<OrderSnapshot>().Where(snapshot => snapshot.CommunityFullName!.Contains(it.FullName)).Count(),
  544. HasChild = SqlFunc.Subqueryable<CommunityInfo>().Where(c => c.ParentCode == it.Id).Any()
  545. });
  546. #if DEBUG
  547. var sql = query.ToSqlString();
  548. #endif
  549. return query;
  550. }
  551. public ISugarQueryable<CommunityStatisticsDetailsOutDto> GetCommunityStatisticsDetails(CommunityStatisticsDetailsInDto dto)
  552. {
  553. var communityFullName = _communityInfoRepository.Queryable().Where(c => c.Id == dto.CommunityCode).Select(c => c.FullName).First();
  554. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  555. .LeftJoin<Order>((snapshot, order) => snapshot.Id == order.Id)
  556. .Where((snapshot, order) => snapshot.CreationTime >= dto.StartTime && snapshot.CreationTime <= dto.EndTime && snapshot.CommunityFullName!.Contains(communityFullName))
  557. .WhereIF(dto.IndustryId.NotNullOrEmpty(), (snapshot, order) => snapshot.IndustryId == dto.IndustryId)
  558. .Select((snapshot, order) => new CommunityStatisticsDetailsOutDto(), true);
  559. #if DEBUG
  560. var sql = query.ToSqlString();
  561. #endif
  562. return query;
  563. }
  564. [ExportExcel("随手拍区域统计", "County")]
  565. public ISugarQueryable<CountyRedPackStatisticsOutDto> GetCountyRedPackStatistics(CountyRedPackStatisticsInDto dto)
  566. {
  567. var parentId = _appOptions.Value.AreaCode;
  568. var query = _systemAreaRepository.Queryable(includeDeleted: true)
  569. .LeftJoin<Order>((area, order) => order.County == area.AreaName && order.CreationTime >= dto.StartTime && order.CreationTime <= dto.EndTime && order.County != null)
  570. .LeftJoin<OrderSnapshot>((area, order, snapshot) => snapshot.Id == order.Id && (dto.IndustryId.IsNullOrEmpty() || snapshot.IndustryId == dto.IndustryId))
  571. .LeftJoin<RedPackAudit>((area, order, snapshot, audit) => snapshot.Id == audit.OrderId)
  572. .LeftJoin<RedPackRecord>((area, order, snapshot, audit, record) => audit.Id == record.RedPackAuditId)
  573. .LeftJoin<SupplementRecord>((area, order, snapshot, audit, record, supplement) => supplement.RedPackAuditId == audit.Id)
  574. .LeftJoin<RedPackGuiderAudit>((area, order, snapshot, audit, record, supplement, guiderAudit) => guiderAudit.OrderId == snapshot.Id)
  575. .LeftJoin<RedPackRecord>((area, order, snapshot, audit, record, supplement, guiderAudit, guiderRecord) => guiderAudit.Id == guiderRecord.RedPackAuditId)
  576. .LeftJoin<Industry>((area, order, snapshot, audit, record, supplement, guiderAudit, guiderRecord, industry) => snapshot.IndustryId == industry.Id)
  577. .Where((area, order, snapshot, audit, record, supplement, guiderAudit, guiderRecord) => area.ParentId == parentId || area.AreaName == "外地")
  578. .GroupBy((area, order, snapshot, audit, record, supplement, guiderAudit, guiderRecord) => new { area.AreaName, order.County })
  579. .Select((area, order, snapshot, audit, record, supplement, guiderAudit, guiderRecord, industry) => new CountyRedPackStatisticsOutDto
  580. {
  581. County = area.AreaName,
  582. JuBaoZongShu = SqlFunc.AggregateSum(SqlFunc.IIF(industry.IndustryType == EIndustryType.Clue, 1, 0)),
  583. YiBanJieShu = SqlFunc.AggregateSum(SqlFunc.IIF(order.Status >= EOrderStatus.Filed && industry.IndustryType == EIndustryType.Clue, 1, 0)),
  584. ShiMinShenHeTongGuoShu = SqlFunc.AggregateSum(SqlFunc.IIF(record.PeopleType == EReadPackUserType.Citizen && audit.Status == ERedPackAuditStatus.Agree, 1, 0)),
  585. ShiMinZongJinE = SqlFunc.AggregateSum(SqlFunc.IIF(record.PeopleType == EReadPackUserType.Citizen && audit.Status == ERedPackAuditStatus.Agree, audit.ApprovedAmount, 0)),
  586. ShiMinYiLing = SqlFunc.AggregateSum(SqlFunc.IIF(record.PeopleType == EReadPackUserType.Citizen && record.PickupStatus == ERedPackPickupStatus.Received, record.Amount, 0)),
  587. ShiMinDaiLing = SqlFunc.AggregateSum(SqlFunc.IIF(record.PeopleType == EReadPackUserType.Citizen && record.PickupStatus == ERedPackPickupStatus.Unreceived, record.Amount, 0)),
  588. ShiMinDaiBuLing = SqlFunc.AggregateSum(SqlFunc.IIF(record.PeopleType == EReadPackUserType.Citizen && supplement.Id != null, supplement.ReplenishAmount, 0)),
  589. WangGeYuanZongJianShu = SqlFunc.AggregateSum(SqlFunc.IIF(snapshot.IsDeal == true, 1, 0)),
  590. WangGeYuanShenHeTongGuoShu = SqlFunc.AggregateSum(SqlFunc.IIF(guiderAudit.LevelTwoStatus == ERedPackAuditStatus.Agree, 1, 0)),
  591. WangGeYuanZongJinE = SqlFunc.AggregateSum(SqlFunc.IIF(guiderAudit.LevelTwoStatus == ERedPackAuditStatus.Agree, guiderAudit.ApprovedAmount, 0)),
  592. WangGeYuanYiLing = SqlFunc.AggregateSum(SqlFunc.IIF(guiderRecord.PickupStatus == ERedPackPickupStatus.Received, guiderRecord.Amount, 0)),
  593. WangGeYuanDaiLing = SqlFunc.AggregateSum(SqlFunc.IIF(guiderRecord.PickupStatus == ERedPackPickupStatus.Unreceived, guiderRecord.Amount, 0)),
  594. });
  595. #if DEBUG
  596. var sql = query.ToSqlString();
  597. #endif
  598. return query;
  599. }
  600. /// <summary>
  601. /// 部门考核统计-随手拍
  602. /// </summary>
  603. /// <param name="dto"></param>
  604. /// <returns></returns>
  605. /// <exception cref="NotImplementedException"></exception>
  606. public ISugarQueryable<SnapshotDepartmentStatisticsOutDto> GetSnapshotDepartmentStatistics(SnapshotDepartmentStatisticsInDto dto)
  607. {
  608. bool IsCenter = _sessionContext.OrgIsCenter;
  609. var orgLevel = _sessionContext.OrgLevel;
  610. string orgLevelStr = (_sessionContext.RequiredOrgId.Length + 3).ToString();
  611. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  612. .LeftJoin<Order>((snapshot, order) => snapshot.Id == order.Id)
  613. .LeftJoin<OrderSendBackAudit>((snapshot, order, back) => snapshot.Id == back.OrderId && back.State == ESendBackAuditState.End)
  614. .LeftJoin<OrderVisit>((snapshot, order, back, visit) => snapshot.Id == visit.OrderId && visit.VisitState == EVisitState.Visited)
  615. .LeftJoin<OrderSecondaryHandling>((snapshot, order, back, visit, second) => snapshot.Id == second.OrderId && second.State == ESecondaryHandlingState.End)
  616. .Where((snapshot, order) => snapshot.CreationTime >= dto.StartTime && snapshot.CreationTime <= dto.EndTime && order.ActualHandleOrgCode != null)
  617. .GroupBy((snapshot, order) => new
  618. {
  619. OrgCode = order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6"))
  620. })
  621. .Select((snapshot, order, back, visit, second) => new SnapshotDepartmentStatisticsOutDto()
  622. {
  623. OrgCode = order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6")),
  624. OrderCountNum = SqlFunc.AggregateCount(snapshot.Id),
  625. ReceiveIn4Hour = SqlFunc.AggregateCount(SqlFunc.IIF(order.ActualHandleStepCreateTime != null && order.ActualHandleStepCreateTime.Value.AddHours(-4) <= order.CreationTime, 1, 0)),
  626. TimeOutField = SqlFunc.AggregateCount(SqlFunc.IIF(order.Status >= EOrderStatus.Filed && order.FiledTime > order.ExpiredTime, 1, 0)),
  627. TimeOutNoField = SqlFunc.AggregateCount(SqlFunc.IIF(order.Status < EOrderStatus.Filed && DateTime.Now > order.ExpiredTime, 1, 0)),
  628. OnTimeCount = SqlFunc.AggregateSum(SqlFunc.IIF(order.FiledTime <= order.ExpiredTime, 1, 0)),
  629. SatisfiedCount = SqlFunc.AggregateSum(SqlFunc.IIF(SqlFunc.JsonListObjectAny(visit.NowEvaluate, "key", "1") || SqlFunc.JsonListObjectAny(visit.NowEvaluate, "key", "2"), 1, 0)),
  630. NoSatisfiedCount = SqlFunc.AggregateSum(SqlFunc.IIF(SqlFunc.JsonListObjectAny(visit.NowEvaluate, "key", "1") || SqlFunc.JsonListObjectAny(visit.NowEvaluate, "key", "2"), 0, 1)),
  631. SecondNum = SqlFunc.AggregateCount(second.Id),
  632. FirstFiledOrderCount = SqlFunc.AggregateCount(SqlFunc.IIF(order.Status >= EOrderStatus.Filed && second.Id == null, 1, 0)),
  633. SecondSatisfied = SqlFunc.AggregateCount(SqlFunc.IIF(second.State == ESecondaryHandlingState.Handled && (SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "1" || SqlFunc.JsonField(order.OrgProcessingResults, "Key") == "2"), 1, 0)),
  634. })
  635. .MergeTable()
  636. .LeftJoin<SystemOrganize>((it, o) => it.OrgCode == o.Id)
  637. .Select((it, o) => new SnapshotDepartmentStatisticsOutDto()
  638. {
  639. OrgName = o.Name,
  640. OrgCode = it.OrgCode,
  641. ReceiveIn4Hour = it.ReceiveIn4Hour,
  642. TimeOutField = it.TimeOutField,
  643. TimeOutNoField = it.TimeOutNoField,
  644. OnTimeCount = it.OnTimeCount,
  645. SatisfiedCount = it.SatisfiedCount,
  646. NoSatisfiedCount = it.NoSatisfiedCount,
  647. SecondNum = it.SecondNum,
  648. FirstFiledOrderCount = it.FirstFiledOrderCount,
  649. SecondSatisfied = it.SecondSatisfied
  650. });
  651. return query;
  652. }
  653. public ISugarQueryable<SnapshotDepartmentAveTimeStatisticsOutDto> GetSnapshotDepartmentAveTimeStatistics(SnapshotDepartmentAveTimeStatisticsInDto dto)
  654. {
  655. bool IsCenter = _sessionContext.OrgIsCenter;
  656. var orgLevel = _sessionContext.OrgLevel;
  657. string orgLevelStr = (_sessionContext.RequiredOrgId.Length + 3).ToString();
  658. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  659. .LeftJoin<Order>((snapshot, order) => snapshot.Id == order.Id)
  660. .LeftJoin<OrderSendBackAudit>((snapshot, order, back) => snapshot.Id == back.OrderId && back.State == ESendBackAuditState.End)
  661. .LeftJoin<OrderVisit>((snapshot, order, back, visit) => snapshot.Id == visit.OrderId && visit.VisitState == EVisitState.Visited)
  662. .LeftJoin<OrderSecondaryHandling>((snapshot, order, back, visit, second) => snapshot.Id == second.OrderId && second.State == ESecondaryHandlingState.End)
  663. .Where((snapshot, order) => snapshot.CreationTime >= dto.StartTime && snapshot.CreationTime <= dto.EndTime && order.ActualHandleOrgCode != null)
  664. .GroupBy((snapshot, order) => new
  665. {
  666. OrgCode = order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6"))
  667. })
  668. .Select((snapshot, order, back, visit, second) => new SnapshotDepartmentAveTimeStatisticsOutDto()
  669. {
  670. OrgCode = order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6")),
  671. OrderCountNum = SqlFunc.AggregateCount(snapshot.Id),
  672. TotalSeconds = SqlFunc.AggregateSum(order.AllDuration),
  673. })
  674. .MergeTable()
  675. .LeftJoin<SystemOrganize>((it, o) => it.OrgCode == o.Id)
  676. .Select((it, o) => new SnapshotDepartmentAveTimeStatisticsOutDto()
  677. {
  678. OrgName = o.Name,
  679. OrgCode = it.OrgCode,
  680. TotalSeconds = it.TotalSeconds,
  681. OrderCountNum = it.OrderCountNum
  682. });
  683. return query;
  684. }
  685. public ISugarQueryable<SnapshotDepartmentAveTimeStatisticsDetailsOutDto> GetSnapshotDepartmentAveTimeStatisticsDtails(SnapshotDepartmentAveTimeStatisticsDetailsInDto dto)
  686. {
  687. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  688. .LeftJoin<Order>((snapshot, order) => snapshot.Id == order.Id)
  689. .Where((snapshot, order) => order.CreationTime >= dto.StartTime && order.CreationTime <= dto.EndTime && order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6")) == dto.OrgCode)
  690. .Select((snapshot, order) => new SnapshotDepartmentAveTimeStatisticsDetailsOutDto(), true);
  691. return query;
  692. }
  693. [ExportExcel("检查合规统计", "OrgName")]
  694. public ISugarQueryable<CompliantStatisticsOutDto> GetCompliantStatistics(CompliantStatisticsInDto dto)
  695. {
  696. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  697. .LeftJoin<Order>((snapshot, order) => snapshot.Id == order.Id)
  698. .LeftJoin<OrderSendBackAudit>((snapshot, order, back) => snapshot.Id == back.OrderId && back.State == ESendBackAuditState.End)
  699. .LeftJoin<OrderVisit>((snapshot, order, back, visit) => snapshot.Id == visit.OrderId && visit.VisitState == EVisitState.Visited)
  700. .LeftJoin<OrderSecondaryHandling>((snapshot, order, back, visit, second) => snapshot.Id == second.OrderId && second.State == ESecondaryHandlingState.End)
  701. .Where((snapshot, order) => snapshot.CreationTime >= dto.StartTime && snapshot.CreationTime <= dto.EndTime
  702. && order.ActualHandleOrgCode != null && order.ActualHandleOrgCode != "001")
  703. .GroupBy((snapshot, order) => new
  704. {
  705. OrgCode = order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6"))
  706. })
  707. .Select((snapshot, order, back, visit, second) => new CompliantStatisticsOutDto()
  708. {
  709. OrgCode = order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6")),
  710. //OrderCountNum = SqlFunc.AggregateCount(snapshot.Id),
  711. First = SqlFunc.AggregateSum(SqlFunc.IIF(snapshot.CompliantType == ECompliantType.First, 1, 0)),
  712. Second = SqlFunc.AggregateSum(SqlFunc.IIF(snapshot.CompliantType == ECompliantType.Second, 1, 0)),
  713. Third = SqlFunc.AggregateSum(SqlFunc.IIF(snapshot.CompliantType == ECompliantType.Third, 1, 0)),
  714. Not = SqlFunc.AggregateSum(SqlFunc.IIF(snapshot.CompliantType == ECompliantType.Not, 1, 0)),
  715. })
  716. .MergeTable()
  717. .LeftJoin<SystemOrganize>((it, o) => it.OrgCode == o.Id)
  718. .Select((it, o) => new CompliantStatisticsOutDto()
  719. {
  720. OrgName = o.Name,
  721. OrgCode = it.OrgCode,
  722. //OrderCountNum = it.OrderCountNum,
  723. First = it.First,
  724. Second = it.Second,
  725. Third = it.Third,
  726. Not = it.Not
  727. });
  728. return query;
  729. }
  730. public ISugarQueryable<CompliantStatisticsDetailsOutDto> GetCompliantStatisticsDetails(CompliantStatisticsDetailsInDto dto)
  731. {
  732. dto.FieldName = dto.FieldName.ToLower();
  733. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  734. .LeftJoin<Order>((snapshot, order) => order.Id == snapshot.Id)
  735. .Where((snapshot, order) => snapshot.CreationTime >= dto.StartTime && snapshot.CreationTime <= dto.EndTime)
  736. .WhereIF(dto.OrgCode.NotNullOrEmpty(), (snapshot, order) =>
  737. order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6")) == dto.OrgCode);
  738. query = dto.FieldName switch
  739. {
  740. "ordercountnum" => query.Where(snapshot => snapshot.CompliantType != null),
  741. "first" => query.Where(snapshot => snapshot.CompliantType == ECompliantType.First),
  742. "second" => query.Where(snapshot => snapshot.CompliantType == ECompliantType.Second),
  743. "third" => query.Where(snapshot => snapshot.CompliantType == ECompliantType.Third),
  744. "not" => query.Where(snapshot => snapshot.CompliantType == ECompliantType.Not),
  745. _ => throw new UserFriendlyException($"入参: {dto.FieldName} 异常")
  746. };
  747. var b = query.Select((snapshot, order) => new CompliantStatisticsDetailsOutDto
  748. {
  749. }, true);
  750. #if DEBUG
  751. var sql = b.ToSqlString();
  752. #endif
  753. return b;
  754. }
  755. [ExportExcel("随手拍重办统计")]
  756. public List<Dictionary<string, object>> GetReTransactStatistics(ReTransactStatisticsInDto dto)
  757. {
  758. var rawData = _orderSnapshotRepository.Queryable(includeDeleted: true)
  759. .LeftJoin<Order>((snapshot, order) => order.Id == snapshot.Id)
  760. .Where((snapshot, order) => snapshot.SpecialReasonId != null && snapshot.CreationTime >= dto.StartTime && snapshot.CreationTime <= dto.EndTime)
  761. .GroupBy((snapshot, order) => new
  762. {
  763. OrgCode = order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6")),
  764. SpecialReasonId = snapshot.SpecialReasonId,
  765. snapshot.SpecialReasonName
  766. })
  767. .Select((snapshot, order) => new
  768. {
  769. OrgCode = order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6")),
  770. OrderCountNum = SqlFunc.AggregateCount(snapshot.Id),
  771. SpecialReasonId = snapshot.SpecialReasonId,
  772. SpecialReasonName = snapshot.SpecialReasonName
  773. }, true)
  774. .MergeTable()
  775. .LeftJoin<SystemOrganize>((it, o) => it.OrgCode == o.Id)
  776. .Select((it, o) => new
  777. {
  778. OrgName = o.Name,
  779. OrgCode = it.OrgCode,
  780. OrderCountNum = it.OrderCountNum,
  781. SpecialReasonId = it.SpecialReasonId,
  782. SpecialReasonName = it.SpecialReasonName
  783. }).ToList();
  784. var pivotResult = new List<Dictionary<string, object>>();
  785. var orgGroups = rawData.GroupBy(x => new { x.OrgCode, x.OrgName });
  786. var specialReasons = rawData
  787. .Select(x => x.SpecialReasonId)
  788. .Distinct()
  789. .ToList();
  790. foreach (var orgGroup in orgGroups)
  791. {
  792. var row = new Dictionary<string, object>
  793. {
  794. ["OrgName"] = orgGroup.Key.OrgName,
  795. ["OrgCode"] = orgGroup.Key.OrgCode
  796. };
  797. foreach (var reason in specialReasons)
  798. {
  799. row[reason] = 0;
  800. }
  801. foreach (var item in orgGroup)
  802. {
  803. if (item.SpecialReasonId != null)
  804. {
  805. row[item.SpecialReasonId] = item.OrderCountNum;
  806. }
  807. }
  808. pivotResult.Add(row);
  809. }
  810. pivotResult.AddSumLine("OrgName");
  811. return pivotResult;
  812. }
  813. [ExportExcel("随手拍重办统计")]
  814. public ISugarQueryable<ReTransactStatisticsDetailsOutDto> GetReTransactStatisticsDetail(ReTransactStatisticsDetailsInDto dto)
  815. {
  816. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  817. .LeftJoin<Order>((snapshot, order) => order.Id == snapshot.Id)
  818. .LeftJoin<OrderSpecial>((snapshot, order, special) => special.OrderId == order.Id)
  819. .LeftJoin<SystemOrganize>((snapshot, order, special, org) => org.Id == special.OrgId.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6")))
  820. .Where((snapshot, order, special) => order.CreationTime >= dto.StartTime && order.CreationTime <= dto.EndTime && order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6")) == dto.OrgCode && special.Id != null && snapshot.SpecialReasonId != null && special.SpecialType == ESpecialType.ReTransact)
  821. .WhereIF(dto.FieldName.NotNullOrEmpty(), (snapshot, order) => dto.FieldName == snapshot.SpecialReasonId)
  822. .OrderByDescending((snapshot, order) => order.CreationTime)
  823. .Select((snapshot, order, special, org) => new ReTransactStatisticsDetailsOutDto
  824. {
  825. ReTransactOrgName = special.OrgName, // 被重办部门
  826. ReTransactOneOrgName = org.Name, // 被重办一级部门
  827. ReTransactTime = special.CreationTime, // 重办时间
  828. ReTransactHandlerName = special.CreatorName, // 重办操作人
  829. ReTransactContent = special.Opinion // 重办理由
  830. }, true);
  831. #if DEBUG
  832. var sql = query.ToSqlString();
  833. #endif
  834. return query;
  835. }
  836. public List<Dictionary<string, object>> GetIndustryStatistics(IndustryStatisticsInDto dto)
  837. {
  838. var parentId = _appOptions.Value.AreaCode;
  839. var query = _systemAreaRepository.Queryable(includeDeleted: true)
  840. .LeftJoin<Order>((area, order) => order.County == area.AreaName && order.CreationTime >= dto.StartTime && order.CreationTime <= dto.EndTime && order.County != null)
  841. .LeftJoin<OrderSnapshot>((area, order, snapshot) => order.Id == snapshot.Id)
  842. .Where((area, order, snapshot) => area.ParentId == parentId)
  843. .GroupBy((area, order, snapshot) => new
  844. {
  845. IndustryId = snapshot.IndustryId,
  846. snapshot.IndustryName,
  847. area.AreaName
  848. })
  849. .Select((area, order, snapshot) => new
  850. {
  851. OrgName = area.AreaName,
  852. OrderCountNum = SqlFunc.AggregateCount(snapshot.Id),
  853. AllDuration = SqlFunc.AggregateSum(order.AllDuration),
  854. IsEmphasis = SqlFunc.AggregateSum(SqlFunc.IIF(snapshot.IsEmphasis == true, 1, 0)),
  855. IndustryName = snapshot.IndustryName,
  856. IndustryId = snapshot.IndustryId,
  857. })
  858. .FullJoin<Industry>((it, o) => it.IndustryId == o.Id)
  859. .Select((it, o) => new
  860. {
  861. IndustryName = o.Name,
  862. IndustryId = o.Id,
  863. OrderCountNum = it.OrderCountNum,
  864. AllDuration = it.AllDuration,
  865. OrgName = it.OrgName,
  866. });
  867. #if DEBUG
  868. var sql = query.ToSqlString();
  869. #endif
  870. var rawData = query.ToList();
  871. var pivotResult = new List<Dictionary<string, object>>();
  872. var industryGroups = rawData.GroupBy(x => new { x.IndustryId, x.IndustryName }).ToList();
  873. var orgNames = rawData
  874. .Select(x => x.OrgName)
  875. .Distinct()
  876. .ToList();
  877. foreach (var industryItem in industryGroups)
  878. {
  879. if (industryItem.Key.IndustryName == null) continue;
  880. var row = new Dictionary<string, object>
  881. {
  882. ["IndustryName"] = industryItem.Key.IndustryName,
  883. ["IndustryId"] = industryItem.Key.IndustryId,
  884. ["AllDuration"] = 0,
  885. ["OrderCountNum"] = 0,
  886. };
  887. foreach (var orgCode in orgNames)
  888. {
  889. if (orgCode != null)
  890. {
  891. row[orgCode] = 0;
  892. }
  893. }
  894. foreach (var item in industryItem)
  895. {
  896. if (item.IndustryId != null && item.OrgName != null)
  897. {
  898. row[item.OrgName] = item.OrderCountNum;
  899. row["AllDuration"] = item.AllDuration;
  900. row["OrderCountNum"] = int.Parse(row["OrderCountNum"].ToString()) + item.OrderCountNum;
  901. if (item.AllDuration != null && item.AllDuration != 0)
  902. row["AllDuration"] = ((double)row["AllDuration"] + (double)item.AllDuration / 60 / 60).ToString("f2");
  903. }
  904. }
  905. pivotResult.Add(row);
  906. }
  907. pivotResult.AddSumLine("IndustryName");
  908. return pivotResult;
  909. }
  910. public ISugarQueryable<IndustryStatisticsDetailsOutDto> GetIndustryStatisticsDetails(IndustryStatisticsDetailsInDto dto)
  911. {
  912. var query = _orderSnapshotRepository.Queryable(includeDeleted: true)
  913. .LeftJoin<Order>((snapshot, order) => snapshot.Id == order.Id)
  914. .Where((snapshot, order) => snapshot.CreationTime >= dto.StartTime && snapshot.CreationTime <= dto.EndTime && snapshot.IndustryId == dto.IndustryId)
  915. .WhereIF(dto.FieldName != "OrderCountNum", (Snapshot, order) => order.County == dto.FieldName)
  916. .Select((snapshot, order) => new IndustryStatisticsDetailsOutDto(), true);
  917. return query;
  918. }
  919. public ISugarQueryable<SnapshotCountyPointsStatisticsOutDto> GetAreaPointsStatistics(SnapshotCountyPointsStatisticsInDto dto)
  920. {
  921. var query = _snapshotPointsRecordRepository.Queryable(includeDeleted: true)
  922. .LeftJoin<Order>((points, order) => points.OrderId == order.Id)
  923. .Where((points, order) => order.CreationTime >= dto.StartTime && order.CreationTime <= dto.EndTime && order.County != "" && order.County != null)
  924. .GroupBy((points, order) => order.County)
  925. .Select((points, order) => new SnapshotCountyPointsStatisticsOutDto
  926. {
  927. CountyName = order.County,
  928. Points = SqlFunc.AggregateSum(SqlFunc.IIF(points.Direction == EPointsDirection.In, points.Points, 0)),
  929. ExchangePoints = SqlFunc.AggregateSum(SqlFunc.IIF(points.Source == EPointsSource.Exchange, points.Points, 0)),
  930. });
  931. #if DEBUG
  932. var sql = query.ToSqlString();
  933. #endif
  934. return query;
  935. }
  936. }