using MySystem.Models.Main;
using Library;
using MySystem.Service.Main;
using System.Collections.Generic;
using System;
using LitJson;
using MySystem.Areas.Api.Controllers;

namespace MySystem
{
    /// <summary>
    /// 我的工具类
    /// </summary>
    public class MySelfUtil
    {
        #region 到账记录列表
        /// <summary>
        /// 到账记录列表
        /// </summary>
        /// <param name="condition">查询条件</param>
        /// <param name="pageSize">条数</param>
        /// <param name="pageNum">页数</param>
        /// <returns></returns>
        public static List<Dictionary<string, object>> CardInComeRecordList(int MerchantId, string TradeMonth, string pageSize, string pageNum)
        {
            DateTime start = new DateTime(int.Parse(TradeMonth.Substring(0, 4)), int.Parse(TradeMonth.Substring(TradeMonth.Length - 2, 2)), 1);
            DateTime end = start.AddMonths(1).AddDays(-1);
            var merchantInfo = MerchantAddInfoService.Query(MerchantId);
            JsonData Info = JsonMapper.ToObject(HaoDaHelper.Instance.CardInComeRecord(merchantInfo.OutMchtNo, start.ToString("yyyy-MM-dd"), end.ToString("yyyy-MM-dd"), merchantInfo.MchtNo, "", "", pageNum, pageSize));
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            if (Info["code"].ToString() == "0")
            {
                var list = Info["data"]["list"];
                for (int i = 0; i < list.Count; i++)
                {
                    Dictionary<string, object> curData = new Dictionary<string, object>();
                    var item = list[i];
                    // if (item["settleAccountName"].ToString() != "四川省润观米科技有限公司")
                    // {
                    curData.Add("Id", item["id"].ToString()); //订单Id
                    curData.Add("SettleStatus", item["settleStatus"].ToString()); //结算状态(0: 入账成功、1: 入账失败、2: 结算超时、3: 入账受理成功、4: 已提交结算请求、5:已拆分批次、6: 初始化批次信息、7:正在提交结算请求)
                    curData.Add("SettleDate", item["settleDate"].ToString().Substring(item["settleDate"].ToString().Length - 5, 5)); //结算日期
                    curData.Add("SettleAmount", (decimal.Parse(item["settleAmount"].ToString()) / 100).ToString("f2")); //结算金额
                    curData.Add("TradeMchtName", item["tradeMchtName"].ToString()); //收单商户名称
                    curData.Add("BankName", item["bankName"].ToString()); //开户行名称
                    curData.Add("SettleAccountName", item["settleAccountName"].ToString()); //开户名(检测到该字段为四川省润观米科技有限公司时需要吧该条到账记录屏蔽)
                    dataList.Add(curData);
                    // }
                }
            }
            return dataList;
        }
        #endregion


        #region 到账记录详情
        /// <summary>
        /// 到账记录详情
        /// </summary>
        /// <param name="condition">查询条件</param>
        /// <param name="pageSize">条数</param>
        /// <param name="pageNum">页数</param>
        /// <returns></returns>
        public static Dictionary<string, object> CardInComeRecordDetail(int MerchantId, string OrderId)
        {
            var merchantInfo = MerchantAddInfoService.Query(MerchantId);
            JsonData Info = JsonMapper.ToObject(HaoDaHelper.Instance.CardInComeDetail(merchantInfo.OutMchtNo, OrderId));
            Dictionary<string, object> obj = new Dictionary<string, object>();
            if (Info["code"].ToString() == "0")
            {
                obj.Add("Id", Info["data"]["id"].ToString()); //订单Id
                obj.Add("SettleStatus", Info["data"]["settleStatus"].ToString()); //结算状态(0: 入账成功、1: 入账失败、2: 结算超时、3: 入账受理成功、4: 已提交结算请求、5:已拆分批次、6: 初始化批次信息、7:正在提交结算请求)
                obj.Add("SettleType", Info["data"]["settleType"].ToString()); //结算周期
                obj.Add("SettleDate", Info["data"]["settleDate"].ToString()); //结算日期
                obj.Add("ClearTime", Info["data"]["clearTime"].ToString()); //出款受理时间
                obj.Add("RemitTime", Info["data"]["remitTime"].ToString()); //出款成功时间
                obj.Add("SettleAmount", (decimal.Parse(Info["data"]["settleAmount"].ToString()) / 100).ToString("f2")); //结算金额
                obj.Add("TradeMchtName", Info["data"]["tradeMchtName"].ToString()); //收单商户名称
                obj.Add("BankName", Info["data"]["bankName"].ToString() + "(" + Info["data"]["cardNo"].ToString().Substring(Info["data"]["cardNo"].ToString().Length - 4, 4) + ")"); //开户行名称
                obj.Add("FailureReason", Info["data"]["remark"].ToString()); //出款失败备注
            }
            return obj;
        }
        #endregion

