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

namespace MySystem
{
    /// <summary>
    /// 工作台工具类
    /// </summary>
    public class WorkSpaceUtil
    {
        #region 主界面统计数据
        /// <summary>
        /// 主界面统计数据
        /// </summary>
        /// <param name="TimeType">时间范围</param>
        /// <param name="MerchantId">商户Id</param>
        /// <returns></returns>
        public static Dictionary<string, object> IndexStat(string TimeType, int MerchantId)
        {
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            decimal TotalAmount = 0;
            decimal TotalOrder = 0;
            int TotalUser = 0;
            decimal TotalActual = 0;
            DateTime Start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");
            if (TimeType == "1")
            {
                Start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");
            }
            else if (TimeType == "2")
            {
                Start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00").AddDays(-6);
            }
            else if (TimeType == "3")
            {
                Start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
            }
            while (Start <= DateTime.Now)
            {
                Dictionary<string, string> dic = MerchantAmountSummayService.Sum("TradeAmount,TradeCount,TotalActual", " and MerchantId=" + MerchantId + " and TradeDate='" + Start.ToString("yyyyMMdd") + "'");
                TotalAmount += decimal.Parse(dic["TradeAmount"]);
                TotalOrder += decimal.Parse(dic["TradeCount"]);
                TotalActual += decimal.Parse(dic["TotalActual"]);
                Start = Start.AddDays(1);
            }
            Obj.Add("TotalAmount", TotalAmount.ToString("f2")); //营总收益
            MerchantInfo merchant = MerchantInfoService.Query(MerchantId);
            decimal AllocationAmount = merchant.ActMaxAmount - merchant.ActCurrentAmount;
            if (AllocationAmount < 0) AllocationAmount = 0;
            Obj.Add("Kind", 2); //通道(1 直连 2 银联)
            Obj.Add("AllocationAmount", AllocationAmount); //未分配活动金额
            Obj.Add("TotalOrder", TotalOrder); //累计订单
            Obj.Add("TotalUser", TotalUser); //会员数
            Obj.Add("TotalActual", TotalActual); //实收总额
            return Obj;
        }
        #endregion


        #region 商户活动参数
        /// <summary>
        /// 商户活动参数
        /// </summary>
        /// <param name="MerchantId">商户Id</param>
        /// <returns></returns>
        public static Dictionary<string, object> Detail(int MerchantId)
        {
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            var query = MerchantParamSetService.Query(MerchantId);
            if (query.Id == 0)
            {
                query = new MerchantParamSet();
                query.IsAll = 1;
            }
            Obj.Add("IsAll", query.IsAll); //是否收全额
            Obj.Add("MinPayMoney", query.MinPayMoney); //订单参与门槛
            Obj.Add("GetPercent", query.GetPercent); //商家实收比例
            Obj.Add("ProfitDays", query.ProfitDays); //分红期限（天）
            Obj.Add("DiviPercent", query.DiviPercent); //最大分红比例
            Obj.Add("DiviPersons", query.DiviPersons); //单笔订单分红人数
            return Obj;
        }
        #endregion

    }
}