using MySystem.Models.Main;
using Library;
using System;
using MySystem.Service.Main;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;

namespace MySystem
{
    /// <summary>
    /// 商户进件资料工具类
    /// </summary>
    public class MerchantAddInfoUtil
    {
        #region 修改商户进件资料记录
        /// <summary>
        /// 修改商户进件资料记录
        /// </summary>
        /// <param name="Id">商户资料Id</param>
        /// <param name="ContactName">管理员姓名</param>
        /// <param name="ContactIdNumber">管理员身份证件号码</param>
        /// <param name="MobilePhone">联系手机</param>
        /// <param name="ContactEmail">联系邮箱</param>
        /// <param name="SubjectType">主体类型</param>
        /// <param name="LicenseCopy">营业执照照片</param>
        /// <param name="LicenseNumber">统一社会信用代码</param>
        /// <param name="CertLegalPerson">">体户经营者/法人姓名</param>
        /// <param name="CertMerchantName">商户名称</param>
        /// <param name="CertLegalPerson">法人姓名</param>
        /// <param name="ServicePhone">客服电话</param>
        /// <param name="BizStoreName">门店名称</param>
        /// <param name="BizAddressCode">门店省市编码</param>
        /// <param name="BizStoreAddress">门店地址</param>
        /// <param name="StoreEntrancePic">门店门头照片</param>
        /// <param name="IndoorPic">店内环境照片</param>
        /// <param name="QualificationType">所属行业</param>
        /// <param name="SettlementId">入驻结算规则ID</param>
        /// <param name="BankAccountType">账户类型</param>
        /// <param name="AccountName">开户名称</param>
        /// <param name="AccountBank">开户银行</param>
        /// <param name="BankAddressCode">开户银行省市编码</param>
        /// <param name="BankName">开户银行全称</param>
        /// <param name="BankBranchId"></param>
        /// <param name="AccountNumber">银行账号</param>
        /// <param name="IdCardCopy">身份证人像面照片</param>
        /// <param name="IdCardNational">身份证国徽面照片</param>
        /// <param name="IdCardName">身份证姓名</param>
        /// <param name="IdCardNumber">身份证号码</param>
        /// <param name="IdCardAddress">身份证居住地址</param>
        /// <param name="CardPeriodBegin">身份证有效期开始时间</param>
        /// <param name="CardPeriodEnd">身份证有效期结束时间</param>
        /// <param name="MerchantShortname"></param>
        /// <param name="Qualifications">特殊资质图片</param>
        /// <param name="ActivitiesId"></param>
        /// <param name="ActivitiesRate"></param>
        /// <param name="AlipayAccount"></param>
        /// <param name="AlipayMccCode"></param>
        /// <param name="PeriodBegin">商户类型 有执照填000002无执照填000001</param>
        /// <param name="PeriodEnd">商户类型 有执照填000002无执照填000001</param>
        /// <param name="ChannelKind">商户类型 有执照填000002无执照填000001</param>
        /// <param name="BusinessId">经营范围 参考经营范围表</param>
        /// <param name="OpenAccountLicenses">开户许可证</param>
        /// <param name="RentAgreement">租赁协议</param>
        /// <param name="OperatorMcht">盒伙人商户合影</param>
        /// <param name="AgentProtocol">委托结算协议</param>
        /// <param name="SignaturePicture">商户签名图片</param>
        /// <param name="Support">辅助照片</param>
        /// <param name="CheckstandImg">收银台</param>
        /// <param name="ProductImg">商品照片</param>
        /// <param name="Agreement">合同签名</param>
        /// <param name="TaxImage">税务登记证</param>
        /// <param name="OrganizationCopy">组织机构证照片</param>
        /// <param name="IndustryLicense1">行业许可证</param>
        /// <param name="FaceRecognition">正面人像图片</param>
        /// <param name="Extra1">补充资料</param>
        /// <param name="BankCard">银行卡照片</param>
        /// <param name="PersonWithIdCard">手持身份证</param>
        /// <param name="TakeCreditCard">拍卡</param>
        /// <param name="MchtNo">收款商户号</param>
        /// <returns></returns>
        // public static AppResultJson EditMerchantAddInfo(int Id, string ContactName, string ContactIdNumber, string MobilePhone, string ContactEmail, string SubjectType, string LicenseCopy, string LicenseNumber, string LegalPerson, string CertMerchantName, string CertLegalPerson, string ServicePhone, string BizStoreName, string BizAddressCode, string BizStoreAddress, string StoreEntrancePic, string IndoorPic, string QualificationType, string SettlementId, string BankAccountType, string AccountName, string AccountBank, string BankAddressCode, string BankName, string BankBranchId, string AccountNumber, string IdCardCopy, string IdCardNational, string IdCardName, string IdCardNumber, string IdCardAddress, DateTime? CardPeriodBegin, DateTime? CardPeriodEnd, string MerchantShortname, string Qualifications, string ActivitiesId, string ActivitiesRate, string AlipayAccount, string AlipayMccCode, string PeriodBegin,string PeriodEnd,string ChannelKind, string SalesScenesType, string OpenAccountLicenses, string RentAgreement, string OperatorMcht, string AgentProtocol, string SignaturePicture, string Support, string CheckstandImg, string ProductImg, string Agreement, string TaxImage, string OrganizationCopy, string IndustryLicense1, string FaceRecognition, string Extra1, string BankCard, string PersonWithIdCard, string TakeCreditCard, string MchtNo)

