using NFine.Data.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NFine.Code;
using NFine.Repository;
using System.Data.Common;
using NFine.Domain._03_Entity.SRM;
using ICS.Application.Entity;
using Newtonsoft.Json;
using System.Configuration;
using System.Data.SqlClient;
using ICS.Data;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using NFine.Domain._03_Entity.WMS;

namespace NFine.Application.WMS
{
    public class ICSHGErrorHandleApp : RepositoryFactory<ICSVendor>
    {
        //销售出库过账SAP
        public string WMSInterfaceReCall(string IDList, string WorkPoint)
        {
            string msg = "";
            try
            {
                string result = "";
                string postUrlStr = System.Configuration.ConfigurationManager.AppSettings["WMSpostUrlStr"].ToString();
                string LogIDList = "";
                foreach (string ID in IDList.TrimEnd(',').Split(','))
                {
                    LogIDList += "'" + ID + "',";
                }
                #region 再次调用货柜用WMS接口
                string sql = @"select ID,DocNO,InputJSON from ICSHG_WMSEorrorLog
                               where ID in ({0}) AND WorkPoint='{1}'";
                sql = string.Format(sql, LogIDList.TrimEnd(','), WorkPoint);
                DataTable Sapdt = SqlHelper.GetDataTableBySql(sql);
                foreach (DataRow dr in Sapdt.Rows)
                {
                    result = HTTPHelper.HttpPost("WMS手动回传货柜数据接口", postUrlStr, dr["InputJSON"].ToString());
                    WMSResult wmsresult = JsonConvert.DeserializeObject<WMSResult>(result);
                    if (wmsresult.Success == true)
                    {
                        sql = @"update ICSHG_WMSEorrorLog set EATTRIBUTE1='已完成'
                                where DocNO='{0}' and WorkPoint='{1}'";
                        sql = string.Format(sql, dr["DocNO"].ToString(), WorkPoint);
                        SqlHelper.CmdExecuteNonQueryLi(sql);
                    }
                    else
                    {
                        sql = @"update ICSHG_WMSEorrorLog set ErrorMessage='{0}'
                                where DocNO='{1}' and WorkPoint='{2}'";
                        sql = string.Format(sql, wmsresult.Message, dr["DocNO"].ToString(), WorkPoint);
                        SqlHelper.CmdExecuteNonQueryLi(sql);
                    }
                }
                #endregion
                return msg;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                return msg;
            }
        }
        public static string HttpPost(string url, string body)
        {
            try
            {
                Encoding encoding = Encoding.UTF8;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
                request.ContentType = "application/json; charset=utf-8";

                byte[] buffer = encoding.GetBytes(body);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (WebException ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public class WMSResult
        {
            public bool Success { get; set; }

            public string Message { get; set; }
        }
    }
}