using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.FJC.Entity;
using QMFrameWork.Data;
using QMAPP.Entity;
using System.Reflection;
using QMAPP.FJC.Entity.Basic;
namespace QMAPP.FJC.TRACING.DAInterface
{
///
/// 采集数据对象
///
public class DAObject:IDisposable
{
public DAObject():this(AppDataFactory.CreateMainSession())
{
//AnalyzedValue = new Dictionary();
}
public Dictionary AttachData { get; set; }
public DAObject(IDataSession datasession)
{
this.DataSession = datasession;
PersistentEntityList = new Dictionary();
SQLCommands = new List();
ObjectCacheList = new List();
}
///
/// 统一事务数据会话
///
public IDataSession DataSession { get; private set; }
///
/// 工位状态
///
public WorkLocState WorkLocState { get; set; }
///
/// 物料号
///
public string MaterialCode { get; set; }
///
/// 采集点
///
public Entity.QT.DAI DAI { get; set; }
///
/// 采集值
///
public object DAValue { get; set; }
///
/// 解析后的值
///
public string AnalyzedValue
{
get;
set;
}
///
/// 数据对象主键
///
public string ObjectPID
{
get;
set;
}
///
/// userid
///
public string UserID { get; set; }
///
/// 设备代码
///
public string MachineCode { get; set; }
///
/// 设备名称
///
public string MachineName { get; set; }
///
/// 设备是否受控制 0:不发送不读取 1:只发送不读取 2:不发送至读取 3:即发送也读取
///
public int IsControl { get; set; }
///
/// 当前设备是否发送信号
///
public bool SendSignal
{
get { return (IsControl & 1) > 0; }
}
///
/// 当前设备是否读取信号
///
public bool ReadSignal
{
get { return (IsControl & 2) > 0; }
}
///
/// 数据对象缓存
/// 将重复查询的数据存入此列表,二次查询时可直接从此列表搜索,减少数据库操作;
///
public List ObjectCacheList { get; private set; }
///
/// 获取单个数据
///
///
///
///
public T GetObjectFromCache(Predicate match)
{
var result = from obj in ObjectCacheList
where obj.GetType() == typeof(T)
&& match.Invoke((T)obj)
select (T)obj;
return result.FirstOrDefault();
}
///
/// 获取数据列表
///
///
///
///
public List GetObjectsFromCache(Predicate match)
{
var result = from obj in ObjectCacheList
where obj.GetType() == typeof(T)
&& match.Invoke((T)obj)
select (T)obj;
return result.ToList();
}
///
/// 需要持久化的实体列表
/// Key的格式为“{ClassName}:{PID}" 例如"Product:QWER230-123F1QF-FWGER220-234W11"
///
public Dictionary PersistentEntityList
{
get;
private set;
}
///
/// 添加实体到持久化列表
///
///
public void AddToPersistentList(BaseEntity entity,string pid)
{
string classname = entity.GetType().Name;
string key = string.Format("{0}:{1}", classname, pid);
if (PersistentEntityList.ContainsKey(key))
{
PersistentEntityList[key] = entity;
}
else
{
PersistentEntityList.Add(key, entity);
}
}
///
/// 获取持久化列表中的实体
///
///
///
///
public T GetPersistentEntity(string pid) where T : BaseEntity
{
string classname = typeof(T).Name;
string key = string.Format("{0}:{1}", classname, pid);
if (PersistentEntityList.ContainsKey(key))
{
return (T)PersistentEntityList[key];
}
else
{
return null;
}
}
///
/// 持久化实体数据
///
public void PersistEntitys()
{
var dbsessiontype=this.DataSession.GetType();
var methods = dbsessiontype.GetMethods();
foreach (var entity in PersistentEntityList.Values)
{
MethodInfo method = null;
if (entity.IsDelete)
{
method = methods.FirstOrDefault(p => p.Name == "Delete" && p.ToString() == "Int32 Delete[T](T)");
}
else if (entity.IsNewInfo)
{
method = methods.FirstOrDefault(p => p.Name == "Insert" && p.ToString() == "Int32 Insert[T](T)");
}
else
{
method = methods.FirstOrDefault(p => p.Name == "Update" && p.ToString() == "Int32 Update[T](T)");
}
if (method != null)
{
var closedmethod = method.MakeGenericMethod(entity.GetType());
closedmethod.Invoke(this.DataSession, new object[] { entity });
}
else
{
throw new Exception("method not found!");
}
}
}
///
/// SQL命令列表
///
public List SQLCommands { get; private set; }
///
/// 执行SQL
///
public void ExcuteSQL()
{
foreach (var cmd in SQLCommands)
{
this.DataSession.ExecuteSql(cmd.SQL, cmd.Parameters.ToArray());
}
}
public void Dispose()
{
if (this.DataSession != null)
{
this.DataSession.Dispose();
}
if (PersistentEntityList != null)
{
PersistentEntityList.Clear();
PersistentEntityList = null;
}
if (ObjectCacheList != null)
{
ObjectCacheList.Clear();
ObjectCacheList = null;
}
}
///
/// 模架号
///
public string MouldCode { get; set; }
///
/// 采集时间
///
public DateTime Date { get; set; }
///
/// 当前班次
///
public ProduceShift Shift { get; set; }
///
/// 派工单ID
///
public string WorkOrderID { get; set; }
///
/// 生产计划
///
public Entity.ProductionPlan.WorkOrder OrderPlan { get; set; }
///
/// True代表本次采集为提前输入
///
public bool PreInput { get; set; }
public string MaterialName { get; set; }
///
/// 模架信息是否重新定位
///
public bool MouldReLocate { get; set; }
}
}