using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using QMAPP.BLL;
using QMAPP.Entity;
using QMAPP.FJC.BLL.Basic;
using QMAPP.FJC.BLL.Dict;
using QMAPP.FJC.DAL.Basic;
using QMAPP.FJC.DAL.Equipment;
using QMAPP.FJC.DAL.MachineCheck;
using QMAPP.FJC.Entity.Basic;
using QMAPP.FJC.Entity.Equipment;
using QMAPP.FJC.Entity.MachineCheck;
using QMAPP.MD.BLL;
using QMAPP.MD.Entity;
using QMFrameWork.Data;
using QMFrameWork.Log;
using Resource = QMAPP.FJC.Entity.Resource;
namespace QMAPP.FJC.BLL.MachineCheck
{
///
/// 模块编号:
/// 作 用:设备点检
/// 作 者:周晓东
/// 编写日期:2017年11月29日
///
public class MachineCheckBLL : BaseBLL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// 信息
public MachineCheckEntity Get(MachineCheckEntity model)
{
try
{
return new MachineCheckDAL().Get(model);
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "设备点检--获取信息"
});
throw ex;
}
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(MachineCheckEntity condition, DataPage page)
{
//try
//{
// return new MachineCheckDAL().GetList(condition, page);
//}
//DataResult result = new DataResult();
try
{
//获取班组信息列表
DataPage dataPage = new MachineCheckDAL().GetList(condition, page);
#region 转换显示类型
List InfoList = dataPage.Result as List;
var shiftList = new ProduceShiftBLL().GetWorkcenterShifts("");
var workCellList = new WorkCellBLL().GetAllList(new WorkCell());
var workLocList = new WorkLocBLL().GetAllList(new WorkLoc());
var machineInfoList = new MachineInfoBLL().GetAllList(new MachineInfo());
DictManageBLL dictCHECKTYPE = new DictManageBLL(DictKind.CHECKTYPE);
foreach (MachineCheckEntity m in InfoList)
{
//班次
var shift = shiftList.FirstOrDefault(x => x.PRODUCESHIFTTCODE == m.SHIFT_CODE.Trim());
if (shift != null)
m.SHIFT_CODE_TXT = shift.PRODUCESHIFTNAME;
//工序
var workCell = workCellList.FirstOrDefault(x => x.WORKCELL_CODE == m.WORKCELL_CODE);
if (workCell != null)
m.WORKCELL_CODE_TXT = workCell.WORKCELL_NAME;
//工位
var workLoc = workLocList.FirstOrDefault(x => x.WORKLOC_CODE == m.WORKLOC_CODE);
if (workLoc != null)
m.WORKLOC_CODE_TXT = workLoc.WORKLOC_NAME;
//设备
var machineInfo = machineInfoList.FirstOrDefault(x => x.MACHINECODDE == m.MACHINECODE);
if (machineInfo != null)
m.MACHINECODETXT = machineInfo.MACHINENAME;
//点检类型
m.CHECKTYPETXT = dictCHECKTYPE.GetDictValue(m.CHECKTYPE);
}
#endregion
return dataPage;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "设备点检--获取列表"
});
throw ex;
}
}
public List GetListNew(MachineCheckEntity condition)
{
try
{
return new MachineCheckDAL().GetListNew(condition);
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "设备点检--获取列表"
});
throw ex;
}
}
#endregion
#region 插入信息
///
/// 插入信息(单表)
///
/// 信息
/// 插入行数
public DataResult Insert(MachineCheckEntity model)
{
DataResult result = new DataResult();
try
{
//基本信息
model.PID = Guid.NewGuid().ToString();
model.CREATEUSER = this.LoginUser.UserID;
model.CREATEDATE = DateTime.Now;
model.UPDATEUSER = model.CREATEUSER;
model.UPDATEDATE = model.CREATEDATE;
//MachineCheckDAL cmdDAL = new MachineCheckDAL();
//if (ExistsEquipmentMaintain(model) == true)
//{
// result.IsSuccess = false;
// result.Msg = Resource.IsHaving;
// return result;
//}
result.Result = new MachineCheckDAL().Insert(model);
result.IsSuccess = true;;
result.Msg = Resource.MsgSuccess;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "设备点检--插入信息"
});
result.IsSuccess = false;
result.Msg = Resource.SystemException;
}
return result;
}
#endregion
#region 更新信息
///
/// 更新信息
///
///
/// 更新行数
public DataResult Update(string strs)
{
var mc = new MachineCheckEntity();
mc.PID = strs;
var model = Get(mc);
DataResult result = new DataResult();
result.IsSuccess = false;
result.Msg = Resource.SystemException;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//2018-6-1闫永刚
//由于设备资产号维护错误,先通过设备编号获取正确的设备资产号
string getrealMachineSql = string.Format("select isnull(substring(MACHINECHECK_URL,charindex('assetnum=',t.MACHINECHECK_URL)+9,((charindex('jpnum=',t.MACHINECHECK_URL)-1)-(charindex('assetnum=',t.MACHINECHECK_URL)+9))),' ') from T_BD_MACHINECHECKURL t where machinecode='{0}' and CHECKTYPE='{1}'", model.MACHINECODE, model.CHECKTYPE);
//通过正确的设备资产号获取MAXIMO数据
object realMachineCoddeObj = session.ExecuteSqlScalar(getrealMachineSql, new List().ToArray());
if (realMachineCoddeObj == null)
{
return result;
}
var realMachineCodde = realMachineCoddeObj.ToString();
if (string.IsNullOrEmpty(realMachineCodde))
{
return result;
}
//获取中间表状态为初始化的数据
var tempMACHINECODE = model.MACHINECODE;
model.MACHINECODE = realMachineCodde;
//获取中间表状态为初始化的数据;
var selectMachineCheck = new MachineCheckDAL().GetSelectMachineCheck(model);
if (selectMachineCheck != null)
{
// 开始事务
session.OpenTs();
//把原先的MACHINECODE还原回来
model.MACHINECODE = tempMACHINECODE;
model.CHECKSTATUS = "1"; //点检完成
model.CHECKRESULT = selectMachineCheck.CHECKRESULT;
model.CHECKDATE = selectMachineCheck.DATETIME;
model.UPDATEUSER = selectMachineCheck.CHECKER;
model.UPDATEDATE = DateTime.Now;
//更新设备点检表
result.Result = session.Update(model);
result.IsSuccess = true;
result.Msg = Resource.MsgSuccess;
// 事务提交
session.CommitTs();
}
else
{
result.Msg = "没有查到数据,点检没有完成.";
}
}
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "设备点检--更新信息"
});
result.IsSuccess = false;
result.Msg = Resource.SystemException;
}
return result;
}
#endregion
#region 删除
///
/// 删除信息
///
///
/// 删除个数
public DataResult Delete(string strs)
{
DataResult result = new DataResult();
string[] list = strs.Split(":".ToCharArray());
try
{
foreach (string str in list)
{
result.Result += this.Deleted(new MachineCheckEntity { PID = str });
}
result.IsSuccess = true;
result.Msg = Resource.MsgSuccess;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "设备点检--删除信息"
});
result.IsSuccess = false;
result.Msg = Resource.SystemException;
}
return result;
}
///
/// 删除信息
///
/// 信息
/// 删除个数
public int Deleted(MachineCheckEntity model)
{
int count = 0;
try
{
count = new MachineCheckDAL().Delete(model);
return count;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "设备点检--删除停机信息"
});
throw ex;
}
}
#endregion
#region 导出数据
///
/// 获取导出的数据
///
/// 查询条件
/// 数据
public DataTable GetExportData(MachineCheckEntity model)
{
try
{
return new MachineCheckDAL().GetExportData(model);
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "设备点检--导出停机信息"
});
throw ex;
}
}
#endregion
#region 导入数据
public DataResult GetImportData(List list)
{
DataResult result = new DataResult();
MachineCheckDAL cmDal = new MachineCheckDAL();
List List = new List();
int index = 0;
try
{
result.Result = new ImportMessage();
result.Result.Errors = new List();
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//状态判断
foreach (MachineCheckEntity ma in list)
{
index++;
if (string.IsNullOrEmpty(ma.InfoError) == false)
{
ma.PID = null;
result.Result.failureNum += 1;
continue;
}
//修改改时根据主键等信息获取详细内容信息
MachineCheckEntity oldInfo = cmDal.Get(ma);
if (oldInfo != null)
{
//更新
ma.PID = oldInfo.PID;
ma.CREATEUSER = oldInfo.CREATEUSER;
ma.CREATEDATE = oldInfo.CREATEDATE;
ma.UPDATEUSER = this.LoginUser.UserID;
ma.UPDATEDATE = oldInfo.UPDATEDATE;
ma.IsNewInfo = false;
result.Result.updateNum += 1;
}
else
{
//新增
oldInfo = new MachineCheckEntity();
ma.PID = Guid.NewGuid().ToString();
ma.CREATEUSER = this.LoginUser.UserID;
ma.CREATEDATE = DateTime.Now;
ma.UPDATEUSER = ma.CREATEUSER;
ma.UPDATEDATE = ma.CREATEDATE;
ma.IsNewInfo = true;
result.Result.insertNum += 1;
}
List.Add(ma);
}
}
//导入
cmDal.GetImportData(List);
result.Msg = "导入成功";
result.IsSuccess = true;
return result;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "设备点检--导入停机信息"
});
result.IsSuccess = false;
result.Ex = ex;
return result;
}
}
#endregion
public MachineCheckEntity GetMachineCheckUrl(MachineCheckEntity condition)
{
try
{
return new MachineCheckDAL().GetMachineCheckUrl(condition);
}
catch (Exception ex)
{
throw ex;
}
}
#region 点检中间表读取并更新-事物
public void SelectMachineCheckAndUpdateDB()
{
//获取T_BD_MACHINECHECK表状态为初始化的数据;
var selectMachineCheckList = new MachineCheckDAL().GetListNew(new MachineCheckEntity
{
CHECKSTATUS = "0"
});
if (selectMachineCheckList != null)
{
foreach (var machineCheckEntity in selectMachineCheckList)
{
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//2018-6-1闫永刚
//由于设备资产号维护错误,先通过设备编号获取正确的设备资产号
string getrealMachineSql = string.Format("select isnull(substring(MACHINECHECK_URL,charindex('assetnum=',t.MACHINECHECK_URL)+9,((charindex('jpnum=',t.MACHINECHECK_URL)-1)-(charindex('assetnum=',t.MACHINECHECK_URL)+9))),' ') from T_BD_MACHINECHECKURL t where machinecode='{0}' and CHECKTYPE='{1}'", machineCheckEntity.MACHINECODE, machineCheckEntity.CHECKTYPE);
//通过正确的设备资产号获取MAXIMO数据
object realMachineCoddeObj = session.ExecuteSqlScalar(getrealMachineSql, new List().ToArray());
if (realMachineCoddeObj == null)
{
continue;
}
var realMachineCodde = realMachineCoddeObj.ToString();
if (string.IsNullOrEmpty(realMachineCodde))
{
continue;
}
//获取中间表状态为初始化的数据
var tempMACHINECODE = machineCheckEntity.MACHINECODE;
machineCheckEntity.MACHINECODE = realMachineCodde;
var model = new MachineCheckDAL().GetSelectMachineCheck(machineCheckEntity);
if (model != null)
{
//把原先的MACHINECODE还原回来
machineCheckEntity.MACHINECODE = tempMACHINECODE;
machineCheckEntity.CHECKRESULT = model.CHECKRESULT;
machineCheckEntity.CHECKSTATUS = "1";
machineCheckEntity.UPDATEDATE = DateTime.Now;
//更新设备点检表
session.Update(machineCheckEntity);
}
}
}
catch (Exception ex)
{
//LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex });
int i = 1;
continue;
}
}
}
}
#endregion
}
}