        public static AppResultJson EditMerchantAddInfo(Dictionary<string, object> Fields, int Id)
        {
            // Dictionary<string, object> Fields = new Dictionary<string, object>();
            // Fields.Add("ContactName", ContactName); //管理员姓名
            // Fields.Add("ContactIdNumber", ContactIdNumber); //管理员身份证件号码
            // Fields.Add("MobilePhone", MobilePhone); //联系手机
            // Fields.Add("ContactEmail", ContactEmail); //联系邮箱
            // Fields.Add("SubjectType", SubjectType); //主体类型
            // Fields.Add("LicenseCopy", LicenseCopy); //营业执照照片
            // Fields.Add("LicenseNumber", LicenseNumber); //统一社会信用代码
            // Fields.Add("LegalPerson", CertLegalPerson); //);体户经营者/法人姓名
            // Fields.Add("CertMerchantName", CertMerchantName); //商户名称
            // Fields.Add("CertLegalPerson", CertLegalPerson); //法人姓名
            // Fields.Add("ServicePhone", ServicePhone); //客服电话
            // Fields.Add("BizStoreName", BizStoreName); //门店名称
            // Fields.Add("BizAddressCode", BizAddressCode); //门店省市编码
            // Fields.Add("BizStoreAddress", BizStoreAddress); //门店地址
            // Fields.Add("StoreEntrancePic", StoreEntrancePic); //门店门头照片
            // Fields.Add("IndoorPic", IndoorPic); //店内环境照片
            // Fields.Add("QualificationType", QualificationType); //所属行业
            // Fields.Add("SettlementId", SettlementId); //入驻结算规则ID
            // Fields.Add("BankAccountType", BankAccountType); //账户类型
            // Fields.Add("AccountName", AccountName); //开户名称
            // Fields.Add("AccountBank", AccountBank); //开户银行
            // Fields.Add("BankAddressCode", BankAddressCode); //开户银行省市编码
            // Fields.Add("BankName", BankName); //开户银行全称
            // Fields.Add("BankBranchId", BankBranchId);
            // Fields.Add("AccountNumber", AccountNumber); //银行账号
            // Fields.Add("IdCardCopy", IdCardCopy); //身份证人像面照片
            // Fields.Add("IdCardNational", IdCardNational); //身份证国徽面照片
            // Fields.Add("IdCardName", IdCardName); //身份证姓名
            // Fields.Add("IdCardNumber", IdCardNumber); //身份证号码
            // Fields.Add("IdCardAddress", IdCardAddress); //身份证居住地址
            // Fields.Add("CardPeriodBegin", CardPeriodBegin); //身份证有效期开始时间
            // Fields.Add("CardPeriodEnd", CardPeriodEnd); //身份证有效期结束时间
            // Fields.Add("MerchantShortname", MerchantShortname);
            // Fields.Add("Qualifications", Qualifications); //特殊资质图片
            // Fields.Add("ActivitiesId", ActivitiesId);
            // Fields.Add("ActivitiesRate", ActivitiesRate);
            // Fields.Add("CreateMan", AlipayAccount);
            // Fields.Add("UpdateMan", AlipayMccCode);
            // Fields.Add("PeriodBegin", PeriodBegin); //营业执照有效开始时间
            // Fields.Add("PeriodEnd", PeriodEnd); //营业执照有效结束时间
            // Fields.Add("ChannelKind", ChannelKind); //商户类型 有执照填000002无执照填000001
            // // Fields.Add("SalesScenesType", SalesScenesType); //经营范围 参考经营范围表
            // Fields.Add("BusinessId", BusinessId); //经营范围 参考经营范围表
            // Fields.Add("OpenAccountLicenses", OpenAccountLicenses); //开户许可证(对公凭证)
            // // Fields.Add("RentAgreement", RentAgreement); //租赁协议
            // // Fields.Add("OperatorMcht", OperatorMcht); //盒伙人商户合影
            // // Fields.Add("AgentProtocol", AgentProtocol); //委托结算协议
            // // Fields.Add("SignaturePicture", SignaturePicture); //商户签名图片
            // // Fields.Add("Support", Support); //辅助照片
            // Fields.Add("CheckstandImg", CheckstandImg); //收银台
            // // Fields.Add("ProductImg", ProductImg); //商品照片
            // // Fields.Add("Agreement", Agreement); //合同签名
            // // Fields.Add("TaxImage", TaxImage); //税务登记证
            // // Fields.Add("OrganizationCopy", OrganizationCopy); //组织机构证照片
            // Fields.Add("IndustryLicense1", IndustryLicense1); //行业许可证
            // // Fields.Add("FaceRecognition", FaceRecognition); //正面人像图片
            // Fields.Add("Extra1", Extra1); //补充资料
            // Fields.Add("BankCard", BankCard); //银行卡照片
            // Fields.Add("PersonWithIdCard", PersonWithIdCard); //手持身份证
            // // Fields.Add("TakeCreditCard", TakeCreditCard); //拍卡
            // Fields.Add("MchtNo", MchtNo); //收款商户号
            AppResultJson info = new AppResultJson();
            if (Id == 0)
            {
                info = MerchantAddInfoService.Add(Fields, false);
            }
            else
            {
                if (MerchantAddInfoService.Exist(Id))
                {
                    MerchantAddInfo addinfo = MerchantAddInfoService.Query(Id);
                    if (addinfo.HdStatus == 0)
                    {
                        Fields.Add("Status", 0);
                        Fields.Add("WeChatRemark", null);
                        Fields.Add("AlipayRemark", null);
                    }
                    info = MerchantAddInfoService.Edit(Fields, Id, false);
                }
                else
                {
                    Fields.Add("Id", Id);
                    info = MerchantAddInfoService.Add(Fields, false);
                }
            }
            return info;
        }
        #endregion

