using Cksoft.Data;
using DllEapEntity;
using DllEapEntity.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DllEapDal.OFILM.MES
{
public class OfTrackMstDal
{
private IDatabase CurrDb = null;
string[] pcodes = new string[4] { "DA", "WB", "LHA", "AA" };
public OfTrackMstDal(IDatabase db)
{
CurrDb = db;
}
public int Insert(OfTrackMst trackMst, string userCode)
{
return CurrDb.InsertFor(trackMst, userCode);
}
///
/// 验证记录是否可以进出站
///
///
///
///
public bool Check(OfTrackDetail trackDetail, ref string errorinfo)
{
return true;
var mac = CurrDb.FindEntityFor(trackDetail.MacId);
var currentPCode = mac.PCode;
var exists = CurrDb.FindListForCondition($" and a.stripId='{trackDetail.StripId}'",
ref errorinfo);
if (exists == null || exists.Count() <= 0)
{
if (currentPCode != pcodes.First())
{
errorinfo = $"当前STRIP还没有生产[{pcodes.First()}]站";
return false;
}
}
else
{
var last = exists.OrderBy(c => c.Id).Last();
var lastPcode = last.ProcessCode;
if (lastPcode == currentPCode)
{
if (trackDetail.Type == 1)
{
if (last.TrackInTime != null)
{
errorinfo = $"当前STRIP已开始[{lastPcode}]站生产,请勿重复操作";
return false;
}
}
else
{
if (last.TrackInTime == null)
{
errorinfo = $"当前STRIP在[{lastPcode}]还未进站";
return false;
}
}
}
else
{
var currentIndex = pcodes.ToList().IndexOf(currentPCode);
var lastIndex = pcodes.ToList().IndexOf(lastPcode);
if (lastIndex != currentIndex - 1)
{
errorinfo = $"当前STRIP还没有生产完前道工序";
return false;
}
else
{
if (last.TrackOutTime == null)
{
errorinfo = $"当前STRIP在[{last.ProcessCode}]站还出站," +
$"不能开始下一站";
return false;
}
}
}
}
return true;
}
///
/// TrackIn操作
///
///
///
///
public int TrackIn(OfTrackDetail trackDetail, string userCode, ref string errorinfo)
{
if (!Check(trackDetail, ref errorinfo))
return -1;
var mst = new OfTrackMst
{
MacId = trackDetail.MacId,
ModCode = userCode,
ModTime = DateTime.Now,
ProcessId = trackDetail.ProcessId,
RecCode = userCode,
RecTime = DateTime.Now,
StripId = trackDetail.StripId,
};
if (CurrDb.InsertFor(mst, userCode) < 0)
return -1;
var sql = "select @@identity;";
var mstId = Convert.ToInt32(CurrDb.FindList(sql).FirstOrDefault() ?? "-1");
trackDetail.MstId = mstId;
trackDetail.ModCode = userCode;
trackDetail.RecCode = userCode;
trackDetail.FDate = DateTime.Now;
trackDetail.IsSuccess = 1;
trackDetail.Type = 1;
if (CurrDb.InsertFor(trackDetail, userCode) < 0)
return -1;
mst = CurrDb.FindEntityFor(mstId);
mst.TrackInTime = DateTime.Now;
if (CurrDb.UpdateFor(mst, userCode) < 0)
return -1;
return 1;
}
///
/// TrackOut操作
///
///
///
///
///
public int TrackOut(OfTrackDetail trackDetail, string userCode, ref string errorinfo)
{
if (!Check(trackDetail, ref errorinfo))
return -1;
var mst = CurrDb.FindListForCondition($" and a.processId={trackDetail.ProcessId}" +
$" and a.StripId='{trackDetail.StripId}'", ref errorinfo).FirstOrDefault();
if (mst == null)
{
errorinfo = "未找到该Strip的进站记录";
return -1;
}
var mstId = mst.Id;
trackDetail.MstId = mstId;
trackDetail.ModCode = userCode;
trackDetail.RecCode = userCode;
trackDetail.FDate = DateTime.Now;
trackDetail.IsSuccess = 1;
trackDetail.Type = -1;
if (CurrDb.InsertFor(trackDetail, userCode) < 0)
return -1;
mst.TrackOutTime = DateTime.Now;
if (CurrDb.UpdateFor(mst, userCode) < 0)
return -1;
return 1;
}
public IEnumerable GetTrackMst(PagedQuery query, out int total)
{
string errorinfo = string.Empty;
var entities = CurrDb.FindListForCondition($" {query.Filter} " +
$"order by {query.SortField} {query.SortOrder} limit {(query.PageIndex - 1) * query.PageSize},{query.PageSize}",
ref errorinfo);
var countSql = new OfTrackMst().GetCountSql() + $" where 1=1 {query.Filter}";
total = Convert.ToInt32(CurrDb.FindList(countSql).FirstOrDefault() ?? "-1");
return entities;
}
public IEnumerable GetTrackDetail(PagedQuery query, out int total)
{
string errorinfo = string.Empty;
var entities = CurrDb.FindListForCondition($" {query.Filter} " +
$"order by {query.SortField} {query.SortOrder} limit {(query.PageIndex - 1) * query.PageSize},{query.PageSize}",
ref errorinfo);
var countSql = new OfTrackDetail().GetCountSql() + $" where 1=1 {query.Filter}";
total = Convert.ToInt32(CurrDb.FindList(countSql).FirstOrDefault() ?? "-1");
return entities;
}
///
/// 根据STRIPID获取Strip的状态
///
///
///
public OfTrackMst Get(string stripId, int processId)
{
string errorinfo = string.Empty;
var entity = CurrDb.FindListForCondition($" and a.StripID='{stripId}' " +
$"and a.processId={processId}",
ref errorinfo).FirstOrDefault();
if (entity != null)
{
if (entity.TrackInTime == null)
{
entity.CurrentState = "未进站";
}
else if (entity.TrackOutTime == null)
{
entity.CurrentState = "已进站";
}
else
{
entity.CurrentState = "已出站";
}
return entity;
}
return new OfTrackMst
{
CurrentState = "未进站"
};
}
///
/// 进出站
///
///
///
///
public int TrackInOut(string macCode, string stripId, string userCode, int? type, ref string errorinfo)
{
try
{
OfTrackMstDal dal = new OfTrackMstDal(CurrDb);
var machine = CurrDb.FindListForCondition($" and a.fcode='{macCode}'",
ref errorinfo).FirstOrDefault();
var mst = CurrDb.FindListForCondition($" and a.macid='{machine.ID}' " +
$"and a.processid='{machine.ProcessId}' and stripId='{stripId}'", ref errorinfo).FirstOrDefault();
var detail = new OfTrackDetail
{
MacId = machine.ID,
FDate = DateTime.Now,
StripId = stripId,
IsSuccess = 1,
ModTime = DateTime.Now,
ProcessId = machine.ProcessId,
OperatorType = 1,
RecTime = DateTime.Now
};
if (type == null || type == 1)
{
detail.Type = 1;
if (dal.TrackIn(detail, userCode, ref errorinfo) < 0)
{
return -1;
}
}
else
{
detail.Type = -1;
if (dal.TrackOut(detail, userCode, ref errorinfo) < 0)
{
return -1;
}
}
//if (mst == null || mst.TrackInTime == null)
//{
// detail.Type = 1;
// if (dal.TrackIn(detail, userCode, ref errorinfo) < 0)
// {
// return -1;
// }
//}
//else
//{
// detail.Type = -1;
// if (dal.TrackOut(detail, userCode, ref errorinfo) < 0)
// {
// return -1;
// }
//}
return 1;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return -1;
}
}
}
}