public async Task <bool> AddConfigDataAsync(HealthManjuuCoreContext context, JobConfiguration entity) { await context.JobConfigurations.AddAsync(entity); int count = await context.SaveChangesAsync(); return(count > 0); }
public async Task <DataBoxDto <EquipmentDto> > QuantitativeTargetsAsync(int current, int capacity) { try { using (HealthManjuuCoreContext context = new HealthManjuuCoreContext()) { var query = context.MachineInfos.Where(p => p.State != DataState.Disable).AsNoTracking(); if (!await query.AnyAsync()) { return(new DataBoxDto <EquipmentDto>()); } int total = await query.CountAsync(); query.OrderBy(p => p.Id); if (capacity > 0) { query = query.Skip((current - 1) * capacity).Take(capacity); } List <MachineInfo> machineInfos = await query.ToListAsync(); var eqs = EntityAutoMapper.Instance.GetMapperResult <List <EquipmentDto> >(_mapperCfg, machineInfos); if (null == eqs || !eqs.Any()) { NLogMgr.ErrorExLog(_errorLog, " List<MachineInfo> 转换 List<EquipmentDto>失败", null); return(new DataBoxDto <EquipmentDto>()); } var box = new DataBoxDto <EquipmentDto>(); box.Data = eqs; box.Total = total; return(box); } } catch (System.Exception ex) { NLogMgr.ErrorExLog(_errorLog, " 批量获取检测目标异常", ex); return(new DataBoxDto <EquipmentDto>()); } }
public async Task <bool> AddConfigDataAsync(CheckConfig config) { try { JobConfiguration jobConfiguration = EntityAutoMapper.Instance.GetMapperResult <JobConfiguration>(_mapperCfg, config); if (null == jobConfiguration) { return(false); } using (HealthManjuuCoreContext context = new HealthManjuuCoreContext()) { return(await AddConfigDataAsync(context, jobConfiguration)); } } catch (System.Exception ex) { NLogMgr.ErrorExLog(_errorLog, "添加检测工具配置异常", ex); return(false); } }
public async Task <List <ToolConfigDto> > GetValidConfigsAsync() { try { using (HealthManjuuCoreContext context = new HealthManjuuCoreContext()) { var query = context.JobConfigurations.Where(p => p.State != DataState.Disable).AsNoTracking(); if (!await query.AnyAsync()) { return(null); } List <JobConfiguration> jobList = await query.ToListAsync(); return(EntityAutoMapper.Instance.GetMapperResult <List <ToolConfigDto> >(_mapperCfg, jobList)); } } catch (System.Exception ex) { NLogMgr.ErrorExLog(_errorLog, "获取检测工具配置异常", ex); return(null); } }
public async Task <bool> UpdateConfigDataAsync(CheckConfig config) { try { if (null == config) { return(false); } JobConfiguration jobConfiguration = EntityAutoMapper.Instance.GetMapperResult <JobConfiguration>(_mapperCfg, config); if (null == jobConfiguration) { return(false); } using (HealthManjuuCoreContext context = new HealthManjuuCoreContext()) { //时间问题,不考虑并发修改问题 using (var transaction = await context.Database.BeginTransactionAsync()) { try { var query = context.JobConfigurations.Where(p => p.Id == config.Id && p.State == DataState.Enable); if (await query.AnyAsync()) { JobConfiguration jobCfg = await query.FirstAsync(); jobCfg.State = DataState.Disable; } jobConfiguration.Id = 0; //数据库会自动生成,把id重置为默认值 bool result = await AddConfigDataAsync(context, jobConfiguration); if (result) { transaction.Commit(); await context.SaveChangesAsync(); } else { transaction.Rollback(); } return(result); } catch (System.Exception ex) { NLogMgr.ErrorExLog(_errorLog, "更新检测工具配置异常", ex); if (null != transaction) { transaction.Rollback(); } return(false); } } } } catch (System.Exception ex) { NLogMgr.ErrorExLog(_errorLog, "执行更新工具配置方法异常", ex); return(false); } }
public async Task <bool> ReplaceTargetsAsync(List <EquipmentDto> equipmentDtos) { try { if (null == equipmentDtos || !equipmentDtos.Any()) { NLogMgr.DebugLog(_programLog, "并没有需要导入的数据"); return(false); } List <MachineInfo> machineInfoList = EntityAutoMapper.Instance.GetMapperResult <List <MachineInfo> >(_mapperCfg, equipmentDtos); if (null == machineInfoList || !machineInfoList.Any()) { NLogMgr.ErrorExLog(_errorLog, " List<EquipmentDto>转换List<MachineInfo>异常", null); return(false); } using (HealthManjuuCoreContext context = new HealthManjuuCoreContext()) { //时间问题,不考虑并发修改问题 using (var transaction = await context.Database.BeginTransactionAsync()) { try { //走这步之前,记得IPC通知客户端通知检测 //Console.WriteLine("重新替换检设备数据,正在删除旧数据"); NLogMgr.DebugLog(_programLog, "重新替换检设备数据,正在删除旧数据"); //把以前的数据清空 var deletedCoount = await context.Database.ExecuteSqlCommandAsync(new RawSqlString($"delete from {nameof(MachineInfo)}s")); //Console.WriteLine($"删除完成,共删除{deletedCoount.ToString()}条数据"); NLogMgr.DebugLog(_programLog, $"删除完成,共删除{deletedCoount.ToString()}条数据"); //Console.WriteLine("进行批量导入数据"); NLogMgr.DebugLog(_programLog, "进行批量导入数据"); await context.MachineInfos.AddRangeAsync(machineInfoList); transaction.Commit(); await context.SaveChangesAsync(); //Console.WriteLine($"成功导入{machineInfoList.Count}条数据"); NLogMgr.DebugLog(_programLog, $"成功导入{machineInfoList.Count}条数据"); return(true); } catch (System.Exception ex) { //System.Console.WriteLine(ex.Message); NLogMgr.ErrorExLog(_errorLog, "批量导入检测目标数据异常", ex); if (null != transaction) { transaction.Rollback(); } return(false); } } } } catch (System.Exception ex) { //System.Console.WriteLine(ex.Message); NLogMgr.ErrorExLog(_errorLog, "方法运行异常", ex); return(false); } }