新框架PC后端代码(祈禧6月初版本)
南骏 池
3 天以前 72449a1b8699b65712e57fba8abce5a8240e9465
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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 + "条数据!");
        }
    }
}