You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
5.6 KiB
136 lines
5.6 KiB
using Magicodes.ExporterAndImporter.Excel;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using TaskManager.Entity;
|
|
using TaskManager.EntityFramework.Repository;
|
|
using TaskManager.EntityFramework;
|
|
using Wood.Service.Controllers;
|
|
using Magicodes.ExporterAndImporter.Core.Models;
|
|
using TaskManager.Entity.Entitys;
|
|
using System.Transactions;
|
|
using Wood.Util;
|
|
|
|
namespace Wood.Service.Datas
|
|
{
|
|
public class SupplierProAttachmentDataDtService : NormalBaseController<SUPPLIER_PRO_ATTACHMENT_DATA_DT>
|
|
{
|
|
const string _taskName = "附件类数据";
|
|
|
|
private readonly IRepository<TaskSub> _taskSubRepository;
|
|
private readonly IRepository<SUPPLIER_PRO_ATTACHMENT_DATA> _supplierProAttachmentDataRepository;
|
|
private readonly IRepository<TaskConifgure> _taskConfigureRepository;
|
|
private readonly CommonService _commonService;
|
|
|
|
|
|
public SupplierProAttachmentDataDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository<SUPPLIER_PRO_ATTACHMENT_DATA_DT> repository, IRepository<TaskSub> taskSubRepository, IRepository<SUPPLIER_PRO_ATTACHMENT_DATA> supplierProAttachmentDataRepository, IRepository<TaskConifgure> taskConfigureRepository, CommonService commonService) : base(context, builder, configuration, repository)
|
|
{
|
|
_taskSubRepository = taskSubRepository;
|
|
_supplierProAttachmentDataRepository = supplierProAttachmentDataRepository;
|
|
_taskConfigureRepository = taskConfigureRepository;
|
|
|
|
_taskSubRepository.SetDbContext(_context);
|
|
_supplierProAttachmentDataRepository.SetDbContext(_context);
|
|
_taskConfigureRepository.SetDbContext(_context);
|
|
_commonService = commonService;
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("import")]
|
|
public override async Task<IActionResult> Import(IFormFile file)
|
|
{
|
|
if (file == null || file.Length == 0)
|
|
{
|
|
return NotFound();
|
|
}
|
|
MemoryStream memStream = new MemoryStream();
|
|
await file.CopyToAsync(memStream);
|
|
|
|
var importer = new ExcelImporter();
|
|
ImportResult<SUPPLIER_PRO_ATTACHMENT_DATA_DT> impResult = await importer.Import<SUPPLIER_PRO_ATTACHMENT_DATA_DT>(memStream);
|
|
if (impResult.HasError)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
foreach (var rowErr in impResult.RowErrors)
|
|
{
|
|
string temp = string.Join(";", rowErr.FieldErrors.Select(itm => $"第{rowErr.RowIndex}行:{itm.Key}-{itm.Value}"));
|
|
sb.AppendLine(temp);
|
|
}
|
|
foreach (var templateErr in impResult.TemplateErrors)
|
|
{
|
|
string temp = $"列名:{templateErr.RequireColumnName},错误信息:{templateErr.Message}";
|
|
sb.AppendLine(temp);
|
|
}
|
|
throw new Exception(sb.ToString());
|
|
}
|
|
var entityLst = impResult.Data;
|
|
|
|
using var transaction = _context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
#region 数据库操作
|
|
//添加任务
|
|
TaskSub taskSubObj = _commonService.BuildTaskSub(entityLst.Count, _taskName);
|
|
await _taskSubRepository.AddAsync(taskSubObj);
|
|
//添加任务明细
|
|
foreach (var empDtObj in entityLst)
|
|
{
|
|
SUPPLIER_PRO_ATTACHMENT_DATA empObj = ExpressionGenericMapper<SUPPLIER_PRO_ATTACHMENT_DATA_DT, SUPPLIER_PRO_ATTACHMENT_DATA>.Trans(empDtObj);
|
|
empObj.TaskId = taskSubObj.TaskId;
|
|
await _supplierProAttachmentDataRepository.AddAsync(empObj);
|
|
}
|
|
|
|
foreach (var empDtObj in entityLst)
|
|
{
|
|
//存储新增数据
|
|
var ret = await base.Create(empDtObj);
|
|
}
|
|
#endregion
|
|
|
|
transaction.Commit();
|
|
return Ok(true);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
transaction.Rollback();
|
|
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
|
|
}
|
|
}
|
|
|
|
public override async Task<ActionResult<SUPPLIER_PRO_ATTACHMENT_DATA_DT>> Create(SUPPLIER_PRO_ATTACHMENT_DATA_DT entity)
|
|
{
|
|
using var transaction = _context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
#region 数据库操作
|
|
//添加任务
|
|
TaskSub taskSubObj = _commonService.BuildTaskSub(1, _taskName);
|
|
await _taskSubRepository.AddAsync(taskSubObj);
|
|
//添加任务明细
|
|
SUPPLIER_PRO_ATTACHMENT_DATA empObj = ExpressionGenericMapper<SUPPLIER_PRO_ATTACHMENT_DATA_DT, SUPPLIER_PRO_ATTACHMENT_DATA>.Trans(entity);
|
|
empObj.TaskId = taskSubObj.TaskId;
|
|
await _supplierProAttachmentDataRepository.AddAsync(empObj);
|
|
|
|
var ret = await base.Create(entity);
|
|
|
|
#endregion
|
|
|
|
transaction.Commit();
|
|
return Ok(true);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
transaction.Rollback();
|
|
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|