using Cksoft.Data;
using Cksoft.Unity;
using DllEapEntity;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using DllHsmsWeb;
using DllHsms;
namespace DllEapDal
{
public class KnsMacAreaCountMstDal
{
private IDatabase CurrDb = null;
public KnsMacAreaCountMstDal(IDatabase db)
{
CurrDb = db;
}
///
///
///
///
///
///
///
///
///
///
public int MacAreaCountMst01(Machine mac, int eventtypeid,DateTime ptime, List details,string usercode, ref string errorinfo)
{
try
{
int result = 0;
//查找该机台是否存在进行中的任务
string condition = $" and a.MacID={mac.ID} and a.StatusID=1";
List msts = CurrDb.FindListForCondition(condition, ref errorinfo).ToList();
MacAreaCountMst mst = null;
if (eventtypeid==-1&&msts.Count>0)
{
mst = msts[0];
//当前是开始事件,且当前机台存在进行中的任务,这种情况属于异常,直接将进行中的任务结束
//将开始作为当前进行中得结束数据
List templist = CurrDb.FindListForCondition($" and a.preid={msts[0].ID}", ref errorinfo).ToList();
result = AddMacCount02(mac.ID, 1, ptime, templist, details, usercode, ref errorinfo);
if (result <= 0)
return -1;
//汇总明细数量
result = AccountMacAreaCountMst(msts[0].ID, ref errorinfo);
if (result <= 0)
return -1;
//更新主记录状态
mst.StatusID = -1;
mst.CompleteTime = ptime;
CurrDb.UpdateFor(mst, usercode);
//根据列表添加新的任务
mst = AddMacAreaCountMst(eventtypeid, mac,1,1, ptime, details, usercode, ref errorinfo);
if (mst==null)
return -1;
}
if (eventtypeid == -1 && msts.Count <= 0)
{
//当前是开始事件,且当前机台不存在进行中的任务,这种情况属于正常,直接添加记录
//根据列表添加新的任务
mst = AddMacAreaCountMst(eventtypeid, mac, 1, 1, ptime, details, usercode, ref errorinfo);
if (mst == null)
return -1;
}
if (eventtypeid == 1 && msts.Count > 0)
{
mst = msts[0];
//结束且存在进行中的,属于正常情况
//读取明细
List templist = CurrDb.FindListForCondition($" and a.preid={msts[0].ID}", ref errorinfo).ToList();
//添加原始数据记录
result = AddMacCount02(mac.ID, eventtypeid, ptime, templist, details, usercode, ref errorinfo);
if (result<=0)
return -1;
//汇总明细数量
result=AccountMacAreaCountMst(msts[0].ID, ref errorinfo);
if (result <= 0)
return -1;
//更新主记录状态
mst.StatusID = -1;
mst.CompleteTime = ptime;
CurrDb.UpdateFor(mst, usercode);
}
if (eventtypeid == 1 && msts.Count <= 0)
{
//结束且不存在进行中的,属于不正常情况
//读取明细
//mst = AddMacAreaCountMst(eventtypeid, mac, -1, -1, ptime, details, usercode, ref errorinfo);
//if (mst == null)
// return -1;
//忽略,直接返回
return 1;
}
return 1;
}
catch(Exception ex)
{
errorinfo = ex.Message.ToString();
return -1;
}
}
public int MacAreaCountMst(Machine mac, int eventtypeid, DateTime ptime, List details, string usercode, ref string errorinfo)
{
try
{
if(eventtypeid==-1)
{
//每条开始时间,则添加每条记录
}
List templist = new List();
foreach (var item in details)
{
MacCount02 entity = new MacCount02();
entity.PreID = 0;
entity.ParamCode = item.FCode;
entity.EventTypeID = eventtypeid;
entity.MacID = mac.ID;
entity.FCount = int.Parse(item.FVal);
entity.FDate = ptime;
entity.RecTypeID = 1;
templist.Add(entity);
}
return CurrDb.InsertFor(templist, usercode);
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return -1;
}
}
public int AccountMacAreaCountMst(int mstid, ref string errorinfo)
{
try
{
string sqlstr = $@"update macareacountdetail
set FCount = (SELECT sum(EventTypeID * FCount) FROM MacCount02 where preid = macareacountdetail.id and ParamCode = macareacountdetail.ParamCode)
where preid = {mstid} and id> 0";
CurrDb.ExecuteBySql(sqlstr);
return 1;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return -1;
}
}
public MacAreaCountMst AddMacAreaCountMst(int eventtypeid,Machine mac,int statusid,int isrightid,DateTime ptime, List details, string usercode, ref string errorinfo)
{
try
{
//添加主档
MacAreaCountMst mst = AddMacAreaCountMst(mac.ID,statusid,isrightid, ptime, usercode, ref errorinfo);
if (mst == null)
return null;
//添加明细
int result = AddMacAreaCountDetail(mst, details, usercode, ref errorinfo);
if (result < 0)
return null;
//读取明细
List templist = CurrDb.FindListForCondition($" and a.preid={mst.ID}", ref errorinfo).ToList();
//添加原始数据记录
result = AddMacCount02(mac.ID, eventtypeid, ptime, templist, details, usercode, ref errorinfo);
if (result < 0)
return null;
return mst;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return null;
}
}
public int AddMacCount02(int macid,int eventtypeid,DateTime ptime,List areadetails, List details, string usercode, ref string errorinfo)
{
try
{
List templist = new List();
List areatemp = null;
MacAreaCountDetail tempentity = null;
foreach (var item in details)
{
areatemp = areadetails.Where(t => t.ParamCode == item.FCode).ToList();
if(areatemp.Count<=0)
{
tempentity = AddMacAreaCountDetail(areadetails[0].PreID, item.FCode, usercode, ref errorinfo);
if (tempentity == null)
return -1;
}
else
{
tempentity = areatemp[0];
}
MacCount02 entity = new MacCount02();
entity.PreID = tempentity.ID;
entity.ParamCode = item.FCode;
entity.EventTypeID = eventtypeid;
entity.MacID = macid;
entity.FCount = int.Parse(item.FVal);
entity.FDate = ptime;
entity.RecTypeID = 1;
templist.Add(entity);
}
return CurrDb.InsertFor(templist, usercode);
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return -1;
}
}
public int AddMacAreaCountDetail(MacAreaCountMst mst, List details, string usercode, ref string errorinfo)
{
try
{
MacAreaCountDetail tempentity = null;
foreach (var item in details)
{
tempentity = AddMacAreaCountDetail(mst.ID, item.FCode, usercode, ref errorinfo);
if (tempentity == null)
return -1;
}
return 1;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return -1;
}
}
public MacAreaCountDetail AddMacAreaCountDetail(int preid,string paramcode, string usercode, ref string errorinfo)
{
try
{
MacAreaCountDetail entity = new MacAreaCountDetail();
entity.PreID = preid;
entity.ParamCode = paramcode;
entity.FCount = 0;
CurrDb.InsertFor(entity, usercode);
object objid = CurrDb.FindObject("select @@IDENTITY");
if (objid.ToString() == "")
{
return null;
}
int id = int.Parse(objid.ToString());
entity = CurrDb.FindEntityFor(id);
return entity;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return null;
}
}
public MacAreaCountMst AddMacAreaCountMst(int macid,int statusid,int isrightid, DateTime ptime, string usercode, ref string errorinfo)
{
try
{
MacAreaCountMst mst = new MacAreaCountMst();
mst.MacID = macid;
mst.StartTime = ptime;
mst.StatusID = statusid;
mst.IsRightID = isrightid;
CurrDb.InsertFor(mst, usercode);
object objid = CurrDb.FindObject("select @@IDENTITY");
if (objid.ToString() == "")
{
return null;
}
int id = int.Parse(objid.ToString());
mst = CurrDb.FindEntityFor(id);
return mst;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return null;
}
}
public KnsMacCount AreaProcessStart(Machine mac, DateTime ptime, string usercode, ref string errorinfo)
{
try
{
int result = 0;
//查找该机台是否存在进行中的任务
string condition = $" and a.MacID={mac.ID} and a.ProcessStatusID=-1";
List msts = CurrDb.FindListForCondition(condition, ref errorinfo).ToList();
KnsMacCount entity = new KnsMacCount();
entity.StartTime = ptime;
entity.ProcessStatusID = -1;
entity.YieldStatusID = -1;
entity.MacID = mac.ID;
entity.RecTypeID = 1;
CurrDb.InsertFor(entity, usercode);
object objid = CurrDb.FindObject("select @@IDENTITY");
if (objid.ToString() == "")
{
return null;
}
int id = int.Parse(objid.ToString());
KnsMacCount resultentity = CurrDb.FindEntityFor(id);
if (msts.Count > 0)
{
result= AreaProcessComplete(msts[0], ptime, usercode, ref errorinfo);
if (result <= 0)
return null;
}
return resultentity;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return null;
}
}
///
/// 每条完成事件函数
///
///
///
///
///
///
public int AreaProcessComplete(Machine mac, DateTime ptime, string usercode, ref string errorinfo)
{
try
{
int result = 0;
//查找该机台是否存在进行中的任务
string condition = $" and a.MacID={mac.ID} and a.ProcessStatusID=-1";
List msts = CurrDb.FindListForCondition(condition, ref errorinfo).ToList();
if(msts.Count>0)
{
return AreaProcessComplete(msts[0], ptime, usercode, ref errorinfo);
}
return 1;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return -1;
}
}
private int AreaProcessComplete(KnsMacCount knsarea, DateTime ptime, string usercode, ref string errorinfo)
{
try
{
knsarea.EndTime = ptime;
knsarea.ProcessStatusID = 1;
CurrDb.UpdateFor(knsarea, usercode);
//发送读取产量指令
int result = SendS2F15DataSetTime(knsarea, ref errorinfo);
if (result < 0)
return -1;
result= SendS2F41(knsarea, ref errorinfo);
if (result !=0&&result!=4)
return -1;
return 1;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return -1;
}
}
private int SendS2F15DataSetTime(KnsMacCount maccount, ref string errorinfo)
{
try
{
string condition = $" and a.preid=(SELECT preid FROM macorder where macid={maccount.MacID}) and a.sval=2 and a.fval=15";
List orders = CurrDb.FindListForCondition(condition, ref errorinfo).ToList();
if (orders.Count <= 0)
{
errorinfo = $"未找到机台{maccount.MacFCode}对应的S2F15指令。";
return -1;
}
List datas = GetS2F15DataSetTime(maccount.StartTime,maccount.EndTime, orders[0].ID, ref errorinfo);
if (datas == null)
return -1;
HsmsWeb accessmac = new HsmsWeb();
OrderBlock rec = accessmac.SendOrderFor(maccount.MacFCode, orders[0], datas, ref errorinfo);
if (rec == null)
return -1;
return int.Parse(rec.Datalists[0].FContent);
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return -1;
}
}
private List GetS2F41Data(int preid, ref string errorinfo)
{
string rcmd = "CALC_PM_DATA";
List ldata = new List();
OrderData lentity = new OrderData();
lentity.ID = 1;
lentity.ParentID = 0;
lentity.PreID = preid;
lentity.FCode = "L";
lentity.FLen = 2;
ldata.Add(lentity);
OrderData entity = new OrderData();
entity.ID = 2;
entity.ParentID = 1;
entity.FNum = 10;
entity.PreID = preid;
entity.FCode = "A";
entity.FContent = rcmd;
entity.FLen = rcmd.Length;
ldata.Add(entity);
entity = new OrderData();//添加L节点
entity.ID = 3;
entity.ParentID = 1;
entity.FNum = 20;
entity.PreID = preid;
entity.FCode = "L";
entity.FLen = 1;
ldata.Add(entity);
return ldata;
}
private int SendS2F41(KnsMacCount maccount, ref string errorinfo)
{
try
{
string condition = $" and a.preid=(SELECT preid FROM macorder where macid={maccount.MacID}) and a.sval=2 and a.fval=41";
List orders = CurrDb.FindListForCondition(condition, ref errorinfo).ToList();
if (orders.Count <= 0)
{
errorinfo = $"未找到机台{maccount.MacFCode}对应的S2F41指令。";
return -1;
}
List datas = GetS2F41Data(orders[0].ID, ref errorinfo);
if (datas == null)
return -1;
HsmsWeb accessmac = new HsmsWeb();
OrderBlock rec = accessmac.SendOrderFor(maccount.MacFCode, orders[0], datas, ref errorinfo);
if (rec == null)
return -1;
return int.Parse(rec.Datalists[1].FContent);
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return -1;
}
}
//
private static List GetS2F15DataSetTime(DateTime start,DateTime end, int preid, ref string errorinfo)
{
List ldata = new List();
OrderData lentity = GetOrderData(1, preid, 0, "L", 3, "",10, ref errorinfo);
ldata.Add(lentity);
OrderData l1 = GetOrderData(2, preid, 1, "L", 2, "",20, ref errorinfo);
ldata.Add(l1);
OrderData entity = GetOrderData(3, preid, 2, "U2", 1, "1500",30, ref errorinfo);
ldata.Add(entity);
entity = GetOrderData(4, preid, 2, "U1", 1, "0",40, ref errorinfo);
ldata.Add(entity);
OrderData l2 = GetOrderData(5, preid, 1, "L", 2, "",50, ref errorinfo);
ldata.Add(l2);
entity = GetOrderData(6, preid, 5, "U2", 1, "1501",60, ref errorinfo);
ldata.Add(entity);
entity = GetOrderData(7, preid, 5, "A", 14, start.ToString("yyyyMMddHHmmss"),70, ref errorinfo);
ldata.Add(entity);
OrderData l3 = GetOrderData(8, preid, 1, "L", 2, "",80, ref errorinfo);
ldata.Add(l3);
entity = GetOrderData(9, preid, 8, "U2", 1, "1502",90, ref errorinfo);
ldata.Add(entity);
entity = GetOrderData(10, preid, 8, "A", 14, end.ToString("yyyyMMddHHmmss"),100, ref errorinfo);
ldata.Add(entity);
return ldata;
}
private static OrderData GetOrderData(int id, int preid,int parentid,string fcode,int flen,string fcontent,int fnum, ref string errorinfo)
{
OrderData lentity = new OrderData();
lentity.ID = id;
lentity.ParentID = parentid;
lentity.PreID = preid;
lentity.FCode = fcode;
lentity.FLen = flen;
lentity.FContent = fcontent;
lentity.FNum = fnum;
return lentity;
}
public int KnsYield(Machine mac,List lists, string usercode, ref string errorinfo)
{
try
{
int result = 0;
//查找该机台是否存在进行中的任务
string condition = $" and a.MacID={mac.ID} and a.ProcessStatusID=1 and a.YieldStatusID=-1";
List msts = CurrDb.FindListForCondition(condition, ref errorinfo).ToList();
if (msts.Count <= 0)
{
return 100;
}
List CurrReportDetail = CurrDb.FindListForCondition("", ref errorinfo).ToList();
if (CurrReportDetail == null)
{
errorinfo = $"读取数据失败,错误信息:{errorinfo}";
return -1;
}
List tempdt = HsmsUnity.GetOrderS6F11Dt(lists, CurrReportDetail, ref errorinfo);
if (tempdt == null)
{
errorinfo = "GetOrderS6F11Dt函数错误:" + errorinfo;
return -1;
}
foreach(var item in tempdt)
{
KnsMacCountDetail detail = new KnsMacCountDetail();
detail.MstID = msts[0].ID;
detail.ParamCode = item.FCode;
detail.FCount = int.Parse(item.FVal);
CurrDb.InsertFor(detail, usercode);
}
msts[0].YieldStatusID = 1;
CurrDb.UpdateFor(msts[0], usercode);
return 1;
}
catch (Exception ex)
{
errorinfo = ex.Message.ToString();
return -1;
}
}
public int SendStopOrder(string maccode,ref string errorinfo)
{
try
{
string condition = $" and a.fcode='{maccode}'";
List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList();
if(macs.Count<=0)
{
errorinfo = $"未找到机台【{maccode}】信息。";
return -1;
}
condition = $" and a.MacID={macs[0].ID}";
List macorders = CurrDb.FindListForCondition(condition, ref errorinfo).ToList();
if(macorders.Count<=0)
{
errorinfo = $"未找到机台【{maccode}】对应的指令信息。";
return -1;
}
int orderid = macorders[0].PreID;
condition = $" and a.PreID={orderid} and a.FName='S2F41stop'";
List orderdetails = CurrDb.FindListForCondition(condition, ref errorinfo).ToList();
return 1;
}
catch(Exception ex)
{
errorinfo = ex.Message.ToString();
return -1;
}
}
}
}