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

namespace MySystem
{
    /// <summary>
    /// 经营数据工具类
    /// </summary>
    public class BusinessUtil
    {

        #region 经营数据记录(月)
        /// <summary>
        /// 经营数据记录(月)
        /// </summary>
        /// <param name="MerchantId">商户Id</param>
        /// <param name="TradeMonth">月份</param>
        /// <param name="pageSize">条数</param>
        /// <param name="pageNum">页数</param>
        /// <returns></returns>
        public static List<Dictionary<string, object>> BussinessRecordForMonth(int MerchantId, string TradeMonth, int pageSize, int pageNum)
        {
            List<RelationData> relationData = new List<RelationData>();
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            if (!new MerchantTradeSummaryService().CheckTableExist(TradeMonth))
            {
                return dataList;
            }
            List<Dictionary<string, object>> source = MerchantTradeSummaryService.List2(relationData, " and MerchantId=" + MerchantId, TradeMonth, pageNum, pageSize,"TradeDate desc");
            foreach (Dictionary<string, object> subdata in source)
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();
                curData.Add("TradeDate", subdata["TradeDate"].ToString()); //日期
                curData.Add("TotalAmount", decimal.Parse(subdata["TradeAmount"].ToString()).ToString("f2")); //营业总金额
                curData.Add("InFactAmount", decimal.Parse(subdata["InFactAmount"].ToString()).ToString("f2")); //实收总金额
                curData.Add("ActAmount", decimal.Parse(subdata["ActAmount"].ToString()).ToString("f2")); //活动总金额
                curData.Add("NonActAmount", decimal.Parse(subdata["NonActAmount"].ToString()).ToString("f2")); //非活动总金额
                curData.Add("WeChatInFactAmount", decimal.Parse(subdata["WeChatInfactAmount"].ToString()).ToString("f2")); //微信实收
                curData.Add("AliPayInFactAmount", decimal.Parse(subdata["AliPayInFactAmount"].ToString()).ToString("f2")); //支付宝实收
                curData.Add("OrderCount", subdata["OderCount"].ToString()); //订单数
                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion


        #region 经营数据记录(半年)
        /// <summary>
        /// 经营数据记录(半年)
        /// </summary>
        /// <param name="MerchantId">商户Id</param>
        /// <param name="pageSize">条数</param>
        /// <param name="pageNum">页数</param>
        /// <returns></returns>
        public static List<Dictionary<string, object>> BussinessRecordForHalfYear(int MerchantId, int pageSize, int pageNum)
        {
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            List<Dictionary<string, object>> source = UserTradeDbconn.Instance.GetMonthTradeList(MerchantId, "TradeAmount,InFactAmount,ActAmount,NonActAmount,WeChatInfactAmount,AliPayInFactAmount,OderCount");
            foreach (Dictionary<string, object> subdata in source)
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();
                curData.Add("TradeDate", subdata["TradeDate"].ToString()); //日期
                curData.Add("TotalAmount", decimal.Parse(subdata["TradeAmount"].ToString()).ToString("f2")); //营业总金额
                curData.Add("InFactAmount", decimal.Parse(subdata["InFactAmount"].ToString()).ToString("f2")); //实收总金额
                curData.Add("ActAmount", decimal.Parse(subdata["ActAmount"].ToString()).ToString("f2")); //活动总金额
                curData.Add("NonActAmount", decimal.Parse(subdata["NonActAmount"].ToString()).ToString("f2")); //非活动总金额
                curData.Add("WeChatInFactAmount", decimal.Parse(subdata["WeChatInfactAmount"].ToString()).ToString("f2")); //微信实收
                curData.Add("AliPayInFactAmount", decimal.Parse(subdata["AliPayInFactAmount"].ToString()).ToString("f2")); //支付宝实收
                curData.Add("OrderCount", subdata["OderCount"].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 List<Dictionary<string, object>> DayDetailList(string condition, int pageSize, int pageNum)
        {
            List<RelationData> relationData = new List<RelationData>();
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            List<Dictionary<string, object>> source = ConsumerOrdersService.List(relationData, condition, pageNum, pageSize);
            foreach (Dictionary<string, object> subdata in source)
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();
                curData.Add("Id", subdata["Id"].ToString()); //订单Id
                curData.Add("TradeDate", DateTime.Parse(subdata["CreateDate"].ToString()).ToString("HH:mm:ss")); //日期
                curData.Add("PayMode", subdata["PayMode"].ToString()); //支付方式(1 支付宝 2 微信)
                curData.Add("IsAct", subdata["IsAct"].ToString()); //是否活动(1 是 0 否)
                curData.Add("PayMoney", decimal.Parse(subdata["PayMoney"].ToString()).ToString("f2")); //订单金额
                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion


        #region 交易详情
        /// <summary>
        /// 交易详情
        /// </summary>
        /// <param name="condition">查询条件</param>
        /// <returns></returns>
        public static Dictionary<string, object> OrderDetail(string condition)
        {
            List<RelationData> relationData = new List<RelationData>();
            Dictionary<string, object> obj = new Dictionary<string, object>();
            List<Dictionary<string, object>> source = ConsumerOrdersService.List(relationData, condition, 1, 999);
            foreach (Dictionary<string, object> subdata in source)
            {
                obj.Add("TradeDate", DateTime.Parse(subdata["CreateDate"].ToString()).ToString("yyyy-MM-dd HH:mm:ss")); //日期
                obj.Add("PayMode", subdata["PayMode"].ToString()); //支付方式(1 微信 2 支付宝)
                obj.Add("PayMoney", decimal.Parse(subdata["PayMoney"].ToString()).ToString("f2")); //支付金额
                obj.Add("IsAct", subdata["IsAct"].ToString()); //是否活动(1 是 0 否)
                obj.Add("OrderNo", subdata["OrderNo"].ToString()); //订单号
                obj.Add("MerchantName", MerchantInfoService.Query(int.Parse(subdata["MerchantId"].ToString())).Name); //交易门店
                List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
                List<Dictionary<string, object>> dics = ConsumerProfitService.List(relationData, " and OrderId=" + int.Parse(subdata["Id"].ToString()) + "", 1, 999);
                foreach (Dictionary<string, object> dic in dics)
                {
                    Dictionary<string, object> cdata = new Dictionary<string, object>();
                    var CreateDate = DateTime.Parse(dic["CreateDate"].ToString());
                    var TradeDate = CreateDate.Month + "月" + CreateDate.Day + "日" + " " + CreateDate.ToString("HH:mm");
                    cdata.Add("ProfitDate", TradeDate); //分红时间
                    cdata.Add("ProfitAmount", decimal.Parse(dic["GetMoney"].ToString()).ToString("f2")); //分红金额
                    dataList.Add(cdata);
                }
                obj.Add("ProfitTotalAmount", ConsumerProfitService.Sum(" OrderId=" + int.Parse(subdata["Id"].ToString()) + "", "GetMoney").ToString("f2")); //分红总金额
                obj.Add("ProfitList", dataList); //订单分红记录列表
            }
            return obj;
        }
        #endregion

    }
}