WelddingPost.cs 13.2 KB
using Accessibility;
using Newtonsoft.Json.Linq;
using NPOI.HPSF;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace HHWelding.WeldingTask
{
    public class WelddingPost
    {
        ///<summary>
        /// 构造函数
        /// </summary>
        List<string> list = new List<string>();

        //Dictionary<string, object> keyValuePairs = new Dictionary<string,object>();
        string kname = string.Empty;
        string DwgPtah = string.Empty;
        ///<summary>
        /// 管段总材料接口
        /// </summary>
        string method = "TaskOperation/ReceiveTask";

        /// <summary>
        /// EP3D图纸接口
        /// </summary>
        string tethod = "TaskOperation/UpdateFreeLocation";
        #region MES地址
        private static string _MesPost = string.Empty;
        public static string MesPost
        {
            get
            {
                if (_MesPost == string.Empty)
                {
                    string Tem = ConfigurationManager.AppSettings["MesApiKey"];
                    _MesPost = Tem;
                }
                return _MesPost;
            }
            set { _MesPost = value; }
        }
        #endregion
        #region 下发图片接口
        public void GetDistinctValuesFromDataGrid(DataSet dataset)
        {
            if (dataset != null)
            {
                // 用于存储所有不重复值的列表
                int count = 0;
                List<string> distinctValues = new List<string>();
                foreach (DataTable table in dataset.Tables)
                {
                    // 从每个DataTable中获取指定字段(这里假设字段名为 "ColumnName",需替换为实际字段名)的不重复值
                    var tableDistinctValues = table.AsEnumerable()
                      .Select(row => row.Field<string>("管线号"))
                      .Distinct();
                    // 将每个表中的不重复值添加到总的列表中
                    distinctValues.AddRange(tableDistinctValues);
                }
                // 去除总的列表中可能存在的重复值(因为不同表中可能有重复元素)
                distinctValues = distinctValues.Distinct().ToList();

                // 可以对获取到的不重复值进行进一步操作,比如遍历展示等
                foreach (string value in distinctValues)
                {
                    // 打开配置文件
                    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    var appSettings = config.AppSettings.Settings;
                    if (appSettings["TrueUrl"].Value.Contains(value))
                    {
                        //MessageBox.Show(value+"已经上传给产线系统");
                        kname += value + ",";
                    }
                    // 要修改的键
                    string keyToModify = "TrueUrl";
                    // 新的值
                    string newValueToAppend = value;
                    if (appSettings[keyToModify] != null)
                    {
                        string existingValue = appSettings[keyToModify].Value;
                        // 组合新值和旧值
                        string combinedValue = existingValue + newValueToAppend;
                        appSettings[keyToModify].Value = combinedValue;
                    }
                    // 保存修改后的配置文件
                    config.Save(ConfigurationSaveMode.Modified);
                    // 刷新配置,使修改生效
                    ConfigurationManager.RefreshSection("appSettings");
                    string filename = value + ".dwg";
                    string dwgpath = appSettings["UrlCsv"].Value + @"\" + filename;
                    MessageBox.Show(dwgpath);
                    count++;
                }
                //调用mes接口
                //回传逻辑实现
                MessageBox.Show("成功上传数据" + count + "条成功");
                //显示已经回传成功的数据
                MessageBox.Show(kname + "已经上传给产线系统");
            }
        }

        #endregion
        /// <summary>
        /// Post请求方法
        /// </summary>
        /// <param name="url"></param>
        /// <param name="body"></param>
        /// <param name="errText"></param>
        /// <returns></returns>

        #region 下发管段总材料接口
        public void mThreadMtd(DataSet data)
        {
            JObject job = new JObject();
            string _msg = string.Empty;
            if (data.Tables[0].Rows == null)
                MessageBox.Show("表格中无数据");
            foreach (DataRow dr in data.Tables[0].Rows)
            {

                string Pipelinenumber1 = dr["管线号1"].ToString();
                string Pipelinenumber2 = dr["管线号2"].ToString();
                string pipename = dr["名称"].ToString();
                string gczj = dr["公称直径"].ToString();
                string yz = dr["英制"].ToString();
                string gldj = dr["管路等级"].ToString();
                string clbm = dr["材料编码"].ToString();
                string bz = dr["备注(描述)"].ToString();
                double count = Convert.ToDouble(dr["数量"]);
                string dw = dr["单位"].ToString();
                string czwd = dr["操作温度"].ToString();
                string sjwd = dr["设计温度"].ToString();
                string gryq = dr["隔热要求"].ToString();
                string hd = dr["厚度"].ToString();
                string sjyl = dr["设计压力"].ToString();

                job.Add("Pipelinenumber1", Pipelinenumber1);
                job.Add("Pipelinenumber2", Pipelinenumber2);
                job.Add("pipename", pipename);
                job.Add("gczj", gczj);
                job.Add("yz", yz);
                job.Add("gldj", gldj);
                job.Add("clbm", clbm);
                job.Add("bz", bz);
                job.Add("count", count);
                job.Add("dw", dw);
                job.Add("czwd", czwd);
                job.Add("sjwd", sjwd);
                job.Add("gryq", gryq);
                job.Add("hd", hd);
                job.Add("sjyl", sjyl);

                MessageBox.Show(job.ToString());
                string res = Post(MesPost + method, job.ToString(), ref _msg);
                if (res.Length <= 0 || res.Length > 0)
                {
                    MessageBox.Show("下发MES管段总材料失败");
                }
                else
                {
                    JObject js = JObject.Parse(res);
                    string code = js.GetValue("Code").ToString();
                    string msg = js.GetValue("Msg").ToString();
                    if (code == "200")
                    {
                        MessageBox.Show("下发MES管段总材料成功");
                    }
                    else
                    {
                        MessageBox.Show("下发MES管段总材料失败原因:" + msg);
                    }
                }
                job.RemoveAll();
            }


        }
        #endregion
        #region 下发焊口接口
        public void mThreadWtd(DataSet data)
        {
            JObject job = new JObject();
            string _msg = string.Empty;
            if (data.Tables[0].Rows == null)
                MessageBox.Show("表格中无数据");
            foreach (DataRow dr in data.Tables[0].Rows)
            {

                string Pipelinenumber = dr["管线号"].ToString();
                string ym = dr["页码"].ToString();
                string hdbh = dr["焊点编号"].ToString();
                string gdbm = dr["管段编号"].ToString();
                string gczj = dr["公称直径"].ToString();
                string hdwj = dr["焊点外径"].ToString();
                string yz = dr["英制"].ToString();
                string sj = dr["吋径"].ToString();
                string bhxl1 = dr["壁厚系列1"].ToString();
                string bhxl2 = dr["壁厚系列2"].ToString();
                string cz1 = dr["材质1"].ToString();
                string cz2 = dr["材质2"].ToString();
                string hdlx = dr["焊点类型"].ToString();
                string hjlx = dr["焊接类型"].ToString();
                string gldj = dr["管路等级"].ToString();
                string rcl = dr["热处理"].ToString();
                string gdjb = dr["管道级别"].ToString();
                string hjcl1 = dr["焊接材料1"].ToString();
                string ms1 = dr["描述1"].ToString();
                string hjcl2 = dr["焊接材料2"].ToString();
                string ms2 = dr["描述2"].ToString();
                string hdwz = dr["焊点位置"].ToString();

                job.Add("Pipelinenumber", Pipelinenumber);
                job.Add("ym", ym);
                job.Add("hdbh", hdbh);
                job.Add("gdbm", gdbm);
                job.Add("gczj", gczj);
                job.Add("hdwj", hdwj);
                job.Add("yz", yz);
                job.Add("sj", sj);
                job.Add("bhxl1", bhxl1);
                job.Add("bhxl2", bhxl2);
                job.Add("cz1", cz1);
                job.Add("cz2", cz2);
                job.Add("hdlx", hdlx);
                job.Add("hjlx", hjlx);
                job.Add("gldj", gldj);
                job.Add("rcl", rcl);
                job.Add("gdjb", gdjb);
                job.Add("hjcl1", hjcl1);
                job.Add("ms1", ms1);
                job.Add("hjcl2", hjcl2);
                job.Add("ms2", ms2);
                job.Add("hdwz", hdwz);

                MessageBox.Show(job.ToString());
                //string res = Post(MesPost + method, job.ToString(), ref _msg);
                //if (res.Length <= 0 || res.Length > 0)
                //{
                //    MessageBox.Show("下发MES管段总材料失败");
                //}
                //else
                //{
                //    JObject js = JObject.Parse(res);
                //    string code = js.GetValue("Code").ToString();
                //    string msg = js.GetValue("Msg").ToString();
                //    if (code == "200")
                //    {
                //        MessageBox.Show("下发MES管段总材料成功");
                //    }
                //    else
                //    {
                //        MessageBox.Show("下发MES管段总材料失败原因:" + msg);
                //    }
                //}
                job.RemoveAll();
            }

        }
        #endregion
        #region 下发切割接口
        public void mThreeWeldingTask(DataSet data)
        {

        }
        #endregion
        #region POST
        public string Post(string url, string body, ref string errText)
        {
            string jsonStr = string.Empty;
            errText = string.Empty;

            try
            {
                //创建Http请求
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.Timeout = 20000;
                req.ContentType = "application/json;charset=utf-8";
                //req.ContentType = "application/x-www-form-urlencoded";
                var postData = Encoding.UTF8.GetBytes(body);
                var reqStream = req.GetRequestStream();
                reqStream.Write(postData, 0, body.Length);
                reqStream.Close();
                var rsp = (HttpWebResponse)req.GetResponse();
                jsonStr = InStream2String(rsp.GetResponseStream(), ref errText);
                rsp.Close();
                return jsonStr;
            }
            catch (WebException ex)
            {
                if (ex.Response != null)
                {
                    Stream stream = ex.Response.GetResponseStream();
                    //从连接错误流获取返回错误信息
                    String errorMsg = InStream2String(stream, ref errText);
                    errText = "提交失败,错误信息:" + errorMsg;
                }
                else { errText = ex.Message; }

                return "";
            }

        }
        #endregion
        #region 读取流信息并且释放流
        /// <summary>
        /// 读取流信息并且释放流
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        private static string InStream2String(Stream stream, ref string errText)
        {
            StreamReader reader = null;
            errText = string.Empty;
            try
            {
                reader = new StreamReader(stream);
                return reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                errText = ex.Message;
                return null;
            }
            finally
            {
                // 释放资源
                if (reader != null) reader.Close();
                if (stream != null) stream.Close();
            }
        }
        #endregion
    }
}