You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

368 lines
16 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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()
{
}
/// <summary>
/// 查询所有结果
/// </summary>
/// <returns></returns>
public static ISugarQueryable<T_Result> QueryResults()
{
return DBHelper.sqlSugarDb.Queryable<T_Result>();
}
/// <summary>
/// 通过Sql查询第一列
/// </summary>
/// <param name="sql"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static int QueryResultIntBySql(string sql, object paras)
{
return DBHelper.sqlSugarDb.Ado.GetInt(sql, paras);
}
/// <summary>
/// 校验铭牌码绑定的产品条码
/// </summary>
/// <param name="productCode"></param>
/// <param name="barCode"></param>
public static bool CheckBarcodeAndProductCode(string productCode, string barCode)
{
try
{
var list = DBHelper.sqlSugarDb.Queryable<T_Result>().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;
}
}
/// <summary>
/// 根据产品编码校验该产品在当前工位是否已生产过
/// </summary>
/// <param name="stationCode"></param>
/// <param name="productCode"></param>
/// <returns></returns>
public static bool CheckProductHasProducted(string stationCode, string productCode)
{
try
{
var list = DBHelper.sqlSugarDb.Queryable<T_Result>().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;
}
}
/// <summary>
/// 查询当天的生产合格数
/// </summary>
/// <returns></returns>
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);
}
/// <summary>
/// 查询当天的生产合格数
/// </summary>
/// <returns></returns>
public static async Task<int> 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);
}
/// <summary>
/// 根据机型查询当天的生产总数
/// </summary>
/// <param name="productSfcCode">产品编码</param>
/// <param name="stationCode"></param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 查询当天的生产总数
/// </summary>
/// <returns></returns>
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;
}
}
/// <summary>
/// 查询当天的生产总数
/// </summary>
/// <returns></returns>
public static async Task<int> 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;
}
}
/// <summary>
/// 查询各状态的的生产数量
/// </summary>
/// <returns></returns>
public static async Task<List<QueryCountVM>> 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<QueryCountVM>(sql, paras);
}
catch (Exception ex)
{
var msg = ex == null ? "执行SyncQueryTotalCount方法时异常" : ex.Message;
LogHelper.WriteLog($"执行SyncQueryTotalCount方法时异常{msg}");
return null;
}
}
/// <summary>
/// 查询已完成条码
/// </summary>
/// <param name="stationCode"></param>
/// <returns></returns>
public static List<ResultFinishedCodeVM> 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<ResultFinishedCodeVM>(sql, paras);
}
catch (Exception ex)
{
var msg = ex == null ? "执行QueryFinishedCodesBySql方法时异常" : ex.Message;
LogHelper.WriteLog($"执行QueryFinishedCodesBySql方法时异常{msg}");
return null;
}
}
/// <summary>
/// 查询已完成条码
/// </summary>
/// <param name="stationCode"></param>
/// <returns></returns>
public static async Task<List<ResultFinishedCodeVM>> 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<ResultFinishedCodeVM>(sql, paras);
}
catch (Exception ex)
{
var msg = ex == null ? "执行SyncQueryFinishedCodesBySql方法时异常" : ex.Message;
LogHelper.WriteLog($"执行SyncQueryFinishedCodesBySql方法时异常{msg}");
return null;
}
}
/// <summary>
/// 新增结果
/// </summary>
/// <param name="t_Result"></param>
/// <returns></returns>
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;
}
}
/// <summary>
///
/// </summary>
/// <param name="stationCode"></param>
/// <param name="code">产品主条码</param>
/// <param name="isUploaded">是否已上传</param>
/// <param name="uploadMsg">上传结果</param>
/// <returns></returns>
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<T_Result>()
.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;
}
}
/// <summary>
/// 查询所有返回实体集合
/// </summary>
/// <param name="sql"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static ISugarQueryable<ResultVM> QueryBySql(string sql, SugarParameter[] paras = null)
{
try
{
return DBHelper.sqlSugarDb.SqlQueryable<ResultVM>(sql).AddParameters(paras);
}
catch (Exception ex)
{
var msg = ex == null ? "执行QueryBySql方法时异常" : ex.Message;
LogHelper.WriteLog($"执行QueryBySql方法时异常{msg}");
return null;
}
}
/// <summary>
/// 根据创建时间删除数据
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static int DelResult(DateTime time)
{
return DBHelper.sqlSugarDb.Deleteable<T_Result>().Where(m => m.CreatedTime <= time).ExecuteCommand();
}
}
}