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

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

        #region 统计交易额
        public decimal GetTrade(int UserId, string TradeDateOrMonth, int IsAct, string kind = "self")
        {
            string timeField = TradeDateOrMonth.Length == 8 ? "TradeDate" : "TradeMonth";
            DataTable dt = CustomerSqlConn.dtable("select sum(TotalAmount) from UserAmountSummary where UserId=" + UserId + " and " + timeField + "='" + TradeDateOrMonth + "' and IsAct=" + IsAct + " and SeoTitle='" + kind + "'", AppConfig.Base.MpSqlConn);
            if (dt.Rows.Count > 0)
            {
                return decimal.Parse(function.CheckNum(dt.Rows[0][0].ToString()));
            }
            return 0M;
        }
        public decimal GetTrade(int UserId, int IsAct, string kind = "self")
        {
            DataTable dt = CustomerSqlConn.dtable("select sum(TotalAmount) from UserAmountSummary where UserId=" + UserId + " and IsAct=" + IsAct + " and SeoTitle='" + kind + "'", AppConfig.Base.MpSqlConn);
            if(dt.Rows.Count > 0)
            {
                return decimal.Parse(function.CheckNum(dt.Rows[0][0].ToString()));
            }
            return 0M;
        }
        
        #endregion


        #region 统计激活数
        public int GetActCountByDate(int UserId, string TradeDateOrMonth, string kind = "self")
        {
            string timeField = TradeDateOrMonth.Length == 8 ? "SeoKeyword" : "TradeMonth";
            DataTable dt = CustomerSqlConn.dtable("select sum(ActiveBuddyMerStatus) from UserTradeMonthSummary where UserId=" + UserId + " and " + timeField + "='" + TradeDateOrMonth + "' and SeoTitle='" + kind + "'", AppConfig.Base.MpSqlConn);
            if (dt.Rows.Count > 0)
            {
                return int.Parse(function.CheckNum(dt.Rows[0][0].ToString()));
            }
            return 0;
        }
        public int GetActCount(int UserId, string kind = "self")
        {
            DataTable dt = CustomerSqlConn.dtable("select sum(ActiveBuddyMerStatus) from UserTradeMonthSummary where UserId=" + UserId + " and SeoTitle='" + kind + "'", AppConfig.Base.MpSqlConn);
            if(dt.Rows.Count > 0)
            {
                return int.Parse(function.CheckNum(dt.Rows[0][0].ToString()));
            }
            return 0;
        }
        
        #endregion

    }
}
