using Library;
using MySystem.Service.Main1;
using System.Collections.Generic;
using MySystem.Service.KxsMain;
using System.Linq;
namespace MySystem.Service.Main1
{
    /// <summary>
    /// 直连商户进件表工具类
    /// </summary>
    public class MerchantAddInfoUtil
    {
        #region 进件管理-直连进件详情
        /// <summary>
        /// 进件管理-直连进件详情
        /// </summary>
        /// <param name="MerchantId">商户Id</param>
        /// <returns></returns>
        public static Dictionary<string, object> MerchantAddInfoDetail(int MerchantId)
        {
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            var query = MerchantAddInfoService.Query(MerchantId);
            Dictionary<string, string> Subjects = MerchantInfoUtil.GetSubjects();
            Dictionary<string, string> AccountTypes = MerchantInfoUtil.GetAccountTypes();
            List<SettleData> Settles = MerchantInfoUtil.GetSettles(query.SubjectType);
            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); //主体类型
            //主体类型名称
            if (Subjects.ContainsKey(function.CheckNull(query.SubjectType)))
            {
                Obj.Add("SubjectTypeName", Subjects[function.CheckNull(query.SubjectType)]);
            }
            else
            {
                Obj.Add("SubjectTypeName", "");
            }
            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("SalesScenesType", query.SalesScenesType); //经营场景类型
            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); //所属行业
            //所属行业名称
            SettleData item = Settles.FirstOrDefault(m => m.value == query.QualificationType);
            if (item != null)
            {
                Obj.Add("QualificationTypeName", item.ptext + "," + item.text);
            }
            else
            {
                Obj.Add("QualificationTypeName", "");
            }
            Obj.Add("Qualifications", query.Qualifications); //特殊资质图片
            Obj.Add("ActivitiesId", query.ActivitiesId); //优惠费率活动ID
            Obj.Add("ActivitiesRate", query.ActivitiesRate); //优惠费率活动值
            Obj.Add("BankAccountType", query.BankAccountType); //账户类型
            //账户类型名称
            if (AccountTypes.ContainsKey(function.CheckNull(query.BankAccountType)))
            {
                Obj.Add("BankAccountTypeName", AccountTypes[function.CheckNull(query.BankAccountType)]);
            }
            else
            {
                Obj.Add("BankAccountTypeName", "");
            }
            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("Status", query.Status); //状态
            Obj.Add("CreateDate", query.CreateDate); //创建时间
            Obj.Add("AlipayAccount", query.CreateMan); //支付宝账号

            return Obj;
        }
        #endregion



        #region 进件管理-直连提交进件
        /// <summary>
        /// 进件管理-直连提交进件
        /// </summary>
        /// <param name="MerchantId">商户Id</param>
        /// <returns></returns>
        public static string DirectSubmitDo(int MerchantId)
        {
            var merInfo = MerchantInfoService.Query(MerchantId);
            if (merInfo.Sort != 0 || merInfo.QueryCount != 2 || merInfo.Status != 2)
            {

                Dictionary<string, object> Fiel = new Dictionary<string, object>();
                Fiel.Add("Sort", 1);
                MerchantInfoService.Edit(Fiel, MerchantId, false);
                MerchantAddInfoService.Edit(Fiel, MerchantId, false);
                RedisDbconn.Instance.AddList("MerchantConfirmQueue", "{\"MerchantId\":\"" + MerchantId + "\"}");
            }
            else
            {
                return "提交失败，该商户为暂存状态或流程已结束";
            }
            return "success";
        }
        #endregion


        #region 商户管理-直连查询商户交易信息
        /// <summary>
        /// 商户管理-直连查询商户交易信息
        /// </summary>
        /// <param name="MerchantName">商户名称</param>
        /// <param name="Mobile">手机号码</param>
        /// <param name="MakerCode">所属创客</param>
        /// <param name="MatchNo">商户号</param>
        /// <param name="AliPID">支付宝PID</param>
        /// <param name="WeChatNo">微信商户号</param>
        /// <param name="SubjectType">主体类型(1 企业 2 个体)</param>
        /// <returns></returns>
        public static Dictionary<string, object> DirectQueryMerchantTradeInfoDo(string MerchantName, string Mobile, string MakerCode, string MatchNo, string AliPID, string WeChatNo, string SubjectType)
        {
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            string condition = "";
            if (!string.IsNullOrEmpty(MerchantName))
            {
                condition += " and CertMerchantName='" + MerchantName + "'";
            }
            if (!string.IsNullOrEmpty(Mobile))
            {
                condition += " and MobilePhone='" + Mobile + "'";
            }
            if (!string.IsNullOrEmpty(MakerCode))
            {
                var Ids = MerchantInfoUtil.QueryMerchantIdByMakerCode(MakerCode);
                condition += " and Id in (" + Ids + ")";
            }
            if (!string.IsNullOrEmpty(AliPID))
            {
                condition += " and AlipayPid='" + AliPID + "'";
            }
            if (!string.IsNullOrEmpty(WeChatNo))
            {
                condition += " and SubMchid='" + WeChatNo + "'";
            }
            if (!string.IsNullOrEmpty(SubjectType))
            {
                if (SubjectType == "1") SubjectType = "SUBJECT_TYPE_ENTERPRISE"; //企业公司
                if (SubjectType == "2") SubjectType = "SUBJECT_TYPE_INDIVIDUAL"; //个体工商户
                condition += " and SubjectType='" + SubjectType + "'";
            }
            var query = MerchantAddInfoService.Query(condition);
            Obj.Add("MerchantId", query.Id); //商户Id
            Obj.Add("MerchantName", query.CertMerchantName); //商户名称
            Obj.Add("Mobile", query.MobilePhone); //手机号码
            Obj.Add("MakerCode", UsersService.Query(MerchantInfoService.Query(query.Id).UserId).MakerCode); //所属创客
            var subjectType = 0;
            if (query.SubjectType == "SUBJECT_TYPE_ENTERPRISE") subjectType = 1; //企业公司
            if (query.SubjectType == "SUBJECT_TYPE_INDIVIDUAL") subjectType = 2; //个体工商户
            Obj.Add("SubjectType", subjectType); //主体类型(1 企业 2 个体)
            Obj.Add("AliPID", query.AlipayPid); //支付宝PID
            Obj.Add("WeChatNo", query.SubMchid); //微信商户号
            Obj.Add("OrderCount", ConsumerOrdersService.Count(" and Status>0 and MerchantId=" + query.Id + "")); //订单总数
            Obj.Add("TradeAmount", ConsumerOrdersService.Sum(" and Status>0 and MerchantId=" + query.Id + "", "PayMoney")); //营业总金额
            Obj.Add("InComeAmount", ConsumerOrdersService.Sum(" and Status>0 and MerchantId=" + query.Id + "", "MerchantActualAmount")); //实收总金额
            return Obj;
        }
        #endregion

    }
}