using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.ServiceModel;
using System.Text;
using ICS.WCF.Base;
using UFSoft.UBF.Service;
using UFIDA.U9.ISV.TransferInISV;

namespace UFIDA.ISV.BFTransferInApprove
{
    public class CustBFTransferInApproveSrvSub
    {

        public bool CustApproveRec(long DocID,string OrgID)
        {
            string DocCode = "";
            string EntityType = "";
            #region 服务调用框架结构
            //实例化代理类 
            ApproveTransferDocClient client = new ApproveTransferDocClient();
            ISVContext context;
            //传入 参数
            RcvInfoDTOData[] pOList;
            try
            {
                DataTable dt = GetData(DocID);
                if (!string.IsNullOrEmpty(dt.Rows[0][0].ToString()) || !string.IsNullOrEmpty(dt.Rows[0][1].ToString()))
                {
                    DocID = long.Parse(dt.Rows[0][0].ToString());
                }
                else
                {
                    throw new Exception("输入的单据号不正确!");
                }

                int acttype = int.Parse(ConfigurationManager.AppSettings["acttype"].ToString());
                //给上下文信息赋值
                context = CreateContextObj(OrgID);
                //给传入参数赋值
                List<RcvInfoDTOData> transferInfoList = new List<RcvInfoDTOData>();
                RcvInfoDTOData transferInfo = new RcvInfoDTOData();
                transferInfo.TransferInDoc = DocID;
                transferInfo.IsTransIn = true;
                transferInfoList.Add(transferInfo);
                pOList = transferInfoList.ToArray();

                //pOList = SetPoDtos(U9Info, poInfoList);
                //服务调用
                (client.Endpoint.Binding as BasicHttpBinding).MaxReceivedMessageSize = int.MaxValue;
                (client.Endpoint.Binding as BasicHttpBinding).MaxBufferSize = int.MaxValue;
                
                client.Do(context, pOList);

                return true;
                //来源调出单号:Tra2020070070
                //完工报告单列表:'WG-2007010069','WG-2007010070'
                //来源调出单行号:20
                //料品数量:3.000000
                //批号
                //来源调出单行号:10
                //料品数量:3.000000
                //批号
            }
            catch (Exception ex)
            {
                StringBuilder str = new StringBuilder();
                str.AppendLine("审核调入单");
                str.AppendLine("单号:" + DocCode);
                str.AppendLine("错误:" + GetExceptionMessage(ex));
                str.AppendLine("结果:" + "False");
                Appconfig.WriteLogFile(str.ToString(), "审核调入单");
                return false;
            }
            #endregion
        }

        private static DataTable GetData(long DocID)
        {
            string sql = @"select ID,DocNo from InvDoc_TransferIn 
                           where ID='" + DocID + "'";
            return DBhlper.Query(sql, Appconfig.GetU9ConnStr());
        }

        #region 提取异常信息
        /// <summary>
        /// 提取异常信息
        /// </summary>
        /// <param name="ex"></param>
        private static string GetExceptionMessage(Exception ex)
        {
            string faultMessage = "未知错误，请查看ERP日志！";

            System.TimeoutException timeoutEx = ex as System.TimeoutException;
            if (timeoutEx != null)
            {
                faultMessage = "访问服务超时，请修改配置信息！";
            }
            return faultMessage;
        }
        #endregion

        #region 给上下文信息赋值
        /// <summary>
        /// 给上下文信息赋值
        /// </summary>
        /// <returns></returns>
        private static ISVContext CreateContextObj(string OrgID)
        {
            // 实例化应用上下文对象
            ISVContext thContext = new ISVContext();
            thContext.UserCode = ConfigurationManager.AppSettings["userCode"].ToString();
            thContext.OrgID = long.Parse(OrgID);
            thContext.OrgCode = ConfigurationManager.AppSettings["orgCode"].ToString();
            thContext.CultureName = ConfigurationManager.AppSettings["cultureName"].ToString();
            thContext.EntCode = ConfigurationManager.AppSettings["enterpriseID"].ToString();

            return thContext;
        }
        #endregion
    }
}
