using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using ZJ_BYD.Common;
using ZJ_BYD.Model;
using ZJ_BYD.Untils;
using ZJ_BYD.ViewModel;
namespace ZJ_BYD.DB
{
public class PlcPointHelper
{
public PlcPointHelper() : base()
{
}
///
/// 查询所有PLC点位
///
///
public static ISugarQueryable QueryPlcPoints()
{
return DBHelper.sqlSugarDb.Queryable();
}
///
/// 根据IdPLC点位
///
///
///
public static T_PlcPoint QueryPlcPointById(int id)
{
return DBHelper.sqlSugarDb.Queryable().First(m => m.Id == id);
}
///
/// 查询未删除的PLC点位
///
///
public static ISugarQueryable QueryActivePlcPoints()
{
return DBHelper.sqlSugarDb.Queryable().Where(m => !m.IsDeleted);
}
///
/// 查询未删除的PLC点位
///
///
public static ISugarQueryable QueryActiveCustomPlcPoints(string ipcId, string lineCode, string stationCode)
{
return DBHelper.sqlSugarDb.Queryable()
.Where(m => m.IpcId == ipcId && m.LineCode == lineCode && m.StationCode == stationCode && m.IsSaveDb && m.IsActive && !m.IsDeleted && !string.IsNullOrWhiteSpace(m.ResultField))
.Select(m => new PcPointVM
{
PointName = m.PointName,
ResultField = m.ResultField,
}).OrderBy(m => m.SortIndex);
}
///
/// 根据Ids删除PLC点位
/// 逻辑删除
///
///
///
public static int DelPlcPointsByIds(List ids)
{
return DBHelper.sqlSugarDb.Updateable()
.SetColumns(m => m.IsDeleted == true)
.SetColumns(m => m.UpdatedTime == DateTime.Now)
.SetColumns(m => m.UpdatedBy == CurrentUser.UserName)
.Where(m => ids.Contains(m.Id)).ExecuteCommand();
}
///
/// 根据Ids物理删除PLC点位
///
///
///
public static int DoDelPlcPointsByIds(List ids)
{
return DBHelper.sqlSugarDb.Deleteable().Where(m => ids.Contains(m.Id)).ExecuteCommand();
}
///
/// 新增PLC点位
///
///
///
public static int AddPlcPoint(T_PlcPoint t_PlcPoint)
{
return DBHelper.sqlSugarDb.Insertable(t_PlcPoint).ExecuteCommand();
}
///
/// 修改PLC点位
///
///
///
public static int UpdatePlcPoint(T_PlcPoint t_PlcPoint)
{
return DBHelper.sqlSugarDb.Updateable(t_PlcPoint).ExecuteCommand();
}
///
/// 批量新增PLC点位
///
///
///
public static bool AddPlcPoints(List t_PlcPoints)
{
var result = DBHelper.sqlSugarDb.UseTran(() =>
{
foreach (var item in t_PlcPoints)
{
AddPlcPoint(item);
}
});
return result.IsSuccess;
}
public static DataTable GetStationData(string stationIndex, string startAddress, string len, string productSfcCode)
{
var stationSortIndex = int.Parse(stationIndex);
var begingAddress = int.Parse(startAddress);
var endAddress = begingAddress + int.Parse(len);
var station = DBHelper.sqlSugarDb.Queryable().First(m => !m.IsDeleted && m.SortIndex == stationSortIndex);
var newDb = DBHelper.GetConnStrByIp(station.IpcId.Split('-')[1].ToString());
var cols = newDb.Queryable()
.Where(m => m.StationCode == station.StationCode
&& m.SortIndex >= begingAddress && m.SortIndex < endAddress && !m.IsDeleted && m.IsActive).ToList();
var sqlCol = "";
if (cols.Count > 0)
{
for (int i = 0; i < cols.Count; i++)
{
sqlCol += $"coalesce({cols[i].ResultField},'') as \"{cols[i].ResultField}\",";
}
var paras = new SugarParameter[] {
new SugarParameter("@stationcode", station.StationCode),
new SugarParameter("@productsfccode", productSfcCode.Replace("\r", ""))
};
var sql = string.Format("select {0} stationcode,productsfccode from t_result where stationcode=@stationcode and productsfccode=@productsfccode order by createdtime desc LIMIT 1", sqlCol);
var dt = newDb.Ado.GetDataTable(sql, paras);
return dt;
}
else
{
return null;
}
}
///
/// 查询PLC点位中已使用的列
///
///
///
public static List GetUsedCol(string stationCode)
{
return DBHelper.sqlSugarDb.Queryable().Where(m => m.IpcId == Program.CurrentIpcId && m.LineCode == Program.CurrentLineCode
&& m.StationCode == stationCode && m.IsActive && !m.IsDeleted)
.Select(m => m.ResultField).ToList();
}
}
}