using ProductionSystem_Log;
using ProductionSystem_Model.DbModel;
using ProductionSystem_Model.DbModel.Para;
using ProductionSystem_Model.ViewModel.Request.System;
using ProductionSystem_Model.ViewModel.Response;
using ProductionSystem_Model.ViewModel.Response.Para;
using ProductionSystem_Model.ViewModel.Response.System;
using SqlSugar;
using System;
using System.Collections.Generic;
namespace ProductionSystem_Service
{
public class PlcPointService : DbContext
{
///
/// 分页查询PLC点位
///
///
///
///
public List GetPlcPointPageList(QueryPlcPointVM queryPlcPointVM, out int totalCount)
{
totalCount = 0;
try
{
return db.Queryable()
.WhereIF(!string.IsNullOrEmpty(queryPlcPointVM.PointName), a => a.PointName.Contains(queryPlcPointVM.PointName))
.WhereIF(!string.IsNullOrEmpty(queryPlcPointVM.PointAddress), a => a.PointStartAddress == queryPlcPointVM.PointAddress)
.Select((a) => new PlcPointVM
{
Id = a.Id,
SerialNumber = SqlFunc.RowNumber(SqlFunc.Desc(a.CreatedTime)),
PointCode = a.PointCode,
PointName = a.PointName,
PointStartAddress = a.PointStartAddress,
PointLength = a.PointLength,
PointDataType = a.PointDataType,
StrIsSaveDb = a.IsSaveDb ? "是" : "否",
ResultField = a.ResultField,
StrIsShowMain = a.IsShowMain ? "是" : "否",
StrIsTestItem = a.IsTestItem ? "是" : "否",
StrIsRealTimeCollect = a.IsRealTimeCollect ? "是" : "否",
IsActive = a.IsActive,
StrIsActive = a.IsActive ? "是" : "否",
SortIndex = a.SortIndex,
Remark = a.Remark,
CreatedTime = a.CreatedTime,
CreatedBy = a.CreatedBy,
UpdatedTime = a.UpdatedTime,
UpdatedBy = a.UpdatedBy
}).ToPageList(queryPlcPointVM.PageIndex, queryPlcPointVM.PageSize, ref totalCount);
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行PlcPointService下GetPlcPointPageList时异常");
return null;
}
}
///
/// 查询所有PLC点位
///
///
public ISugarQueryable QueryPlcPoints()
{
try
{
return db.Queryable();
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行PlcPointService下QueryPlcPoints时异常");
return null;
}
}
///
/// 查询所有有效PLC点位
///
///
public ISugarQueryable QueryActivePlcPoints()
{
try
{
return db.Queryable().Where(m => m.IsActive).OrderBy(m => m.SortIndex);
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行PlcPointService下QueryActivePlcPoints时异常");
return null;
}
}
///
/// 根据Id查询PLC点位
///
///
public T_PlcPoint QueryPlcPointById(string id)
{
try
{
return db.Queryable().First(m => m.Id == id);
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行PlcPointService下QueryPlcPointById时异常");
return null;
}
}
///
/// 查询所有有效需要存数据库的PLC点位
///
///
public List QueryActiveSaveDbPlcPoints()
{
try
{
//采集数据点位
var collectDataPoints = db.Queryable()
.InnerJoin((a, b) => a.PointCode == b.CollectPointAddress)
.Where((a, b) => a.IsActive && a.IsSaveDb && b.IsActive)
.Select((a, b) => new SaveDbPointVM
{
PointCode = a.PointCode,
PointAddress = a.PointStartAddress,
PointName = a.PointName,
ResultField = a.ResultField,
SortIndex = a.SortIndex
});
//采集结果数据点位
var testResultDataPoints = db.Queryable()
.InnerJoin((a, b) => a.PointCode == b.TestResultPointAddress)
.Where((a, b) => a.IsActive && a.IsSaveDb && b.IsActive)
.Select((a, b) => new SaveDbPointVM
{
PointCode = a.PointCode,
PointAddress = a.PointStartAddress,
PointName = a.PointName,
ResultField = a.ResultField,
SortIndex = a.SortIndex
});
//主界面顶部的那些点位
var topDataPoints = db.Queryable().Where(a => a.IsActive && a.IsSaveDb && !a.IsTestItem)
.Select(a => new SaveDbPointVM
{
PointCode = a.PointCode,
PointAddress = a.PointStartAddress,
PointName = a.PointName,
ResultField = a.ResultField,
SortIndex = a.SortIndex
});
return db.UnionAll(collectDataPoints, testResultDataPoints, topDataPoints).OrderBy(m => m.SortIndex).ToList();
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行PlcPointService下QueryActiveSaveDbPlcPoints时异常");
return null;
}
}
///
/// 查询首页中间部分的PLC点位
///
///
public ISugarQueryable QueryHomeMiddlePlcPoints()
{
try
{
var resultPoints = db.Queryable()
.InnerJoin((a, b) => a.PointCode == b.TestResultPointAddress && b.IsActive)
.Where(a => a.IsActive)
.Select((a, b) => new PointKeyValue
{
Key = a.PointCode,
KeyDesc = a.PointName,
Address = a.PointStartAddress,
Length = a.PointLength,
DataType = a.PointDataType,
IsShowMain = a.IsShowMain,
IsTestItem = a.IsTestItem,
IsSaveDb = a.IsSaveDb,
IsRealTimeCollect = a.IsRealTimeCollect,
ResultFieldName = a.ResultField,
ParaCategory = b.ParaCategory,
SortIndex = a.SortIndex
});
var valPoints = db.Queryable()
.InnerJoin((a, b) => a.PointCode == b.CollectPointAddress && b.IsActive)
.Where(a => a.IsActive)
.Select((a, b) => new PointKeyValue
{
Key = a.PointCode,
KeyDesc = a.PointName,
Address = a.PointStartAddress,
Length = a.PointLength,
DataType = a.PointDataType,
IsShowMain = a.IsShowMain,
IsTestItem = a.IsTestItem,
IsSaveDb = a.IsSaveDb,
IsRealTimeCollect = a.IsRealTimeCollect,
ResultFieldName = a.ResultField,
ParaCategory = b.ParaCategory,
SortIndex = a.SortIndex
});
;
return db.UnionAll(resultPoints, valPoints);
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行PlcPointService下QueryHomeMiddlePlcPoints时异常");
return null;
}
}
///
/// 新增PLC点位
///
///
///
public int AddPlcPoint(T_PlcPoint t_PlcPoint)
{
try
{
return db.Insertable(t_PlcPoint).ExecuteCommand();
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行PlcPointService下AddPlcPoint时异常");
return -1;
}
}
///
/// 批量新增PLC点位
///
///
///
public bool AddPlcPoints(List t_PlcPoints)
{
var result = db.UseTran(() =>
{
foreach (var item in t_PlcPoints)
{
AddPlcPoint(item);
}
});
return result.IsSuccess;
}
///
/// 修改PLC点位
///
///
///
public int UpdatePlcPoint(T_PlcPoint t_PlcPoint)
{
try
{
return db.Updateable(t_PlcPoint).ExecuteCommand();
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行PlcPointService下UpdatePlcPoint时异常");
return -1;
}
}
///
/// 根据Id集合物理删除PLC点位
///
///
///
public int DoDelPlcPointsById(string id)
{
try
{
return db.Deleteable().Where(m => m.Id == id).ExecuteCommand();
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行PlcPointService下DoDelPlcPointsById时异常");
return -1;
}
}
///
/// 查询PLC点位中已使用的列
///
///
public List GetUsedCol()
{
try
{
return db.Queryable().Where(m => m.IsActive)
.Select(m => m.ResultField).ToList();
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行PlcPointService下GetUsedCol时异常");
return null;
}
}
}
}