using System;
using System.Collections.Generic;
using Library;
using System.Linq;
using System.Data;
using MySystem.MainModels;

namespace MySystem
{
    public class MerchantAmountSummary
    {
        public readonly static MerchantAmountSummary Instance = new MerchantAmountSummary();
        public MerchantAmountSummary()
        { }

        public decimal GetTrade(int MerchantId)
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.MerchantAmountSummay.Any(m => m.MerchantId == MerchantId);
            if (check)
            {
                amt = db.MerchantAmountSummay.Where(m => m.MerchantId == MerchantId).Sum(m => m.TradeAmount);
            }
            db.Dispose();
            return amt;
        }
        public decimal GetTradeByMonth(int MerchantId, string TradeMonth)
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.MerchantAmountSummay.Any(m => m.MerchantId == MerchantId && m.TradeMonth == TradeMonth);
            if (check)
            {
                amt = db.MerchantAmountSummay.Where(m => m.MerchantId == MerchantId && m.TradeMonth == TradeMonth).Sum(m => m.TradeAmount);
            }
            db.Dispose();
            return amt;
        }

        public decimal GetTradeByDate(int MerchantId, string TradeDate)
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.MerchantAmountSummay.Any(m => m.MerchantId == MerchantId && m.TradeDate == TradeDate);
            if (check)
            {
                amt = db.MerchantAmountSummay.Where(m => m.MerchantId == MerchantId && m.TradeDate == TradeDate).Sum(m => m.TradeAmount);
            }
            db.Dispose();
            return amt;
        }

        public decimal GetTradeByMode(int MerchantId, int PayMode, string TradeDate)
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.MerchantAmountSummay.Any(m => m.MerchantId == MerchantId && m.PayMode == PayMode && m.TradeDate == TradeDate);
            if (check)
            {
                amt = db.MerchantAmountSummay.Where(m => m.MerchantId == MerchantId && m.PayMode == PayMode && m.TradeDate == TradeDate).Sum(m => m.TradeAmount);
            }
            db.Dispose();
            return amt;
        }

        public decimal GetActualTradeByDate(int MerchantId, string TradeDate)
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.MerchantAmountSummay.Any(m => m.MerchantId == MerchantId && m.TradeDate == TradeDate);
            if (check)
            {
                amt = db.MerchantAmountSummay.Where(m => m.MerchantId == MerchantId && m.TradeDate == TradeDate).Sum(m => m.TotalActual);
            }
            db.Dispose();
            return amt;
        }

        public decimal GetActualTradeByMode(int MerchantId, int PayMode, string TradeDate)
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.MerchantAmountSummay.Any(m => m.MerchantId == MerchantId && m.PayMode == PayMode && m.TradeDate == TradeDate);
            if (check)
            {
                amt = db.MerchantAmountSummay.Where(m => m.MerchantId == MerchantId && m.PayMode == PayMode && m.TradeDate == TradeDate).Sum(m => m.TotalActual);
            }
            db.Dispose();
            return amt;
        }

        public DataTable GetTradeListByDate(int MerchantId, string TradeMonth, int PageNum, int PageSize)
        {
            int skipNum = 0;
            if (PageNum > 1)
            {
                skipNum = PageSize * (PageNum - 1);
            }
            DataTable dt = CustomerSqlConn.dtable("select TradeDate,sum(if(PayMode=1, TotalActual, 0)),sum(if(PayMode=2, TotalActual, 0)),sum(TotalActual),sum(TradeCount),sum(TradeAmount) from MerchantAmountSummay where MerchantId=" + MerchantId + " and TradeMonth='" + TradeMonth + "' group by TradeDate order by TradeDate desc limit " + skipNum + "," + PageSize, AppConfig.Base.SqlConnStr);
            return dt;
        }

        public DataTable GetTradeListByMonth(int MerchantId, int PageNum, int PageSize)
        {
            int skipNum = 0;
            if (PageNum > 1)
            {
                skipNum = PageSize * (PageNum - 1);
            }
            DataTable dt = CustomerSqlConn.dtable("select TradeMonth,sum(if(PayMode=1, TotalActual, 0)),sum(if(PayMode=2, TotalActual, 0)),sum(TotalActual),sum(TradeCount),sum(TradeAmount) from MerchantAmountSummay where MerchantId=" + MerchantId + " group by TradeMonth order by TradeMonth desc limit " + skipNum + "," + PageSize, AppConfig.Base.SqlConnStr);
            return dt;
        }
    }
}
