using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using TellerSystem.ServiceProxy.Ext.ServiceHelper;
using Platform.Common.RunningParameters;
using System.Windows;
using TellerSystem.Library.Ext.TradeExtension;
using TellerSystem.Communication;
using TellerSystem.Library.Ext.Function;

namespace TellerSystem.Contract.Function
{
    /// <summary>
    /// 与核心通讯，查询账户黑白名单状态/限制渠道
    /// 黑名单-阻断，灰名单-提示，白名单-通过
    /// </summary>
    public class CheckBlackList : TFOutput
    {
        #region Output
        /// <summary>
        /// 标志位
        /// 0-，1-阻断，2-提示
        /// </summary>
        public string BZW { get; internal set; }
        /// <summary>
        /// 提示信息
        ///
        /// </summary>
        public string info { get; internal set; }
        /// <summary>
        /// 全渠道限制标志
        /// 0-不限制，1-限制
        /// </summary>
        public int allrestrict { get; internal set; }
        /// <summary>
        /// 只进不出限制标志
        /// 0-不限制，1-限制
        /// </summary>
        public int onleinrestrict { get; internal set; }
        #endregion

        #region Input
        /// <summary>
        /// 参数清单
        /// </summary>
        public class Input : TFInput
        {
            /// <summary>
            /// 查询类型
            /// 1-按账号，2-按证件
            /// </summary>
            public string CXLX { get; set; }

            /// <summary>
            /// 账号 0300
            /// (null) 
            /// </summary>
            public string ZH { get; set; }

            /// <summary>
            /// 账户序号
            /// (null) 
            /// </summary>
            public string ZHXH { get; set; }

            /// <summary>
            /// 证件类型
            /// 
            /// </summary>>
            public string ZJLX { get; set; }

            /// <summary>
            /// 证件号码
            /// (null)
            /// </summary>
            public string ZJHM { get; set; }

            /// <summary>
            /// 客户号
            /// (null)
            /// </summary>>
            public string KHH { get; set; }

