using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authorization;
using MySystem.Service.Main;
using System.Web;
using MySystem.Models.Main;
using LitJson;
using Library;
using System.Text.RegularExpressions;

namespace MySystem.Areas.Api.Controllers.v1
{
    [Area("Api")]
    [Route("Api/v1/[controller]/[action]")]
    public class MerchantAddInfoController : BaseController
    {
        public MerchantAddInfoController(IHttpContextAccessor accessor) : base(accessor)
        {
        }



        #region 创客-首页-新增商户
        // [Authorize]
        public JsonResult Add(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = AddDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        public AppResultJson AddDo(string value)
        {
            function.WriteLog(value, "创客-首页-新增商户");
            JsonData data = JsonMapper.ToObject(value);
            string LicenseCopy = data.getItem("LicenseCopy").ToString(); //- 营业执照
            string CertMerchantName = data.getItem("CertMerchantName").ToString(); //- 商户名称
            string LicenseNumber = data.getItem("LicenseNumber").ToString(); //- 商户信用代码
            string SubjectType = data.getItem("SubjectType").ToString(); //- 主体类型
            string MerchantShortname = data.getItem("MerchantShortname").ToString(); //- 用户名称简写
            string ServicePhone = data.getItem("ServicePhone").ToString(); //- 商户电话
            string SalesScenesType = data.getItem("SalesScenesType").ToString(); //- 所属行业
            string SalesScenesTypeName = data.getItem("SalesScenesTypeName").ToString(); //- 所属行业
            string MchtNo = data.getItem("MchtNo").ToString(); // 收款商户号
            string BizStoreName = data.getItem("BizStoreName").ToString(); //- 门店名称
            string BizAddressCode = data.getItem("BizAddressCode").ToString(); //- 门店区域
            string BizStoreAddress = data.getItem("BizStoreAddress").ToString(); //- 门店地址
            string IdCardCopy = data.getItem("IdCardCopy").ToString(); //- 身份证正面
            string IdCardNational = data.getItem("IdCardNational").ToString(); //- 身份证反面
            string CertLegalPerson = data.getItem("CertLegalPerson").ToString(); //- 真实姓名
            string IdCardNumber = data.getItem("IdCardNumber").ToString(); //- 身份证号码
            string CardPeriodBegin = data.getItem("CardPeriodBegin").ToString(); //- 身份证有效期开始
            string CardPeriodEnd = data.getItem("CardPeriodEnd").ToString(); //- 身份证有效期结束
            string IdCardAddress = data.getItem("IdCardAddress").ToString();//身份证居住地址
            string PeriodBegin = data.getItem("PeriodBegin").ToString(); //- 营业执照有效期开始
            string PeriodEnd = data.getItem("PeriodEnd").ToString(); //- 营业执照有效期结束
            if (string.IsNullOrEmpty(CardPeriodBegin))
            {
                CardPeriodBegin = "1900-01-01";
            }
            if (string.IsNullOrEmpty(CardPeriodEnd))
            {
                CardPeriodEnd = "1900-01-01";
            }
            if (string.IsNullOrEmpty(PeriodBegin))
            {
                PeriodBegin = "1900-01-01";
            }
            if (string.IsNullOrEmpty(PeriodEnd))
            {
                PeriodEnd = "1900-01-01";
            }
            string MobilePhone = data.getItem("MobilePhone").ToString(); //- 手机号码
            string AccountName = data.getItem("AccountName").ToString(); //- 开户名称
            string ChannelKind = data.getItem("ChannelKind").ToString(); // 商户类型
            string BankAccountType = data.getItem("BankAccountType").ToString(); //- 账户类型
            string AccountNumber = data.getItem("AccountNumber").ToString(); //- 银行卡号
            string AccountBank = data.getItem("AccountBank").ToString(); //- 开户行
            string BankCard = data.getItem("BankCard").ToString(); // 银行卡照片
            string OpenAccountLicenses = data.getItem("OpenAccountLicenses").ToString(); // 开户许可证
            string BankName = data.getItem("BankName").ToString(); //- 开户行全称
            string BankAddressCode = data.getItem("BankAddressCode").ToString(); //- 开户地区
            string PersonWithIdCard = data.getItem("PersonWithIdCard").ToString(); //- 手持身份证照片（小微）
            string StoreEntrancePic = data.getItem("StoreEntrancePic").ToString(); // 门头照片组
            string IndoorPic = data.getItem("IndoorPic").ToString(); // 环境照片组
            string CheckstandImg = data.getItem("CheckstandImg").ToString(); // 收银照片组
            string Extra1 = data.getItem("Extra1").ToString(); // 补充资料照片组
            string IndustryLicense1 = data.getItem("IndustryLicense1").ToString(); //- 特殊资质照片组
            string ResserveMobile = data.getItem("ResserveMobile").ToString(); //银行预留手机号 channelkind为000001时必填

            string OpenBankAccout = data.getItem("OpenBankAccout").ToString(); // 开户许可证-银行卡号
            string OpenBankName = data.getItem("OpenBankName").ToString(); // 开户许可证-开户行全称
            string OpenBankAccName = data.getItem("OpenBankAccName").ToString(); // 开户许可证-开户名
            string OpenBankRegionCode = data.getItem("OpenBankRegionCode").ToString(); // 开户许可证-开户支行区域

            int PlaceType = int.Parse(function.CheckInt(data.getItem("PlaceType").ToString())); // 小微商户经营场所类型(0 固定场所 1 流动动摊贩)

            //添加银联进件品牌选择
            int BrandId = 0;
            if (!string.IsNullOrEmpty(data.getItem("BrandId").ToString()))
            {
                BrandId = int.Parse(function.CheckInt(data.getItem("BrandId").ToString())); // 品牌(0 银联 1 好哒)
            }


            // string ContactName = data["ContactName"].ToString(); //管理员姓名
            // string ContactIdNumber = data["ContactIdNumber"].ToString(); //管理员身份证件号码
            // string MobilePhone = data["MobilePhone"].ToString(); //联系手机
            // string ContactEmail = data["ContactEmail"].ToString(); //联系邮箱
            // string SubjectType = data["SubjectType"].ToString(); //主体类型 0-企业法人营业执照 1-个体工商户营业执照
            // string LicenseCopy = data["LicenseCopy"].ToString(); //营业执照照片
            // string LicenseNumber = data["LicenseNumber"].ToString(); //统一社会信用代码
            // string CertMerchantName = data["CertMerchantName"].ToString(); //商户名称
            // string CertLegalPerson = data["CertLegalPerson"].ToString(); //法人姓名
            // string IdCardCopy = data["IdCardCopy"].ToString(); //身份证人像面照片
            // string IdCardNational = data["IdCardNational"].ToString(); //身份证国徽面照片
            // string IdCardName = data["IdCardName"].ToString(); //身份证姓名
            // string IdCardNumber = data["IdCardNumber"].ToString(); //身份证号码
            // string IdCardAddress = data["IdCardAddress"].ToString(); //身份证号码
            // string CardPeriodBeginString = data["CardPeriodBegin"].ToString();
            // if (string.IsNullOrEmpty(CardPeriodBeginString))
            // {
            //     CardPeriodBeginString = "1900-01-01";
            // }
            // DateTime CardPeriodBegin = DateTime.Parse(CardPeriodBeginString); //身份证有效期开始时间
            // string CardPeriodEndString = data["CardPeriodEnd"].ToString();
            // if (string.IsNullOrEmpty(CardPeriodEndString))
            // {
            //     CardPeriodEndString = "1900-01-01";
            // }
            // DateTime CardPeriodEnd = DateTime.Parse(CardPeriodEndString); //身份证有效期结束时间
            // string ServicePhone = data["ServicePhone"].ToString(); //客服电话
            // string BizStoreName = data["BizStoreName"].ToString(); //门店名称
            // string BizAddressCode = data["BizAddressCode"].ToString(); //门店省市编码
            // string BizStoreAddress = data["BizStoreAddress"].ToString(); //门店地址
            // string StoreEntrancePic = data["StoreEntrancePic"].ToString(); //门店门头照片
            // string IndoorPic = data["IndoorPic"].ToString(); //店内环境照片
            // string QualificationType = data["QualificationType"].ToString(); //所属行业
            // string SettlementId = data["SettlementId"].ToString(); //入驻结算规则ID
            // string AlipayMccCode = "";
            // if (!string.IsNullOrEmpty(SettlementId))
            // {
            //     AlipayMccCode = SettlementId.Substring(SettlementId.IndexOf("_") + 1);
            //     SettlementId = SettlementId.Substring(0, SettlementId.IndexOf("_"));
            // }
            // string AlipayAccount = data["AlipayAccount"].ToString(); //支付宝账号
            // string BankAccountType = data["BankAccountType"].ToString(); //账户类型 1 对私 0 对公
            // string AccountName = data["AccountName"].ToString(); //开户名称
            // string AccountBank = data["AccountBank"].ToString(); //开户银行
            // string BankAddressCode = data["BankAddressCode"].ToString(); //开户银行省市编码
            // string MerchantShortname = data["MerchantShortname"].ToString();
            // string BankName = data["BankName"].ToString(); //开户银行全称
            // string AccountNumber = data["AccountNumber"].ToString(); //银行账号
            // string Qualifications = data["Qualifications"].ToString(); //特殊资质图

            // //new add parameters:
            // string PeriodBegin = data["PeriodBegin"].ToString();//营业执照有效开始时间
            // string PeriodEnd = data["PeriodEnd"].ToString();//营业执照有效结束时间
            // string ChannelKind = data["ChannelKind"].ToString(); //商户类型 有执照填000002无执照填000001
            // string BusinessId = data["BusinessId"].ToString(); //经营范围 参考经营范围表
            // string OpenAccountLicenses = data["OpenAccountLicenses"].ToString(); //开户许可证(对公凭证)
            // // string RentAgreement = data["RentAgreement"].ToString(); //租赁协议
            // // string OperatorMcht = data["OperatorMcht"].ToString(); //盒伙人商户合影
            // // string AgentProtocol = data["AgentProtocol"].ToString(); //委托结算协议
            // // string SignaturePicture = data["SignaturePicture"].ToString(); //商户签名图片
            // // string Support = data["Support"].ToString(); //辅助照片
            // string CheckstandImg = data["CheckstandImg"].ToString(); //收银台
            // // string ProductImg = data["ProductImg"].ToString(); //商品照片
            // // string Agreement = data["Agreement"].ToString(); //合同签名
            // // string TaxImage = data["TaxImage"].ToString(); //税务登记证
            // // string OrganizationCopy = data["OrganizationCopy"].ToString(); //组织机构证照片
            // string IndustryLicense1 = data["IndustryLicense1"].ToString(); //行业许可证
            // // string FaceRecognition = data["FaceRecognition"].ToString(); //正面人像图片
            // string Extra1 = data["Extra1"].ToString(); //补充资料
            // string BankCard = data["BankCard"].ToString(); //银行卡照片
            // string PersonWithIdCard = data["PersonWithIdCard"].ToString(); //手持身份证
            // // string TakeCreditCard = data["TakeCreditCard"].ToString(); //拍卡
            // string MchtNo = data["MchtNo"].ToString(); //收款商户号



            int Finish = int.Parse(function.CheckInt(data.getItem("Finish").ToString()));
            int UserId = int.Parse(function.CheckInt(data.getItem("UserId").ToString()));
            int MerchantAddInfoId = int.Parse(function.CheckInt(data.getItem("Id").ToString()));
            string BankBranchId = "";
            if (Finish == 1)
            {
                var openBank = OpenBankTableService.Query(" BankName='" + BankName + "'");
                if (openBank == null)
                {
                    return new AppResultJson() { Status = "-1", Info = "开户银行全称不正确" };
                }
                BankBranchId = openBank.BankCode;
            }
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            Dictionary<string, object> Fields = new Dictionary<string, object>();
            if (MerchantAddInfoId == 0)
            {
                if (SubjectType == "SUBJECT_TYPE_SMALL")
                {
                    // CertMerchantName += function.MD5_16(Guid.NewGuid().ToString("N"));
                    var check = MerchantAddInfoService.Query(" CertMerchantName like '" + CertMerchantName + "%' and Status<1 and QueryCount<1 and BrandId=" + BrandId);
                    if (check.Id > 0)
                    {
                        return new AppResultJson() { Status = "-1", Info = "该商户已暂存或进件，勿重复进件！", Data = Obj };
                    }
                    CertMerchantName += function.get_Random(00000000, 99999999);
                }
                else
                {
                    var query = MerchantAddInfoService.Query(" CertMerchantName='" + CertMerchantName + "' and BrandId=" + BrandId);
                    if (query.Id == 0)
                    {
                        Dictionary<string, object> Fiel = new Dictionary<string, object>();
                        Fiel.Add("CertMerchantName", CertMerchantName);
                        Fields.Add("MchtNo", MchtNo); // 收款商户号
                        MerchantAddInfoId = (int)MerchantAddInfoService.Add(Fiel, false).Data;

                        Fiel = new Dictionary<string, object>();
                        Fiel.Add("Id", MerchantAddInfoId);
                        Fiel.Add("UserId", UserId);
                        Fiel.Add("Name", CertMerchantName);
                        MerchantInfoService.Add(Fiel, false);
                    }
                    else
                    {
                        return new AppResultJson() { Status = "-1", Info = "该商户已进件", Data = Obj };
                    }
                }
            }
            else
            {
                if (SubjectType == "SUBJECT_TYPE_SMALL")
                {
                    CertMerchantName = MerchantInfoService.Query(MerchantAddInfoId).Name;
                }
                else
                {
                    Fields.Add("CertMerchantName", CertMerchantName);
                }
            }
            // var ActivitiesId = "20191030111cff5b5e";
            // var ActivitiesRate = "0.38";
            // MerchantAddInfoUtil.EditMerchantAddInfo(query.Id, ContactName, ContactIdNumber, MobilePhone, ContactEmail, SubjectType, LicenseCopy, LicenseNumber, CertLegalPerson, CertMerchantName, CertLegalPerson, ServicePhone, BizStoreName, BizAddressCode, BizStoreAddress, StoreEntrancePic, IndoorPic, QualificationType, SettlementId, BankAccountType, AccountName, AccountBank, BankAddressCode, BankName, BankBranchId, AccountNumber, IdCardCopy, IdCardNational, IdCardName, IdCardNumber, IdCardAddress, CardPeriodBegin, CardPeriodEnd, MerchantShortname, Qualifications, ActivitiesId, ActivitiesRate, AlipayAccount, AlipayMccCode, ChannelKind, SalesScenesType, OpenAccountLicenses, RentAgreement, OperatorMcht, AgentProtocol, SignaturePicture, Support, CheckstandImg, ProductImg, Agreement, TaxImage, OrganizationCopy, IndustryLicense1, FaceRecognition, Extra1, BankCard, PersonWithIdCard, TakeCreditCard, MchtNo);

            Fields.Add("LicenseCopy", LicenseCopy); //- 营业执照
            if (!Fields.ContainsKey("CertMerchantName")) Fields.Add("CertMerchantName", CertMerchantName); //- 商户名称
            Fields.Add("LicenseNumber", LicenseNumber); //- 商户信用代码
            Fields.Add("SubjectType", SubjectType); //- 主体类型
            var LicenceType = "";
            int BindStatus = 0;
            if (SubjectType == "SUBJECT_TYPE_ENTERPRISE")
            {
                LicenceType = "0";
                BindStatus = 2;
            } //- 企业公司
            if (SubjectType == "SUBJECT_TYPE_INDIVIDUAL")
            {
                LicenceType = "1";
                BindStatus = 1;
            } //- 个体工商户
            if (SubjectType == "SUBJECT_TYPE_SMALL") BindStatus = 3; //- 小微
            Fields.Add("LicenceType", LicenceType); //- 主体类型
            Fields.Add("MerchantShortname", MerchantShortname); //- 用户名称简写
            Fields.Add("ServicePhone", ServicePhone); //- 商户电话
            Fields.Add("QualificationType", SalesScenesType); //- 所属行业
            Fields.Add("SalesScenesType", SalesScenesTypeName); //- 所属行业
            Fields.Add("BusinessId", SalesScenesType); //- 所属行业Id
            Fields.Add("BizStoreName", BizStoreName); //- 门店名称
            Fields.Add("BizAddressCode", BizAddressCode); //- 门店区域
            Fields.Add("AreaNo", HaoDaAreaCodeService.Query("CityName like '%" + BizAddressCode + "%'").CityCode); //- 经营地区区域码
            Fields.Add("BizStoreAddress", BizStoreAddress); //- 门店地址
            Fields.Add("IdCardCopy", IdCardCopy); //- 身份证正面
            Fields.Add("IdCardNational", IdCardNational); //- 身份证反面
            Fields.Add("CertLegalPerson", CertLegalPerson); //- 真实姓名
            Fields.Add("IdCardNumber", IdCardNumber); //- 身份证号码
            Fields.Add("CardPeriodBegin", DateTime.Parse(CardPeriodBegin)); //- 身份证有效期开始
            Fields.Add("CardPeriodEnd", DateTime.Parse(CardPeriodEnd)); //- 身份证有效期结束
            Fields.Add("PeriodBegin", DateTime.Parse(PeriodBegin)); //- 营业执照有效期开始
            Fields.Add("PeriodEnd", DateTime.Parse(PeriodEnd)); //- 营业执照有效期结束
            Fields.Add("IdCardAddress", IdCardAddress); //身份证居住地址            
            Fields.Add("MobilePhone", MobilePhone); //- 手机号码
            Fields.Add("AccountName", AccountName); //- 开户名称
            Fields.Add("ChannelKind", ChannelKind); // 商户类型
            Fields.Add("BankAccountType", BankAccountType); //- 账户类型
            var AccountType = "";
            if (BankAccountType == "BANK_ACCOUNT_TYPE_CORPORATE") AccountType = "0"; //- 对公
            if (BankAccountType == "BANK_ACCOUNT_TYPE_PERSONAL") AccountType = "1"; //- 对私
            Fields.Add("AccountType", AccountType); //- 账户类型
            Fields.Add("AccountNumber", AccountNumber); //- 银行卡号
            Fields.Add("AccountBank", AccountBank); //- 开户行
            Fields.Add("BankCard", BankCard); // 银行卡照片
            Fields.Add("openAccountLicenses", OpenAccountLicenses); // 开户许可证
            Fields.Add("BankName", BankName); //- 开户行全称

            //TODO：待修改
            Fields.Add("OpenBankAccout", OpenBankAccout); //- 开户许可证-银行卡号 企业商户必填(个体工商户对私不填)
            Fields.Add("OpenBankAccName", OpenBankAccName); //- 开户许可证-开户名 企业商户必填(个体工商户对私不填)
            //TODO：待修改
            var bankInfo = OpenBankAllService.Query(" BankChild='" + BankName + "'");
            var kbankInfo = OpenBankAllService.Query(" BankChild='" + OpenBankName + "'");
            Fields.Add("OpenUnionNo", kbankInfo.BankChildNo); //- 开户许可证-开户支行 企业商户必填(个体工商户对私不填)
            //TODO：待修改
            Fields.Add("OpenCollectBankCode", kbankInfo.BankParentNo); //- 开户许可证-银行代码(总行行号) 企业商户必填(个体工商户对私不填)
            //TODO：待修改
            Fields.Add("OpenBankRegionCode", HaoDaAreaCodeService.Query("CityName like '%" + OpenBankRegionCode + "%'").CityCode); //- 开户银行支行区域码 参考区域码表（联系对接人提供）

            Fields.Add("ZBankNo", bankInfo.BankChildNo); //- 开户银行支行号 参考支行号表（联系对接人提供）
            Fields.Add("BankAddressCode", BankAddressCode); //- 开户地区
            Fields.Add("ZbankRegionCode", HaoDaAreaCodeService.Query("CityName like '%" + BankAddressCode + "%'").CityCode); //- 开户地区
            Fields.Add("PersonWithIdCard", PersonWithIdCard); //- 手持身份证照片（小微）
            Fields.Add("StoreEntrancePic", StoreEntrancePic); // 门头照片组
            Fields.Add("IndoorPic", IndoorPic); // 环境照片组
            Fields.Add("checkstandImg", CheckstandImg); // 收银照片组
            Fields.Add("extra1", Extra1); // 补充资料照片组
            Fields.Add("industryLicense1", IndustryLicense1); //- 特殊资质照片组
            Fields.Add("ResserveMobile", ResserveMobile); //银行预留手机号
            Fields.Add("PlaceType", PlaceType); // 小微商户经营场所类型(0 固定场所 1 流动动摊贩)
            Fields.Add("BrandId", BrandId); // 品牌(0 银联 1 好哒)
            MerchantAddInfoId = (int)MerchantInfoUtil.EditMerchantInfo(MerchantAddInfoId, UserId, CertMerchantName, MobilePhone, BizAddressCode, BizStoreAddress, BindStatus, BrandId).Data;
            MerchantAddInfoUtil.EditMerchantAddInfo(Fields, MerchantAddInfoId);
            if (Finish == 1)
            {
                Dictionary<string, object> Fiel = new Dictionary<string, object>();
                Fiel.Add("Sort", 1);
                MerchantInfoService.Edit(Fiel, MerchantAddInfoId, false);
                // 如果提交完整资料，则发送到BsServer执行进件操作
                RedisDbconn.Instance.AddList("MerchantConfirmHdQueue", "{\"MerchantId\":\"" + MerchantAddInfoId + "\"}");
            }
            else
            {
                Dictionary<string, object> Fiel = new Dictionary<string, object>();
                Fiel.Add("Sort", 0);
                MerchantInfoService.Edit(Fiel, MerchantAddInfoId, false);
            }
            Obj.Add("Id", MerchantAddInfoId); //Id
            return new AppResultJson() { Status = "1", Info = "", Data = Obj };
        }
        #endregion


        #region 创客-首页-商户进件详情
        // [Authorize]
        public JsonResult Detail(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Obj = DetailDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
        }
        public Dictionary<string, object> DetailDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
            return MerchantAddInfoUtil.MerchantAddInfoDetail(Id);
        }
        #endregion


