812 lines
33 KiB
C#
812 lines
33 KiB
C#
|
|
using Cowain.Bake.Common;
|
|||
|
|
using Cowain.Bake.Common.Core;
|
|||
|
|
using Cowain.Bake.Common.Enums;
|
|||
|
|
using Cowain.Bake.Model;
|
|||
|
|
using Cowain.Bake.Model.Entity;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Data;
|
|||
|
|
using System.Linq;
|
|||
|
|
using Unity;
|
|||
|
|
|
|||
|
|
namespace Cowain.Bake.BLL
|
|||
|
|
{
|
|||
|
|
public class BatteryInfoService : ServiceBase
|
|||
|
|
{
|
|||
|
|
public BatteryInfoService(IUnityContainer unityContainer) : base(unityContainer)
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 托盘中是否还有电芯
|
|||
|
|
/// </summary>
|
|||
|
|
public bool IsIntoStation(int palletVID)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
TBatteryInfo battery = (from b in Context.Set<TBatteryInfo>()
|
|||
|
|
where b.BatteryStatus < (int)EBatteryStatus.Over && b.PalletVirtualId == palletVID
|
|||
|
|
select b).FirstOrDefault();
|
|||
|
|
if (null == battery)
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public int UpdateCoolTempAndUnBindingTime(List<TBatteryInfo> batters, List<TBatteryInfo> tempBattery)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
foreach (var battery in batters)
|
|||
|
|
{
|
|||
|
|
var temp = tempBattery.Where(x => x.BatteryCode == battery.BatteryCode).FirstOrDefault();
|
|||
|
|
if (null == temp)
|
|||
|
|
{
|
|||
|
|
battery.CoolTemp = 30;
|
|||
|
|
LogHelper.Instance.Fatal($"冷却下料通过电芯条码找查失败,电芯条码为:{battery.BatteryCode}");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
battery.CoolTemp = temp.CoolTemp;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
battery.UnbindingTime = DateTime.Now;
|
|||
|
|
Context.Entry(battery).State = System.Data.Entity.EntityState.Modified;
|
|||
|
|
}
|
|||
|
|
return Context.SaveChanges();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public List<TBatteryInfo> GetBatteryInfos(int palletVID)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Set<TBatteryInfo>().Where(x => x.PalletVirtualId == palletVID).ToList();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public int InsertBattery(string batteryCode, int status, int dummyStatus, string remarks)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Database.SqlQuery<int>($"call ProcGetBatteryVirtualId('{batteryCode}',{status},{dummyStatus},'{remarks}')").FirstOrDefault();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public List<int> InsertBattery(string batteryCodes)
|
|||
|
|
{
|
|||
|
|
string sql = $"Call ProcGetBatterysVirtualId('{batteryCodes}')";
|
|||
|
|
DataTable dt = GetDataTable(sql);
|
|||
|
|
// 通过索引获取列数据
|
|||
|
|
List<int> VIds = new List<int>();
|
|||
|
|
foreach (DataRow row in dt.Rows)
|
|||
|
|
{
|
|||
|
|
VIds.Add((int)row[0]);
|
|||
|
|
}
|
|||
|
|
return VIds;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 电芯条码是否重复
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="batteryCode"></param>
|
|||
|
|
/// <returns></returns>B
|
|||
|
|
public bool IsBatteryCodeRepeat(string batteryCode)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
TBatteryInfo battery = Context.Set<TBatteryInfo>().Where(x => x.BatteryCode == batteryCode).FirstOrDefault(); //FirstOrDefault EF并不会继续检索整个表
|
|||
|
|
if (null == battery)
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public int InsertBatch(int[] batteryVirtualId, int palletVirtualId)
|
|||
|
|
{
|
|||
|
|
string sql = "";
|
|||
|
|
int index = 0;
|
|||
|
|
if (Global.PALLET_ROWS < Global.PALLET_COLS) //7行少,24列多
|
|||
|
|
{
|
|||
|
|
for (int x = 0; x < Global.PALLET_ROWS; ++x)
|
|||
|
|
{
|
|||
|
|
for (int y = 0; y < Global.PALLET_COLS; ++y)
|
|||
|
|
{
|
|||
|
|
index = (x * Global.PALLET_COLS) + y + 1; //数组是0...120,数据是1...120,所以下标从1开始
|
|||
|
|
if (0 != batteryVirtualId[index])
|
|||
|
|
{
|
|||
|
|
sql += $"update TBatteryInfo set PalletVirtualId={palletVirtualId}, BatteryStatus={(int)EBatteryStatus.ToPallet},PositionX={x + 1}, PositionY={y + 1},BindingTime=NOW() where Id={batteryVirtualId[index]};";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
for (int y = 0; y < Global.PALLET_COLS; ++y) //2 //48行,2列,行多列少
|
|||
|
|
{
|
|||
|
|
for (int x = 0; x < Global.PALLET_ROWS; ++x) //48
|
|||
|
|
{
|
|||
|
|
index = (y * Global.PALLET_ROWS) + x + 1; //数组是0...120,数据是1...120,所以下标从1开始
|
|||
|
|
if (0 != batteryVirtualId[index])
|
|||
|
|
{
|
|||
|
|
sql += $"update TBatteryInfo set PalletVirtualId={palletVirtualId}, BatteryStatus={(int)EBatteryStatus.ToPallet},PositionX={x + 1}, PositionY={y + 1},BindingTime=NOW() where Id={batteryVirtualId[index]};";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (string.IsNullOrEmpty(sql))
|
|||
|
|
{
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
sql = "START TRANSACTION;" + sql + "COMMIT;"; //使用事务批量更新
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Database.ExecuteSqlCommand(sql);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public List<TBatteryInfo> GetCavityBattery(string cavityName)
|
|||
|
|
{
|
|||
|
|
string sql = $@"SELECT *
|
|||
|
|
FROM TCavityInfo td
|
|||
|
|
LEFT JOIN TPalletInfo ti ON td.PalletId=ti.Id
|
|||
|
|
LEFT JOIN TBatteryInfo tbi ON ti.VirtualId=tbi.PalletVirtualId
|
|||
|
|
WHERE td.Name='{cavityName}' AND tbi.BatteryCode is not null ORDER BY PositionX, PositionY ";
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Database.SqlQuery<TBatteryInfo>(sql).ToList();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public bool IsDummy(int batteryId)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
TBatteryInfo battery = Context.Set<TBatteryInfo>().Where(x => x.Id == batteryId && x.DummyFlag == true).FirstOrDefault(); //FirstOrDefault EF并不会继续检索整个表
|
|||
|
|
if (null == battery)
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//public bool IsDummy(string batteryCode)
|
|||
|
|
//{
|
|||
|
|
// using (var Context = new BakingEntities())
|
|||
|
|
// {
|
|||
|
|
// TBatteryInfo battery = Context.Set<TBatteryInfo>().Where(x => x.BatteryCode == batteryCode && x.DummyFlag == true).FirstOrDefault(); //FirstOrDefault EF并不会继续检索整个表
|
|||
|
|
// if (null == battery)
|
|||
|
|
// {
|
|||
|
|
// return false;
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// return true;
|
|||
|
|
// }
|
|||
|
|
//}
|
|||
|
|
public TBatteryInfo FindDummy(TPalletInfo palletInfo, List<TBatteryInfo> batterys)
|
|||
|
|
{
|
|||
|
|
TBatteryInfo batteryInfo = null;
|
|||
|
|
TBatteryInfo dummyInfo = batterys.Where(x => x.DummyFlag == true).FirstOrDefault();
|
|||
|
|
|
|||
|
|
//正常情况下是不会出现的
|
|||
|
|
if (null == dummyInfo) //找水含量电芯位置
|
|||
|
|
{
|
|||
|
|
LogHelper.Instance.Error($"夹具【{palletInfo.PalletCode}】下料过种中,没有找到水含量电芯!", true);
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (dummyInfo.BatteryStatus <= (int)EBatteryStatus.ToPallet)
|
|||
|
|
{
|
|||
|
|
return dummyInfo;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
//假电芯取走,取同一行相邻的假电芯
|
|||
|
|
if (Global.PALLET_ROWS < Global.PALLET_COLS) //7行少,24列多
|
|||
|
|
{
|
|||
|
|
batteryInfo = (from b in batterys
|
|||
|
|
where b.BatteryStatus <= (int)EBatteryStatus.ToPallet && b.PositionX == dummyInfo.PositionX
|
|||
|
|
orderby Math.Abs(b.PositionY.Value - dummyInfo.PositionY.Value)
|
|||
|
|
select b).FirstOrDefault(); //b.PositionY - dummyInfo.PositionY
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
batteryInfo = (from b in batterys
|
|||
|
|
where b.BatteryStatus <= (int)EBatteryStatus.ToPallet && b.PositionY == dummyInfo.PositionY
|
|||
|
|
orderby Math.Abs(b.PositionX.Value - dummyInfo.PositionX.Value)
|
|||
|
|
select b).FirstOrDefault(); //b.PositionY - dummyInfo.PositionY
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
if (null != batteryInfo) //同一行相邻的假电芯
|
|||
|
|
{
|
|||
|
|
SetDummy(batteryInfo.Id);
|
|||
|
|
return batteryInfo;//取同一行相邻的假电芯,直接返回
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (Global.PALLET_ROWS < Global.PALLET_COLS) //7行少,24列多
|
|||
|
|
{
|
|||
|
|
batteryInfo = (from b in batterys
|
|||
|
|
where b.BatteryStatus <= (int)EBatteryStatus.ToPallet
|
|||
|
|
orderby Math.Abs(b.PositionY.Value - dummyInfo.PositionY.Value)
|
|||
|
|
select b).FirstOrDefault(); //b.PositionY - dummyInfo.PositionY
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
batteryInfo = (from b in batterys
|
|||
|
|
where b.BatteryStatus <= (int)EBatteryStatus.ToPallet
|
|||
|
|
orderby Math.Abs(b.PositionX.Value - dummyInfo.PositionX.Value)
|
|||
|
|
select b).FirstOrDefault(); //b.PositionY - dummyInfo.PositionY
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (null != batteryInfo) //另一行相邻的假电芯
|
|||
|
|
{
|
|||
|
|
SetDummy(batteryInfo.Id);
|
|||
|
|
return batteryInfo;//另一行相邻的假电芯,直接返回
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
LogHelper.Instance.Error($"空夹具,没有电芯了");
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public int SetDummy(int id)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
TBatteryInfo dummy = Context.Set<TBatteryInfo>().Where(x => x.Id == id).FirstOrDefault();
|
|||
|
|
if (null == dummy)
|
|||
|
|
{
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
dummy.DummyFlag = true;
|
|||
|
|
Context.Entry(dummy).State = System.Data.Entity.EntityState.Modified;
|
|||
|
|
return Context.SaveChanges();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public List<TBatteryInfo> GetBatterys(List<int> Ids)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Set<TBatteryInfo>().Where(x => Ids.Contains(x.Id)).ToList();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public int SetDummyToWaterPlatform(int palletId)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
var dummys = (from pallet in Context.Set<TPalletInfo>()
|
|||
|
|
join battery in Context.Set<TBatteryInfo>() on pallet.VirtualId equals battery.PalletVirtualId
|
|||
|
|
where pallet.Id == palletId && battery.DummyFlag == true && battery.BatteryStatus < (int)EBatteryStatus.ToWaterPlatform
|
|||
|
|
select battery).ToList();
|
|||
|
|
|
|||
|
|
if (0 == dummys.Count)
|
|||
|
|
{
|
|||
|
|
LogHelper.Instance.Warn($"下料时,却没有假电芯的信息,托盘号:{palletId}");
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
dummys.ForEach(x => x.BatteryStatus = (sbyte)EBatteryStatus.ToWaterPlatform);
|
|||
|
|
|
|||
|
|
int result = Context.SaveChanges();
|
|||
|
|
if (result != dummys.Count)
|
|||
|
|
{
|
|||
|
|
LogHelper.Instance.Debug($"下料修改假电芯状态失败,palletId:{palletId},在测水含量数:{dummys.Count},实际修改数:{result}");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public int UpdateStatus(int id, int status)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
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 List<BatteryInfoEntity> GetOutbound()
|
|||
|
|
{
|
|||
|
|
string sql = $@"SELECT
|
|||
|
|
TBatteryInfo.Id,
|
|||
|
|
TBatteryInfo.PalletVirtualId,
|
|||
|
|
TBatteryInfo.BatteryStatus,
|
|||
|
|
TBatteryInfo.BatteryCode,
|
|||
|
|
TBatteryInfo.PositionX,
|
|||
|
|
TBatteryInfo.PositionY ,
|
|||
|
|
TBatteryInfo.ScanTime,
|
|||
|
|
TPalletInfo1.PalletCode,
|
|||
|
|
TPalletInfo1.LoadingBegingTime,
|
|||
|
|
TPalletInfo1.LoadingOverTime,
|
|||
|
|
TPalletInfo1.BakingPosition,
|
|||
|
|
TPalletInfo1.BakingBeginTime,
|
|||
|
|
TPalletInfo1.BakingOverTime,
|
|||
|
|
TPalletInfo1.UnLoadingBegingTime,
|
|||
|
|
TPalletInfo1.UnLoadingOverTime,
|
|||
|
|
TPalletInfo1.WaterValue
|
|||
|
|
FROM TBatteryInfo
|
|||
|
|
LEFT JOIN
|
|||
|
|
(SELECT VirtualId, PalletCode, LoadingBegingTime,LoadingOverTime, BakingPosition, BakingBeginTime, BakingOverTime,UnLoadingBegingTime, UnLoadingOverTime, WaterValue
|
|||
|
|
FROM TPalletInfo
|
|||
|
|
UNION
|
|||
|
|
SELECT VirtualId, PalletCode, LoadingBegingTime,LoadingOverTime, BakingPosition, BakingBeginTime, BakingOverTime, UnLoadingBegingTime,UnLoadingOverTime, WaterValue
|
|||
|
|
FROM TPalletInfoHistory) AS TPalletInfo1
|
|||
|
|
ON
|
|||
|
|
TBatteryInfo.PalletVirtualId = TPalletInfo1.VirtualId
|
|||
|
|
where TBatteryInfo.BatteryStatus = {(int)EBatteryStatus.Over}; ";
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Database.SqlQuery<BatteryInfoEntity>(sql).Take(100).ToList();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
LogHelper.Instance.Error($"GetOutbound;失败,{ex.Message},{ex.StackTrace}");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
public bool IsExistBattery(int palletVirtualId, int positionX, int positionY)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
var batteryInfo = Context.Set<TBatteryInfo>().Where(x => x.PalletVirtualId == palletVirtualId
|
|||
|
|
&& x.PositionX == positionX && x.PositionY == positionY).FirstOrDefault();
|
|||
|
|
|
|||
|
|
if (null == batteryInfo)
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public List<TBatteryInfo> GetWaterBatteryCode(int? VirtualId)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Set<TBatteryInfo>().Where(x => x.PalletVirtualId == VirtualId && x.DummyFlag == true).ToList();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public int SetBatteryOutBound(int palletVID)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
string sql = $"update TBatteryInfo set BatteryStatus={(int)EBatteryStatus.Over} where PalletVirtualId={palletVID} and BatteryStatus < {(int)EBatteryStatus.Over};"; //表示已经出站了
|
|||
|
|
return Context.Database.ExecuteSqlCommand(sql);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public List<TBatteryInfo> QueryBattery(DateTime startTime, DateTime endTime, string batteryCodes = "")
|
|||
|
|
{
|
|||
|
|
var strTolist = CommonCoreHelper.Instance.StringToListConverter(batteryCodes);
|
|||
|
|
List<TBatteryInfo> cellList = new List<TBatteryInfo>();
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
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 bool IsPalletDummy(int palletVID)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
TBatteryInfo dummy = (from b in Context.Set<TBatteryInfo>()
|
|||
|
|
where b.DummyFlag == true && b.PalletVirtualId == palletVID
|
|||
|
|
select b).FirstOrDefault();
|
|||
|
|
if (null == dummy)
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public TBatteryInfo GetBatteryInfos(string batteryCode)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Set<TBatteryInfo>().Where(x => x.BatteryCode == batteryCode).OrderByDescending(x => x.Id).FirstOrDefault();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public List<BatteryInfoEntity> Query(string batteryCode)
|
|||
|
|
{
|
|||
|
|
string sql = $@"SELECT
|
|||
|
|
TBatteryInfo.Id,
|
|||
|
|
TBatteryInfo.PalletVirtualId,
|
|||
|
|
TBatteryInfo.BatteryStatus,
|
|||
|
|
TBatteryInfo.BatteryCode,
|
|||
|
|
TBatteryInfo.PositionX,
|
|||
|
|
TBatteryInfo.PositionY ,
|
|||
|
|
TBatteryInfo.ScanTime,
|
|||
|
|
TBatteryInfo.CoolTemp,
|
|||
|
|
TPalletInfo1.PalletCode,
|
|||
|
|
TPalletInfo1.LoadingBegingTime,
|
|||
|
|
TPalletInfo1.LoadingOverTime,
|
|||
|
|
TPalletInfo1.BakingPosition,
|
|||
|
|
TPalletInfo1.BakingBeginTime,
|
|||
|
|
TPalletInfo1.BakingOverTime,
|
|||
|
|
TPalletInfo1.UnLoadingBegingTime,
|
|||
|
|
TPalletInfo1.UnLoadingOverTime,
|
|||
|
|
TPalletInfo1.BakingCount,
|
|||
|
|
TPalletInfo1.WaterValue
|
|||
|
|
FROM TBatteryInfo
|
|||
|
|
LEFT JOIN
|
|||
|
|
(SELECT VirtualId, PalletCode, LoadingBegingTime,LoadingOverTime, BakingPosition, BakingBeginTime, BakingOverTime,UnLoadingBegingTime, UnLoadingOverTime,BakingCount, WaterValue
|
|||
|
|
FROM TPalletInfo
|
|||
|
|
UNION
|
|||
|
|
SELECT VirtualId, PalletCode, LoadingBegingTime,LoadingOverTime, BakingPosition, BakingBeginTime, BakingOverTime, UnLoadingBegingTime,UnLoadingOverTime,BakingCount, WaterValue
|
|||
|
|
FROM TPalletInfoHistory) AS TPalletInfo1
|
|||
|
|
ON
|
|||
|
|
TBatteryInfo.PalletVirtualId = TPalletInfo1.VirtualId
|
|||
|
|
where TBatteryInfo.BatteryCode = '{batteryCode}'; ";
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Database.SqlQuery<BatteryInfoEntity>(sql).ToList();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public List<BatteryInfoEntity> Query(DateTime startTime, DateTime endTime)
|
|||
|
|
{
|
|||
|
|
string sql = $@"SELECT
|
|||
|
|
TBatteryInfo.Id,
|
|||
|
|
TBatteryInfo.PalletVirtualId,
|
|||
|
|
TBatteryInfo.BatteryStatus,
|
|||
|
|
TBatteryInfo.BatteryCode,
|
|||
|
|
TBatteryInfo.PositionX,
|
|||
|
|
TBatteryInfo.PositionY ,
|
|||
|
|
TBatteryInfo.ScanTime,
|
|||
|
|
TBatteryInfo.CoolTemp,
|
|||
|
|
TPalletInfo1.PalletCode,
|
|||
|
|
TPalletInfo1.LoadingBegingTime,
|
|||
|
|
TPalletInfo1.LoadingOverTime,
|
|||
|
|
TPalletInfo1.BakingPosition,
|
|||
|
|
TPalletInfo1.BakingBeginTime,
|
|||
|
|
TPalletInfo1.BakingOverTime,
|
|||
|
|
TPalletInfo1.UnLoadingBegingTime,
|
|||
|
|
TPalletInfo1.UnLoadingOverTime,
|
|||
|
|
TPalletInfo1.BakingCount,
|
|||
|
|
TPalletInfo1.WaterValue
|
|||
|
|
FROM TBatteryInfo
|
|||
|
|
LEFT JOIN
|
|||
|
|
(SELECT VirtualId, PalletCode, LoadingBegingTime,LoadingOverTime, BakingPosition, BakingBeginTime, BakingOverTime,UnLoadingBegingTime, UnLoadingOverTime, BakingCount, WaterValue
|
|||
|
|
FROM TPalletInfo
|
|||
|
|
UNION
|
|||
|
|
SELECT VirtualId, PalletCode, LoadingBegingTime,LoadingOverTime, BakingPosition, BakingBeginTime, BakingOverTime, UnLoadingBegingTime,UnLoadingOverTime, BakingCount , WaterValue
|
|||
|
|
FROM TPalletInfoHistory) AS TPalletInfo1
|
|||
|
|
ON
|
|||
|
|
TBatteryInfo.PalletVirtualId = TPalletInfo1.VirtualId
|
|||
|
|
where TBatteryInfo.ScanTime >= '{startTime}' and TBatteryInfo.ScanTime <= '{endTime}'; ";
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Database.SqlQuery<BatteryInfoEntity>(sql).ToList();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public List<BatteryInfoEntity> GetBatteryToPallet(int palletVID)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return (from b in Context.Set<TBatteryInfo>()
|
|||
|
|
join p in Context.Set<TPalletInfo>() on b.PalletVirtualId equals p.VirtualId
|
|||
|
|
where b.PalletVirtualId == palletVID
|
|||
|
|
select new BatteryInfoEntity()
|
|||
|
|
{
|
|||
|
|
Id = b.Id,
|
|||
|
|
BatteryCode = b.BatteryCode,
|
|||
|
|
PalletCode = p.PalletCode,
|
|||
|
|
PositionX = b.PositionX,
|
|||
|
|
PositionY = b.PositionY,
|
|||
|
|
ScanTime = b.ScanTime,
|
|||
|
|
BindingTime = b.BindingTime
|
|||
|
|
}).ToList();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public BatteryInfoEntity QueryBatteryWaterValue(string code) //modify by lsm 三张表联查
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
var cellPallet = Context.Set<TBatteryInfo>().Join(Context.Set<TPalletInfo>(),
|
|||
|
|
c => c.PalletVirtualId,
|
|||
|
|
p => p.VirtualId,
|
|||
|
|
(c, p) => new { BatteryInfo = c, PalletInfo = p }).Join(Context.Set<TCavityInfo>(),
|
|||
|
|
c => c.PalletInfo.BakingPosition,
|
|||
|
|
p => p.Id,
|
|||
|
|
(c, p) =>
|
|||
|
|
new BatteryInfoEntity
|
|||
|
|
{
|
|||
|
|
Id = c.PalletInfo.Id,
|
|||
|
|
BatteryStatus = c.BatteryInfo.BatteryStatus,
|
|||
|
|
BatteryCode = c.BatteryInfo.BatteryCode,
|
|||
|
|
PositionX = c.BatteryInfo.PositionX,
|
|||
|
|
PositionY = c.BatteryInfo.PositionY,
|
|||
|
|
ScanTime = c.PalletInfo.ScanTime,
|
|||
|
|
PalletCode = c.PalletInfo.PalletCode,
|
|||
|
|
BakingLocation = p.Name,
|
|||
|
|
BakingBeginTime = c.PalletInfo.BakingBeginTime,
|
|||
|
|
BakingOverTime = c.PalletInfo.BakingOverTime,
|
|||
|
|
PalletVirtualId = c.PalletInfo.VirtualId,
|
|||
|
|
WaterValue = c.PalletInfo.WaterValue
|
|||
|
|
}).Where(x => x.BatteryCode == code).OrderBy(x => x.Id).FirstOrDefault();
|
|||
|
|
|
|||
|
|
if (null != cellPallet)
|
|||
|
|
{
|
|||
|
|
return cellPallet;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return Context.Set<TBatteryInfo>().Join(Context.Set<TPalletInfoHistory>(),
|
|||
|
|
c => c.PalletVirtualId,
|
|||
|
|
p => p.VirtualId,
|
|||
|
|
(c, p) => new { BatteryInfo = c, PalletInfo = p }).Join(Context.Set<TCavityInfo>(),
|
|||
|
|
c => c.PalletInfo.BakingPosition,
|
|||
|
|
p => p.Id,
|
|||
|
|
(c, p) =>
|
|||
|
|
new BatteryInfoEntity
|
|||
|
|
{
|
|||
|
|
Id = c.PalletInfo.Id,
|
|||
|
|
BatteryStatus = c.BatteryInfo.BatteryStatus,
|
|||
|
|
BatteryCode = c.BatteryInfo.BatteryCode,
|
|||
|
|
PositionX = c.BatteryInfo.PositionX,
|
|||
|
|
PositionY = c.BatteryInfo.PositionY,
|
|||
|
|
ScanTime = c.PalletInfo.ScanTime,
|
|||
|
|
PalletCode = c.PalletInfo.PalletCode,
|
|||
|
|
BakingLocation = p.Name,
|
|||
|
|
BakingBeginTime = c.PalletInfo.BakingBeginTime,
|
|||
|
|
BakingOverTime = c.PalletInfo.BakingOverTime,
|
|||
|
|
PalletVirtualId = c.PalletInfo.VirtualId,
|
|||
|
|
WaterValue = c.PalletInfo.WaterValue
|
|||
|
|
}).Where(x => x.BatteryCode == code).OrderBy(x => x.Id).FirstOrDefault();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public bool IsDummyByPalletId(int palletId)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
TBatteryInfo dummy = (from battery in Context.Set<TBatteryInfo>()
|
|||
|
|
join pallet in Context.Set<TPalletInfo>() on battery.PalletVirtualId equals pallet.VirtualId
|
|||
|
|
where pallet.Id == palletId && battery.DummyFlag == true
|
|||
|
|
select battery).FirstOrDefault();
|
|||
|
|
|
|||
|
|
if (null == dummy)
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
//int GetMaxPosX()
|
|||
|
|
//{
|
|||
|
|
// using (var Context = new BakingEntities())
|
|||
|
|
// {
|
|||
|
|
// return (Context.Set<TBatteryInfo>().Max(x=>x.PositionY) ?? 0) + 1;
|
|||
|
|
// }
|
|||
|
|
//}
|
|||
|
|
|
|||
|
|
//public int Inbound(int[] batteryIds, int layer, int stoveNumber,int batteryStatus = (int)EBatteryStatus.InBound)
|
|||
|
|
//{
|
|||
|
|
// string sql = "";
|
|||
|
|
// for (int x = 0; x < batteryIds.Count(); ++x)
|
|||
|
|
// {
|
|||
|
|
// if (0 != batteryIds[x])
|
|||
|
|
// {
|
|||
|
|
// sql += $"update TBatteryInfo set BatteryStatus={batteryStatus},Layer={layer},PositionY={x + 1},PositionX={GetMaxPosX()},InboundTime=NOW(),StoveNumber={stoveNumber} where Id={batteryIds[x]};";
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// if (string.IsNullOrEmpty(sql))
|
|||
|
|
// {
|
|||
|
|
// return 0;
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// using (var Context = new BakingEntities())
|
|||
|
|
// {
|
|||
|
|
// return Context.Database.ExecuteSqlCommand(sql);
|
|||
|
|
// }
|
|||
|
|
//}
|
|||
|
|
|
|||
|
|
//public bool UpdatePreheatStatus()
|
|||
|
|
//{
|
|||
|
|
// string sql = $"UPDATE TBatteryInfo set BatteryStatus = {(int)EBatteryStatus.Preheat} WHERE BatteryStatus = {(int)EBatteryStatus.InBound};";
|
|||
|
|
// using (var Context = new BakingEntities())
|
|||
|
|
// {
|
|||
|
|
// return Context.Database.ExecuteSqlCommand(sql) > 0 ? true : false;
|
|||
|
|
// }
|
|||
|
|
//}
|
|||
|
|
|
|||
|
|
//public bool UpdateOutbound(int[] batteryIds, 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},OutboundTime=NOW() where Id={batteryIds[x]};";
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// using (var Context = new BakingEntities())
|
|||
|
|
// {
|
|||
|
|
// return Context.Database.ExecuteSqlCommand(sql) > 0 ? true : false;
|
|||
|
|
// }
|
|||
|
|
//}
|
|||
|
|
|
|||
|
|
public int InsertBattery(string batteryCode, sbyte status, sbyte scannPos)
|
|||
|
|
{
|
|||
|
|
string sql = $"Call ProcGetBatteryVirtualId('{batteryCode}',{status},{scannPos})";
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Database.SqlQuery<int>(sql).FirstOrDefault();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public TBatteryInfo GetBattery(int batteryId)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Set<TBatteryInfo>().Where(x => x.Id == batteryId).FirstOrDefault();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public List<TBatteryInfo> GetBattery(int[] batteryIds)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Set<TBatteryInfo>().Where(x => batteryIds.Contains(x.Id)).ToList();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//一个电芯会存在多次!电芯条码不是唯一
|
|||
|
|
public List<TBatteryInfo> GetBatterys(List<string> codeBatterys)
|
|||
|
|
{
|
|||
|
|
string sql = "";
|
|||
|
|
string codes = "";
|
|||
|
|
|
|||
|
|
if (0 == codeBatterys.Count)
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
foreach (var code in codeBatterys)
|
|||
|
|
{
|
|||
|
|
codes += $"'{code}',";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
codes = codes.Remove(codes.Length - 1, 1);
|
|||
|
|
sql = $"SELECT * FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY BatteryCode ORDER BY ID DESC ) AS rn FROM " +
|
|||
|
|
$"TBatteryInfo WHERE BatteryCode IN({ codes})) AS temp WHERE rn = 1; ";
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Database.SqlQuery<TBatteryInfo>(sql).ToList();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public TBatteryInfo GetBattery(string batteryCode)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Set<TBatteryInfo>().Where(x => x.BatteryCode == batteryCode).OrderByDescending(x => x.Id).FirstOrDefault();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public bool IsExist(string batteryCode, int batteryId)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
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 BakingEntities())
|
|||
|
|
{
|
|||
|
|
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 BakingEntities())
|
|||
|
|
{
|
|||
|
|
List<OutputEntity> output = Context.Database.SqlQuery<OutputEntity>($"call ProcProductionQtyQuery ('{startDateTime}','{endDateTime}')").ToList();
|
|||
|
|
if (output == null)
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
return output;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public List<TBatteryInfo> QueryIncomingCell(DateTime startTime, DateTime endTime)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
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 BakingEntities())
|
|||
|
|
{
|
|||
|
|
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, int stoveNumber = 1)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Set<TBatteryInfo>().Where(x => x.ScanTime >= startTime && x.ScanTime <= endTime).Count();
|
|||
|
|
//List<TBatteryInfo> list = Context.Set<TBatteryInfo>().Where(x => x.ScanTime >= startTime && x.ScanTime <= endTime).ToList();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|