352 lines
15 KiB
C#
352 lines
15 KiB
C#
using DocumentFormat.OpenXml.Office2010.Excel;
|
||
using DocumentFormat.OpenXml.Spreadsheet;
|
||
using Newtonsoft.Json;
|
||
using S7.Net.Types;
|
||
using SqlSugar;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
|
||
namespace LeatherApp.Utils
|
||
{
|
||
public class DBUtils
|
||
{
|
||
private static SqlSugarClient db;
|
||
//public static dbBackup()
|
||
//{
|
||
// //mysqldump -uroot-p123456 mydb > /data/mysqlDump/mydb.sql
|
||
// //mysqldump - uroot - p123456 mydb > / data / mysqlDump / mydb.sql
|
||
//}
|
||
|
||
public static SqlSugarClient getErpDBCon(SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer)
|
||
{
|
||
if (string.IsNullOrEmpty(Config.ErpDBConStr))
|
||
return null;
|
||
if (db != null) return db;
|
||
|
||
db = new SqlSugarClient(new ConnectionConfig()
|
||
{
|
||
ConnectionString = Config.ErpDBConStr,
|
||
DbType = dbType,
|
||
IsAutoCloseConnection = true
|
||
},
|
||
db =>
|
||
{
|
||
db.Aop.OnLogExecuting = (sql, pars) =>
|
||
{
|
||
Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响
|
||
|
||
//获取原生SQL推荐 5.1.4.63 性能OK
|
||
//UtilMethods.GetNativeSql(sql,pars)
|
||
|
||
//获取无参数化SQL 对性能有影响,特别大的SQL参数多的,调试使用
|
||
//UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)
|
||
};
|
||
|
||
});
|
||
return db;
|
||
}
|
||
public static DataTable execSql(string sql, List<SugarParameter> parameters, SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer)
|
||
{
|
||
if (string.IsNullOrEmpty(Config.ErpDBType) || Config.ErpDBType == "SqlServer")
|
||
{
|
||
//查询表的所有
|
||
var mydb = getErpDBCon(dbType);
|
||
if (mydb == null)
|
||
{
|
||
MessageBox.Show($"数据库连接失败:{sql}");
|
||
return null;
|
||
}
|
||
|
||
if (!mydb.Ado.IsValidConnection())
|
||
mydb.Ado.Open();
|
||
return mydb.Ado.GetDataTable(sql, parameters);
|
||
}
|
||
else if (Config.ErpDBType == "Oracle")
|
||
{
|
||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||
{
|
||
DbType = SqlSugar.DbType.Oracle,
|
||
ConnectionString = Config.ErpDBConStr,
|
||
InitKeyType = InitKeyType.Attribute,
|
||
IsAutoCloseConnection = true,
|
||
AopEvents = new AopEvents
|
||
{
|
||
OnLogExecuting = (tsql, p) =>
|
||
{
|
||
Console.WriteLine(tsql);
|
||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||
}
|
||
}
|
||
});
|
||
//var dt = db.Ado.GetDataTable(sql);
|
||
//if (dt != null)
|
||
//{
|
||
// for (int i = 0; i < dt.Rows.Count; i++)
|
||
// {
|
||
// Console.WriteLine($"PJXTBH:{dt.Rows[i]["PJXTBH"]},WPH:{dt.Rows[i]["WPH"]},WPMC:{dt.Rows[i]["WPMC"]},SL:{dt.Rows[i]["SL"]},PH:{dt.Rows[i]["PH"]},JH:{dt.Rows[i]["JH"]}");
|
||
// }
|
||
//}
|
||
string sqlstr = $"{sql}'{parameters[0].Value}'";
|
||
//string sqlstr = $"select * from tb_qc_prodinfo where PJXTBH='{parameters[0].Value}'";
|
||
var dt2 = db.Ado.GetDataTable(sqlstr);
|
||
//dt2.Columns.RemoveAt(0);
|
||
//dt2.Columns.RemoveAt(0);
|
||
return dt2;
|
||
//var dt2 = db.Ado.GetDataTable(sqlstr);
|
||
}
|
||
else if (Config.ErpDBType == "PostgreSQL")
|
||
{
|
||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||
{
|
||
DbType = SqlSugar.DbType.PostgreSQL,
|
||
ConnectionString = Config.ErpDBConStr,
|
||
IsAutoCloseConnection = true,
|
||
AopEvents = new AopEvents
|
||
{
|
||
OnLogExecuting = (tsql, p) =>
|
||
{
|
||
Console.WriteLine(tsql);
|
||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||
}
|
||
}
|
||
});
|
||
|
||
//string sqlstr = $"select * from mingxin_prod where PJXTBH='{parameters[0].Value}'";
|
||
//var dt2 = db.Ado.GetDataTable(sqlstr);
|
||
//dt2.Columns.RemoveAt(0);
|
||
//dt2.Columns.RemoveAt(0);
|
||
|
||
//条码查询 mfg_material_goods
|
||
string sqlstr = $"select id,current_qty,batch_no,goods_code,material_id,mo_id from mfg_material_goods where id={parameters[0].Value}";
|
||
var dt2 = db.Ado.GetDataTable(sqlstr);
|
||
//根据mo_id 从 order_main_order 查询 material_id
|
||
sqlstr = $"select id,material_id from order_main_order where id={dt2.Rows[0]["mo_id"]}";
|
||
var dt4 = db.Ado.GetDataTable(sqlstr);
|
||
//根据material_id 从 base_material 查询 material_code material_name
|
||
sqlstr = $"select id,material_code,material_name from base_material where id={dt4.Rows[0]["material_id"]}";
|
||
var dt3 = db.Ado.GetDataTable(sqlstr);
|
||
|
||
DataTable dt = new DataTable(); //建立个数据表WPMC,SL,PH,JH
|
||
dt.Columns.Add(new DataColumn("WPMC", typeof(string)));//在表中添加string类型的列
|
||
dt.Columns.Add(new DataColumn("SL", typeof(string)));//在表中添加string类型的列
|
||
dt.Columns.Add(new DataColumn("PH", typeof(string)));//在表中添加string类型的列
|
||
dt.Columns.Add(new DataColumn("JH", typeof(string)));//在表中添加string类型的列
|
||
dt.Columns.Add(new DataColumn("ERPID", typeof(string)));//在表中添加string类型的列
|
||
|
||
DataRow dr;//行
|
||
dr = dt.NewRow();
|
||
dr["WPMC"] = dt3.Rows[0]["material_name"];
|
||
dr["SL"] = dt2.Rows[0]["current_qty"];
|
||
dr["PH"] = dt2.Rows[0]["batch_no"];
|
||
dr["JH"] = dt2.Rows[0]["goods_code"];
|
||
dr["ERPID"] = dt3.Rows[0]["material_code"];
|
||
|
||
dt.Rows.Add(dr);//在表的对象的行里添加此行
|
||
return dt;
|
||
}
|
||
else
|
||
return null;
|
||
}
|
||
|
||
#region ERP数据上传
|
||
class XCLErpUploadData
|
||
{
|
||
public string PJXTBH { get; set; }
|
||
public string PH { get; set; }
|
||
public string JH { get; set; }
|
||
[SugarColumn(IsNullable = true)]
|
||
public string FJH { get; set; }
|
||
public string Grade { get; set; }
|
||
public double Len { get; set; }
|
||
public double NetLen { get; set; }
|
||
public int SegmentCnt { get; set; }
|
||
public string SegmentLen { get; set; }
|
||
[SugarColumn(IsNullable = true)]
|
||
public string DefectType { get; set; }
|
||
|
||
[SugarColumn(IsNullable = true)]
|
||
public double MF1 { get; set; }
|
||
[SugarColumn(IsNullable = true)]
|
||
public double MF2 { get; set; }
|
||
[SugarColumn(IsNullable = true)]
|
||
public double MF3 { get; set; }
|
||
public double AvgMF { get; set; }
|
||
|
||
|
||
[SugarColumn(IsNullable = true)]
|
||
public double HD1 { get; set; }
|
||
[SugarColumn(IsNullable = true)]
|
||
public double HD2 { get; set; }
|
||
[SugarColumn(IsNullable = true)]
|
||
public double HD3 { get; set; }
|
||
public double AvgHD { get; set; }
|
||
|
||
public double Weight { get; set; }
|
||
public string Class { get; set; }
|
||
public string DownGradeInfo { get; set; }
|
||
|
||
[SugarColumn(IsNullable = true)]
|
||
public System.DateTime CreateTime { get; set; }
|
||
[SugarColumn(IsNullable = true)]
|
||
public int Status { get; set; }
|
||
[SugarColumn(IsNullable = true)]
|
||
public string UserName { get; set; }
|
||
[SugarColumn(IsNullable = true)]
|
||
public string DUETIME { get; set; }
|
||
[SugarColumn(IsNullable = true)]
|
||
public string REMARK { get; set; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// erp数据上传
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static bool DataUploadErp(Models.Records records)
|
||
{
|
||
if (string.IsNullOrEmpty(Config.ErpDBType) || Config.ErpDBType == "Oracle")
|
||
{
|
||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||
{
|
||
DbType = SqlSugar.DbType.Oracle,
|
||
ConnectionString = Config.ErpDBConStr,
|
||
InitKeyType = InitKeyType.Attribute,
|
||
IsAutoCloseConnection = true,
|
||
AopEvents = new AopEvents
|
||
{
|
||
OnLogExecuting = (tsql, p) =>
|
||
{
|
||
//Console.WriteLine(tsql);
|
||
//Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||
}
|
||
}
|
||
});
|
||
//建表更新
|
||
//db.CodeFirst.InitTables<XCLErpUploadData>();
|
||
//查询是否已有数据
|
||
var haveData = db.Queryable<XCLErpUploadData>().First(m => m.PH == records.BatchId && m.JH == records.ReelId && m.FJH == records.PartReelId);
|
||
if (haveData != null) {
|
||
return false;
|
||
}
|
||
//数据插入
|
||
string grade = "一等品";
|
||
string gradeinfo = "无";
|
||
if(records.DowngradeInformation != null && records.DowngradeInformation.Count >0)
|
||
{
|
||
gradeinfo = "";
|
||
grade = records.DowngradeInformation[records.DowngradeInformation.Count - 1][2];
|
||
foreach (var item in records.DowngradeInformation)
|
||
{
|
||
gradeinfo += $"{item[0]},{item[1]};";// item[1] + ";";
|
||
}
|
||
if (gradeinfo.Length > 0)
|
||
gradeinfo = gradeinfo.Remove(gradeinfo.Length - 1);
|
||
}
|
||
//长度,净长,重量
|
||
double slen = records.Len;
|
||
double jlen = slen;
|
||
double zl = 0;
|
||
if (records.LengthWeightInfor != null && records.LengthWeightInfor.Count() > 3)
|
||
{
|
||
slen = records.LengthWeightInfor[0];
|
||
jlen = records.LengthWeightInfor[2];
|
||
zl = records.LengthWeightInfor[3];
|
||
}
|
||
if(slen == 0 && records.Len > 0)
|
||
slen = records.Len;
|
||
if (jlen == 0 && records.Len > 0)
|
||
jlen = records.Len;
|
||
//分段
|
||
int fd = 0;
|
||
string fdinfo = "无";
|
||
if(records.FDInfor != null && records.FDInfor.Count>0)
|
||
{
|
||
fdinfo = "";
|
||
fd = records.FDInfor.Count;
|
||
foreach(var item in records.FDInfor)
|
||
{
|
||
fdinfo += item[0].ToString() + ",";
|
||
}
|
||
if(fdinfo.Length > 0)
|
||
fdinfo = fdinfo.Remove(fdinfo.Length - 1);
|
||
}
|
||
//厚度均值
|
||
double agvhd = 0;
|
||
double hd1 = 0, hd2 = 0, hd3 = 0;
|
||
if (records.ThicknessList != null && records.ThicknessList.Count > 0)
|
||
{
|
||
List<double> data = new List<double>();
|
||
List<double> data1 = new List<double>();
|
||
List<double> data2 = new List<double>();
|
||
List<double> data3 = new List<double>();
|
||
foreach (var item in records.ThicknessList)
|
||
{
|
||
data.Add(item.Value1);
|
||
data.Add(item.Value2);
|
||
data.Add(item.Value3);
|
||
data1.Add(item.Value1);
|
||
data2.Add(item.Value2);
|
||
data3.Add(item.Value3);
|
||
}
|
||
agvhd = data.Average();
|
||
hd1 = data1.Average();
|
||
hd2 = data2.Average();
|
||
hd3 = data3.Average();
|
||
}
|
||
//幅宽
|
||
double agvmf = 0;
|
||
double mf1 = 0, mf2 = 0, mf3 = 0;
|
||
if (records.FacePointList.Count > 10)
|
||
mf1 = records.FacePointList[10][1];
|
||
else
|
||
mf1 = records.FacePointList[0][1];
|
||
if (records.FacePointList.Count > 10)
|
||
mf3 = records.FacePointList[records.FacePointList.Count - 10][1];
|
||
else
|
||
mf3 = records.FacePointList[records.FacePointList.Count - 1][1];
|
||
if (records.FacePointList.Count > 0)
|
||
mf2 = records.FacePointList[records.FacePointList.Count/2][1];
|
||
|
||
agvmf = (mf1 + mf2 + mf3)/3;
|
||
//新数据插入
|
||
db.Insertable(new XCLErpUploadData()
|
||
{
|
||
PJXTBH = records.PJXTBH,
|
||
PH = records.BatchId,
|
||
JH = records.ReelId,
|
||
FJH = string.IsNullOrEmpty(records.PartReelId) ? "0" : records.PartReelId,
|
||
Grade = string.IsNullOrEmpty(grade) ? "一等品" : grade,
|
||
Len = slen,
|
||
NetLen = jlen,
|
||
SegmentCnt = fd,
|
||
SegmentLen = string.IsNullOrEmpty(fdinfo) ? "无" : fdinfo,
|
||
DefectType = string.IsNullOrEmpty(records.DefectType) ? "错误" : records.DefectType,
|
||
|
||
MF1 = mf1,
|
||
MF2 = mf2,
|
||
MF3 = mf3,
|
||
AvgMF = records.FacePointList.Select(x => x[1]).ToList().Average(),
|
||
|
||
HD1= hd1,
|
||
HD2= hd2,
|
||
HD3= hd3,
|
||
AvgHD = agvhd,
|
||
|
||
Weight = zl,
|
||
Class = string.IsNullOrEmpty(records.WorkTeam)?"无": records.WorkTeam,
|
||
DownGradeInfo = string.IsNullOrEmpty(gradeinfo)? "无" : gradeinfo,
|
||
CreateTime = System.DateTime.Now,
|
||
Status = 0,
|
||
UserName = string.IsNullOrEmpty(records.UserName) ? "admin" : records.UserName,
|
||
}).ExecuteCommand();
|
||
}
|
||
return true;
|
||
}
|
||
#endregion
|
||
}
|
||
}
|