using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.XPath;
using System.IO;
using DSWeb.DsWebService.XmlManifest;
using DSWeb.DsWebService.DAL.DsWebService;
using DSWeb.MvcShipping.Models.MsOpSeai;
using DSWeb.MvcShipping.DAL.MsOpSeaiDAL;
using DSWeb.MvcShipping.DAL.MsOpApplyDAL;
using DSWeb.MvcShipping.Helper;

namespace DsWebService
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "DsWebService")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class DsWebService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public string UpdateCustomMainfast(string Xdoc, string XdocAfter, string Corpid, string SenderOp, string SenderHandphone, string SenderEmail, string SenderFax,string Mblno)
        {

           try
            {
                bool TranAfter = false;
                var filename =Mblno;
                var filenameAfter = filename+"_";

                var FileToZip = @"d:\Manifest\Sendmain\" + filename + ".xml";

                var ZipedFile = @"d:\Manifest\Sendmain\" + filename + ".zip";
                var FileToZipAfter = @"d:\Manifest\Sendafter\" + filenameAfter + ".xml";


                var ZipedFileAfter = @"d:\Manifest\Sendafter\" + filenameAfter + ".zip";

                XmlDocument xobj = new XmlDocument();
                xobj.LoadXml(Xdoc);
                xobj.Save(FileToZip);

   

//                if (File.Exists(FileToZip))
//                {
 //                    DsWebServiceDAL.ZipFile2(FileToZip, ZipedFile, 6,4096);
////                    DsWebServiceDAL.CreateZipFile(FileToZip, ZipedFile);
//                }

//                if (File.Exists(FileToZip))
//                {
//                    File.Delete(FileToZip);
//                }
      

//                if (File.Exists(FileToZipAfter))
//                {
//                    DsWebServiceDAL.ZipFile(FileToZipAfter, ZipedFileAfter, "");
////                    DsWebServiceDAL.CreateZipFile(FileToZip, ZipedFile);
//                }

//                if (File.Exists(FileToZipAfter))
//                {
//                    File.Delete(FileToZipAfter);
//                }


                StringReader rdr = new StringReader(Xdoc);
                XmlSerializer serializer = new XmlSerializer(typeof(Manifest));

                Manifest xmlDoc = (Manifest)serializer.Deserialize(rdr);
                if (XdocAfter == "1") {
                    XdocAfter = Xdoc.Replace("<FunctionCode>9</FunctionCode>", "<FunctionCode>0</FunctionCode>");

                    XdocAfter = XdocAfter.Replace("<MessageID>" + xmlDoc.Head.MessageID + "</MessageID>", "<MessageID>A" + xmlDoc.Head.MessageID + "</MessageID>");
                }
                if (XdocAfter != "")
                {
                    xobj.LoadXml(XdocAfter);
                    xobj.Save(FileToZipAfter);
                    TranAfter = true;
                }



                DsWebServiceDAL.AddBill(xmlDoc, SenderOp, Corpid, SenderHandphone, SenderEmail, SenderFax, filename, TranAfter);


                return "成功";

               
            }
            catch (Exception execError)
            {
                return execError.ToString();
            }
            finally
            {
            }


        }

        [WebMethod]
        public string LoadCustomMainfastStatus(string Mblno)
        {
            if (Mblno == "")
                return "";
            else
                return DsWebServiceDAL.LoadBillStatus(Mblno);

        }
        [WebMethod]
        public string GetSeaiBsDataList(string LoginName, string LoginPass, string Mobile, string Mblno, int start, int limit)
        {

            var SeaiManifest = DsWebServiceDAL.GetSeaiBsDataList(LoginName, LoginPass, Mobile, Mblno, start, limit);

            if (SeaiManifest != null)
            {

                var filename = Guid.NewGuid().ToString();
                var path = Server.MapPath("/EDIFiles/SeaiEdi");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                filename = path + "\\" + Mblno.Replace("/", "_") + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xml";
                if (System.IO.File.Exists(filename))
                {
                    System.IO.File.Delete(filename);
                }

                //                 var FileToZip = @"d:\Manifest\" + filename + ".XML";
                using (StreamWriter sw = new StreamWriter(filename))
                {
                    XmlSerializer xs = new XmlSerializer(SeaiManifest.GetType());
                    xs.Serialize(sw, SeaiManifest);
                }

                XmlDocument xobj = new XmlDocument();
                xobj.Load(filename);
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
                return DsWebServiceDAL.Xml2String(xobj);
            }
            else return "";

        }



        [WebMethod]
        public string GetSeaiBsData(string LoginName, string LoginPass, string Mobile, string Pono)
        {

            var SeaiManifest = DsWebServiceDAL.GetSeaiBsData(LoginName, LoginPass, Mobile, Pono);

            if (SeaiManifest != null)
            {

                var filename = Guid.NewGuid().ToString();
                var path = Server.MapPath("/EDIFiles/SeaiEdi");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                filename = path + "\\" + Pono.Replace("/", "_") + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xml";
                if (System.IO.File.Exists(filename))
                {
                    System.IO.File.Delete(filename);
                }

                //                 var FileToZip = @"d:\Manifest\" + filename + ".XML";
                using (StreamWriter sw = new StreamWriter(filename))
                {
                    XmlSerializer xs = new XmlSerializer(SeaiManifest.GetType());
                    xs.Serialize(sw, SeaiManifest);
                }

                XmlDocument xobj = new XmlDocument();
                xobj.Load(filename);
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
                return DsWebServiceDAL.Xml2String(xobj);
            }
            else return "";

        }

        [WebMethod]
        public string GetSeaiData(string LoginName, string LoginPass, string Mblno, string Pono)
        {
            if (LoginName != "qdtaize" || LoginPass != "EBBE3242-D49E-4398-BBFE-0133CA655EB5")
            {
  
                return "账号密码不正确";
            }
            if (Mblno == ""&& Pono == "")
            {
                return "提单号和PO号不能为空";
            }
            if (Mblno != "") {
                MsOpSeaiHead head = new MsOpSeaiHead() ;
                var opseai= MsOpSeaiDAL.GetData("MBLNO='"+Mblno+"'");
                if (opseai.MBLNO == "") {
                    return "提单号不存在";
                }
                var dataList = MsOpApplyDAL.GetBodyList(" BsNo='" + opseai.BSNO + "'");
                head.SEAIHEAD = opseai;
                head.GOODSDETAIL = dataList;
                var json = JsonConvert.Serialize( new {data = head });
                return json;

            }
            if (Pono != "")
            {
                MsOpSeaiHead head = new MsOpSeaiHead();

                var dataList = MsOpApplyDAL.GetBodyList(" PONO='" + Pono + "'");
                if (dataList.Count==0)
                {
                    return "PONO号不存在";
                }
                var opseai = MsOpSeaiDAL.GetData("BSNO='" + dataList[0].BsNo+ "'");
                if (opseai.OP == "")
                {
                    return "业务信息不存在";
                }
                dataList = MsOpApplyDAL.GetBodyList(" BSNO='" + opseai.BSNO+ "'");
                head.SEAIHEAD = opseai;
                head.GOODSDETAIL = dataList;
                var json = JsonConvert.Serialize(new { data = head });
                return json;

            }
            return "未知错误";

        }



        [WebMethod]
        public string GetSeaeBsDataList(string LoginName, string LoginPass, string Mobile, string Mblno, int start, int limit)
        {

            var SeaiManifest = DsWebServiceDAL.GetSeaeBsDataList(LoginName, LoginPass, Mobile, Mblno, start, limit);

            if (SeaiManifest != null)
            {

                var filename = Guid.NewGuid().ToString();
                var path = Server.MapPath("/EDIFiles/SeaiEdi");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                filename = path + "\\" + Mblno.Replace("/", "_") + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xml";
                if (System.IO.File.Exists(filename))
                {
                    System.IO.File.Delete(filename);
                }

                //                 var FileToZip = @"d:\Manifest\" + filename + ".XML";
                using (StreamWriter sw = new StreamWriter(filename))
                {
                    XmlSerializer xs = new XmlSerializer(SeaiManifest.GetType());
                    xs.Serialize(sw, SeaiManifest);
                }

                XmlDocument xobj = new XmlDocument();
                xobj.Load(filename);
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
                return DsWebServiceDAL.Xml2String(xobj);
            }
            else return "";

        }
        [WebMethod]
        public string GetSeaeBsData(string LoginName, string LoginPass, string Mobile, string Mblno)
        {

            var SeaiManifest = DsWebServiceDAL.GetSeaeBsData(LoginName, LoginPass, Mobile, Mblno);

            if (SeaiManifest != null)
            {

                var filename = Guid.NewGuid().ToString();
                var path = Server.MapPath("/EDIFiles/SeaiEdi");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                filename = path + "\\" + Mblno.Replace("/", "_") + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xml";
                if (System.IO.File.Exists(filename))
                {
                    System.IO.File.Delete(filename);
                }

                //                 var FileToZip = @"d:\Manifest\" + filename + ".XML";
                using (StreamWriter sw = new StreamWriter(filename))
                {
                    XmlSerializer xs = new XmlSerializer(SeaiManifest.GetType());
                    xs.Serialize(sw, SeaiManifest);
                }

                XmlDocument xobj = new XmlDocument();
                xobj.Load(filename);
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
                return DsWebServiceDAL.Xml2String(xobj);
            }
            else return "";

        }



    }
}