        #region 创客-首页-经营范围
        // [Authorize]
        public JsonResult BusinessScope(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = BusinessScopeDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        public List<Dictionary<string, object>> BusinessScopeDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            List<RelationData> relationData = new List<RelationData>();
            List<Dictionary<string, object>> listf = BusinessScopeTypeService.List(relationData, "", 1, 99999);
            foreach (Dictionary<string, object> dicf in listf)
            {
                Dictionary<string, object> item = new Dictionary<string, object>();
                item.Add("value", dicf["Id"].ToString());
                item.Add("text", dicf["KindName"].ToString());
                List<Dictionary<string, object>> sublist = new List<Dictionary<string, object>>();
                List<Dictionary<string, object>> lists = BusinessScopeService.List(relationData, " and KindId='" + dicf["Id"].ToString() + "'", 1, 99999);
                foreach (Dictionary<string, object> dics in lists)
                {
                    Dictionary<string, object> subitem = new Dictionary<string, object>();
                    subitem.Add("value", dics["Id"].ToString());
                    subitem.Add("text", dics["Name"].ToString());
                    sublist.Add(subitem);
                }
                item.Add("children", sublist);
                dataList.Add(item);
            }
            return dataList;
        }
        #endregion


        #region 创客-首页-进件选项接口
        // [Authorize]
        public JsonResult Options(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = OptionsDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        public List<Dictionary<string, object>> OptionsDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            string Kind = data["Kind"].ToString(); //分类
            int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
            int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            // if (Kind == "2")
            // {
            //     dataList = GetPersonSettles();
            // }
            // else if (Kind == "21")
            // {
            //     dataList = GetBusinessSettles();
            // }
            // if (!string.IsNullOrEmpty(Kind))
            if (Kind == "2" || Kind == "21")
            {
                Dictionary<string, object> item = new Dictionary<string, object>();
                var first = maindb.BusinessScopeType.OrderBy(m => m.Id).ToList();
                foreach (var itemf in first)
                {
                    item.Add("Id", itemf.Id);
                    item.Add("Name", itemf.KindName);
                    var second = maindb.BusinessScope.Where(m => m.KindId == itemf.Id).OrderBy(m => m.Id).ToList();
                    List<Dictionary<string, object>> sublist = new List<Dictionary<string, object>>();
                    foreach (var items in second)
                    {
                        Dictionary<string, object> subitem = new Dictionary<string, object>();
                        subitem.Add("Id", items.Id);
                        subitem.Add("Name", items.Name);
                        sublist.Add(subitem);
                    }
                    item.Add("Children", sublist);
                    dataList.Add(item);
                }
            }
            else
            {
                Dictionary<string, string> query = new Dictionary<string, string>();
                if (Kind == "1")
                {
                    query = MerchantAddInfoUtil.GetSubjects();
                }
                else if (Kind == "3")
                {
                    query = MerchantAddInfoUtil.GetAccountTypes();
                }
                else if (Kind == "4")
                {
                    query = GetBanks();
                }
                foreach (string key in query.Keys)
                {
                    Dictionary<string, object> curData = new Dictionary<string, object>();
                    curData.Add("text", query[key]); //文本
                    curData.Add("value", key); //值
                    dataList.Add(curData);
                }
            }
            return dataList;
        }
        public List<SettleData> GetSettles(string Kind)
        {
            if (Kind == "SUBJECT_TYPE_INDIVIDUAL")
            {
                return GetPersonSettleItems();
            }
            return GetBusinessSettleItems();
        }

