using DevExpress.DirectX.Common.Direct2D;
using Newtonsoft.Json;
using Seagull.BarTender.PrintServer.Tasks;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ZJ_BYD.Model;
using ZJ_BYD.Untils;
using ZJ_BYD.ViewModel;
namespace ZJ_BYD.DB
{
public class ResultHelper
{
public ResultHelper() : base()
{
}
///
/// 查询所有结果
///
///
public static ISugarQueryable QueryResults()
{
return DBHelper.sqlSugarDb.Queryable();
}
///
/// 通过Sql查询第一列
///
///
///
///
public static int QueryResultIntBySql(string sql, object paras)
{
return DBHelper.sqlSugarDb.Ado.GetInt(sql, paras);
}
///
/// 校验铭牌码绑定的产品条码
///
///
///
public static bool CheckBarcodeAndProductCode(string productCode, string barCode)
{
try
{
var list = DBHelper.sqlSugarDb.Queryable().Where(m => m.BarCode == barCode && m.ProductSfcCode != productCode).ToList();
return list.Count <= 0;
}
catch (Exception ex)
{
var msg = ex == null ? "执行CheckBarcodeAndProductCode方法时异常" : ex.Message;
LogHelper.WriteLog($"执行CheckBarcodeAndProductCode方法时异常:{msg}");
return false;
}
}
///
/// 根据产品编码校验该产品在当前工位是否已生产过
///
///
///
///
public static bool CheckProductHasProducted(string stationCode, string productCode)
{
try
{
var list = DBHelper.sqlSugarDb.Queryable().Where(m => m.StationCode == stationCode && m.ProductSfcCode == productCode && m.TotalStatus.ToUpper() == "OK").ToList();
return list.Count > 0;
}
catch (Exception ex)
{
var msg = ex == null ? "执行CheckProductHasProducted方法时异常" : ex.Message;
LogHelper.WriteLog($"执行CheckProductHasProducted方法时异常:{msg}");
return false;
}
}
///
/// 查询当天的生产合格数
///
///
public static int QueryOkCount()
{
var beginDate = DateTime.Now.ToString("yyyy/MM/dd") + " 00:00:00";
var endDate = DateTime.Now.ToString("yyyy/MM/dd") + " 23:59:59";
var sql = "select COUNT(*) from t_result where totalstatus='OK' and stationcode=@stationcode and createdtime>= @begindate and createdtime<= @enddate";
var paras = new SugarParameter[] {
new SugarParameter("@begindate",DateTime.Parse(beginDate)),
new SugarParameter("@enddate",DateTime.Parse(endDate)),
new SugarParameter("@stationcode",Program.ActiveStatinCode)
};
return DBHelper.sqlSugarDb.Ado.GetInt(sql, paras);
}
///
/// 查询当天的生产合格数
///
///
public static async Task SyncQueryOkCount()
{
var beginDate = DateTime.Now.ToString("yyyy/MM/dd") + " 00:00:00";
var endDate = DateTime.Now.ToString("yyyy/MM/dd") + " 23:59:59";
var sql = "select COUNT(*) from t_result where totalstatus='OK' and stationcode=@stationcode and createdtime>= @begindate and createdtime<= @enddate";
var paras = new SugarParameter[] {
new SugarParameter("@begindate",DateTime.Parse(beginDate)),
new SugarParameter("@enddate",DateTime.Parse(endDate)),
new SugarParameter("@stationcode",Program.ActiveStatinCode)
};
return await DBHelper.sqlSugarDb.Ado.GetIntAsync(sql, paras);
}
///
/// 根据机型查询当天的生产总数
///
/// 产品编码
///
///
public static int QueryTotalCountByProductSfcCode(string productSfcCode,string stationCode)
{
try
{
if (string.IsNullOrWhiteSpace(productSfcCode))
{
LogHelper.WriteLog($"执行QueryTotalCountByProductSfcCode方法时参数productSfcCode为空!");
return 0;
}
var trimVal=productSfcCode.Trim('*');
var beginDate = DateTime.Now.ToString("yyyy/MM/dd") + " 00:00:00";
var endDate = DateTime.Now.ToString("yyyy/MM/dd") + " 23:59:59";
var sql = "select COUNT(*) from t_result where stationcode=@stationcode and LEFT(ProductSfcCode,Length(Replace(@productsfccode, '*', '')))=@trimVal and createdtime>= @begindate and createdtime<= @enddate";
var paras = new SugarParameter[] {
new SugarParameter("@begindate",DateTime.Parse(beginDate)),
new SugarParameter("@enddate",DateTime.Parse(endDate)),
new SugarParameter("@stationcode",stationCode),
new SugarParameter("@productsfccode",productSfcCode),
new SugarParameter("@trimVal",trimVal)
};
return DBHelper.sqlSugarDb.Ado.GetInt(sql, paras);
}
catch (Exception ex)
{
var msg = ex == null ? "执行QueryTotalCountByProductSfcCode方法时异常" : ex.Message;
LogHelper.WriteLog($"执行QueryTotalCountByProductSfcCode方法时异常:{msg}");
return 0;
}
}
///
/// 查询当天的生产总数
///
///
public static int QueryTotalCount()
{
try
{
var beginDate = DateTime.Now.ToString("yyyy/MM/dd") + " 00:00:00";
var endDate = DateTime.Now.ToString("yyyy/MM/dd") + " 23:59:59";
var sql = "select COUNT(*) from t_result where stationcode=@stationcode and createdtime>= @begindate and createdtime<= @enddate";
var paras = new SugarParameter[] {
new SugarParameter("@begindate",DateTime.Parse(beginDate)),
new SugarParameter("@enddate",DateTime.Parse(endDate)),
new SugarParameter("@stationcode",Program.ActiveStatinCode)
};
return DBHelper.sqlSugarDb.Ado.GetInt(sql, paras);
}
catch (Exception ex)
{
var msg = ex == null ? "执行QueryTotalCount方法时异常" : ex.Message;
LogHelper.WriteLog($"执行QueryTotalCount方法时异常:{msg}");
return 0;
}
}
///
/// 查询当天的生产总数
///
///
public static async Task SyncQueryTotalCount()
{
try
{
var beginDate = DateTime.Now.ToString("yyyy/MM/dd") + " 00:00:00";
var endDate = DateTime.Now.ToString("yyyy/MM/dd") + " 23:59:59";
var sql = "select COUNT(*) from t_result where stationcode=@stationcode and createdtime>= @begindate and createdtime<= @enddate";
var paras = new SugarParameter[] {
new SugarParameter("@begindate",DateTime.Parse(beginDate)),
new SugarParameter("@enddate",DateTime.Parse(endDate)),
new SugarParameter("@stationcode",Program.ActiveStatinCode)
};
return await DBHelper.sqlSugarDb.Ado.GetIntAsync(sql, paras);
}
catch (Exception ex)
{
var msg = ex == null ? "执行SyncQueryTotalCount方法时异常" : ex.Message;
LogHelper.WriteLog($"执行SyncQueryTotalCount方法时异常:{msg}");
return 0;
}
}
///
/// 查询各状态的的生产数量
///
///
public static async Task> SyncQueryCountByGroupStatus()
{
try
{
var beginDate = DateTime.Now.ToString("yyyy/MM/dd") + " 00:00:00";
var endDate = DateTime.Now.ToString("yyyy/MM/dd") + " 23:59:59";
var sql = "select totalstatus ,COUNT(totalstatus) as counts from t_result where stationcode=@stationcode and createdtime>= @begindate and createdtime<= @enddate group by totalstatus";
var paras = new SugarParameter[] {
new SugarParameter("@begindate",DateTime.Parse(beginDate)),
new SugarParameter("@enddate",DateTime.Parse(endDate)),
new SugarParameter("@stationcode",Program.ActiveStatinCode)
};
return await DBHelper.sqlSugarDb.Ado.SqlQueryAsync(sql, paras);
}
catch (Exception ex)
{
var msg = ex == null ? "执行SyncQueryTotalCount方法时异常" : ex.Message;
LogHelper.WriteLog($"执行SyncQueryTotalCount方法时异常:{msg}");
return null;
}
}
///
/// 查询已完成条码
///
///
///
public static List QueryFinishedCodesBySql(string stationCode)
{
try
{
var beginDate = DateTime.Parse(DateTime.Now.ToString("yyyy/MM/dd") + " 00:00:00");
var endDate = DateTime.Parse(DateTime.Now.ToString("yyyy/MM/dd") + " 23:59:59");
var sql = @"select productsfccode as ProductSfcCode from t_result
WHERE stationcode=@stationcode and createdtime>=@begindate and createdtime<=@enddate ORDER BY createdtime DESC LIMIT 6";
var paras = new SugarParameter[] {
new SugarParameter("@stationcode",stationCode),
new SugarParameter("@begindate",beginDate),
new SugarParameter("@enddate",endDate)
};
return DBHelper.sqlSugarDb.Ado.SqlQuery(sql, paras);
}
catch (Exception ex)
{
var msg = ex == null ? "执行QueryFinishedCodesBySql方法时异常" : ex.Message;
LogHelper.WriteLog($"执行QueryFinishedCodesBySql方法时异常:{msg}");
return null;
}
}
///
/// 查询已完成条码
///
///
///
public static async Task> SyncQueryFinishedCodesBySql(string stationCode)
{
try
{
var beginDate = DateTime.Parse(DateTime.Now.ToString("yyyy/MM/dd") + " 00:00:00");
var endDate = DateTime.Parse(DateTime.Now.ToString("yyyy/MM/dd") + " 23:59:59");
var sql = @"select productsfccode as ProductSfcCode from t_result
WHERE stationcode=@stationcode and createdtime>=@begindate and createdtime<=@enddate ORDER BY createdtime DESC LIMIT 5";
var paras = new SugarParameter[] {
new SugarParameter("@stationcode",stationCode),
new SugarParameter("@begindate",beginDate),
new SugarParameter("@enddate",endDate)
};
return await DBHelper.sqlSugarDb.Ado.SqlQueryAsync(sql, paras);
}
catch (Exception ex)
{
var msg = ex == null ? "执行SyncQueryFinishedCodesBySql方法时异常" : ex.Message;
LogHelper.WriteLog($"执行SyncQueryFinishedCodesBySql方法时异常:{msg}");
return null;
}
}
///
/// 新增结果
///
///
///
public static int AddResult(T_Result t_Result)
{
try
{
t_Result.Id = Guid.NewGuid().ToString();
return DBHelper.sqlSugarDb.Insertable(t_Result).ExecuteCommand();
}
catch (Exception ex)
{
var msg = ex == null ? "未知异常" : ex.Message;
LogHelper.WriteLog($"{t_Result.StationCode}增结果失败:{msg},数据:{JsonConvert.SerializeObject(t_Result)}");
return 0;
}
}
///
///
///
///
/// 产品主条码
/// 是否已上传
/// 上传结果
///
public static int UpdateResultByProductSfcCode(string stationCode, string code, bool isUploaded, string uploadMsg)
{
try
{
if (string.IsNullOrWhiteSpace(code))
{
LogHelper.WriteLog($"执行UpdateResultByProductSfcCode方法时参数code为空!");
return -1;
}
return DBHelper.sqlSugarDb.Updateable()
.SetColumns(m => m.IsUploaded == isUploaded)
.SetColumns(m => m.UploadMsg == uploadMsg)
.Where(m => m.StationCode == stationCode && m.ProductSfcCode == code).ExecuteCommand();
}
catch (Exception ex)
{
var msg = ex == null ? "执行UpdateResultByProductSfcCode方法时异常" : ex.Message;
LogHelper.WriteLog($"执行UpdateResultByProductSfcCode方法时异常:{msg}");
return -1;
}
}
///
/// 查询所有返回实体集合
///
///
///
///
public static ISugarQueryable QueryBySql(string sql, SugarParameter[] paras = null)
{
try
{
return DBHelper.sqlSugarDb.SqlQueryable(sql).AddParameters(paras);
}
catch (Exception ex)
{
var msg = ex == null ? "执行QueryBySql方法时异常" : ex.Message;
LogHelper.WriteLog($"执行QueryBySql方法时异常:{msg}");
return null;
}
}
///
/// 根据创建时间删除数据
///
///
///
public static int DelResult(DateTime time)
{
return DBHelper.sqlSugarDb.Deleteable().Where(m => m.CreatedTime <= time).ExecuteCommand();
}
}
}