|
@@ -15,6 +15,10 @@ using ZR.Model.Dto.Inspect;
|
|
|
using ZR.Model.Models.Inspect;
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
using AspectCore.DynamicProxy.Parameters;
|
|
|
+using ExcelDataReader;
|
|
|
+using System.Data;
|
|
|
+using Microsoft.IdentityModel.Tokens;
|
|
|
+using Aliyun.OSS;
|
|
|
|
|
|
namespace ZR.Admin.WebApi.Controllers.Inspect
|
|
|
{
|
|
@@ -135,59 +139,161 @@ namespace ZR.Admin.WebApi.Controllers.Inspect
|
|
|
[ActionPermissionFilter(Permission = "business:sapdeliverrecord:import")]
|
|
|
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile, [FromForm(Name = "category")] string category)
|
|
|
{
|
|
|
- List<dynamic>? list = new();
|
|
|
+ //List<dynamic>? list = new();
|
|
|
+ DataSet dataSet = new();
|
|
|
//读取上传文件内容。
|
|
|
using (var stream = formFile.OpenReadStream())
|
|
|
{
|
|
|
- var SheetNames = stream.GetSheetNames();
|
|
|
- //list = stream.Query<SapDeliverRecord>(startCell:"A6").ToList();
|
|
|
- //list = stream.Query(sheetName:"Sheet2").ToList();
|
|
|
- list = stream.Query(sheetName: SheetNames[1]).ToList();
|
|
|
+ IExcelDataReader? readerData = null;
|
|
|
+ //根据excel扩展名选择对应的读取文件流方式
|
|
|
+ switch (formFile.FileName.Split('.').Last())
|
|
|
+ {
|
|
|
+ case "xls": readerData = ExcelReaderFactory.CreateBinaryReader(stream); break;
|
|
|
+ case "xlsx": readerData = ExcelReaderFactory.CreateOpenXmlReader(stream); break;
|
|
|
+ default: break;
|
|
|
+ }
|
|
|
+ if (readerData == null)
|
|
|
+ {
|
|
|
+ return SUCCESS("导入失败。<br/><br/>导入交验单文件错误。文件不是xls文件也不是xlsx文件。");
|
|
|
+ }
|
|
|
+ //验证交验单表头
|
|
|
+ dataSet = readerData.AsDataSet();
|
|
|
+ if (!dataSet.Tables[0].Rows[0][0].ToString().Contains("北京七星华创"))
|
|
|
+ {
|
|
|
+ return SUCCESS($"导入失败。<br/><br/>导入Excel的【{dataSet.Tables[0].TableName}】中<br/>表头为:{dataSet.Tables[0].Rows[0][0]}<br/>请确认文件无误。");
|
|
|
+ }
|
|
|
+ //通过交验单号,验证是否以存在交验单
|
|
|
+ string checkDeliverNo = dataSet.Tables[1].Rows[1][2].ToString().Trim();
|
|
|
+ if (string.IsNullOrEmpty( checkDeliverNo))
|
|
|
+ {
|
|
|
+ return SUCCESS($"导入失败。<br/><br/>导入Excel的【{dataSet.Tables[1].TableName}】中<br/>C列交验单号有误。");
|
|
|
+ }
|
|
|
+ var response = _SapDeliverRecordService.GetList(
|
|
|
+ new SapDeliverRecordQueryDto { DeliverNo = checkDeliverNo });
|
|
|
+ if (response.Result.Count>0)
|
|
|
+ {
|
|
|
+ return SUCCESS($"导入失败。<br/><br/>导入交验单【{checkDeliverNo}】已存在。");
|
|
|
+ }
|
|
|
+
|
|
|
+ //MiniExcel读取导入的Excel文件,因不支持xls格式,弃用。
|
|
|
+ //var SheetNames = stream.GetSheetNames();
|
|
|
+ //list = stream.Query(sheetName: SheetNames[1]).ToList();
|
|
|
|
|
|
}
|
|
|
- //string deliverNo = list[2].M;
|
|
|
- //if (string.IsNullOrEmpty(deliverNo))
|
|
|
- //{
|
|
|
- // return SUCCESS("未找到交验单编号,导入文件格式错误");
|
|
|
- //}
|
|
|
- //deliverNo = deliverNo.Replace("编号", "").Replace(":", "").Replace(":", "");
|
|
|
- //string checkName = list[3].A;
|
|
|
- //checkName = string.IsNullOrEmpty(checkName) ? "" : checkName.Replace("送检人员", "").Replace(":", "").Replace(":", "");
|
|
|
+
|
|
|
List<SapDeliverRecord> sapDeliverRecords = new();
|
|
|
- for (int i = 1; i < list.Count; i++)
|
|
|
+ //实例化交验单表对象
|
|
|
+ for (int i = 1; i < dataSet.Tables[1].Rows.Count - 1; i++)
|
|
|
+ //去除第一行标题行,和最后一行合计行
|
|
|
{
|
|
|
- if (string.IsNullOrEmpty($"{list[i].A}"))
|
|
|
+ //row中空单元格会转化为空字符串而不是null
|
|
|
+ SapDeliverRecord sapDeliverRecord = new();
|
|
|
+ if (string.IsNullOrEmpty(dataSet.Tables[1].Rows[i][2].ToString().Trim()))
|
|
|
+ {
|
|
|
+ return SUCCESS($"导入失败。<br/><br/>导入Excel的【{dataSet.Tables[1].TableName}】中{i + 1}行C列,交验单编号有误。");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ sapDeliverRecord.DeliverNo = dataSet.Tables[1].Rows[i][2].ToString().Trim();//*
|
|
|
+ sapDeliverRecord.CheckName = dataSet.Tables[1].Rows[i][32].ToString().Trim();
|
|
|
+ if (string.IsNullOrEmpty(dataSet.Tables[1].Rows[i][14]!.ToString().Trim()))
|
|
|
+ {
|
|
|
+ return SUCCESS($"导入失败。<br/><br/>导入Excel的【{dataSet.Tables[1].TableName}】中{i + 1}行O列,生产令号有误。");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ sapDeliverRecord.BomNo = dataSet.Tables[1].Rows[i][14].ToString().Trim();//*
|
|
|
+ if (string.IsNullOrEmpty(dataSet.Tables[1].Rows[i][3].ToString().Trim()))
|
|
|
{
|
|
|
- break;//序号一列出现空,即交验单内容结束
|
|
|
+ return SUCCESS($"导入失败。<br/><br/>导入Excel的【{dataSet.Tables[1].TableName}】中{i + 1}行D列,行项目有误。");
|
|
|
}
|
|
|
- SapDeliverRecord sapDeliverRecord = new()
|
|
|
+ else
|
|
|
+ sapDeliverRecord.RowNo = Convert.ToInt32(dataSet.Tables[1].Rows[i][3]);//*
|
|
|
+ if (string.IsNullOrEmpty(dataSet.Tables[1].Rows[i][15].ToString().Trim()))
|
|
|
{
|
|
|
- DeliverNo = list[i].C,
|
|
|
- CheckName = list[i].AG,
|
|
|
- BomNo = list[i].O,
|
|
|
- RowNo = Convert.ToInt32(list[i].D),
|
|
|
- PurchaseOrder = list[i].P.ToString(),
|
|
|
- MaterialCode = $"MAT{list[i].T}",//非空 模拟数据(交验单中没有这个编码,要自己规范一个吗?
|
|
|
- MaterialName = list[i].S,
|
|
|
- MaterialSpec = list[i].T,
|
|
|
- Category = category,
|
|
|
- KeyFlag = i % 2 > 0,//非空 模拟数据
|
|
|
- Qty = Convert.ToInt32(list[i].V),
|
|
|
- SpotCheckQty = Convert.ToInt32(list[i].X),
|
|
|
- CheckQty = Convert.ToInt32(list[i].AB),
|
|
|
- CheckResult = list[i].X > 0 ? ((list[i].AB < list[i].X) ? (list[i].AB > 0 ? "PartOK" : "NG") : "OK") : "Wait",//计算质检结果,抽检数量为0是未质检Wait,合格数为0则NG,大于零小于抽检数则PartOK,合格数量等于抽检数量则全合格OK
|
|
|
- InstoreResult = 99,//非空 模拟数据
|
|
|
-
|
|
|
- CreateBy = HttpContext.GetName(),
|
|
|
- CreateTime = DateTime.Now,
|
|
|
- Remark = "导入数据"
|
|
|
- };
|
|
|
+ return SUCCESS($"导入失败。<br/><br/>导入Excel的【{dataSet.Tables[1].TableName}】中{i + 1}行P列,采购订单号有误。");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ sapDeliverRecord.PurchaseOrder = dataSet.Tables[1].Rows[i][15].ToString().Trim();//*
|
|
|
+
|
|
|
+ sapDeliverRecord.MaterialCode = $"MaterialCode";//*//非空 模拟数据(交验单中没有这个编码,要自己规范一个吗?
|
|
|
+ if (string.IsNullOrEmpty(dataSet.Tables[1].Rows[i][18].ToString().Trim()))
|
|
|
+ {
|
|
|
+ return SUCCESS($"导入失败。<br/><br/>导入Excel的【{dataSet.Tables[1].TableName}】中{i + 1}行S列,物料名称有误。");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ sapDeliverRecord.MaterialName = dataSet.Tables[1].Rows[i][18].ToString().Trim();//*
|
|
|
+ if (string.IsNullOrEmpty(dataSet.Tables[1].Rows[i][19].ToString().Trim()))
|
|
|
+ {
|
|
|
+ return SUCCESS($"导入失败。<br/><br/>导入Excel的【{dataSet.Tables[1].TableName}】中{i + 1}行T列,规格型号/图号有误。");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ sapDeliverRecord.MaterialSpec = dataSet.Tables[1].Rows[i][19].ToString().Trim();//*
|
|
|
+
|
|
|
+ sapDeliverRecord.Category = category;//*
|
|
|
+ sapDeliverRecord.KeyFlag = i % 2 > 0;//*//非空 模拟数据
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(dataSet.Tables[1].Rows[i][21].ToString().Trim()))
|
|
|
+ {
|
|
|
+ return SUCCESS($"导入失败。<br/><br/>导入Excel的【{dataSet.Tables[1].TableName}】中{i + 1}行V列,物料数量有误。");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ sapDeliverRecord.Qty = Convert.ToInt32(dataSet.Tables[1].Rows[i][21]);//*
|
|
|
+ if (string.IsNullOrEmpty(dataSet.Tables[1].Rows[i][23].ToString().Trim()))
|
|
|
+ {
|
|
|
+ return SUCCESS($"导入失败。<br/><br/>导入Excel的【{dataSet.Tables[1].TableName}】中{i + 1}行X列,抽检数量有误。");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ sapDeliverRecord.SpotCheckQty = Convert.ToInt32(dataSet.Tables[1].Rows[i][23]);//*
|
|
|
+
|
|
|
+ sapDeliverRecord.CheckQty = string.IsNullOrEmpty(dataSet.Tables[1].Rows[i][27].ToString().Trim()) ? 0 : Convert.ToInt32(dataSet.Tables[1].Rows[i][27].ToString().Trim());
|
|
|
+ sapDeliverRecord.InstoreResult = 99;//*//非空 模拟数据
|
|
|
+
|
|
|
+ sapDeliverRecord.CreateBy = HttpContext.GetName();
|
|
|
+ sapDeliverRecord.CreateTime = DateTime.Now;
|
|
|
+ sapDeliverRecord.Remark = "导入数据";
|
|
|
+ string v = sapDeliverRecord.SpotCheckQty > 0 ? ((sapDeliverRecord.CheckQty < sapDeliverRecord.SpotCheckQty) ? (sapDeliverRecord.CheckQty > 0 ? "PartOK" : "NG") : "OK") : "Wait";
|
|
|
+ sapDeliverRecord.CheckResult = v;//计算质检结果,抽检数量为0是未质检Wait,合格数为0则NG,大于零小于抽检数则PartOK,合格数量等于抽检数量则全合格OK
|
|
|
sapDeliverRecords.Add(sapDeliverRecord);
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
+ #region MiniExcel读取导入的Excel文件,因不支持xls格式,弃用。
|
|
|
+ //for (int i = 1; i < list.Count; i++)
|
|
|
+ //{
|
|
|
+ // if (string.IsNullOrEmpty($"{list[i].A}"))
|
|
|
+ // {
|
|
|
+ // break;//序号一列出现空,即交验单内容结束
|
|
|
+ // }
|
|
|
+ // SapDeliverRecord sapDeliverRecord = new()
|
|
|
+ // {
|
|
|
+ // DeliverNo = list[i].C,
|
|
|
+ // CheckName = list[i].AG,
|
|
|
+ // BomNo = list[i].O,
|
|
|
+ // RowNo = Convert.ToInt32(list[i].D),
|
|
|
+ // PurchaseOrder = list[i].P.ToString(),
|
|
|
+ // MaterialCode = $"MAT{list[i].T}",//非空 模拟数据(交验单中没有这个编码,要自己规范一个吗?
|
|
|
+ // MaterialName = list[i].S,
|
|
|
+ // MaterialSpec = list[i].T,
|
|
|
+ // Category = category,
|
|
|
+ // KeyFlag = i % 2 > 0,//非空 模拟数据
|
|
|
+ // Qty = Convert.ToInt32(list[i].V),
|
|
|
+ // SpotCheckQty = Convert.ToInt32(list[i].X),
|
|
|
+ // CheckQty = Convert.ToInt32(list[i].AB),
|
|
|
+ // CheckResult = list[i].X > 0 ? ((list[i].AB < list[i].X) ? (list[i].AB > 0 ? "PartOK" : "NG") : "OK") : "Wait",//计算质检结果,抽检数量为0是未质检Wait,合格数为0则NG,大于零小于抽检数则PartOK,合格数量等于抽检数量则全合格OK
|
|
|
+ // InstoreResult = 99,//非空 模拟数据
|
|
|
+
|
|
|
+ // CreateBy = HttpContext.GetName(),
|
|
|
+ // CreateTime = DateTime.Now,
|
|
|
+ // Remark = "导入数据"
|
|
|
+ // };
|
|
|
+ // sapDeliverRecords.Add(sapDeliverRecord);
|
|
|
+ //}
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
string msg = _SapDeliverRecordService.ImportDatas(sapDeliverRecords);
|
|
|
|
|
|
//TODO 业务逻辑,自行插入数据到db
|
|
|
- return SUCCESS(sapDeliverRecords);
|
|
|
+ return SUCCESS("交验单导入成功!");
|
|
|
}
|
|
|
|
|
|
/// <summary>
|