        public List<SettleData> GetPersonSettleItems()
        {
            List<SettleData> dic = new List<SettleData>();
            dic.Add(new SettleData() { ptext = "食品生鲜", text = "肉、禽、蛋及水产品等", value = "719", code = "A0002_B0202" });
            dic.Add(new SettleData() { ptext = "健身运动服务", text = "健身房", value = "719", code = "A0004_B0064" });
            dic.Add(new SettleData() { ptext = "健身运动服务", text = "体育场馆", value = "719", code = "A0004_B0068" });
            dic.Add(new SettleData() { ptext = "居民生活服务", text = "公共交通", value = "719", code = "A0006_B0088" });
            dic.Add(new SettleData() { ptext = "景区/酒店", text = "旅行住宿", value = "719", code = "A0010_B0157" });
            dic.Add(new SettleData() { ptext = "居民生活服务", text = "生活服务", value = "719", code = "A0003_B0116" });
            dic.Add(new SettleData() { ptext = "零售", text = "百货商城", value = "719", code = "A0002_B0017" });
            dic.Add(new SettleData() { ptext = "服饰鞋包", text = "服饰鞋包", value = "719", code = "A0002_B0026" });
            dic.Add(new SettleData() { ptext = "零售", text = "家居家纺建材", value = "719", code = "A0002_B0216" });
            dic.Add(new SettleData() { ptext = "零售", text = "母婴玩具", value = "719", code = "A0002_B0032" });
            dic.Add(new SettleData() { ptext = "零售", text = "数码家电", value = "719", code = "A0003_B0051" });
            dic.Add(new SettleData() { ptext = "零售", text = "图书音像", value = "719", code = "A0002_B0031" });
            dic.Add(new SettleData() { ptext = "居民生活服务", text = "文化玩乐宠物", value = "719", code = "A0004_B0070" });
            dic.Add(new SettleData() { ptext = "咨询/娱乐票务", text = "娱乐票务", value = "719", code = "A0004_B0065" });
            dic.Add(new SettleData() { ptext = "零售", text = "办公用品", value = "719", code = "A0002_B0203" });
            dic.Add(new SettleData() { ptext = "零售", text = "工业产品", value = "719", code = "A0002_B0042" });
            dic.Add(new SettleData() { ptext = "咨询/娱乐票务", text = "专业咨询", value = "719", code = "A0008_B0139" });
            dic.Add(new SettleData() { ptext = "其他", text = "其他类别", value = "716", code = "A0002_B0043" });


            // dic.Add(new SettleData() { ptext = "餐饮", text = "餐饮", code = "C_C05_5814", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "食品生鲜", text = "酒类", code = "C_C05_5921", value = "719", special = 1});
            // dic.Add(new SettleData() { ptext = "食品生鲜", text = "肉、禽、蛋及水产品等", code = "C_C05_5422", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "私立/民营医院/诊所", text = "医疗服务", code = "P_P01_8011", value = "719", special = 1});
            // dic.Add(new SettleData() { ptext = "保健器械/医疗器械/非处方药品", text = "药品、药品经营者（批发商）", code = "B_B04_5122", value = "719", special = 1});
            // dic.Add(new SettleData() { ptext = "保健器械/医疗器械/非处方药品", text = "医疗器械", code = "B_B04_5047", value = "719", special = 1});
            // dic.Add(new SettleData() { ptext = "游艺厅/KTV/网吧", text = "游艺厅/KTV", code = "D_D01_7999", value = "719", special = 1});
            // dic.Add(new SettleData() { ptext = "游艺厅/KTV/网吧", text = "网吧", code = "D_D01_7999", value = "719", special = 1});
            // dic.Add(new SettleData() { ptext = "宠物医院", text = "宠物及宠物用品", code = "C_C10_5995", value = "719", special = 1});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "公共交通", code = "A_A01_4121", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "旅行住宿", code = "A_A04_7011", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "公共仓储、集装整理", code = "A_A02_4225", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "商业服务", code = "S_S05_7338", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "汽车租赁和服务", code = "S_S10_7512", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "生活服务", code = "S_S08_7295", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "百货商城", code = "C_C04_5411", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "服饰鞋包", code = "C_C06_5699", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "家居家纺建材", code = "C_C03_5211", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "母婴玩具", code = "C_C02_5641", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "数码家电", code = "C_C07_5732", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "图书音像", code = "C_C09_5942", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "文化玩乐宠物", code = "C_C10_5995", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "彩票", code = "D_D03_7995", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "娱乐票务", code = "D_D01_7999", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "法律咨询和律师事务所", code = "P_P04_8111", value = "719", special = 1});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "办公用品", code = "B_B01_5943", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "工业产品", code = "B_B02_5065", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "汽车和运输工具", code = "B_B03_5599", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/其他", text = "专业咨询", code = "P_P04_8999", value = "719", special = 0});
            // dic.Add(new SettleData() { ptext = "话费通讯", text = "话费充值与缴费", code = "D_D04_4815", value = "720", special = 1});
            // dic.Add(new SettleData() { ptext = "加油", text = "加油站、服务站", code = "B_B03_5541", value = "721", special = 1});
            return dic;
        }
        public List<Dictionary<string, object>> GetPersonSettles()
        {
            List<SettleData> dic = GetPersonSettleItems();

            int i = 0;
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            foreach (string sub in dic.Select(m => m.ptext).Distinct().ToList())
            {
                i += 1;
                Dictionary<string, object> bigDic = new Dictionary<string, object>();
                bigDic.Add("value", i.ToString());
                bigDic.Add("text", sub);
                List<Dictionary<string, object>> children = new List<Dictionary<string, object>>();
                foreach (SettleData ssub in dic.Where(m => m.ptext == sub).ToList())
                {
                    Dictionary<string, object> smallDic = new Dictionary<string, object>();
                    smallDic.Add("value", ssub.value + "_" + ssub.code);
                    smallDic.Add("text", ssub.text);
                    smallDic.Add("special", ssub.special);
                    children.Add(smallDic);
                }
                bigDic.Add("children", children);
                list.Add(bigDic);
            }

            return list;
        }
        public List<SettleData> GetBusinessSettleItems()
        {
            List<SettleData> dic = new List<SettleData>();
            dic.Add(new SettleData() { ptext = "餐饮", text = "餐饮", value = "716", code = "A0001_B0001" });
            dic.Add(new SettleData() { ptext = "食品生鲜", text = "肉、禽、蛋及水产品等", value = "716", code = "A0002_B0202" });
            dic.Add(new SettleData() { ptext = "健身运动服务", text = "健身房", value = "716", code = "A0004_B0064" });
            dic.Add(new SettleData() { ptext = "健身运动服务", text = "体育场馆", value = "716", code = "A0004_B0068" });
            dic.Add(new SettleData() { ptext = "城市公共交通", text = "公共交通", value = "716", code = "A0006_B0088" });
            dic.Add(new SettleData() { ptext = "景区/酒店", text = "旅行住宿", value = "716", code = "A0010_B0157" });
            dic.Add(new SettleData() { ptext = "居民生活服务", text = "生活服务", value = "716", code = "A0003_B0116" });
            dic.Add(new SettleData() { ptext = "零售", text = "百货商城", value = "716", code = "A0002_B0017" });
            dic.Add(new SettleData() { ptext = "服饰鞋包", text = "服饰鞋包", value = "716", code = "A0002_B0026" });
            dic.Add(new SettleData() { ptext = "零售", text = "家居家纺建材", value = "716", code = "A0002_B0216" });
            dic.Add(new SettleData() { ptext = "零售", text = "母婴玩具", value = "716", code = "A0002_B0032" });
            dic.Add(new SettleData() { ptext = "零售", text = "数码家电", value = "716", code = "A0003_B0051" });
            dic.Add(new SettleData() { ptext = "零售", text = "图书音像", value = "716", code = "A0002_B0031" });
            dic.Add(new SettleData() { ptext = "居民生活服务", text = "文化玩乐宠物", value = "716", code = "A0004_B0070" });
            dic.Add(new SettleData() { ptext = "咨询/娱乐票务", text = "娱乐票务", value = "716", code = "A0004_B0065" });
            dic.Add(new SettleData() { ptext = "零售", text = "办公用品", value = "716", code = "A0002_B0203" });
            dic.Add(new SettleData() { ptext = "零售", text = "工业产品", value = "716", code = "A0002_B0042" });
            dic.Add(new SettleData() { ptext = "婚介平台/就业信息平台/其他信息服务平台", text = "婚介服务", value = "720", code = "A0003_B0053" });
            dic.Add(new SettleData() { ptext = "零售", text = "其它类别", value = "716", code = "A0002_B0043" });


            // dic.Add(new SettleData() { ptext = "餐饮", text = "餐饮", code = "C_C05_5814", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "电商平台", text = "电商平台", code = "C_C05_5814", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "食品生鲜", text = "酒类", code = "C_C05_5921", value = "716", special = 1});
            // dic.Add(new SettleData() { ptext = "食品生鲜", text = "肉、禽、蛋及水产品等", code = "C_C05_5422", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "电信运营商/宽带收费", text = "电信通讯", code = "D_D04_4814", value = "716", special = 1});
            // dic.Add(new SettleData() { ptext = "私立/民营医院/诊所", text = "医疗服务", code = "P_P01_8011", value = "716", special = 1});
            // dic.Add(new SettleData() { ptext = "保健器械/医疗器械/非处方药品", text = "医疗器械", code = "B_B04_5047", value = "716", special = 1});
            // dic.Add(new SettleData() { ptext = "保健器械/医疗器械/非处方药品", text = "药品、药品经营者（批发商）", code = "B_B04_5122", value = "716", special = 1});
            // dic.Add(new SettleData() { ptext = "游艺厅/KTV/网吧", text = "游艺厅/KTV", code = "D_D01_7999", value = "716", special = 1});
            // dic.Add(new SettleData() { ptext = "游艺厅/KTV/网吧", text = "网吧", code = "D_D01_7999", value = "716", special = 1});
            // dic.Add(new SettleData() { ptext = "宠物医院", text = "宠物及宠物用品", code = "C_C10_5995", value = "716", special = 1});
            // dic.Add(new SettleData() { ptext = "旅行社", text = "旅行社和旅游服务", code = "A_A04_4722", value = "716", special = 1});
            // dic.Add(new SettleData() { ptext = "宗教组织", text = "宗教物品", code = "C_C10_5973", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "宗教组织", text = "其他宗教服务", code = "P_P04_8999", value = "716", special = 1});
            // dic.Add(new SettleData() { ptext = "文物经营/文物复制品销售", text = "文物经营/文物复制品销售", code = "C_C10_5971", value = "716", special = 1});
            // dic.Add(new SettleData() { ptext = "拍卖典当", text = "典当行", code = "S_S04_5933", value = "716", special = 1});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "公共交通", code = "A_A01_4121", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "旅行住宿", code = "A_A04_7011", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "公共仓储、集装整理", code = "A_A02_4225", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "商业服务", code = "S_S05_7338", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "汽车租赁和服务", code = "S_S10_7512", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "生活服务", code = "S_S08_7295", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "百货商城", code = "C_C04_5411", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "服饰鞋包", code = "C_C06_5699", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "家居家纺建材", code = "C_C03_5211", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "母婴玩具", code = "C_C02_5641", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "数码家电", code = "C_C07_5732", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "图书音像", code = "C_C09_5942", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "文化玩乐宠物", code = "C_C10_5995", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "彩票", code = "D_D03_7995", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "娱乐票务", code = "D_D01_7999", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "法律咨询和律师事务所", code = "P_P04_8111", value = "716", special = 1});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "办公用品", code = "B_B01_5943", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "工业产品", code = "B_B02_5065", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "汽车和运输工具", code = "B_B03_5599", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "零售批发/生活娱乐/网上商城/其他", text = "专业咨询", code = "P_P04_8999", value = "716", special = 0});
            // dic.Add(new SettleData() { ptext = "话费通讯", text = "话费充值与缴费", code = "D_D04_4815", value = "728", special = 1});
            // dic.Add(new SettleData() { ptext = "婚介平台/就业信息平台/其他", text = "婚介服务", code = "S_S08_7273", value = "728", special = 0});
            // dic.Add(new SettleData() { ptext = "门户论坛/网络广告及推广/软件开发/其他互联网服务", text = "计算机服务/其他", code = "D_D05_7372", value = "711", special = 0});
            // dic.Add(new SettleData() { ptext = "快递", text = "快递服务", code = "A_A02_4215", value = "717", special = 1});
            // dic.Add(new SettleData() { ptext = "加油", text = "加油站、服务站", code = "B_B03_5541", value = "717", special = 1});
            // dic.Add(new SettleData() { ptext = "公共事业（水电煤气）", text = "公共事业（电、气、水）", code = "S_S06_4900", value = "730", special = 1});
            // dic.Add(new SettleData() { ptext = "公共事业（水电煤气）", text = "公共事业-电力缴费", code = "S_S06_4901", value = "730", special = 1});
            // dic.Add(new SettleData() { ptext = "公共事业（水电煤气）", text = "公共事业-煤气缴费", code = "S_S06_4902", value = "730", special = 1});
            // dic.Add(new SettleData() { ptext = "公共事业（水电煤气）", text = "公共事业-自来水缴费", code = "S_S06_4903", value = "730", special = 1});
            return dic;
        }
        public List<Dictionary<string, object>> GetBusinessSettles()
        {
            List<SettleData> dic = GetBusinessSettleItems();

            int i = 0;
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            foreach (string sub in dic.Select(m => m.ptext).Distinct().ToList())
            {
                i += 1;
                Dictionary<string, object> bigDic = new Dictionary<string, object>();
                bigDic.Add("value", i.ToString());
                bigDic.Add("text", sub);
                List<Dictionary<string, object>> children = new List<Dictionary<string, object>>();
                foreach (SettleData ssub in dic.Where(m => m.ptext == sub).ToList())
                {
                    Dictionary<string, object> smallDic = new Dictionary<string, object>();
                    smallDic.Add("value", ssub.value + "_" + ssub.code);
                    smallDic.Add("text", ssub.text);
                    smallDic.Add("special", ssub.special);
                    children.Add(smallDic);
                }
                bigDic.Add("children", children);
                list.Add(bigDic);
            }

            return list;
        }

