using Gs.Toolbox;
|
using Microsoft.AspNetCore.Mvc;
|
using Newtonsoft.Json.Linq;
|
using System.Data;
|
using System.Data.SqlClient;
|
|
namespace Gs.OpenApi.Services
|
{
|
|
[ApiGroup(ApiGroupNames.ErpMes)]
|
public class ErpMesController : IRomteService
|
{
|
|
/// <summary>
|
/// 向Mes推数据
|
/// </summary>
|
/// <param name="model"></param>
|
/// <returns></returns>
|
[RequestMethod(RequestMethods.POST)]
|
public ReturnDto<string?> PushByCategory([FromBody] dynamic model)
|
{
|
string category = model.category;
|
JArray jArray = model.list;
|
string tmpGuid = Guid.NewGuid().ToString();
|
int it1 = 0, it2 = 0;
|
//建立table
|
System.Data.DataTable dt = new DataTable();
|
dt.Columns.Add("tmpGuid", Type.GetType("System.Guid"));
|
dt.Columns.Add("category", Type.GetType("System.String"));
|
for (int r = 0; r < 20; r++)
|
{
|
dt.Columns.Add("t" + r.ToString(), Type.GetType("System.String"));
|
}
|
//解析json
|
try
|
{
|
foreach (var jsonitem in jArray)
|
{
|
JObject _job = (JObject)jsonitem;
|
string _t1 = _job["t1"].ToString();
|
string _t2 = _job["t2"].ToString();
|
DataRow _row = dt.NewRow();
|
_row["tmpGuid"] = tmpGuid;
|
_row["category"] = category;
|
_row["t1"] = _t1;
|
_row["t2"] = _t2;
|
dt.Rows.Add(_row);
|
}
|
}
|
catch (Exception ex)
|
{
|
Gs.Toolbox.LogHelper.Debug(this.ToString(), "PushMesByCategory error:" + ex.Message);
|
return ReturnDto<string>.QuickReturn(tmpGuid.ToString(), ReturnCode.Default, "操作失败,请检查json格式:" + ex.Message);
|
}
|
//加入临时表
|
try
|
{
|
using (SqlConnection destinationConnection = new SqlConnection(DbHelperSQL.strConn))
|
{
|
destinationConnection.Open();
|
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection))
|
{
|
bulkCopy.DestinationTableName = "tmp_Import";
|
bulkCopy.BatchSize = dt.Rows.Count;
|
bulkCopy.ColumnMappings.Add("tmpGuid", "tmpGuid");
|
bulkCopy.ColumnMappings.Add("category", "category");
|
bulkCopy.ColumnMappings.Add("t1", "t1");
|
bulkCopy.ColumnMappings.Add("t2", "t2");
|
bulkCopy.WriteToServer(dt);
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
Gs.Toolbox.LogHelper.Debug(this.ToString(), "PushMesByCategory error:" + ex.Message);
|
return ReturnDto<string>.QuickReturn(tmpGuid.ToString(), ReturnCode.Default, "操作失败,插入临时表时错误:" + ex.Message);
|
}
|
//执行数据转移
|
try
|
{
|
using (SqlConnection conn = new SqlConnection(DbHelperSQL.strConn))
|
{
|
using (SqlCommand cmd = new SqlCommand("[prc_push_mes]", conn))
|
{
|
conn.Open();
|
cmd.CommandType = CommandType.StoredProcedure;
|
SqlParameter[] parameters = new SqlParameter[] {
|
new SqlParameter("@outMsg",SqlDbType.NVarChar,300),
|
new SqlParameter("@outInt1",SqlDbType.Int),
|
new SqlParameter("@outInt2",SqlDbType.Int),
|
new SqlParameter("@tmpGuid",tmpGuid),
|
new SqlParameter("@category",category),
|
};
|
parameters[0].Direction = ParameterDirection.Output;
|
parameters[1].Direction = ParameterDirection.Output;
|
parameters[2].Direction = ParameterDirection.Output;
|
foreach (SqlParameter parameter in parameters)
|
{
|
cmd.Parameters.Add(parameter);
|
}
|
cmd.ExecuteNonQuery();
|
it1 = int.Parse(parameters[1].Value.ToString());
|
it2 = int.Parse(parameters[2].Value.ToString());
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
Gs.Toolbox.LogHelper.Debug(this.ToString(), "PushMesByCategory error:" + ex.Message);
|
return ReturnDto<string>.QuickReturn(tmpGuid.ToString(), ReturnCode.Default, "操作失败,转移数据时错误:" + ex.Message);
|
}
|
return ReturnDto<string>.QuickReturn(tmpGuid.ToString(), ReturnCode.Success, "操作成功,本次共增加" + it1.ToString() + "条数据,更新" + it2 + "条数据!");
|
}
|
}
|
}
|