        /// <summary>
        /// 商户进件详情
        /// </summary>
        /// <param name="query"></param>
        /// <param name="Subjects"></param>
        /// <param name="AccountTypes"></param>
        /// <param name="Settles"></param>
        /// <returns></returns>
        public static Dictionary<string, object> MerchantAddInfoDetail(int Id)
        {
            var query = MerchantAddInfoService.Query(Id);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            Obj.Add("BrandId", query.BrandId); //品牌(0 银联 1 好哒)
            Obj.Add("Kind", 2); //通道(1 直连 2 银联)
            Obj.Add("ContactName", query.ContactName); //管理员姓名
            Obj.Add("ContactIdNumber", query.ContactIdNumber); //管理员身份证件号码
            Obj.Add("OpenId", query.OpenId); //管理员微信openid
            Obj.Add("MobilePhone", query.MobilePhone); //联系手机
            Obj.Add("ContactEmail", query.ContactEmail); //联系邮箱
            Obj.Add("SubjectType", query.SubjectType); //主体类型
            Obj.Add("SubjectTypeName", GetDicValue(GetSubjects(), query.SubjectType));
            Obj.Add("LicenseCopy", query.LicenseCopy); //营业执照照片
            Obj.Add("LicenseNumber", query.LicenseNumber); //统一社会信用代码
            Obj.Add("LegalPerson", query.LegalPerson); //个体户经营者/法人姓名
            Obj.Add("CertCopy", query.CertCopy); //登记证书照片
            Obj.Add("CertType", query.CertType); //登记证书类型
            Obj.Add("CertNumber", query.CertNumber); //证书号
            Obj.Add("CertMerchantName", query.CertMerchantName); //商户名称
            Obj.Add("CompanyAddress", query.CompanyAddress); //注册地址
            Obj.Add("CertLegalPerson", query.CertLegalPerson); //法人姓名
            Obj.Add("PeriodBegin", query.PeriodBegin == null ? "" : query.PeriodBegin.Value.ToString("yyyy-MM-dd HH:mm:ss")); //有效期限开始日期
            Obj.Add("PeriodEnd", query.PeriodEnd == null ? "" : query.PeriodEnd.Value.ToString("yyyy-MM-dd HH:mm:ss")); //有效期限结束日期
            Obj.Add("OrganizationCopy", query.OrganizationCopy); //组织机构代码证照片
            Obj.Add("OrganizationCode", query.OrganizationCode); //组织机构代码
            Obj.Add("OrgPeriodBegin", query.OrgPeriodBegin == null ? "" : query.OrgPeriodBegin.Value.ToString("yyyy-MM-dd HH:mm:ss")); //组织机构代码证有效期开始日期
            Obj.Add("OrgPeriodEnd", query.OrgPeriodEnd == null ? "" : query.OrgPeriodEnd.Value.ToString("yyyy-MM-dd HH:mm:ss")); //组织机构代码证有效期结束日期
            Obj.Add("CertificateLetterCopy", query.CertificateLetterCopy); //单位证明函照片
            Obj.Add("IdDocType", query.IdDocType); //证件类型
            Obj.Add("Owner", query.Owner); //经营者/法人是否为受益人
            Obj.Add("UboInfoIdType", query.UboInfoIdType); //最终受益人证件类型
            Obj.Add("UboInfoIdCardCopy", query.UboInfoIdCardCopy); //最终受益人身份证人像面照片
            Obj.Add("UboInfoIdCardNational", query.UboInfoIdCardNational); //身份证国徽面照片
            Obj.Add("UboInfoIdDocCopy", query.UboInfoIdDocCopy); //最终受益人证件照片
            Obj.Add("UboInfoName", query.UboInfoName); //受益人姓名
            Obj.Add("UboInfoIdNumber", query.UboInfoIdNumber); //最终受益人证件号码
            Obj.Add("UboInfoIdPeriodBegin", query.UboInfoIdPeriodBegin == null ? "" : query.UboInfoIdPeriodBegin.Value.ToString("yyyy-MM-dd HH:mm:ss")); //证件有效期开始时间
            Obj.Add("UboInfoIdPeriodEnd", query.UboInfoIdPeriodEnd == null ? "" : query.UboInfoIdPeriodEnd.Value.ToString("yyyy-MM-dd HH:mm:ss")); //证件有效期结束时间
            Obj.Add("MerchantShortname", query.MerchantShortname); //商户简称
            Obj.Add("ServicePhone", query.ServicePhone); //客服电话
            Obj.Add("BizStoreName", query.BizStoreName); //门店名称
            Obj.Add("BizAddressCode", query.BizAddressCode); //门店省市编码
            Obj.Add("BizStoreAddress", query.BizStoreAddress); //门店地址
            Obj.Add("StoreEntrancePic", query.StoreEntrancePic); //门店门头照片
            Obj.Add("IndoorPic", query.IndoorPic); //店内环境照片
            Obj.Add("BizSubAppid", query.BizSubAppid); //线下场所对应的商家APPID
            Obj.Add("MpAppid", query.MpAppid); //服务商公众号APPID
            Obj.Add("MpSubAppid", query.MpSubAppid); //商家公众号APPID
            Obj.Add("MpPics", query.MpPics); //公众号页面截图
            Obj.Add("MiniProgramAppid", query.MiniProgramAppid); //服务商小程序APPID
            Obj.Add("MiniProgramSubAppid", query.MiniProgramSubAppid); //商家小程序APPID
            Obj.Add("MiniProgramPics", query.MiniProgramPics); //小程序截图
            Obj.Add("AppAppid", query.AppAppid); //服务商应用APPID
            Obj.Add("AppSubAppid", query.AppSubAppid); //商家应用APPID
            Obj.Add("AppPics", query.AppPics); //APP截图
            Obj.Add("WebDomain", query.WebDomain); //互联网网站域名
            Obj.Add("WebAuthorisation", query.WebAuthorisation); //网站授权函
            Obj.Add("WebAppId", query.WebAppId); //互联网网站对应的商家APPID
            Obj.Add("SubCorpId", query.SubCorpId); //商家企业微信CorpID
            Obj.Add("WeworkPics", query.WeworkPics); //企业微信页面截图
            Obj.Add("SettlementId", query.SettlementId + "_" + query.UpdateMan); //入驻结算规则ID
            Obj.Add("QualificationType", query.QualificationType); //所属行业
            Obj.Add("Qualifications", query.Qualifications); //特殊资质图片
            Obj.Add("ActivitiesId", query.ActivitiesId); //优惠费率活动ID
            Obj.Add("ActivitiesRate", query.ActivitiesRate); //优惠费率活动值
            Obj.Add("BankAccountType", query.BankAccountType); //账户类型
            Obj.Add("BankAccountTypeName", GetDicValue(GetAccountTypes(), query.BankAccountType));
            Obj.Add("AccountName", query.AccountName); //开户名称
            Obj.Add("AccountBank", query.AccountBank); //开户银行
            Obj.Add("BankAddressCode", query.BankAddressCode); //开户银行省市编码
            Obj.Add("BankBranchId", query.BankBranchId); //开户银行联行号
            Obj.Add("BankName", query.BankName); //开户银行全称
            Obj.Add("AccountNumber", query.AccountNumber); //银行账号
            Obj.Add("ApplymentId", query.ApplymentId); //微信支付申请单号
            Obj.Add("SubMchid", query.SubMchid); //特约商户号
            Obj.Add("SignUrl", query.SignUrl); //超级管理员签约链接
            Obj.Add("ApplymentState", query.ApplymentState); //申请单状态
            Obj.Add("AuditDetail", query.AuditDetail); //驳回原因详情
            Obj.Add("IdCardCopy", query.IdCardCopy); //身份证人像面照片
            Obj.Add("IdCardNational", query.IdCardNational); //身份证国徽面照片
            Obj.Add("IdCardName", query.IdCardName); //身份证姓名
            Obj.Add("IdCardNumber", query.IdCardNumber); //身份证号码
            Obj.Add("IdCardAddress", query.IdCardAddress);
            Obj.Add("CardPeriodBegin", query.CardPeriodBegin == null ? "" : query.CardPeriodBegin.Value.ToString("yyyy-MM-dd HH:mm:ss")); //身份证有效期开始时间
            Obj.Add("CardPeriodEnd", query.CardPeriodEnd == null ? "" : query.CardPeriodEnd.Value.ToString("yyyy-MM-dd HH:mm:ss")); //身份证有效期结束时间
            Obj.Add("Id", query.Id); //Id
            Obj.Add("Status", query.Status); //状态
            Obj.Add("CreateDate", query.CreateDate); //创建时间
            Obj.Add("AlipayAccount", query.CreateMan); //支付宝账号

            Obj.Add("MchtNo", query.MchtNo); //好哒商户号
            Obj.Add("BankCard", query.BankCard); //收款人银行卡照片
            Obj.Add("PersonWithIdCard", query.PersonWithIdCard); //法人手持身份证正面
            Obj.Add("IndustryLicense1", query.IndustryLicense1); //行业许可证
            Obj.Add("Extra1", query.Extra1); //补充资料
            Obj.Add("CheckstandImg", query.CheckstandImg); //收银台
            Obj.Add("SalesScenesType", query.QualificationType); //经营场景类型
            // BusinessScope businessScope = BusinessScopeService.Query(int.Parse(function.CheckInt(query.SalesScenesType)));
            // BusinessScopeType businessScopeType = BusinessScopeTypeService.Query(businessScope.KindId);
            Obj.Add("SalesScenesTypeName", query.SalesScenesType);
            Obj.Add("OpenAccountLicenses", query.OpenAccountLicenses); //开户许可证
            Obj.Add("ResserveMobile", query.ResserveMobile); //银行预留手机号
            Obj.Add("ChannelKind", query.ChannelKind); // 商户类型

            Obj.Add("OpenBankAccout", query.OpenBankAccout); // 开户许可证-银行卡号
            Obj.Add("OpenBankAccName", query.OpenBankAccName); // 开户许可证-开户名
            Obj.Add("OpenBankName", OpenBankAllService.Query(" BankChildNo='" + query.OpenUnionNo + "'").BankChild); // 开户许可证-开户行全称
            var str = "";
            str = HaoDaAreaCodeService.Query("CityCode='" + query.OpenBankRegionCode + "'").CityName;
            string OpenBankRegionCode = "";
            if (!string.IsNullOrEmpty(str))
            {
                OpenBankRegionCode = str.Substring(str.IndexOf(',') + 1, str.Length - str.IndexOf(',') - 1);
            }
            Obj.Add("OpenBankRegionCode", OpenBankRegionCode); // 开户银行支行区域码
            Obj.Add("PlaceType", query.PlaceType); // 小微商户经营场所类型(0 固定场所 1 流动动摊贩)

            return Obj;
        }

        public static Dictionary<string, string> GetSubjects()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("SUBJECT_TYPE_INDIVIDUAL", "个体工商户");
            dic.Add("SUBJECT_TYPE_ENTERPRISE", "企业公司");
            dic.Add("SUBJECT_TYPE_SMALL", "小微商户");
            // dic.Add("SUBJECT_TYPE_INSTITUTIONS", "党政、机关及事业单位");
            // dic.Add("SUBJECT_TYPE_OTHERS", "其他组织");
            return dic;
        }

        public static Dictionary<string, string> GetAccountTypes()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("BANK_ACCOUNT_TYPE_CORPORATE", "企业对公银行账号");
            dic.Add("BANK_ACCOUNT_TYPE_PERSONAL", "法人个人银行卡");
            return dic;
        }

        public static string GetDicValue(Dictionary<string, string> dic, string key)
        {
            key = function.CheckNull(key);
            if (dic.ContainsKey(key))
            {
                return dic[key];
            }
            return "";
        }

    }
}