        public Dictionary<string, string> GetBanks()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("工商银行", "工商银行");
            dic.Add("农业银行", "农业银行");
            dic.Add("邮储银行", "邮储银行");
            dic.Add("建设银行", "建设银行");
            dic.Add("招商银行", "招商银行");
            dic.Add("中国银行", "中国银行");
            dic.Add("交通银行", "交通银行");
            dic.Add("浦发银行", "浦发银行");
            dic.Add("广发银行", "广发银行");
            dic.Add("民生银行", "民生银行");
            dic.Add("平安银行", "平安银行");
            dic.Add("光大银行", "光大银行");
            dic.Add("兴业银行", "兴业银行");
            dic.Add("中信银行", "中信银行");
            dic.Add("上海银行", "上海银行");
            dic.Add("其他银行", "其他银行");
            return dic;
        }
        #endregion







        #region 检查签名是否合法，合法返回1，不合法返回提示信息

        /// <summary>
        /// 检查签名是否合法，合法返回1，不合法返回提示信息
        /// </summary>
        /// <param name="value">请求的参数(json字符串)</param>
        /// <param name="signField">要签名的字段</param>
        /// <returns></returns>
        private string CheckSign(string value, string[] signField)
        {
            JsonData json = JsonMapper.ToObject(value);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            for (int i = 0; i < signField.Length; i++)
            {
                dic.Add(signField[i], json[signField[i]].ToString());
            }
            string sign = json["sign"].ToString(); //客户端签名字符串
            return new Sign().sign(dic, sign);
        }

        #endregion

    }
}
