/*
 * 商户
 */

using System;
using System.Web;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
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 PosMerchantInfoController : BaseController
    {
        public PosMerchantInfoController(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(PosMerchantInfo 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(PosMerchantInfo data, string MerIdcardNo, string MerchantName, string MakerCode, string RealName, string MerMakerCode, string StoreNo, string StoreName, string MerStatusSelect, string ActiveStatusSelect, string ActTypeSelect, string MerUserTypeSelect, int page = 1, int limit = 30)
        {

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

            Fields.Add("BrandId", "1");
            Fields.Add("MerchantNo", "1"); //商户编号
            Fields.Add("MerchantName", "1"); //商户姓名
            Fields.Add("MerchantMobile", "1"); //商户手机号
            Fields.Add("KqMerNo", "1"); //快钱商户编码
            Fields.Add("KqSnNo", "1"); //快钱SN号
            Fields.Add("KqRegTime", "3"); //渠道注册时间
            Fields.Add("TopUserId", "0"); //顶级创客

            string condition = " and Status>-1";
            //身份证号
            if (!string.IsNullOrEmpty(MerIdcardNo))
            {
                condition += " and MerchantName like '%" + MerIdcardNo + "%'";
            }
            //商户姓名
            if (!string.IsNullOrEmpty(MerchantName))
            {
                condition += " and MerchantName like '%" + MerchantName + "%'";
            }
            //创客编号
            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(MerMakerCode))
            {
                condition += " and MerUserId in (select UserId from UserForMakerCode where MakerCode='" + MerMakerCode + "')";
            }
            //仓库编号
            if (!string.IsNullOrEmpty(StoreNo))
            {
                condition += " and StoreId in (select StoreId from StoreForCode where Code='" + StoreNo + "')";
            }
            //仓库名称
            if (!string.IsNullOrEmpty(RealName))
            {
                condition += " and StoreId in (select StoreId from StoreForName where Name='" + StoreName + "')";
            }
            //商户状态
            if (!string.IsNullOrEmpty(MerStatusSelect))
            {
                condition += " and MerStatus=" + MerStatusSelect;
            }
            //商户激活状态
            if (!string.IsNullOrEmpty(ActiveStatusSelect))
            {
                condition += " and ActiveStatus=" + ActiveStatusSelect;
            }
            //激活类型
            if (!string.IsNullOrEmpty(ActTypeSelect))
            {
                condition += " and ActType=" + ActTypeSelect;
            }
            //商户创客类型
            if (!string.IsNullOrEmpty(MerUserTypeSelect))
            {
                condition += " and MerUserType=" + MerUserTypeSelect;
            }

            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("PosMerchantInfo", 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 puser = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
                dic["MakerCode"] = puser.MakerCode;
                dic["RealName"] = puser.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;
                //商户创客
                int MerUserId = int.Parse(dic["MerUserId"].ToString());
                Users muser = db.Users.FirstOrDefault(m => m.Id == MerUserId) ?? new Users();
                dic["MerMakerCode"] = muser.MakerCode;
                dic["MerRealName"] = muser.RealName;
                //申请创客
                int SnApplyUserId = int.Parse(dic["SnApplyUserId"].ToString());
                Users snuser = db.Users.FirstOrDefault(m => m.Id == SnApplyUserId) ?? new Users();
                dic["SnApplyMakerCode"] = snuser.MakerCode;
                dic["SnApplyRealName"] = snuser.RealName;
                //SN仓库
                int SnStoreId = int.Parse(dic["SnStoreId"].ToString());
                StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == SnStoreId) ?? new StoreHouse();
                dic["StoreNo"] = store.StoreNo;
                dic["StoreName"] = store.StoreName;
                //机具类型
                int SnType = int.Parse(dic["SnType"].ToString());
                if (SnType == 0) dic["SnType"] = "兑换机具";
                if (SnType == 1) dic["SnType"] = "循环机具";
                //返利资格
                dic["RebateQual"] = dic["RebateQual"].ToString() == "1" ? "是" : "否";
                //激活类型
                int ActType = int.Parse(dic["ActType"].ToString());
                if (ActType == 0) dic["ActType"] = "正常激活";
                if (ActType == 1) dic["ActType"] = "首次已激活MPOS";
                if (ActType == 2) dic["ActType"] = "首次已激活KPOS";
                if (ActType == 3) dic["ActType"] = "循环机划拨激活";
                //商户状态
                int MerStatus = int.Parse(dic["MerStatus"].ToString());
                if (MerStatus == 0) dic["MerStatus"] = "正常";
                if (MerStatus == 1) dic["MerStatus"] = "冻结";
                if (MerStatus == 2) dic["MerStatus"] = "关闭";
                //商户激活状态
                int ActiveStatus = int.Parse(dic["ActiveStatus"].ToString());
                if (ActiveStatus == 0) dic["ActiveStatus"] = "未激活";
                if (ActiveStatus == 1) dic["ActiveStatus"] = "已激活";
                if (ActiveStatus == 2) dic["ActiveStatus"] = "SN已返现";
                //商户创客类型
                int MerUserType = int.Parse(dic["MerUserType"].ToString());
                if (MerUserType == 0) dic["MerUserType"] = "非商户型创客";
                if (MerUserType == 1) dic["MerUserType"] = "商户型创客";
            }
            return Json(obj);
        }

        #endregion


        #region 列表
        public IActionResult List(PosMerchantInfo data, string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;

            return View();
        }

        #endregion

        #region 列表
        public JsonResult ListData(PosMerchantInfo data, string MerIdcardNo, string MerchantName, string MakerCode, string RealName, string MerMakerCode, string StoreNo, string StoreName, string MerStatusSelect, string ActiveStatusSelect, string ActTypeSelect, string MerUserTypeSelect, int page = 1, int limit = 30)
        {

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

            Fields.Add("BrandId", "1");
            Fields.Add("MerchantNo", "1"); //商户编号
            Fields.Add("MerchantName", "1"); //商户姓名
            Fields.Add("MerchantMobile", "1"); //商户手机号
            Fields.Add("KqMerNo", "1"); //快钱商户编码
            Fields.Add("KqSnNo", "1"); //快钱SN号
            Fields.Add("KqRegTime", "3"); //渠道注册时间
            Fields.Add("TopUserId", "0"); //顶级创客

            string condition = " and Status>-1";
            //身份证号
            if (!string.IsNullOrEmpty(MerIdcardNo))
            {
                condition += " and MerchantName like '%" + MerIdcardNo + "%'";
            }
            //商户姓名
            if (!string.IsNullOrEmpty(MerchantName))
            {
                condition += " and MerchantName like '%" + MerchantName + "%'";
            }
            //创客编号
            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(MerMakerCode))
            {
                condition += " and MerUserId in (select UserId from UserForMakerCode where MakerCode='" + MerMakerCode + "')";
            }
            //仓库编号
            if (!string.IsNullOrEmpty(StoreNo))
            {
                condition += " and StoreId in (select StoreId from StoreForCode where Code='" + StoreNo + "')";
            }
            //仓库名称
            if (!string.IsNullOrEmpty(RealName))
            {
                condition += " and StoreId in (select StoreId from StoreForName where Name='" + StoreName + "')";
            }
            //商户状态
            if (!string.IsNullOrEmpty(MerStatusSelect))
            {
                condition += " and MerStatus=" + MerStatusSelect;
            }
            //商户激活状态
            if (!string.IsNullOrEmpty(ActiveStatusSelect))
            {
                condition += " and ActiveStatus=" + ActiveStatusSelect;
            }
            //激活类型
            if (!string.IsNullOrEmpty(ActTypeSelect))
            {
                condition += " and ActType=" + ActTypeSelect;
            }
            //商户创客类型
            if (!string.IsNullOrEmpty(MerUserTypeSelect))
            {
                condition += " and MerUserType=" + MerUserTypeSelect;
            }

            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("PosMerchantInfo", 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 puser = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
                dic["MakerCode"] = puser.MakerCode;
                dic["RealName"] = puser.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;
                //商户创客
                int MerUserId = int.Parse(dic["MerUserId"].ToString());
                Users muser = db.Users.FirstOrDefault(m => m.Id == MerUserId) ?? new Users();
                dic["MerMakerCode"] = muser.MakerCode;
                dic["MerRealName"] = muser.RealName;
                //申请创客
                int SnApplyUserId = int.Parse(dic["SnApplyUserId"].ToString());
                Users snuser = db.Users.FirstOrDefault(m => m.Id == SnApplyUserId) ?? new Users();
                dic["SnApplyMakerCode"] = snuser.MakerCode;
                dic["SnApplyRealName"] = snuser.RealName;
                //SN仓库
                int SnStoreId = int.Parse(dic["SnStoreId"].ToString());
                StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == SnStoreId) ?? new StoreHouse();
                dic["StoreNo"] = store.StoreNo;
                dic["StoreName"] = store.StoreName;
                //机具类型
                int SnType = int.Parse(dic["SnType"].ToString());
                if (SnType == 0) dic["SnType"] = "兑换机具";
                if (SnType == 1) dic["SnType"] = "循环机具";
                //返利资格
                dic["RebateQual"] = dic["RebateQual"].ToString() == "1" ? "是" : "否";
                //激活类型
                int ActType = int.Parse(dic["ActType"].ToString());
                if (ActType == 0) dic["ActType"] = "正常激活";
                if (ActType == 1) dic["ActType"] = "首次已激活MPOS";
                if (ActType == 2) dic["ActType"] = "首次已激活KPOS";
                if (ActType == 3) dic["ActType"] = "循环机划拨激活";
                //商户状态
                int MerStatus = int.Parse(dic["MerStatus"].ToString());
                if (MerStatus == 0) dic["MerStatus"] = "正常";
                if (MerStatus == 1) dic["MerStatus"] = "冻结";
                if (MerStatus == 2) dic["MerStatus"] = "关闭";
                //商户激活状态
                int ActiveStatus = int.Parse(dic["ActiveStatus"].ToString());
                if (ActiveStatus == 0) dic["ActiveStatus"] = "未激活";
                if (ActiveStatus == 1) dic["ActiveStatus"] = "已激活";
                if (ActiveStatus == 2) dic["ActiveStatus"] = "SN已返现";
                //商户创客类型
                int MerUserType = int.Parse(dic["MerUserType"].ToString());
                if (MerUserType == 0) dic["MerUserType"] = "非商户型创客";
                if (MerUserType == 1) dic["MerUserType"] = "商户型创客";
            }
            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(PosMerchantInfo 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("PosMerchantInfo", Fields, 0);
            AddSysLog(data.Id.ToString(), "PosMerchantInfo", "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;


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

        #endregion

        #region 修改商户

        /// <summary>
        /// 增加或修改商户信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Edit(PosMerchantInfo 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("PosMerchantInfo", Fields, data.Id);
            AddSysLog(data.Id.ToString(), "PosMerchantInfo", "update");
            db.SaveChanges();

            return "success";
        }

        #endregion

        #region 删除商户信息

        /// <summary>
        /// 删除商户信息
        /// </summary>
        /// <returns></returns>
        public string Delete(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "PosMerchantInfo", "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("PosMerchantInfo", 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, "PosMerchantInfo", "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("PosMerchantInfo", 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, "PosMerchantInfo", "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("PosMerchantInfo", 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("PosMerchantInfo", Sort, Id);

            AddSysLog(Id.ToString(), "PosMerchantInfo", "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.PosMerchantInfo.Add(new PosMerchantInfo()
                {
                    CreateDate = DateTime.Now,
                    UpdateDate = DateTime.Now,

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

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcel(PosMerchantInfo data, string MakerCode, string RealName, string MerMakerCode, string StoreNo, string StoreName, string MerStatusSelect, string ActiveStatusSelect, string ActTypeSelect, string MerUserTypeSelect)
        {
            Dictionary<string, string> Fields = new Dictionary<string, string>();

            Fields.Add("BrandId", "1");
            Fields.Add("MerchantNo", "1"); //商户编号
            Fields.Add("MerchantName", "1"); //商户姓名
            Fields.Add("MerchantMobile", "1"); //商户手机号
            Fields.Add("KqMerNo", "1"); //快钱商户编码
            Fields.Add("KqSnNo", "1"); //快钱SN号
            Fields.Add("KqRegTime", "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(MerMakerCode))
            {
                condition += " and MerUserId in (select UserId from UserForMakerCode where MakerCode='" + MerMakerCode + "')";
            }
            //仓库编号
            if (!string.IsNullOrEmpty(StoreNo))
            {
                condition += " and StoreId in (select StoreId from StoreForCode where Code='" + StoreNo + "')";
            }
            //仓库名称
            if (!string.IsNullOrEmpty(RealName))
            {
                condition += " and StoreId in (select StoreId from StoreForName where Name='" + StoreName + "')";
            }
            //商户状态
            if (!string.IsNullOrEmpty(MerStatusSelect))
            {
                condition += " and MerStatus=" + MerStatusSelect;
            }
            //商户激活状态
            if (!string.IsNullOrEmpty(ActiveStatusSelect))
            {
                condition += " and ActiveStatus=" + ActiveStatusSelect;
            }
            //激活类型
            if (!string.IsNullOrEmpty(ActTypeSelect))
            {
                condition += " and ActType=" + ActTypeSelect;
            }
            //商户创客类型
            if (!string.IsNullOrEmpty(MerUserTypeSelect))
            {
                condition += " and MerUserType=" + MerUserTypeSelect;
            }

            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("PosMerchantInfo", Fields, "Id desc", "0", 1, 20000, condition, "MerchantNo,MerchantName,MerchantMobile,KqMerNo,KqSnNo,MerStatus,ActiveStatus,UserId,MerUserId,SnType,SnApplyUserId,KqRegTime,ActType,SnStoreId,TopUserId,MerUserType,RebateQual", 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 puser = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
                dic["MakerCode"] = puser.MakerCode;
                dic["RealName"] = puser.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;
                //商户创客
                int MerUserId = int.Parse(dic["MerUserId"].ToString());
                Users muser = db.Users.FirstOrDefault(m => m.Id == MerUserId) ?? new Users();
                dic["MerMakerCode"] = muser.MakerCode;
                dic["MerRealName"] = muser.RealName;
                //申请创客
                int SnApplyUserId = int.Parse(dic["SnApplyUserId"].ToString());
                Users snuser = db.Users.FirstOrDefault(m => m.Id == SnApplyUserId) ?? new Users();
                dic["SnApplyMakerCode"] = snuser.MakerCode;
                dic["SnApplyRealName"] = snuser.RealName;
                //SN仓库
                int SnStoreId = int.Parse(dic["SnStoreId"].ToString());
                StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == SnStoreId) ?? new StoreHouse();
                dic["StoreNo"] = store.StoreNo;
                dic["StoreName"] = store.StoreName;
                //机具类型
                int SnType = int.Parse(dic["SnType"].ToString());
                if (SnType == 0) dic["SnType"] = "兑换机具";
                if (SnType == 1) dic["SnType"] = "循环机具";
                //返利资格
                dic["RebateQual"] = dic["RebateQual"].ToString() == "1" ? "是" : "否";
                //激活类型
                int ActType = int.Parse(dic["ActType"].ToString());
                if (ActType == 0) dic["ActType"] = "正常激活";
                if (ActType == 1) dic["ActType"] = "首次已激活MPOS";
                if (ActType == 2) dic["ActType"] = "首次已激活KPOS";
                if (ActType == 3) dic["ActType"] = "循环机划拨激活";
                //商户状态
                int MerStatus = int.Parse(dic["MerStatus"].ToString());
                if (MerStatus == 0) dic["MerStatus"] = "正常";
                if (MerStatus == 1) dic["MerStatus"] = "冻结";
                if (MerStatus == 2) dic["MerStatus"] = "关闭";
                //商户激活状态
                int ActiveStatus = int.Parse(dic["ActiveStatus"].ToString());
                if (ActiveStatus == 0) dic["ActiveStatus"] = "未激活";
                if (ActiveStatus == 1) dic["ActiveStatus"] = "已激活";
                if (ActiveStatus == 2) dic["ActiveStatus"] = "SN已返现";
                //商户创客类型
                int MerUserType = int.Parse(dic["MerUserType"].ToString());
                if (MerUserType == 0) dic["MerUserType"] = "非商户型创客";
                if (MerUserType == 1) dic["MerUserType"] = "商户型创客";
                dic.Remove("UserId");
                dic.Remove("TopUserId");
                dic.Remove("MerUserId");
                dic.Remove("SnApplyUserId");
                dic.Remove("SnStoreId");
            }

            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("MerchantNo", "商户编号");
            ReturnFields.Add("MerchantName", "商户名称");
            ReturnFields.Add("MerchantMobile", "商户手机号");
            ReturnFields.Add("KqMerNo", "快钱商户编码");
            ReturnFields.Add("KqSnNo", "快钱SN号");
            ReturnFields.Add("SnType", "机具类型");
            ReturnFields.Add("RebateQual", "返利资格");
            ReturnFields.Add("ActType", "激活类型");
            ReturnFields.Add("MerStatus", "商户状态");
            ReturnFields.Add("ActiveStatus", "商户激活状态");
            ReturnFields.Add("MerMakerCode", "商户创客编码");
            ReturnFields.Add("MerRealName", "商户创客名称");
            ReturnFields.Add("MerUserType", "商户创客类型");
            ReturnFields.Add("MakerCode", "直属创客编号");
            ReturnFields.Add("RealName", "直属创客姓名");
            ReturnFields.Add("TopMakerCode", "顶级创客编码");
            ReturnFields.Add("TopRealName", "顶级创客名称");
            ReturnFields.Add("StoreNo", "SN仓库编号");
            ReturnFields.Add("StoreName", "SN仓库名称");
            ReturnFields.Add("SnApplyMakerCode", "申请创客编号");
            ReturnFields.Add("SnApplyRealName", "申请创客姓名");
            ReturnFields.Add("KqRegTime", "注册时间");
            result.Add("Fields", ReturnFields);
            AddSysLog("0", "PosMerchantInfo", "ExportExcel");
            return Json(result);
        }

        #endregion





        #region 同步交易额

        public IActionResult SycnTradeAmount(string right, int Id = 0)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;

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

        #endregion

        #region 同步交易额

        [HttpPost]
        public string SycnTradeAmountDo(DateTime sdate, DateTime edate, int MerchantId)
        {
            if (sdate.AddMonths(1) < edate)
            {
                return "时间间隔不能超过1个月";
            }
            if (edate >= DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00"))
            {
                return "结束时间只能是今天之前";
            }
            string check = RedisDbconn.Instance.Get<string>("ResetMerchantTradeQueue:" + MerchantId);
            if (!string.IsNullOrEmpty(check))
            {
                return "请稍后再试";
            }
            try
            {
                RedisDbconn.Instance.AddList("ResetMerchantTradeQueue", MerchantId + "#cut#" + sdate.ToString("yyyy-MM-dd HH:mm:ss") + "#cut#" + edate.ToString("yyyy-MM-dd HH:mm:ss"));
                RedisDbconn.Instance.Set("ResetMerchantTradeQueue:" + MerchantId, "wait");
                RedisDbconn.Instance.SetExpire("ResetMerchantTradeQueue:" + MerchantId, 3600);
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计商户的交易额异常");
                return "同步异常";
            }
            return "success";
        }

        #endregion
    }
}
