// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995 // // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证 using System.ComponentModel; using System.ComponentModel.DataAnnotations; using Admin.NET.Bodk.Core; using Admin.NET.Bodk.Genetic.Entities.Reports; using Admin.NET.Bodk.Genetic.Entities.Tumors; using Admin.NET.Bodk.Genetic.Models.Reports; using Admin.NET.Core; using Admin.NET.Core.Service; using Furion.FriendlyException; using Mapster; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace Admin.NET.Bodk.Genetic; [ApiDescriptionSettings(Groups = new[] { "Bodk Groups" }, Name = "GeneReport", Description = "基因检测服务")] public class GeneReportService( UserManager userManager, SysOrgService sysOrgService, SysUserService sysUserService, SqlSugarRepository geneTestingTumorLocusReportRepository, SqlSugarRepository reportRepository, SqlSugarRepository geneTestingTumorReportRepository) : ServiceBase(userManager, sysOrgService, sysUserService) { [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + "," + SignatureAuthenticationDefaults.AuthenticationScheme)] [DisplayName("Add")] public async Task AddReport(AddReportInput input) { var db = geneTestingTumorLocusReportRepository.Context; try { await db.Ado.BeginTranAsync(); var entity = input.Adapt(); // var entity = new GeneTestingReportEntity() // { // SampleType = input.SampleType, // TestingType = input.TestingType, // TestingMethod = input.TestingMethod, // TofMs = input.TofMs, // GenotypingAnalysis = input.GenotypingAnalysis, // SampleQuality = input.SampleQuality, // GeneticRiskAssessment = input.GeneticRiskAssessment, // Barcode = input.Barcode, // DnaExtraction = input.DnaExtraction, // CustomerId = input.CustomerId, // ReportTime = input.ReportTime, // Report = input.Report, // Pcr = input.Pcr, // SubmissionTime = input.SubmissionTime, // }; entity = await AddBefore(entity); var reportId = await db.Insertable(entity).ExecuteReturnSnowflakeIdAsync(); foreach (var tumor in input.Tumors) { var tumorEntity = new GeneTestingTumorReportEntity() { TumorId = tumor.TumorId, RiskScore = tumor.RiskScore, RiskLevel = tumor.RiskLevel, ReportId = reportId }; tumorEntity = await AddBefore(tumorEntity); var tumorId = await db.Insertable(tumorEntity).ExecuteReturnSnowflakeIdAsync(); foreach (var locus in tumor.Loci) { var locusEntity = new GeneTestingTumorLocusReportEntity() { LocusId = locus.LocusId, RiskId = locus.RiskId, TumorId = tumorId }; locusEntity = await AddBefore(locusEntity); await db.Insertable(locusEntity).ExecuteReturnSnowflakeIdAsync(); } } await db.Ado.CommitTranAsync(); } catch { await db.Ado.RollbackTranAsync(); throw; } } [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + "," + SignatureAuthenticationDefaults.AuthenticationScheme)] [DisplayName("GetList")] public async Task> GetList([Required] long customerId) { var db = reportRepository.Context; var reports = await db.Queryable() .Where(r => r.CustomerId == customerId) .Select(r => new ReportOutput() { SampleType = r.SampleType, TestingType = r.TestingType, TestingMethod = r.TestingMethod, TofMs = r.TofMs, GenotypingAnalysis = r.GenotypingAnalysis, SampleQuality = r.SampleQuality, GeneticRiskAssessment = r.GeneticRiskAssessment, Barcode = r.Barcode, DnaExtraction = r.DnaExtraction, CustomerId = r.CustomerId, Id = r.Id, ReportTime = r.ReportTime, Report = r.Report, Pcr = r.Pcr, SubmissionTime = r.SubmissionTime }).ToListAsync(); foreach (var report in reports) { var tumors = await db.Queryable() .LeftJoin((r, t) => r.TumorId == t.Id) .Where(r => r.ReportId == report.Id) .Select((r, t) => new TumorOutput() { AvaRiskScore = t.AvaRiskScore, TumorName = t.TumorName, Description = t.Description, Code = t.Code, RiskScore = r.RiskScore, RiskLevel = r.RiskLevel, ReportId = report.Id, TumorId = r.TumorId, Id = r.Id, }).ToListAsync(); foreach (var tumor in tumors) { var loci = await db.Queryable() .LeftJoin((l, gl) => l.LocusId == gl.Id) .LeftJoin((l, gl, gt) => l.RiskId == gt.Id) .Where(l => l.TumorId == tumor.Id) .Select((l, gl, gt) => new LocusOutput() { Description = gt.Description, Risk = gt.Risk, GenekType = gt.GenekType, Position = gl.Position, LocusName = gl.Name, Id = l.Id, }).ToListAsync(); tumor.Loci = loci; } report.Tumors = tumors; } return reports; } [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + "," + SignatureAuthenticationDefaults.AuthenticationScheme)] [DisplayName("Bind")] public async Task Bind(BindInput input) { GeneTestingReportEntity entity = new GeneTestingReportEntity() { CustomerId = input.CustomerId, Barcode = input.Barcode }; entity = await AddBefore(entity); return await reportRepository.InsertReturnSnowflakeIdAsync(entity); } [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + "," + SignatureAuthenticationDefaults.AuthenticationScheme)] [DisplayName("Update")] public async Task UpdateReport(UpdateReportInput input) { var db = geneTestingTumorLocusReportRepository.Context; try { await db.Ado.BeginTranAsync(); var entity = new GeneTestingReportEntity() { Id = input.Id, SampleType = input.SampleType, TestingType = input.TestingType, TestingMethod = input.TestingMethod, TofMs = input.TofMs, GenotypingAnalysis = input.GenotypingAnalysis, SampleQuality = input.SampleQuality, GeneticRiskAssessment = input.GeneticRiskAssessment, Barcode = input.Barcode, DnaExtraction = input.DnaExtraction, CustomerId = input.CustomerId, ReportTime = input.ReportTime, Report = input.Report, Pcr = input.Pcr, SubmissionTime = input.SubmissionTime, }; entity = await UpdateBefore(entity); await db.Updateable(entity).ExecuteCommandAsync(); foreach (var tumor in input.Tumors) { var tumorEntity = new GeneTestingTumorReportEntity() { TumorId = tumor.TumorId, RiskScore = tumor.RiskScore, RiskLevel = tumor.RiskLevel, ReportId = input.Id }; long tumorId = 0; if (tumor.Id is null) { tumorEntity = await AddBefore(tumorEntity); tumorId = await db.Insertable(tumorEntity).ExecuteReturnSnowflakeIdAsync(); } else { tumorEntity.Id=tumor.Id.Value; tumorEntity = await UpdateBefore(tumorEntity); await db.Updateable(tumorEntity).ExecuteCommandAsync(); tumorId = tumor.Id.Value; } foreach (var locus in tumor.Loci) { var locusEntity = new GeneTestingTumorLocusReportEntity() { LocusId = locus.LocusId, RiskId = locus.RiskId, TumorId = tumorId }; if (locus.Id is null) { locusEntity = await AddBefore(locusEntity); await db.Insertable(locusEntity).ExecuteReturnSnowflakeIdAsync(); } else { locusEntity.Id=locus.Id.Value; locusEntity = await UpdateBefore(locusEntity); await db.Updateable(locusEntity).ExecuteCommandAsync(); } } } await db.Ado.CommitTranAsync(); } catch { await db.Ado.RollbackTranAsync(); throw; } } // public async Task GetSingle([Required] string barcode) // { // } }