using EFCore.BulkExtensions;
using Magicodes.ExporterAndImporter.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Data.Common;
using System.Linq.Dynamic.Core;
using TaskManager.Contracts.Dtos;
using TaskManager.Controllers;
using TaskManager.Entity;
using TaskManager.Entity.Entitys;
using TaskManager.EntityFramework;
using TaskManager.EntityFramework.Repository;
namespace TaskManager.Controllers
{
///
/// M+6月物料需求计划风险
///
public class CherySupplierMrpMonthService : CheryRecurringJobOutPageController
{
public CherySupplierMrpMonthService(HttpClient httpClient, JobDbContext jobDbContext, LogController log, IRepository repository) : base(httpClient, jobDbContext, log, repository)
{
}
[HttpGet]
public async Task GetDetialByID(String id)
{
SUPPLIER_MRP_MONTH first = new SUPPLIER_MRP_MONTH();
var m = await _jobDbContext.SUPPLIER_MRP_MONTH.FirstOrDefaultAsync(p => p.Id == id);
if (m != null)
{
return m;
}
return first;
}
protected override async Task ConfirmDataInsertAsync(List plist, JobDbContext dbContext, DbTransaction dbTransaction)
{
if (plist.Count > 0)
{
var requestDate = plist.FirstOrDefault()?.RequestDate;
//List list = new List();
//plist.ForEach(p =>
//{
// var con = new SUPPLIER_CON_MMRP();
// con.Id = p.Id;
// con.SupplierCode = "8EG";
// con.ReleaseEdition = p.ReleaseEdition;
// con.MaterialCode = p.MaterialCode;
// con.PlantId = p.PlantId;
// con.FeedbackResults = "0";
// con.VentureType = "";
// con.VentureSpecific = "";
// con.Measures = "";
// con.StartMonth = p.StartMonth;
// con.QuantityMeet1 = p.QuantityDemand1;
// con.QuantityMeet2 = p.QuantityDemand2;
// con.QuantityMeet3 = p.QuantityDemand3;
// con.QuantityMeet4 = p.QuantityDemand4;
// con.QuantityMeet5 = p.QuantityDemand5;
// con.QuantityMeet6 = p.QuantityDemand6;
// con.QuantityMeet7 = p.QuantityDemand7;
// con.QuantityMeet8 = p.QuantityDemand8;
// con.QuantityMeet9 = p.QuantityDemand9;
// con.QuantityMeet10 = p.QuantityDemand10;
// con.QuantityMeet11 = p.QuantityDemand11;
// con.QuantityMeet12 = p.QuantityDemand12;
// con.CreationTime = DateTime.Now;
// list.Add(con);
//});
//dbContext.BulkInsert(list);
var ids = plist.Select(p => p.Id);
var existList = await _jobDbContext.Set().Where(p => ids.Contains(p.Id) && p.ReadState == false).ToListAsync();
if (existList.Any())
{
foreach (var itm in existList)
{
var first = plist.FirstOrDefault(p => p.Id == itm.Id);
if (first != null)
{
itm.Id = first.Id;
itm.SupplierCode = "8EG";
itm.ReleaseEdition = first.ReleaseEdition;
itm.MaterialCode = first.MaterialCode;
itm.PlantId = first.PlantId;
itm.FeedbackResults = "0";
itm.VentureType = "";
itm.VentureSpecific = "";
itm.Measures = "";
itm.StartMonth = first.StartMonth;
itm.QuantityMeet1 = first.QuantityDemand1;
itm.QuantityMeet2 = first.QuantityDemand2;
itm.QuantityMeet3 = first.QuantityDemand3;
itm.QuantityMeet4 = first.QuantityDemand4;
itm.QuantityMeet5 = first.QuantityDemand5;
itm.QuantityMeet6 = first.QuantityDemand6;
itm.QuantityMeet7 = first.QuantityDemand7;
itm.QuantityMeet8 = first.QuantityDemand8;
itm.QuantityMeet9 = first.QuantityDemand9;
itm.QuantityMeet10 = first.QuantityDemand10;
itm.QuantityMeet11 = first.QuantityDemand11;
itm.QuantityMeet12 = first.QuantityDemand12;
itm.CreationTime = DateTime.Now;
itm.RequestDate = requestDate;
}
}
}
if (existList.Any())
{
_jobDbContext.BulkUpdate(existList.ToList());
}
var query = from item in plist
join existItem in existList
on item.Id equals existItem.Id into gj
from subItem in gj.DefaultIfEmpty()
where subItem == null
select item;
List list = new List();
foreach (var p in query)
{
var con = new SUPPLIER_CON_MMRP();
con.Id = p.Id;
con.SupplierCode = "8EG";
con.ReleaseEdition = p.ReleaseEdition;
con.MaterialCode = p.MaterialCode;
con.PlantId = p.PlantId;
con.FeedbackResults = "0";
con.VentureType = "";
con.VentureSpecific = "";
con.Measures = "";
con.StartMonth = p.StartMonth;
con.QuantityMeet1 = p.QuantityDemand1;
con.QuantityMeet2 = p.QuantityDemand2;
con.QuantityMeet3 = p.QuantityDemand3;
con.QuantityMeet4 = p.QuantityDemand4;
con.QuantityMeet5 = p.QuantityDemand5;
con.QuantityMeet6 = p.QuantityDemand6;
con.QuantityMeet7 = p.QuantityDemand7;
con.QuantityMeet8 = p.QuantityDemand8;
con.QuantityMeet9 = p.QuantityDemand9;
con.QuantityMeet10 = p.QuantityDemand10;
con.QuantityMeet11 = p.QuantityDemand11;
con.QuantityMeet12 = p.QuantityDemand12;
con.CreationTime = DateTime.Now;
con.RequestDate = requestDate;
list.Add(con);
}
_jobDbContext.BulkInsert(list);
//await dbContext.BulkMergeAsync(list, options=> { options.Transaction = dbTransaction; options.UseTableLock = false;
// options.ColumnPrimaryKeyExpression = p =>new { p.Id, p.ReadState };
//});
}
return;
}
}
}