        #region 我的-设置-修改登录手机号(新手机验证)
        /// <summary>
        /// 我的-设置-修改登录手机号(新手机验证)
        /// </summary>
        /// <param name="LoginMobile">原手机号</param>
        /// <param name="Mobile">新手机号</param>
        /// <returns></returns>
        public static string ChangeLoginMobile(string LoginMobile, string Mobile)
        {
            var info = MerchantLoginInfoService.Query(" and LoginMobile=" + LoginMobile + "");
            if (info.Id > 0)
            {
                Dictionary<string, object> obj = new Dictionary<string, object>();
                obj.Add("LoginMobile", Mobile);
                MerchantLoginInfoService.Edit(obj, info.Id);
            }
            return "修改成功";
        }
        #endregion


        #region 商户统计数据（个人中心主界面）
        /// <summary>
        /// 商户统计数据（个人中心主界面）
        /// </summary>
        /// <param name="MerchantId">商户Id</param>
        /// <returns></returns>
        public static Dictionary<string, object> StatData(int MerchantId)
        {
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            var query = MerchantInfoService.Query(MerchantId);
            Obj.Add("Name", query.Name); //名称
            Obj.Add("TotalAmount", query.TotalAmount.ToString("f2")); //营收总金额
            Obj.Add("TotalActual", query.TotalActual.ToString("f2")); //实收总金额
            var yesterday = DateTime.Now.AddDays(-1).ToString("yyyyMMdd");
            Obj.Add("YesterDayActual", decimal.Parse(function.CheckNum(UserTradeDbconn.Instance.GetTrade(MerchantId, yesterday, "InFactAmount")["InFactAmount"].ToString())).ToString("f2")); //昨日收入
            Obj.Add("SettleAmount", 0.00); //待结算金额
            Obj.Add("TotalOrder", query.TotalConsumeCount); //累计订单
            Obj.Add("TotalCustomer", query.TotalCustomer); //累计客户
            Obj.Add("Logo", BaseController.DefaultPic(query.Logo)); //Logo图片
            int ConsumeCount = 0;
            decimal WeChatTotal = 0;
            decimal AlipayTotal = 0;
            for (int i = 0; i < 7; i++)
            {
                string Date = DateTime.Now.AddDays(-i).ToString("yyyyMMdd");
                WeChatTotal += decimal.Parse(MerchantAmountSummayService.Sum("TotalActual", " and MerchantId=" + MerchantId + " and PayMode=2 and TradeDate='" + Date + "'")["TotalActual"]);
                AlipayTotal += decimal.Parse(MerchantAmountSummayService.Sum("TotalActual", " and MerchantId=" + MerchantId + " and PayMode=1 and TradeDate='" + Date + "'")["TotalActual"]);
            }
            Obj.Add("SevenDayConsumer", ConsumeCount); //近7日新增会员
            Obj.Add("WeChatTotal", WeChatTotal.ToString("f2")); //微信实收
            Obj.Add("AlipayTotal", AlipayTotal.ToString("f2")); //支付宝实收
            Obj.Add("CreateDate", query.CreateDate == null ? "" : query.CreateDate.Value.ToString("yyyy-MM-dd"));
            return Obj;
        }
        #endregion

    }
}