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.

417 lines
18 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 Dapper;
using Estsh.Core.Dapper;
using Estsh.Core.Model.ExcelModel;
using Estsh.Core.Model.Result;
using Estsh.Core.Models;
using Estsh.Core.Repository.IRepositories;
using Estsh.Core.Util;
using System.Collections;
using System.Data;
using System.Text;
/***************************************************************************************************
*
* 更新人sitong.dong
* 描述:
* 修改时间2022.06.22
* 描述:系统迭代升级
*
**************************************************************************************************/
namespace Estsh.Core.Repositories
{
public class AndonInfoQueryRepository : BaseRepository<SysPartSnRule>, IAndonInfoQueryRepository
{
public AndonInfoQueryRepository(DapperDbContext _dapperDbContext) : base(_dapperDbContext)
{
}
#region 成员方法
/// <summary>
/// 获得菜单列表数据
/// </summary>
public List<SysPartSnRule> getList(string strWhere, string filedOrder)
{
using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
{
dbConn.Open();
StringBuilder strSql = new StringBuilder();
strSql.Append("select a.*, b.part_id, b.part_no from sys_part_sn_rule a, sys_part b where a.part_id=b.part_id ");
if (!strWhere.Trim().Equals(""))
{
strSql.Append(" and " + strWhere);
}
if (filedOrder != null && !filedOrder.Trim().Equals(""))
{
strSql.Append(" order by " + filedOrder);
}
List<SysPartSnRule> result = dbConn.Query<SysPartSnRule>(strSql.ToString()).ToList();
return result;
}
}
/// <summary>
/// 获取分页数据列表
/// </summary>
public Hashtable getListByPage(int PageSize, int PageIndex, string strWhere, string OrderBy)
{
using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
{
dbConn.Open();
Hashtable result = new Hashtable();
strWhere = " e.enum_type = 'g_andon_types' AND g.enabled = 'Y' AND g.start_time <> '' AND g.end_time <> '' " + strWhere; //查询条件
if (OrderBy.Trim() != "")
{
OrderBy = " " + OrderBy; //排序
}
DynamicParameters Params = new DynamicParameters();
Params.Add("@TotalCount", 0, DbType.Int32, ParameterDirection.Output);
Params.Add("@TotalPage", 0, DbType.Int32, ParameterDirection.Output);
Params.Add("@Table", " dbo.g_andon_log g LEFT JOIN dbo.sys_enum e ON g.andon_type = e.enum_value LEFT JOIN dbo.sys_pdline p ON g.pdline_id = p.pdline_id ");
Params.Add("@Column", " g.station_name , p.pdline_name , e.enum_desc ,CONVERT(VARCHAR(20),g.start_time,120) AS start_time ,CONVERT(VARCHAR(20),g.end_time,120) AS end_time , DATEDIFF(SECOND, g.start_time, g.end_time) AS sj ");
Params.Add("@OrderColumn", OrderBy);
Params.Add("@GroupColumn", "");
Params.Add("@PageSize", PageSize);
Params.Add("@CurrentPage", PageIndex);
Params.Add("@Group", 0);
Params.Add("@Condition", strWhere);
List<GAndonLog> dataList = dbConn.Query<GAndonLog>("Com_Pagination", Params, commandType: CommandType.StoredProcedure).ToList();
result.Add("dataList", dataList);
result.Add("totalCount", Params.Get<int>("@TotalCount"));
return result;
}
}
/// <summary>
/// 获取分页数据列表
/// </summary>
public Hashtable getTableListByPage(int PageSize, int PageIndex, string strWhere, string OrderBy)
{
using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
{
dbConn.Open();
Hashtable result = new Hashtable();
strWhere = " e.enum_type = 'g_andon_types' AND g.start_time <> '' AND g.end_time <> '' " + strWhere; //查询条件
if (OrderBy.Trim() != "")
{
OrderBy = " " + OrderBy; //排序
}
DynamicParameters Params = new DynamicParameters();
Params.Add("@TotalCount", 0, DbType.Int32, ParameterDirection.Output);
Params.Add("@TotalPage", 0, DbType.Int32, ParameterDirection.Output);
Params.Add("@Table", " dbo.g_andon_log g LEFT JOIN dbo.sys_enum e ON g.andon_type = e.enum_value LEFT JOIN dbo.sys_pdline p ON g.pdline_id = p.pdline_id ");
Params.Add("@Column", " g.station_name , p.pdline_name , e.enum_desc ,CONVERT(VARCHAR(20),g.start_time,120) AS start_time ,CONVERT(VARCHAR(20),g.end_time,120) AS end_time , DATEDIFF(SECOND, g.start_time, g.end_time) AS sj ");
Params.Add("@OrderColumn", OrderBy);
Params.Add("@GroupColumn", "");
Params.Add("@PageSize", PageSize);
Params.Add("@CurrentPage", PageIndex);
Params.Add("@Group", 0);
Params.Add("@Condition", strWhere);
List<AndonInfoQuery> dataList = dbConn.Query<AndonInfoQuery>("Com_Pagination", Params, commandType: CommandType.StoredProcedure).ToList();
result.Add("dataList", dataList);
result.Add("totalCount", Params.Get<int>("@TotalCount"));
return result;
}
}
/// <summary>
/// 删除菜单数据
/// </summary>
/// <param name="ruid"></param>
/// <returns></returns>
public int deletePartSNRule(String ruid)
{
using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
{
dbConn.Open();
String delStr = "Update sys_part_sn_rule set enabled='N' WHERE ruid = @ruid";
DynamicParameters htparams = new DynamicParameters();
htparams.Add("@ruid", ruid);
int result = dbConn.Execute(delStr, htparams);
return result;
}
}
#endregion 成员方法
/// <summary>
/// 执行sql语句带事务
/// </summary>
/// <param name="SqlStrings"></param>
/// <param name="Parameters"></param>
/// <returns></returns>
public bool ExecuteSqlTransaction(List<string> SqlStrings, List<DynamicParameters> Parameters, ref string msg)
{
using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
{
dbConn.Open();
IDbTransaction transaction = dbConn.BeginTransaction();
try
{
for (int i = 0; i < SqlStrings.Count; i++)
{
dbConn.Execute(SqlStrings[i], Parameters[i], transaction);
}
transaction.Commit();
return true;
}
catch (Exception ex)
{
transaction.Rollback();
msg = ex.Message;
return false;
}
}
}
/// <summary>
/// 获取 类型 信息
/// BY NOAH
/// </summary>
/// <returns></returns>
public List<KeyValueResult> getTypeData()
{
using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
{
dbConn.Open();
StringBuilder strSql = new StringBuilder();
strSql.Append("select DISTINCT type as [value],type as [key] from dbo.sys_part_sn_rule where enabled='Y' order by type");
List<KeyValueResult> result = dbConn.Query<KeyValueResult>(strSql.ToString()).ToList();
return result;
}
}
/// <summary>
/// 判断用户输入的零件号是否存在
/// BY NOAH
/// </summary>
/// <param name="part_no"></param>
/// <returns></returns>
public String isExsitPart_no(String part_no)
{
using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
{
dbConn.Open();
string part_id = null;
string SqlString = "select top 1 part_id from sys_part where enabled='Y' and part_no=@partNo";
DynamicParameters Params = new DynamicParameters();
Params.Add("@partNo", part_no);
List<SysPart> result = dbConn.Query<SysPart>(SqlString, Params).ToList();
if (result.Count < 1)
{
return part_id;
}
else
{
part_id = result[0].PartId.ToString();
return part_id;
}
}
}
/// <summary>
/// 导入数据方法
/// </summary>
/// <param name="dt">需要导入的数据表</param>
/// <returns>true or false</returns>
public bool InsertData(List<SysPartSnRule> dt, int UserID)
{
using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
{
dbConn.Open();
List<String> SqlStrings = new List<string>(dt.Count);
List<DynamicParameters> hs = new List<DynamicParameters>(dt.Count);
Hashtable PartCache = new Hashtable();
for (int i = 0; i < dt.Count; i++)
{
string partNo = dt[0].PartNo.ToString();
// 跳过空行
if (string.IsNullOrEmpty(partNo.Trim()))
continue;
int partID = GetPartID(partNo);
if (partID == 0)
continue;
// 跳过重复值
if (!PartCache.ContainsKey(partID))
{
PartCache.Add(partID, partNo);
}
else
{
continue;
}
StringBuilder SqlStringBuilder = new StringBuilder(1024);
// 已经存在则更新
if (ExistsSNRule(partID))
{
SqlStringBuilder.Append("UPDATE dbo.sys_part_sn_rule ");
SqlStringBuilder.Append("SET type = @type , ");
SqlStringBuilder.Append(" lenght = @length , ");
SqlStringBuilder.Append(" from1 = @from1 , ");
SqlStringBuilder.Append(" to1 = @to1 , ");
SqlStringBuilder.Append(" fix1 = @fix1 , ");
SqlStringBuilder.Append(" from2 = @from2 , ");
SqlStringBuilder.Append(" to2 = @to2 , ");
SqlStringBuilder.Append(" fix2 = @fix2 ,update_userid=@emp_id, ");
SqlStringBuilder.Append(" update_time=CONVERT(varchar(50), GETDATE(), 21) ");
SqlStringBuilder.Append("WHERE part_id = @part_id ");
}
else
{
SqlStringBuilder.Append("INSERT INTO dbo.sys_part_sn_rule ");
SqlStringBuilder.Append(" ( part_id , ");
SqlStringBuilder.Append(" type, ");
SqlStringBuilder.Append(" lenght , ");
SqlStringBuilder.Append(" from1 , ");
SqlStringBuilder.Append(" to1 , ");
SqlStringBuilder.Append(" fix1 , ");
SqlStringBuilder.Append(" from2 , ");
SqlStringBuilder.Append(" to2 , ");
SqlStringBuilder.Append(" fix2 ");
SqlStringBuilder.Append(" ) ");
SqlStringBuilder.Append(" VALUES (@part_id, ");
SqlStringBuilder.Append(" @type, ");
SqlStringBuilder.Append(" @length, ");
SqlStringBuilder.Append(" @from1, ");
SqlStringBuilder.Append(" @to1, ");
SqlStringBuilder.Append(" @fix1, ");
SqlStringBuilder.Append(" @from2, ");
SqlStringBuilder.Append(" @to2, ");
SqlStringBuilder.Append(" @fix2 ");
SqlStringBuilder.Append(" ) ");
}
DynamicParameters values = new DynamicParameters();
values.Add("@emp_id", UserID);
values.Add("@part_id", partID);
values.Add("@type", dt[i].Type.ToString());
values.Add("@length", dt[i].Lenght.ToString());
values.Add("@from1", dt[i].From1.ToString());
values.Add("@to1", dt[i].To1.ToString());
values.Add("@fix1", dt[i].Fix1.ToString());
values.Add("@from2", dt[i].From2.ToString());
values.Add("@to2", dt[i].To2.ToString());
values.Add("@fix2", dt[i].Fix2.ToString());
SqlStrings.Add(SqlStringBuilder.ToString());
hs.Add(values);
}
IDbTransaction transaction = dbConn.BeginTransaction();
try
{
for (int i = 0; i < SqlStrings.Count; i++)
{
dbConn.Execute(SqlStrings[i], hs[i], transaction);
}
transaction.Commit();
return true;
}
catch (Exception ex)
{
transaction.Rollback();
return false;
}
}
}
/// <summary>
/// 获取PartID
/// </summary>
/// <param name="partNo"></param>
/// <returns></returns>
public int GetPartID(string partNo)
{
using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
{
dbConn.Open();
string SqlString = "select part_id from sys_part where enabled='Y' and part_no=@partNo";
DynamicParameters Params = new DynamicParameters();
Params.Add("@partNo", partNo);
List<SysPart> result = dbConn.Query<SysPart>(SqlString, Params).ToList();
if (result == null)
return 0;
if (result.Count == 0)
return 0;
return Convert.ToInt32(result[0].PartId.ToString());
}
}
/// <summary>
/// 判断系统中是否已存在条码规则
/// </summary>
/// <param name="partID">零件编号</param>
/// <returns>是否已存在</returns>
public bool ExistsSNRule(int partID)
{
using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
{
dbConn.Open();
string SqlString = "select 1 from dbo.sys_part_sn_rule where part_id=@part_id";
DynamicParameters Params = new DynamicParameters();
Params.Add("@part_id", partID);
List<SysPartSnRule> result = dbConn.Query<SysPartSnRule>(SqlString.ToString(), Params).ToList();
return result.Count > 0;
}
}
public List<GAndonLog> getAndonCount(string condition)
{
using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
{
dbConn.Open();
string sql = @" SELECT g.station_name ,
COUNT(g.station_name) AS qty
FROM dbo.g_andon_log g
LEFT JOIN dbo.sys_enum e ON g.andon_type = e.enum_value
LEFT JOIN dbo.sys_pdline p ON g.pdline_id = p.pdline_id
WHERE e.enum_type = 'g_andon_types'
AND g.enabled = 'Y'
AND g.start_time <> ''
AND g.end_time <> '' " + condition + @" GROUP BY station_name ORDER BY qty DESC";
List<GAndonLog> result = dbConn.Query<GAndonLog>(sql).ToList();
return result;
}
}
public List<GAndonLog> getAndonTime(string condition)
{
using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
{
dbConn.Open();
string sql = @" SELECT g.station_name ,
CASE SUM(DATEDIFF(SECOND, g.start_time, g.end_time))/60 WHEN 0 THEN 1 ELSE SUM(DATEDIFF(SECOND, g.start_time, g.end_time))/60 END AS sj
FROM dbo.g_andon_log g
LEFT JOIN dbo.sys_enum e ON g.andon_type = e.enum_value
LEFT JOIN dbo.sys_pdline p ON g.pdline_id = p.pdline_id
WHERE e.enum_type = 'g_andon_types'
AND g.enabled = 'Y'
AND g.start_time <> ''
AND g.end_time <> '' " + condition + @" GROUP BY station_name ORDER BY sj DESC";
List<GAndonLog> result = dbConn.Query<GAndonLog>(sql).ToList();
return result;
}
}
}
}