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.
228 lines
10 KiB
228 lines
10 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 SupplierProProcessEquipmentDtService : NormalBaseController<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT>
|
|
{
|
|
private readonly IRepository<TaskSub> _taskSubRepository;
|
|
private readonly IRepository<SUPPLIER_PRO_PROCESS_EQUIPMENT> _supplierProProcessEquipmentRepository;
|
|
private readonly IRepository<TaskConifgure> _taskConfigureRepository;
|
|
|
|
public SupplierProProcessEquipmentDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT> repository, IRepository<TaskSub> taskSubRepository, IRepository<SUPPLIER_PRO_PROCESS_EQUIPMENT> supplierProProcessEquipmentRepository, IRepository<TaskConifgure> taskConfigureRepository) : base(context, builder, configuration, repository)
|
|
{
|
|
_taskSubRepository = taskSubRepository;
|
|
_supplierProProcessEquipmentRepository = supplierProProcessEquipmentRepository;
|
|
_taskConfigureRepository = taskConfigureRepository;
|
|
}
|
|
|
|
[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_PROCESS_EQUIPMENT_DT> impResult = await importer.Import<SUPPLIER_PRO_PROCESS_EQUIPMENT_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 empDtLst = impResult.Data;
|
|
|
|
using TransactionScope scope = new TransactionScope();
|
|
try
|
|
{
|
|
#region 数据库操作
|
|
//添加任务
|
|
TaskSub taskSubObj = await BuildTaskSub(empDtLst.Count);
|
|
await _taskSubRepository.AddAsync(taskSubObj);
|
|
//添加任务明细
|
|
foreach (var empDtObj in empDtLst)
|
|
{
|
|
SUPPLIER_PRO_PROCESS_EQUIPMENT empObj = ExpressionGenericMapper<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT, SUPPLIER_PRO_PROCESS_EQUIPMENT>.Trans(empDtObj);
|
|
empObj.TaskId = taskSubObj.TaskId;
|
|
await _supplierProProcessEquipmentRepository.AddAsync(empObj);
|
|
}
|
|
|
|
foreach (var empDtObj in empDtLst)
|
|
{
|
|
//以供应商代码+奇瑞零件号+工艺编码+工艺版本为唯一数据,做新增或者更新存储
|
|
var firstObj = _context.Set<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT>().FirstOrDefault(
|
|
itm => itm.SupplierCode == empDtObj.SupplierCode
|
|
&& itm.CheryProductNo == empDtObj.CheryProductNo
|
|
&& itm.DeviceCode == empDtObj.DeviceCode
|
|
&& itm.DeviceType == empDtObj.DeviceType
|
|
);
|
|
if (firstObj == null)
|
|
{
|
|
var ret = await base.Create(empDtObj);
|
|
}
|
|
else
|
|
{
|
|
var ret = await base.Update(empDtObj);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
scope.Complete();
|
|
return Ok(true);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
|
|
}
|
|
}
|
|
|
|
public override async Task<ActionResult<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT>> Create(SUPPLIER_PRO_PROCESS_EQUIPMENT_DT entity)
|
|
{
|
|
using TransactionScope scope = new TransactionScope();
|
|
try
|
|
{
|
|
#region 数据库操作
|
|
//添加任务
|
|
TaskSub taskSubObj = await BuildTaskSub(1);
|
|
await _taskSubRepository.AddAsync(taskSubObj);
|
|
//添加任务明细
|
|
SUPPLIER_PRO_PROCESS_EQUIPMENT empObj = ExpressionGenericMapper<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT, SUPPLIER_PRO_PROCESS_EQUIPMENT>.Trans(entity);
|
|
empObj.TaskId = taskSubObj.TaskId;
|
|
await _supplierProProcessEquipmentRepository.AddAsync(empObj);
|
|
|
|
var firstObj = _context.Set<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT>().FirstOrDefault(
|
|
itm => itm.SupplierCode == entity.SupplierCode
|
|
&& itm.CheryProductNo == entity.CheryProductNo
|
|
&& itm.DeviceCode == entity.DeviceCode
|
|
&& itm.DeviceType == entity.DeviceType
|
|
);
|
|
if (firstObj == null)
|
|
{
|
|
var ret = await base.Create(entity);
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("数据库已经存在,不能重复插入");
|
|
}
|
|
#endregion
|
|
|
|
scope.Complete();
|
|
return Ok(true);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
|
|
}
|
|
}
|
|
|
|
public override async Task<IActionResult> Update(SUPPLIER_PRO_PROCESS_EQUIPMENT_DT entity)
|
|
{
|
|
using TransactionScope scope = new TransactionScope();
|
|
try
|
|
{
|
|
#region 数据库操作
|
|
//添加任务
|
|
TaskSub taskSubObj = await BuildTaskSub(1);
|
|
await _taskSubRepository.AddAsync(taskSubObj);
|
|
//添加任务明细
|
|
SUPPLIER_PRO_PROCESS_EQUIPMENT empObj = ExpressionGenericMapper<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT, SUPPLIER_PRO_PROCESS_EQUIPMENT>.Trans(entity);
|
|
empObj.TaskId = taskSubObj.TaskId;
|
|
await _supplierProProcessEquipmentRepository.AddAsync(empObj);
|
|
|
|
var firstObj = _context.Set<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT>().FirstOrDefault(
|
|
itm => itm.SupplierCode == entity.SupplierCode
|
|
&& itm.CheryProductNo == entity.CheryProductNo
|
|
&& itm.DeviceCode == entity.DeviceCode
|
|
&& itm.DeviceType == entity.DeviceType
|
|
);
|
|
if (firstObj == null)
|
|
{
|
|
throw new Exception("数据库不存在,不能更新");
|
|
}
|
|
else
|
|
{
|
|
var ret = await base.Update(entity);
|
|
}
|
|
#endregion
|
|
|
|
scope.Complete();
|
|
return Ok(true);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生成TaskSub
|
|
/// </summary>
|
|
/// <param name="dataCount">任务明细数量</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="Exception"></exception>
|
|
private async Task<TaskSub> BuildTaskSub(int dataCount)
|
|
{
|
|
List<TaskConifgure> taskConfigureLst = (await _taskConfigureRepository.GetAllAsync()).ToList();
|
|
|
|
TaskConifgure? taskConfigureObj = taskConfigureLst.FirstOrDefault(itm => itm.TaskName == "工艺装备");
|
|
if (taskConfigureObj == null)
|
|
{
|
|
throw new Exception("任务配置表没有'工艺装备'任务");
|
|
}
|
|
|
|
TaskSub taskSub = new TaskSub();
|
|
taskSub.TableName = taskConfigureObj.TableName; //关联的数据表名称(如:订单表、用户表等,可为空)
|
|
taskSub.TaskName = taskConfigureObj.TaskName; //任务名称(用于业务层面标识任务,如:数据同步任务、报表生成任务)
|
|
taskSub.DataCount = dataCount; //数据总量(任务处理的数据条目数)
|
|
taskSub.Subscriber = taskConfigureObj.Client; //发布给那个客户
|
|
taskSub.FailedCount = 0; //失败次数(任务执行失败的累计次数)
|
|
taskSub.FailedInfo = ""; //失败详情(记录失败原因、异常堆栈等信息,支持长文本)
|
|
taskSub.Domain = ""; //所属域(多租户场景下标识租户,如:租户A、租户B)
|
|
taskSub.Site = ""; //站点标识(多站点部署时标识所属站点,如:Site1、Site2)
|
|
taskSub.CreateUser = CommonHelper.UserName; //从那个内部系统创建系统
|
|
taskSub.CreateTime = CommonHelper.CurrentTime; //创建时间(任务创建的时间戳)
|
|
taskSub.Remark = ""; //备注信息(任务相关的补充说明,支持长文本)
|
|
taskSub.UpdateUser = ""; //最后更新人(记录任务最后修改者)
|
|
taskSub.UpdateTime = null; //最后更新时间(任务最后修改的时间戳,可为空)
|
|
taskSub.SyncedPageCount = 0; //已同步页数
|
|
taskSub.ReadState = true;
|
|
taskSub.CreationTime = CommonHelper.CurrentTime;
|
|
taskSub.TaskId = CommonHelper.NewGuid;
|
|
return taskSub;
|
|
}
|
|
}
|
|
|
|
}
|
|
|