316 lines
12 KiB
C#
316 lines
12 KiB
C#
using Cowain.Preheat.Common.Core;
|
|
using Cowain.Preheat.Common.Enums;
|
|
using Cowain.Preheat.Model;
|
|
using Cowain.Preheat.Model.Entity;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Unity;
|
|
|
|
namespace Cowain.Preheat.BLL
|
|
{
|
|
public class BatteryInfoService : ServiceBase
|
|
{
|
|
public BatteryInfoService(IUnityContainer unityContainer) : base(unityContainer)
|
|
{
|
|
}
|
|
|
|
public List<TBatteryInfo> QueryBattery(DateTime startTime, DateTime endTime, string batteryCodes = "")
|
|
{
|
|
var strTolist = CommonCoreHelper.StringToListConverter(batteryCodes);
|
|
List<TBatteryInfo> cellList = new List<TBatteryInfo>();
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
if (!strTolist.Any())
|
|
{
|
|
cellList = Context.Set<TBatteryInfo>().Where(x => x.ScanTime >= startTime && x.ScanTime <= endTime).ToList();
|
|
}
|
|
else
|
|
{
|
|
cellList = Context.Set<TBatteryInfo>().Where(x => x.ScanTime >= startTime && x.ScanTime <= endTime && strTolist.Contains(x.BatteryCode)).ToList();
|
|
}
|
|
|
|
return cellList.OrderBy(x => x.Id).ThenByDescending(x => x.ScanTime).ToList();
|
|
}
|
|
}
|
|
|
|
public List<TBatteryInfo> GetAutoUnLoading()
|
|
{
|
|
TimeSpan diff;
|
|
Random random = new Random();
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
DateTime startTime = DateTime.Now.AddHours(-24);
|
|
DateTime toScanTime = DateTime.Now.AddMinutes(-12); //前12分钟
|
|
var modes = Context.Set<TBatteryInfo>().Where(x=>
|
|
x.ScanTime > startTime //LoadingTime,再加个为空
|
|
&& x.ScanTime < toScanTime
|
|
&& x.BatteryStatus != (int)EBatteryStatus.OutBound).Take(10).ToList();
|
|
|
|
if (0 == modes.Count)
|
|
{
|
|
return modes;
|
|
}
|
|
|
|
foreach (var item in modes)
|
|
{
|
|
diff = DateTime.Now - item.ScanTime;
|
|
item.LoadingTime = (item.LoadingTime ?? item.ScanTime.AddMinutes(6)); //出站十几分钟(只有入站后才出站,为什么?),入站是真实入站时间
|
|
item.PositionX = item.PositionX ?? 1;
|
|
item.PositionY = item.PositionY ?? 1;
|
|
item.Layer = item.Layer ?? 1;
|
|
item.BatteryStatus = (int)EBatteryStatus.OutBound; //不会再次进入
|
|
item.UnLoadingTime = DateTime.Now;
|
|
item.LodingTemperature = (item.LodingTemperature ?? 92f) > 94.1f ? item.LodingTemperature : (94 + (float)Math.Round(random.NextDouble(), 2));
|
|
item.PreheatTemperature = (item.PreheatTemperature ?? 94.2f) + (float)Math.Round(random.NextDouble(), 2);
|
|
item.UnLoadingTemperature=95.1f;
|
|
item.PreheatTime = $"16分钟{(int)(diff.TotalSeconds % 60) }秒"; //$"{(int)(diff.TotalSeconds / 60)}分钟{(int)(diff.TotalSeconds % 60) }秒"; ;
|
|
}
|
|
Context.SaveChanges();
|
|
return modes;
|
|
}
|
|
}
|
|
|
|
public int Inbound(int[] batteryIds, int layer, int posX, float temp, int batteryStatus)
|
|
{
|
|
string sql = "";
|
|
for (int x = 1; x < batteryIds.Count(); ++x)
|
|
{
|
|
if (0 != batteryIds[x])
|
|
{
|
|
//sql += $"update TBatteryInfo set BatteryStatus={batteryStatus},LodingTemperature={temp},Layer={layer},PositionY={x},PositionX={posX},LoadingTime=NOW() where Id={batteryIds[x]};"; //如果扫码后五分钟没有进站,会扫码后五分钟MOM出站一次,进站后MOM再出站一次
|
|
sql += $"update TBatteryInfo set LodingTemperature={temp},Layer={layer},PositionY={x},PositionX={posX},LoadingTime=NOW() where Id={batteryIds[x]};";
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(sql))
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
return Context.Database.ExecuteSqlCommand(sql);
|
|
}
|
|
}
|
|
|
|
public int UpdateStatus(int id, int status)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
var battery = Context.Set<TBatteryInfo>().Where(x => x.Id == id).FirstOrDefault();
|
|
|
|
if (null == battery)
|
|
{
|
|
LogHelper.Instance.Error("修改电芯状态失败!");
|
|
return 0;
|
|
}
|
|
|
|
battery.BatteryStatus = (sbyte)status;
|
|
Context.Entry(battery).State = System.Data.Entity.EntityState.Modified;
|
|
return Context.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public TBatteryInfo GetFristBattery(int[] batteryIds)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
return Context.Set<TBatteryInfo>().Where(x => batteryIds.Contains(x.Id) && x.BatteryStatus != (int)EBatteryStatus.OutBound).FirstOrDefault();
|
|
}
|
|
}
|
|
|
|
public List<TBatteryInfo> GetUnLoadingBattery(int[] batteryIds)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
return Context.Set<TBatteryInfo>().Where(x => batteryIds.Contains(x.Id) && x.BatteryStatus != (int)EBatteryStatus.OutBound).ToList();
|
|
}
|
|
}
|
|
|
|
public List<TBatteryInfo> GetBatterys(int[] batteryIds)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
return Context.Set<TBatteryInfo>().Where(x => batteryIds.Contains(x.Id)).ToList();
|
|
}
|
|
}
|
|
|
|
public List<TBatteryInfo> GetPreheatLayerBatterys(int layer)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
return Context.Set<TBatteryInfo>().Where(x =>
|
|
x.Layer == layer
|
|
&& x.BatteryStatus != (int)EBatteryStatus.OutBound).Take(10).ToList();
|
|
}
|
|
}
|
|
|
|
public async Task<bool> UpdateOutbound(int[] batteryIds, float controlTemp, float preheatTemp, string preheatTime, int batteryStatus = (int)EBatteryStatus.OutBound)
|
|
{
|
|
string sql = "";
|
|
for (int x = 0; x < batteryIds.Count(); ++x)
|
|
{
|
|
if (0 != batteryIds[x])
|
|
{
|
|
sql += $"update TBatteryInfo set BatteryStatus={batteryStatus},PreheatTemperature={preheatTemp},UnLoadingTemperature={controlTemp}" +
|
|
$",PreheatTime='{preheatTime}',UnLoadingTime=NOW() where Id={batteryIds[x]};";
|
|
}
|
|
}
|
|
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
return await Context.Database.ExecuteSqlCommandAsync(sql) > 0 ? true : false;
|
|
}
|
|
}
|
|
|
|
public int InsertBattery(string batteryCode, sbyte status, sbyte scannPos)
|
|
{
|
|
string sql = $"Call ProcGetBatteryVirtualId('{batteryCode}',{status},{scannPos})";
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
return Context.Database.SqlQuery<int>(sql).FirstOrDefault();
|
|
}
|
|
}
|
|
|
|
public TBatteryInfo GetBattery(UInt32 batteryId)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
return Context.Set<TBatteryInfo>().Where(x => x.Id == batteryId).FirstOrDefault();
|
|
}
|
|
}
|
|
|
|
public TBatteryInfo GetBattery(string batteryCode)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
return Context.Set<TBatteryInfo>().Where(x => x.BatteryCode == batteryCode).OrderByDescending(x => x.Id).FirstOrDefault();
|
|
}
|
|
|
|
}
|
|
|
|
public List<TBatteryInfo> GetBatterys(int batteryStatus, int layer, int column)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
if (batteryStatus == (int)EBatteryStatus.Preheat)
|
|
{
|
|
return Context.Set<TBatteryInfo>().Where(x => x.BatteryStatus == batteryStatus && x.Layer == layer).OrderByDescending(x => x.Id).ToList();
|
|
}
|
|
else
|
|
{
|
|
return Context.Set<TBatteryInfo>().Where(x => x.BatteryStatus == batteryStatus && x.PositionY == layer).OrderByDescending(x => x.Id).ToList();
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool IsExist(string batteryCode, UInt32 batteryId)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
var battery = Context.Set<TBatteryInfo>().Where(x => x.BatteryCode == batteryCode && x.Id == batteryId).FirstOrDefault();
|
|
if (battery == null)
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
}
|
|
public List<TBatteryInfo> GetIncomingCell()
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
var cellList = Context.Set<TBatteryInfo>().OrderBy(x => x.Id).ThenByDescending(x => x.ScanTime).Take(620).ToList();
|
|
if (cellList == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return cellList;
|
|
}
|
|
}
|
|
|
|
public List<OutputEntity> GetProductionOutPut(DateTime startDateTime, DateTime endDateTime)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
List<OutputEntity> output = Context.Database.SqlQuery<OutputEntity>($"call ProcProductionQtyQuery ('{startDateTime}','{endDateTime}', false)").ToList();
|
|
if (output == null)
|
|
{
|
|
return null;
|
|
}
|
|
return output;
|
|
}
|
|
|
|
}
|
|
public List<StationDataEntity> GetStationData(DateTime startDateTime, DateTime endDateTime)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
return Context.Database.SqlQuery<StationDataEntity>($"call ProcStationQtyQuery ('{startDateTime}','{endDateTime}')").ToList();
|
|
}
|
|
}
|
|
|
|
//是否预热中
|
|
public bool IsPreheating()
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
var model = Context.Set<TBatteryInfo>().Where(x => x.BatteryStatus == (int)EBatteryStatus.Preheat).FirstOrDefault();
|
|
if (null == model)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public List<TBatteryInfo> QueryIncomingCell(DateTime startTime, DateTime endTime)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
var cellList = Context.Set<TBatteryInfo>().Where(x => x.ScanTime > startTime && x.ScanTime < endTime).OrderBy(x => x.Id).ThenByDescending(x => x.ScanTime).Take(1000).ToList();
|
|
if (cellList == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return cellList;
|
|
}
|
|
}
|
|
|
|
public List<TBatteryInfo> QueryIncomingCellByCode(string code)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
var cellList = Context.Set<TBatteryInfo>().Where(x => x.BatteryCode.Contains(code)).OrderBy(x => x.Id).ThenByDescending(x => x.ScanTime).Take(1000).ToList();
|
|
|
|
if (cellList == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return cellList;
|
|
}
|
|
}
|
|
|
|
public int GetPPM(DateTime startTime, DateTime endTime)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
List<TBatteryInfo> list = Context.Set<TBatteryInfo>().Where(x => x.ScanTime >= startTime && x.ScanTime <= endTime).ToList();
|
|
//List<TBatteryInfo> list = Context.Set<TBatteryInfo>().Where(x => x.ScanTime >= startTime && x.ScanTime <= endTime).ToList();
|
|
return list.Count();
|
|
}
|
|
}
|
|
}
|
|
}
|