/*
 * 交易日汇总
 */

using System;
using System.Web;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Data;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MySystem.Models;
using Library;
using LitJson;
using MySystemLib;

namespace MySystem.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class TradeDaySummaryController : BaseController
    {
        public TradeDaySummaryController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
            OtherMySqlConn.connstr = ConfigurationManager.AppSettings["SqlConnStr"].ToString();
        }

        #region 交易日汇总列表

        /// <summary>
        /// 根据条件查询交易日汇总列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index(TradeDaySummary data, string right, string BrandId)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;
            ViewBag.BrandId = BrandId;

            return View();
        }

        #endregion

        #region 根据条件查询交易日汇总列表

        /// <summary>
        /// 交易日汇总列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(TradeDaySummary data, string BrandId, string MakerCode, string RealName, string TradeMonthSelect, string CreateDateData, int page = 1, int limit = 30)
        {

            Dictionary<string, string> Fields = new Dictionary<string, string>();

            Fields.Add("BrandId", "1");
            Fields.Add("TopUserId", "0"); //顶级创客

            string condition = " and Status>-1 and BrandId=" + BrandId;
            //创客编号
            if (!string.IsNullOrEmpty(MakerCode))
            {
                condition += " and UserId in (select UserId from UserForMakerCode where MakerCode='" + MakerCode + "')";
            }
            //创客名称
            if (!string.IsNullOrEmpty(RealName))
            {
                condition += " and UserId in (select UserId from UserForRealName where RealName='" + RealName + "')";
            }
            //交易时间
            if (!string.IsNullOrEmpty(CreateDateData))
            {
                string[] datelist = CreateDateData.Split(new string[] { " - " }, StringSplitOptions.None);
                string start = datelist[0].Replace("-", "");
                string end = datelist[1].Replace("-", "");
                condition += " and TradeDate >= " + start + " and TradeDate <= " + end + " ";
            }
            //交易月份
            if (!string.IsNullOrEmpty(TradeMonthSelect))
            {
                condition += " and TradeMonth=" + TradeMonthSelect.Replace("-", "");
            }

            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("TradeDaySummary", Fields, "Id desc", "0", page, limit, condition);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                //创客
                int UserId = int.Parse(dic["UserId"].ToString());
                Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
                dic["MakerCode"] = user.MakerCode;
                dic["RealName"] = user.RealName;
                //顶级创客
                int TopUserId = int.Parse(dic["TopUserId"].ToString());
                Users tuser = db.Users.FirstOrDefault(m => m.Id == TopUserId) ?? new Users();
                dic["TopMakerCode"] = tuser.MakerCode;
                dic["TopRealName"] = tuser.RealName;
            }
            Dictionary<string, object> other = new Dictionary<string, object>();
            string tHelpDirectTradeAmt = "0.00";//扶持期商户交易总额
            string tNotHelpDirectTradeAmt = "0.00";//非扶持期商户交易总额
            string tProfitDirectTradeAmt = "0.00";//盈利期非商户交易总额
            string pHelpDirectTradeAmt = "0.00";//扶持期商户交易总额
            string pNotHelpDirectTradeAmt = "0.00";//非扶持期商户交易总额
            string pProfitDirectTradeAmt = "0.00";//盈利期非商户交易总额
            DataTable dt = OtherMySqlConn.dtable("SELECT SUM(if(SeoTitle='team',HelpNonDirectTradeAmt + HelpNonDirectDebitTradeAmt,0)),SUM(if(SeoTitle='team',NotHelpNonDirectTradeAmt + NotHelpNonDirectDebitTradeAmt,0)),SUM(if(SeoTitle='team',ProfitNonDirectTradeAmt + ProfitNonDirectDebitTradeAmt,0)),SUM(if(SeoTitle='self',HelpDirectTradeAmt + HelpDirectDebitTradeAmt,0)),SUM(if(SeoTitle='self',NotHelpDirectTradeAmt + NotHelpDirectDebitTradeAmt,0)),SUM(if(SeoTitle='self',ProfitDirectTradeAmt + ProfitDirectDebitTradeAmt,0)) FROM TradeDaySummary where 1=1" + condition);
            if (dt.Rows.Count > 0)
            {
                tHelpDirectTradeAmt = decimal.Parse(function.CheckNum(dt.Rows[0][0].ToString())).ToString("f2");
                tNotHelpDirectTradeAmt = decimal.Parse(function.CheckNum(dt.Rows[0][1].ToString())).ToString("f2");
                tProfitDirectTradeAmt = decimal.Parse(function.CheckNum(dt.Rows[0][2].ToString())).ToString("f2");
                pHelpDirectTradeAmt = decimal.Parse(function.CheckNum(dt.Rows[0][3].ToString())).ToString("f2");
                pNotHelpDirectTradeAmt = decimal.Parse(function.CheckNum(dt.Rows[0][4].ToString())).ToString("f2");
                pProfitDirectTradeAmt = decimal.Parse(function.CheckNum(dt.Rows[0][5].ToString())).ToString("f2");
            }
            other.Add("tHelpDirectTradeAmt", tHelpDirectTradeAmt);
            other.Add("tNotHelpDirectTradeAmt", tNotHelpDirectTradeAmt);
            other.Add("tProfitDirectTradeAmt", tProfitDirectTradeAmt);
            other.Add("pHelpDirectTradeAmt", pHelpDirectTradeAmt);
            other.Add("pNotHelpDirectTradeAmt", pNotHelpDirectTradeAmt);
            other.Add("pProfitDirectTradeAmt", pProfitDirectTradeAmt);
            obj.Add("other", other);
            return Json(obj);
        }

        #endregion

        #region 增加交易日汇总

        /// <summary>
        /// 增加或修改交易日汇总信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Add(string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            return View();
        }

        #endregion

        #region 增加交易日汇总

        /// <summary>
        /// 增加或修改交易日汇总信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Add(TradeDaySummary data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();


            Fields.Add("SeoTitle", data.SeoTitle);
            Fields.Add("SeoKeyword", data.SeoKeyword);
            Fields.Add("SeoDescription", data.SeoDescription);
            int Id = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Add("TradeDaySummary", Fields, 0);
            AddSysLog(data.Id.ToString(), "TradeDaySummary", "add");
            db.SaveChanges();

            return "success";
        }

        #endregion

        #region 修改交易日汇总

        /// <summary>
        /// 增加或修改交易日汇总信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Edit(string right, int Id = 0)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            TradeDaySummary editData = db.TradeDaySummary.FirstOrDefault(m => m.Id == Id) ?? new TradeDaySummary();
            ViewBag.data = editData;
            return View();
        }

        #endregion

        #region 修改交易日汇总

        /// <summary>
        /// 增加或修改交易日汇总信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Edit(TradeDaySummary data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();


            Fields.Add("SeoTitle", data.SeoTitle);
            Fields.Add("SeoKeyword", data.SeoKeyword);
            Fields.Add("SeoDescription", data.SeoDescription);
            new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("TradeDaySummary", Fields, data.Id);
            AddSysLog(data.Id.ToString(), "TradeDaySummary", "update");
            db.SaveChanges();

            return "success";
        }

        #endregion

        #region 删除交易日汇总信息

        /// <summary>
        /// 删除交易日汇总信息
        /// </summary>
        /// <returns></returns>
        public string Delete(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "TradeDaySummary", "del");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", -1);
                new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("TradeDaySummary", Fields, id);
            }
            db.SaveChanges();

            return "success";
        }

        #endregion

        #region 开启

        /// <summary>
        /// 开启
        /// </summary>
        /// <returns></returns>
        public string Open(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "TradeDaySummary", "open");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", 1);
                new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("TradeDaySummary", Fields, id);
            }
            db.SaveChanges();
            return "success";
        }

        #endregion

        #region 关闭

        /// <summary>
        /// 关闭
        /// </summary>
        /// <returns></returns>
        public string Close(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "TradeDaySummary", "close");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", 0);
                new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("TradeDaySummary", Fields, id);
            }
            db.SaveChanges();
            return "success";
        }

        #endregion

        #region 排序
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="Id"></param>
        public string Sort(int Id, int Sort)
        {
            new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Sort("TradeDaySummary", Sort, Id);

            AddSysLog(Id.ToString(), "TradeDaySummary", "sort");
            return "success";
        }
        #endregion

        #region 导入数据
        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="ExcelData"></param>
        public string Import(string ExcelData)
        {
            ExcelData = HttpUtility.UrlDecode(ExcelData);
            JsonData list = JsonMapper.ToObject(ExcelData);
            for (int i = 1; i < list.Count; i++)
            {
                JsonData dr = list[i];

                db.TradeDaySummary.Add(new TradeDaySummary()
                {
                    CreateDate = DateTime.Now,
                    UpdateDate = DateTime.Now,

                });
                db.SaveChanges();
            }
            AddSysLog("0", "TradeDaySummary", "Import");
            return "success";
        }
        #endregion

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcel(TradeDaySummary data, string MakerCode, string RealName, string TradeMonthSelect)
        {
            Dictionary<string, string> Fields = new Dictionary<string, string>();

            Fields.Add("BrandId", "1");
            Fields.Add("CreateDate", "3"); //交易时间
            Fields.Add("TopUserId", "0"); //顶级创客

            string condition = " and Status>-1";
            //创客编号
            if (!string.IsNullOrEmpty(MakerCode))
            {
                condition += " and UserId in (select UserId from UserForMakerCode where MakerCode='" + MakerCode + "')";
            }
            //创客名称
            if (!string.IsNullOrEmpty(RealName))
            {
                condition += " and UserId in (select UserId from UserForRealName where RealName='" + RealName + "')";
            }
            //交易月份
            if (!string.IsNullOrEmpty(TradeMonthSelect))
            {
                condition += " and TradeMonth=" + TradeMonthSelect.Replace("-", "");
            }

            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("TradeDaySummary", Fields, "Id desc", "0", 1, 20000, condition, "UserId,TopUserId,TradeDate,TradeMonth,NotHelpDirectTradeAmt,NotHelpNonDirectTradeAmt,NotHelpDirectCreditTradeAmt,NotHelpNonDirectCreditTradeAmt,NotHelpDirectQrCreditTradeAmt,NotHelpNonDirectQrCreditTradeAmt,NotHelpDirectNonQrCreditTradeAmt,NotHelpNonDirectNonQrCreditTradeAmt,NotHelpDirectDebitTradeAmt,NotHelpDirectDebitCapTradeAmt,NotHelpDirectDebitCapNum,NotHelpNonDirectDebitTradeAmt,NotHelpNonDirectDebitCapTradeAmt,NotHelpNonDirectDebitCapNum,NotHelpDirectQrDebitTradeAmt,NotHelpNonDirectQrDebitTradeAmt,NotHelpDirectNonQrDebitTradeAmt,NotHelpNonDirectNonQrDebitTradeAmt,HelpDirectTradeAmt,HelpNonDirectTradeAmt,HelpDirectCreditTradeAmt,HelpNonDirectCreditTradeAmt,HelpDirectQrCreditTradeAmt,HelpNonDirectQrCreditTradeAmt,HelpDirectNonQrCreditTradeAmt,HelpNonDirectNonQrCreditTradeAmt,HelpDirectDebitTradeAmt,HelpDirectDebitCapTradeAmt,HelpDirectDebitCapNum,HelpNonDirectDebitTradeAmt,HelpNonDirectDebitCapTradeAmt,HelpNonDirectDebitCapNum,HelpDirectQrDebitTradeAmt,HelpNonDirectQrDebitTradeAmt,HelpDirectNonQrDebitTradeAmt,HelpNonDirectNonQrDebitTradeAmt", false);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                //创客
                int UserId = int.Parse(dic["UserId"].ToString());
                Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
                dic["MakerCode"] = user.MakerCode;
                dic["RealName"] = user.RealName;
                //顶级创客
                int TopUserId = int.Parse(dic["TopUserId"].ToString());
                Users tuser = db.Users.FirstOrDefault(m => m.Id == TopUserId) ?? new Users();
                dic["TopMakerCode"] = tuser.MakerCode;
                dic["TopRealName"] = tuser.RealName;
                dic.Remove("UserId");
                dic.Remove("TopUserId");
            }

            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("Status", "1");
            result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
            result.Add("Obj", diclist);
            Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
            ReturnFields.Add("MakerCode", "创客编号");
            ReturnFields.Add("RealName", "创客姓名");
            ReturnFields.Add("TopMakerCode", "顶级创客编号");
            ReturnFields.Add("TopRealName", "顶级创客姓名");
            ReturnFields.Add("TradeDate", "交易日");
            ReturnFields.Add("TradeMonth", "交易月");
            ReturnFields.Add("NotHelpDirectTradeAmt", "直营商户交易总额(非扶持期)");
            ReturnFields.Add("NotHelpNonDirectTradeAmt", "非直营商户交易总额(非扶持期)");
            ReturnFields.Add("NotHelpDirectCreditTradeAmt", "直营贷记卡交易总额(非扶持期)");
            ReturnFields.Add("NotHelpNonDirectCreditTradeAmt", "非直营贷记卡交易总额(非扶持期)");
            ReturnFields.Add("NotHelpDirectQrCreditTradeAmt", "直营云闪付贷记卡交易额(非扶持期)");
            ReturnFields.Add("NotHelpNonDirectQrCreditTradeAmt", "非直营云闪付贷记卡交易额(非扶持期)");
            ReturnFields.Add("NotHelpDirectNonQrCreditTradeAmt", "直营非云闪付贷记卡交易额(非扶持期)");
            ReturnFields.Add("NotHelpNonDirectNonQrCreditTradeAmt", "非直营非云闪付贷记卡交易额(非扶持期)");
            ReturnFields.Add("NotHelpDirectDebitTradeAmt", "直营商户借记卡交易总额(非扶持期)");
            ReturnFields.Add("NotHelpDirectDebitCapTradeAmt", "直营商户借记卡封顶交易总金额(非扶持期)");
            ReturnFields.Add("NotHelpDirectDebitCapNum", "直营商户借记卡封顶交易总数(非扶持期)");
            ReturnFields.Add("NotHelpNonDirectDebitTradeAmt", "非直营商户借记卡交易总额(非扶持期)");
            ReturnFields.Add("NotHelpNonDirectDebitCapTradeAmt", "非直营商户借记卡封顶交易总金额(非扶持期)");
            ReturnFields.Add("NotHelpNonDirectDebitCapNum", "非直营商户借记卡封顶交易总数(非扶持期)");
            ReturnFields.Add("NotHelpDirectQrDebitTradeAmt", "直营云闪付非封顶借记卡(非扶持期)");
            ReturnFields.Add("NotHelpNonDirectQrDebitTradeAmt", "非直营云闪付非封顶借记卡(非扶持期)");
            ReturnFields.Add("NotHelpDirectNonQrDebitTradeAmt", "直营非云闪付非封顶借记卡(非扶持期)");
            ReturnFields.Add("NotHelpNonDirectNonQrDebitTradeAmt", "非直营非云闪付非封顶借记卡(非扶持期)");

            ReturnFields.Add("HelpDirectTradeAmt", "直营商户交易总额(扶持期)");
            ReturnFields.Add("HelpNonDirectTradeAmt", "非直营商户交易总额(扶持期)");
            ReturnFields.Add("HelpDirectCreditTradeAmt", "直营贷记卡交易总额(扶持期)");
            ReturnFields.Add("HelpNonDirectCreditTradeAmt", "非直营贷记卡交易总额(扶持期)");
            ReturnFields.Add("HelpDirectQrCreditTradeAmt", "直营云闪付贷记卡交易额(扶持期)");
            ReturnFields.Add("HelpNonDirectQrCreditTradeAmt", "非直营云闪付贷记卡交易额(扶持期)");
            ReturnFields.Add("HelpDirectNonQrCreditTradeAmt", "直营非云闪付贷记卡交易额(扶持期)");
            ReturnFields.Add("HelpNonDirectNonQrCreditTradeAmt", "非直营非云闪付贷记卡交易额(扶持期)");
            ReturnFields.Add("HelpDirectDebitTradeAmt", "直营商户借记卡交易总额(扶持期)");
            ReturnFields.Add("HelpDirectDebitCapTradeAmt", "直营商户借记卡封顶交易总金额(扶持期)");
            ReturnFields.Add("HelpDirectDebitCapNum", "直营商户借记卡封顶交易总数(扶持期)");
            ReturnFields.Add("HelpNonDirectDebitTradeAmt", "非直营商户借记卡交易总额(扶持期)");
            ReturnFields.Add("HelpNonDirectDebitCapTradeAmt", "非直营商户借记卡封顶交易总金额(扶持期)");
            ReturnFields.Add("HelpNonDirectDebitCapNum", "非直营商户借记卡封顶交易总数(扶持期)");
            ReturnFields.Add("HelpDirectQrDebitTradeAmt", "直营云闪付非封顶借记卡(扶持期)");
            ReturnFields.Add("HelpNonDirectQrDebitTradeAmt", "非直营云闪付非封顶借记卡(扶持期)");
            ReturnFields.Add("HelpDirectNonQrDebitTradeAmt", "直营非云闪付非封顶借记卡(扶持期)");
            ReturnFields.Add("HelpNonDirectNonQrDebitTradeAmt", "非直营非云闪付非封顶借记卡(扶持期)");

            ReturnFields.Add("ProfitDirectTradeAmt", "直营商户交易总额(盈利期)");
            ReturnFields.Add("ProfitNonDirectTradeAmt", "非直营商户交易总额(盈利期)");
            ReturnFields.Add("ProfitDirectCreditTradeAmt", "直营贷记卡交易总额(盈利期)");
            ReturnFields.Add("ProfitNonDirectCreditTradeAmt", "非直营贷记卡交易总额(盈利期)");
            ReturnFields.Add("ProfitDirectQrCreditTradeAmt", "直营云闪付贷记卡交易额(盈利期)");
            ReturnFields.Add("ProfitNonDirectQrCreditTradeAmt", "非直营云闪付贷记卡交易额(盈利期)");
            ReturnFields.Add("ProfitDirectNonQrCreditTradeAmt", "直营非云闪付贷记卡交易额(盈利期)");
            ReturnFields.Add("ProfitNonDirectNonQrCreditTradeAmt", "非直营非云闪付贷记卡交易额(盈利期)");
            ReturnFields.Add("ProfitDirectDebitTradeAmt", "直营商户借记卡交易总额(盈利期)");
            ReturnFields.Add("ProfitDirectDebitCapTradeAmt", "直营商户借记卡封顶交易总金额(盈利期)");
            ReturnFields.Add("ProfitDirectDebitCapNum", "直营商户借记卡封顶交易总数(盈利期)");
            ReturnFields.Add("ProfitNonDirectDebitTradeAmt", "非直营商户借记卡交易总额(盈利期)");
            ReturnFields.Add("ProfitNonDirectDebitCapTradeAmt", "非直营商户借记卡封顶交易总金额(盈利期)");
            ReturnFields.Add("ProfitNonDirectDebitCapNum", "非直营商户借记卡封顶交易总数(盈利期)");
            ReturnFields.Add("ProfitDirectQrDebitTradeAmt", "直营云闪付非封顶借记卡(盈利期)");
            ReturnFields.Add("ProfitNonDirectQrDebitTradeAmt", "非直营云闪付非封顶借记卡(盈利期)");
            ReturnFields.Add("ProfitDirectNonQrDebitTradeAmt", "直营非云闪付非封顶借记卡(盈利期)");
            ReturnFields.Add("ProfitNonDirectNonQrDebitTradeAmt", "非直营非云闪付非封顶借记卡(盈利期)");

            result.Add("Fields", ReturnFields);
            AddSysLog("0", "TradeDaySummary", "ExportExcel");
            return Json(result);
        }

        #endregion

    }
}