            /// <summary>
            /// 是否行内客户报错标志
            /// (null)
            /// </summary>>
            public string SFHNKHBCBZ { get; set; }//核心建议Fd126_5传6255时，8855接口查不到客户时不报错。
            string FLAG = TradeManagerHandle.TT_SYSCONFIG_GetConfigValueByConfigID("2ea3a7e6cfa04f89a335bf1e363295de");
            private string _sqgybh;
            /// <summary>                 
            /// 定义业务逻辑              
            /// </summary>                
            /// <returns></returns>  
            public override TFOutput Raise()
            {
                //拉取数据                                  
                var output = new CheckBlackList();
                MessageData.BitMapType = TellerSystem.Communication.Message.BitMap.Server;
                PageData.TradeAPIs.InitFd(MessageData);
                PageData.TradeAPIs.CommSend(MessageData);
                MessageData.Fd16 = "8854";
                MessageData.Fd71 = CXLX;
                MessageData.Fd30 = ZH;
                MessageData.Fd34 = ZHXH;
                MessageData.Fd70 = ZJLX;
                MessageData.Fd62 = ZJHM;
                MessageData.Fd28 = KHH;
                PageData.TradeAPIs.CallServer(MessageData);
                if (MessageData.Fd12 != "0000")
                {
                    output.IsFailed = true;
                    output.FailureCause = PageData.TradeAPIs.GetError(MessageData);
                    return output;
                }

                output.BZW = (MessageData.Fd73);
                string restrict = MessageData.Fd73;
                if (string.IsNullOrEmpty(restrict) || restrict.Length < 6)
                {
                    output.IsFailed = true;
                    output.FailureCause = "后台系统返回数据异常";
                    return output;
                }
                string tsacc = string.Empty;

                if ("1".Equals(restrict.Substring(0, 1)))
                {
                    tsacc += "非柜面限制";
                }
                if ("1".Equals(restrict.Substring(1, 1)))
                {
                    tsacc += "全渠道限制";
                }
                if ("1".Equals(restrict.Substring(2, 1)))
                {
                    tsacc += "只进不出限制";
                }
                string tsID = string.Empty;
                if ("1".Equals(restrict.Substring(3, 1)))
                {
                    tsID += "非柜面限制";
                }
                if ("1".Equals(restrict.Substring(4, 1)))
                {
                    tsID += "全渠道限制";
                }
                if ("1".Equals(restrict.Substring(5, 1)))
                {
                    tsID += "只进不出限制";
                }

                string info = (string.IsNullOrEmpty(tsacc) ? "" : "该账户做了") + tsacc + (string.IsNullOrEmpty(tsID) ? "" : "，该客户证件做了") + tsID + "请做灰名单维护交易！";
                output.info = info;
                //全渠道限制
                int allrestrict = "1".Equals(restrict.Substring(1, 1)) || "1".Equals(restrict.Substring(4, 1)) ? 1 : 0;
                output.allrestrict = allrestrict;
                //只进不出限制
                int onleinrestrict = "1".Equals(restrict.Substring(2, 1)) || "1".Equals(restrict.Substring(5, 1)) ? 1 : 0;
                output.onleinrestrict = onleinrestrict;
                // 只限制非柜面做提示即可
                if (allrestrict + onleinrestrict < 1 && ("1".Equals(restrict.Substring(0, 1)) || "1".Equals(restrict.Substring(3, 1))))
                {
                    PageData.TradeAPIs.ShowInfoMessageBox(info);
                }
                //具体查询查询客户信息  检查是否登记了客户号控制的黑白名单（且内部账户【9开头的】无需校验黑名单）
                if (ZH == null)
                {
                    PageData.TradeAPIs.InitFd(MessageData);
                    PageData.TradeAPIs.CommSend(MessageData);
                    MessageData.Fd16 = "8855";
                    MessageData.Fd28 = KHH;
                    MessageData.Fd30 = ZH;
                    MessageData.Fd116_4 = ZJLX;
                    MessageData.Fd116_5 = ZJHM;
                    if (!string.IsNullOrEmpty(SFHNKHBCBZ))
                        MessageData.Fd126_5 = SFHNKHBCBZ;
                    PageData.TradeAPIs.CallServer(MessageData);
                    if (MessageData.Fd12 != "0000")
                    {
                        this.TradeAPIs.ShowTip(PageData.TradeAPIs.GetError(MessageData));
                        this.TradeAPIs.SetFocus();
                        return output;
                    }
                    if (MessageData.Fd70.Substring(0, 1) == "1")//1黑名单  0 不是黑名单
                    {
                        if (FLAG == "V5" && LoginUserInfo.KinbrNo.Substring(0, 3) == "301")
                        {
                            this.TradeAPIs.ShowTip("该客户为黑名单客户!");
                            if (AuthAction(new Message() { }))
                            {
                                output.IsFailed = false;
                            }
                            else 
                            {
                                output.IsFailed = true;
                                output.FailureCause = "该客户为黑名单！";
                            }
                            
                        }
                        else
                        {
                            this.TradeAPIs.ShowTip("该客户为黑名单！");
                            if (!string.IsNullOrEmpty(SFHNKHBCBZ))
                            {
                                output.IsFailed = true;
                                output.FailureCause = "该客户为黑名单！";
                            }
                            this.TradeAPIs.SetFocus();
                        }         
                        return output;
                    }
                }
                else
                {
                    if (ZH.Substring(0, 1) != "9")
                    {
                        PageData.TradeAPIs.InitFd(MessageData);
                        PageData.TradeAPIs.CommSend(MessageData);
                        MessageData.Fd16 = "8855";
                        MessageData.Fd28 = KHH;
                        MessageData.Fd30 = ZH;
                        MessageData.Fd116_4 = ZJLX;
                        MessageData.Fd116_5 = ZJHM;

                        if (!string.IsNullOrEmpty(SFHNKHBCBZ))
                            MessageData.Fd126_5 = SFHNKHBCBZ;
                        PageData.TradeAPIs.CallServer(MessageData);
                        if (MessageData.Fd12 != "0000")
                        {
                            this.TradeAPIs.ShowTip(PageData.TradeAPIs.GetError(MessageData));
                            this.TradeAPIs.SetFocus();
                            return output;
                        }
                        if (MessageData.Fd70.Substring(0, 1) == "1")//1黑名单  0 不是黑名单
                        {
                            if (FLAG == "V5" && LoginUserInfo.KinbrNo.Substring(0, 3) == "301")
                            {
                                this.TradeAPIs.ShowTip("该客户为黑名单客户!");
                                if (AuthAction(new Message() { }))
                                {
                                    output.IsFailed = false;
                                }
                                else
                                {
                                    output.IsFailed = true;
                                    output.FailureCause = "该客户为黑名单！";
                                }
                            }
                            else
                            {
                                this.TradeAPIs.ShowTip("该客户为黑名单！");
                                if (!string.IsNullOrEmpty(SFHNKHBCBZ))
                                {
                                    output.IsFailed = true;
                                    output.FailureCause = "该客户为黑名单！";
                                }
                                this.TradeAPIs.SetFocus();
                            }                           
                            return output;
                        }
                    }
                }

                return output;
            }
            /// <summary>
            /// 授权方法
            /// </summary>
            /// <param name="msg"></param>
            /// <returns></returns>
            private bool AuthAction(Message msg)
            {
                // 组织授权检查要素
                TellerSystem.ServiceProxy.Ext.ServiceEntitys.PageData[] page = new TellerSystem.ServiceProxy.Ext.ServiceEntitys.PageData[1];
                page[0] = new TellerSystem.ServiceProxy.Ext.ServiceEntitys.PageData() { index = 1, datas = PageData.SaveTradePage((FrameworkElement)PageData.Content) };
                AuthConditionElement element = new AuthConditionElement();

                AuthManager c = new AuthManager();
                c.AsksuperResultMessage += new AuthManager.AsksuperResultHandle(AsksuperResultMessage);
                return c.DoAuth_ZP(PageData, element, msg, page, "0", "", "", "", "", "黑名单授权", "");
            }

            private void AsksuperResultMessage(AsksuperResultType result, TradeBusData busData)
            {
                _sqgybh = "";
                switch (result)
                {
                    case AsksuperResultType.Success:
                        this.TradeAPIs.ShowTip("授权已经通过。");
                        if (busData != null && busData.ContainsKey("AuthTeller"))
                        {
                            _sqgybh = busData["AuthTeller"];
                        }
                        break;
                    case AsksuperResultType.Failed:
                        this.TradeAPIs.ShowTip("授权没有通过。");
                        break;
                    case AsksuperResultType.Undo:
                        this.TradeAPIs.ShowTip("授权已撤销。");
                        break;
                    default:
                        break;
                }
            }
        }
        #endregion

    }
}
