using Cksoft.Data;
using Cksoft.Data.Repository;
using DllEapEntity;
using DllEapEntity.Dtos;
using DllEapEntity.OFILM;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace DllEapDal
{
public class DaSampleDal
{
private readonly IDatabase Db = null;
public DaSampleDal(IDatabase db)
{
Db=db;
}
///
/// 获取DA sample校验参数数据列表以生成时间倒序排列
///
///
///
///
///
///
///
///
///
///
///
public IEnumerable Get(string maccode, string macnum, string type, string result, DateTime? start, DateTime? end, int pageIndex, int pageSize, out int total)
{
string str = $@"f.FName Factory,
e.FName Plant,
d.FName Floor,
g.PCode PCode,
b.FCode MacCode,
h.MacNumber MacNum,
a.Time Time,
a.Type Type,
a.Result Result,
a.X,
a.Y,
a.T,
a.RecCode,
i.FName NAME ";
var datas = Db.FindList(Sql(maccode, macnum, type, result, start, end, pageIndex, pageSize,str)+$"limit { (pageIndex - 1) * pageSize},{ pageSize}");
var sampleValue = MesGet();
foreach (var item in datas)
{
var temp = sampleValue.Where(c => c.macId == item.MacCode && c.materialNo == item.Type)?.FirstOrDefault();
/* item.aXOffset = temp.aXOffset == null ?"0" : temp.aXOffset;
item.sXOffset = temp.aXOffset == null ? "0" : temp.sXOffset;
item.wXOffset = temp.wXOffset == null ? "0" : temp.wXOffset;
item.aYOffset = temp.aYOffset == null ? "0" : temp.aYOffset;
item.sYOffset = temp.sYOffset == null ? "0" : temp.sYOffset;
item.wYOffset = temp.wYOffset == null ? "0" : temp.wYOffset;
item.aTOffset = temp.aTOffset == null ? "0" : temp.aTOffset;
item.sTOffset = temp.sTOffset == null ? "0" : temp.sTOffset;
item.wTOffset = temp.wTOffset == null ? "0" : temp.wTOffset;*/
item.aXOffset = temp.aXOffset ;
item.sXOffset = temp.sXOffset;
item.wXOffset = temp.wXOffset ;
item.aYOffset = temp.aYOffset ;
item.sYOffset = temp.sYOffset ;
item.wYOffset = temp.wYOffset;
item.aTOffset = temp.aTOffset;
item.sTOffset = temp.sTOffset ;
item.wTOffset = temp.wTOffset;
}
str = "count(1)";
total = Convert.ToInt32(Db.FindObject(Sql(maccode, macnum, type, result, start, end, pageIndex, pageSize, str)));
return datas;
}
public IEnumerable MesGet()
{
using(IDatabase db = DbFactory.Base("qis"))
{
return db.FindList($@"select mac_id macId,Material_NO materialNo,STANDARD_XOFFSET sXOffset,STANDARD_YOFFSET sYOffset,STANDARD_ROTATE sTOffset,WARNING_XOFFSET wXOffset,WARNING_YOFFSET wYOffset,WARNING_ROTATE wTOffset,ABNORMAL_XOFFSET aXOffset,ABNORMAL_YOFFSET aYOffset,ABNORMAL_ROTATE aTOffset FROM ofg_qis.view_mfg_sample_check where 1=1");
}
}
///
/// 构建DA Sample 数据 sql语句
///
///
///
///
///
///
///
///
///
///
public string Sql(string maccode, string macnum, string type,string result,DateTime? start, DateTime? end, int pageIndex, int pageSize, string str)
{
string sql = $@"select {str} FROM
dasample a
LEFT JOIN machine b ON b.id = a.MacId
INNER JOIN factoryregion c ON b.RegionId = c.Id
INNER JOIN factoryregion d ON d.id = c.ParentId
INNER JOIN factoryregion e ON e.id = d.ParentId
INNER JOIN factoryregion f ON f.Id = e.ParentId
LEFT JOIN mactprocess g ON g.MacID = b.ID
LEFT JOIN machinenumber h ON h.MacId = b.id
LEFT JOIN staff i ON i.FCode = a.RecCode where 1=1 ";
if (!string.IsNullOrEmpty(maccode))
{
sql += $" and b.fcode like '%{maccode}%'";
}
if (!string.IsNullOrEmpty(macnum))
{
sql += $" and h.MacNumber like '%{macnum}%'";
}
if (!string.IsNullOrEmpty(type))
{
sql += $" and a.type like '%{type}%'";
}
if (!string.IsNullOrEmpty(result))
{
sql += $" and a.result like '%{result}%'";
}
if (start.HasValue)
{
sql += $" and a.Time > '{start.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
}
if (end.HasValue)
{
sql += $" and a.Time < '{end.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
}
sql += $"order By a.Time desc ";
return sql;
}
///
/// 从数据获取DA parameter 数据列表以时间倒序排列
///
///
///
///
///
///
///
///
///
///
public IEnumerable GetParameter(string maccode, string macnum, string type, DateTime? start, DateTime? end, int pageIndex, int pageSize, out int total)
{
string str = $@"f.FName Factory,
e.FName Plant,
d.FName Floor,
g.PCode PCode,
b.FCode MacCode,
h.MacNumber MacNum,
a.RecTime Time,
a.Type Type,
a.X,
a.Y,
a.T,a.MDP,a.ADP,a.RecCode";
var data = Db.FindList(GetSql(maccode, macnum, type, start, end, pageIndex, pageSize, str));
str = "count(1)";
total = Convert.ToInt32(Db.FindObject(GetSql(maccode, macnum, type, start, end, pageIndex, pageSize, str)));
return data;
}
///
/// 生成DA parameter的sql语句
///
///
///
///
///
///
///
///
///
///
public string GetSql(string maccode, string macnum, string type, DateTime? start, DateTime? end, int pageIndex, int pageSize, string str)
{
string sql = $@"select {str}
FROM
daparameter a
LEFT JOIN machine b ON b.id = a.MacId
INNER JOIN factoryregion c ON b.RegionId = c.Id
INNER JOIN factoryregion d ON d.id = c.ParentId
INNER JOIN factoryregion e ON e.id = d.ParentId
INNER JOIN factoryregion f ON f.Id = e.ParentId
LEFT JOIN mactprocess g ON g.MacID = b.ID
LEFT JOIN machinenumber h ON h.MacId = b.id
where 1=1 ";
if (!string.IsNullOrEmpty(maccode))
{
sql += $" and b.fcode like '%{maccode}%'";
}
if (!string.IsNullOrEmpty(macnum))
{
sql += $" and h.MacNumber like '%{macnum}%'";
}
if (!string.IsNullOrEmpty(type))
{
sql += $" and a.type like '%{type}%'";
}
if (start.HasValue)
{
sql += $" and a.RecTime > '{start.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
}
if (end.HasValue)
{
sql += $" and a.RecTime < '{end.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
}
sql += $"order By a.RecTime desc ";
if (str != "count(1)")
{
sql += $"limit { (pageIndex - 1) * pageSize},{ pageSize}";
}
return sql;
}
///
/// 从DA机台获取参数
///
///
///
public async Task