using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using System.Collections.Generic;
using TaskManager.Contracts.Dtos;
using TaskManager.Entity;
using TaskManager.Entity.Entitys;
using TaskManager.EntityFramework;
namespace TaskManager.Controllers
{
///
/// 日物料需求计划风险确认
///
public class CherySupplierConDateService : CheryRecurringJobInputPageController
{
public CherySupplierConDateService(HttpClient httpClient, JobDbContext jobDbContext, LogController log, IRepository repository) : base(httpClient, jobDbContext, log, repository)
{
}
[HttpGet]
public async Task GetDetialByID(String id)
{
SUPPLIER_MRP_DATE first = new SUPPLIER_MRP_DATE();
var m = await _jobDbContext.SUPPLIER_MRP_DATE.FirstOrDefaultAsync(p => p.Id == id);
if (m != null)
{
return m;
}
return first;
}
///
/// 确认提交
///
///
///
[HttpPost]
public async Task Confirm([FromBody] List entites)
{
if (entites.Any(p =>
string.IsNullOrEmpty(p.SupplierCode) ||
string.IsNullOrEmpty(p.ReleaseEdition) ||
string.IsNullOrEmpty(p.MaterialCode) ||
string.IsNullOrEmpty(p.PlantId) ||
string.IsNullOrEmpty(p.FeedbackResults)
))
{
return new JsonResult(new { Code = 400, Message = "请检查需求发布版次、供应商代码、零件号、工厂代码、起始日期、反馈结果不能为空!" });
}
var taskId = Guid.NewGuid();
var task = new TaskSub();
task.TaskId = taskId;
task.TaskName = "日物料需求计划风险确认";
task.Subscriber = "Chery";
task.TableName = "SUPPLIER_CON_DATE";
task.DataCount = entites.Count;
task.Domain = "安通林";
task.Site = "安通林";
task.FailedCount = 0;
task.CreateTime = DateTime.Now;
task.CreateUser = "admin";
task.CreationTime = DateTime.Now;
task.SyncedPageCount = 0;
task.ReadState = true;
entites.ForEach(p =>
{
p.ReadState = true;
p.TaskId = taskId;
});
using (var transaction = await _jobDbContext.Database.BeginTransactionAsync())
{
var tran = transaction.GetDbTransaction();
try
{
await _jobDbContext.BulkUpdateAsync(entites, options => { options.Transaction = tran; });
await _jobDbContext.AddAsync(task);
_jobDbContext.SaveChanges();
// 提交事务
await transaction.CommitAsync();
}
catch (Exception ex)
{
await transaction.RollbackAsync();
return new JsonResult(new { Code = 400, Message = ex.Message });
}
}
return new JsonResult(new { Code = 200, Message = "修改成功!" });
}
///
/// 批量修改
///
///
///
[HttpPost]
public async Task BatchUpdate([FromBody] List entites)
{
await _jobDbContext.BulkUpdateAsync(entites, options => { options.ColumnPrimaryKeyExpression = p => p.UId; options.UseTableLock = false; });
return new JsonResult(new { Code = 200, Message = "批量修改成功!" });
}
}
}