public void Handle(CreateEquipmentInfoCommand command) { var equipmentInfo = command.MapToEntity <EquipmentInfo>(); var category = _repository.Get <EquipmentCategory>(command.CategoryId); equipmentInfo.EquipmentCategory = category ?? throw new DomainValidationException("产品大类未选择!"); if (command.CategoryId1 != 0) { equipmentInfo.EquipmentCategory1 = new EquipmentCategory { Id = command.CategoryId1 }; } equipmentInfo.ImageUrl = SaveFile(command.File.FileBytes, command.File.FileName); _repository.Create(equipmentInfo); var index = 0; foreach (var categoryColun in category.Columns) { if (categoryColun.ColumnType == EquipmentCategoryColumnType.文件.ToString()) { var firstOrDefault = command.Files.FirstOrDefault(); if (firstOrDefault != null) { var equipmentInfoColumnValue = new EquipmentInfoColumnValue { EquipmentInfo = equipmentInfo, Value = SaveFile(firstOrDefault.FileBytes, firstOrDefault.FileName, false) }; _repository.Create(equipmentInfoColumnValue); command.Files.Remove(firstOrDefault); } } else { var type = category.Columns[index].ColumnType; IsValidTypeValue(type, command.Values[index]); if (type == EquipmentCategoryColumnType.小数.ToString() && string.IsNullOrEmpty(command.Values[index])) { command.Values[index] = "0"; } var equipmentInfoColumnValue = new EquipmentInfoColumnValue { EquipmentInfo = equipmentInfo, Value = command.Values[index] }; _repository.Create(equipmentInfoColumnValue); index++; } } var fileName = SaveQrCodeImage(equipmentInfo); equipmentInfo.QrCodeImage = fileName; _repository.Update(equipmentInfo); }
public void Handle(EditEquipmentInfoCommand command) { var equipmentInfo = _repository.Get <EquipmentInfo>(command.EquipmentInfoId); equipmentInfo.Manufacturer = command.Manufacturer; equipmentInfo.BatchNum = command.BatchNum; equipmentInfo.CheckResult = command.CheckResult; equipmentInfo.Checker = command.Checker; equipmentInfo.ExecuteStandard = command.ExecuteStandard; equipmentInfo.IdentifierNo = command.IdentifierNo; equipmentInfo.Meterial = command.Meterial; equipmentInfo.Name = command.Name; equipmentInfo.OutDateTime = command.OutDateTime; equipmentInfo.Picker = command.Picker; equipmentInfo.SetupLocation = command.SetupLocation; equipmentInfo.Specification = command.Specification; equipmentInfo.Supplier = command.Supplier; equipmentInfo.Technician = command.Technician; if (command.File.FileBytes != null && !string.IsNullOrEmpty(command.File.FileName)) { equipmentInfo.ImageUrl = SaveFile(command.File.FileBytes, command.File.FileName); } if (command.CategoryId1 != 0) { equipmentInfo.EquipmentCategory1 = new EquipmentCategory { Id = command.CategoryId1 }; } _repository.Update(equipmentInfo); var category = equipmentInfo.EquipmentCategory; var index = 0; foreach (var categoryColun in category.Columns) { if (equipmentInfo.EquipmentInfoColumnValues.Count == 0 || index >= equipmentInfo.EquipmentInfoColumnValues.Count) { if (categoryColun.ColumnType == EquipmentCategoryColumnType.文件.ToString()) { var firstOrDefault = command.Files.FirstOrDefault(); if (firstOrDefault != null) { if (firstOrDefault.FileBytes != null && !string.IsNullOrEmpty(firstOrDefault.FileName)) { var value = new EquipmentInfoColumnValue { EquipmentInfo = equipmentInfo, Value = SaveFile(firstOrDefault.FileBytes, firstOrDefault.FileName, false) }; _repository.Create(value); } command.Files.Remove(firstOrDefault); } } else { var type = category.Columns[index].ColumnType; IsValidTypeValue(type, command.Values[index]); var value = new EquipmentInfoColumnValue { EquipmentInfo = equipmentInfo, Value = command.Values[index] }; _repository.Create(value); } } else { var equipmentInfoColumnValue = equipmentInfo.EquipmentInfoColumnValues[index]; if (categoryColun.ColumnType == EquipmentCategoryColumnType.文件.ToString()) { var firstOrDefault = command.Files.FirstOrDefault(); if (firstOrDefault != null) { if (firstOrDefault.FileBytes != null && !string.IsNullOrEmpty(firstOrDefault.FileName)) { if (equipmentInfoColumnValue != null) { equipmentInfoColumnValue.Value = SaveFile(firstOrDefault.FileBytes, firstOrDefault.FileName, false); _repository.Update(equipmentInfoColumnValue); } else { var value = new EquipmentInfoColumnValue { EquipmentInfo = equipmentInfo, Value = SaveFile(firstOrDefault.FileBytes, firstOrDefault.FileName, false) }; _repository.Create(value); } } command.Files.Remove(firstOrDefault); } } else { var type = category.Columns[index].ColumnType; IsValidTypeValue(type, command.Values[index]); if (equipmentInfoColumnValue != null) { equipmentInfoColumnValue.Value = command.Values[index]; _repository.Update(equipmentInfoColumnValue); } else { var value = new EquipmentInfoColumnValue { EquipmentInfo = equipmentInfo, Value = command.Values[index] }; _repository.Create(value); } } } index++; } }
public ImportBatchEquipmentInfoCommand.Result Handle(ImportBatchEquipmentInfoCommand command) { if (command.FileBytes == null || command.FileBytes.Length == 0) { throw new ApplicationException("请选择附件"); } var excelTypes = new[] { ".xlsx", ".xls" }; var extension = Path.GetExtension(command.FileName); if (extension != null) { var extensionName = extension.ToLower(); if (excelTypes.All(x => x != extensionName)) { throw new ApplicationException("请选择EXCEL文件进行导入!"); } } var result = new ImportBatchEquipmentInfoCommand.Result(); using (var stream = new MemoryStream(command.FileBytes)) { stream.Seek(0, SeekOrigin.Begin); var workbook = new HSSFWorkbook(stream); var sheet = workbook.GetSheetAt(0); var row = sheet.GetRow(0); var cell = row.GetCell(0)?.StringCellValue; if (cell != "ID") { throw new ApplicationException("此EXCEL文件不是所导出的模板文件!"); } var categoryId = sheet.GetRow(1).GetCell(0).GetCellValue(); var categories = _repository.Query <EquipmentCategory>(); var category = categories.SingleOrDefault(x => x.Id == int.Parse(categoryId)); if (category == null) { throw new ApplicationException("设备分类匹配失败!"); } var equipmentInfo = new EquipmentInfo { EquipmentCategory = category }; for (int rowNum = 1; rowNum <= sheet.LastRowNum; rowNum++) { var manufacturer = sheet.GetRow(rowNum).GetCell(2).GetCellValue(); var batchNum = sheet.GetRow(rowNum).GetCell(3).GetCellValue(); var categoryName1 = sheet.GetRow(rowNum).GetCell(4).GetCellValue(); var name = sheet.GetRow(rowNum).GetCell(5).GetCellValue(); var identifierNo = sheet.GetRow(rowNum).GetCell(6).GetCellValue(); var specification = sheet.GetRow(rowNum).GetCell(7).GetCellValue(); var meterial = sheet.GetRow(rowNum).GetCell(8).GetCellValue(); var technician = sheet.GetRow(rowNum).GetCell(9).GetCellValue(); var supplier = sheet.GetRow(rowNum).GetCell(10).GetCellValue(); var picker = sheet.GetRow(rowNum).GetCell(11).GetCellValue(); var outDateTime = sheet.GetRow(rowNum).GetCell(12)?.GetCellDateTime()?.ToString(); var checker = sheet.GetRow(rowNum).GetCell(13).GetCellValue(); var checkResult = sheet.GetRow(rowNum).GetCell(14).GetCellValue(); var executeStandard = sheet.GetRow(rowNum).GetCell(15).GetCellValue(); var setupLocation = sheet.GetRow(rowNum).GetCell(16).GetCellValue(); equipmentInfo.Manufacturer = manufacturer; int batchNumResult = 0; if (string.IsNullOrEmpty(batchNum)) { //result.IsSucceed = false; //result.Errors.Add($"第{rowNum}行的批次未填写!"); //break; throw new DomainValidationException($"第{rowNum}行的批次未填写!"); } if (!int.TryParse(batchNum, out batchNumResult)) { //result.IsSucceed = false; //result.Errors.Add($"第{rowNum}行的批次填写错误!"); //break; throw new DomainValidationException($"第{rowNum}行的批次填写错误!"); } equipmentInfo.BatchNum = Convert.ToInt32(batchNum); equipmentInfo.CheckResult = checkResult; equipmentInfo.Checker = checker; equipmentInfo.ExecuteStandard = executeStandard; equipmentInfo.IdentifierNo = identifierNo; equipmentInfo.Meterial = meterial; equipmentInfo.Name = name; if (outDateTime == null) { //result.IsSucceed = false; //result.Errors.Add($"第{rowNum}行的出厂日期未填写!"); //break; throw new DomainValidationException($"第{rowNum}行的出厂日期未填写!"); } equipmentInfo.OutDateTime = Convert.ToDateTime(outDateTime); equipmentInfo.Picker = picker; equipmentInfo.SetupLocation = setupLocation; equipmentInfo.Specification = specification; equipmentInfo.Supplier = supplier; equipmentInfo.Technician = technician; if (!string.IsNullOrEmpty(categoryName1)) { var category1 = categories.FirstOrDefault(x => x.Name == categoryName1); equipmentInfo.EquipmentCategory1 = category1; } _repository.Create(equipmentInfo); var fileName = SaveQrCodeImage(equipmentInfo); equipmentInfo.QrCodeImage = fileName; _repository.Update(equipmentInfo); for (int i = 0; i < category.Columns.Count; i++) { var cellStartIndex = i + 17; var type = category.Columns[i].ColumnType; var currentRow = sheet.GetRow(rowNum); var currentCell = currentRow.GetCell(cellStartIndex); var value = currentCell?.GetCellValue(); if (type == EquipmentCategoryColumnType.日期.ToString()) { value = currentCell?.GetCellDateTime().ToString(); } IsValidTypeValue(type, value, rowNum, cellStartIndex); var columnValue = new EquipmentInfoColumnValue { Value = value, EquipmentInfo = equipmentInfo }; _repository.Create(columnValue); } } } return(result); }