/*
 * 激活记录
 */

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.SpModels;
using Library;
using LitJson;
using MySystemLib;

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

        #region 激活记录列表

        /// <summary>
        /// 根据条件查询激活记录列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index(ActivateRecord data, string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            return View();
        }

        #endregion

        #region 根据条件查询激活记录列表

        /// <summary>
        /// 激活记录列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(ActivateRecord data, string ActivateStatusSelect, string ProductTypeSelect, string ActivateDateData, int page = 1, int limit = 30)
        {

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

            Fields.Add("MerNo", "1"); //商户编号
            Fields.Add("MerName", "1"); //商户名称
            Fields.Add("MerMobile", "1"); //商户手机号
            Fields.Add("SnNo", "1"); //终端机身号
            Fields.Add("ChannelSerial", "1"); //渠道流水号
            Fields.Add("ProductType", "0"); //产品类型


            string condition = " and Status>-1";
            //激活状态
            if (!string.IsNullOrEmpty(ActivateStatusSelect))
            {
                condition += " and ActivateStatus=" + ActivateStatusSelect;
            }
            //产品类型
            if (!string.IsNullOrEmpty(ProductTypeSelect))
            {
                condition += " and ProductType=" + ProductTypeSelect;
            }
            //创建时间
            if (!string.IsNullOrEmpty(ActivateDateData))
            {
                string[] datelist = ActivateDateData.Split(new string[] { " - " }, StringSplitOptions.None);
                string start = datelist[0];
                string end = datelist[1];
                condition += " and ActivateDate>='" + start + " 00:00:00' and ActivateDate<='" + end + " 23:59:59'";
            }


            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.SpTables).IndexData("ActivateRecord", 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)
            {
                //状态
                string Status = dic["Status"].ToString();
                if (Status == "1") dic["Status"] = "待同步";
                if (Status == "2") dic["Status"] = "已同步";
                if (Status == "") dic["Status"] = "";
                //激活状态
                string ActivateStatus = dic["ActivateStatus"].ToString();
                if (ActivateStatus == "0") dic["ActivateStatus"] = "未激活";
                if (ActivateStatus == "1") dic["ActivateStatus"] = "激活";
                //产品类型
                dic["ProductType"] = RelationClass.GetKqProductBrandList(dic["ProductType"].ToString()); 
                //押金
                string SeoTitle = dic["SeoTitle"].ToString();
                if (SeoTitle.Length <= 3)
                {
                    dic["SeoTitle"] = SeoTitle;
                }
                if (SeoTitle.Length == 4)
                {
                    dic["SeoTitle"] = SeoTitle.Substring(0,2);
                }
                if (SeoTitle.Length == 5)
                {
                    dic["SeoTitle"] = SeoTitle.Substring(0,3);
                }
                // //产品类型
                // string ProductType = dic["ProductType"].ToString();
                // if (ProductType == "KYSPOS") dic["ProductType"] = "快益刷";
                // if (ProductType == "KPOS") dic["ProductType"] = "快联盟大POS";
                // if (ProductType == "SIGNPOS") dic["ProductType"] = "快联盟电签POS";
                // if (ProductType == "SUPPOS") dic["ProductType"] = "超级Mpos";
                // if (ProductType == "") dic["ProductType"] = "";

            }
            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(ActivateRecord 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.SpTables).Add("ActivateRecord", Fields, 0);
            AddSysLog(data.Id.ToString(), "ActivateRecord", "add");
            spdb.SaveChanges();

            return "success";
        }

        #endregion

        #region 修改激活记录

        /// <summary>
        /// 增加或修改激活记录信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Edit(string right, int Id = 0)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


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

        #endregion

        #region 修改激活记录

        /// <summary>
        /// 增加或修改激活记录信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Edit(ActivateRecord 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.SpTables).Edit("ActivateRecord", Fields, data.Id);
            AddSysLog(data.Id.ToString(), "ActivateRecord", "update");
            spdb.SaveChanges();

            return "success";
        }

        #endregion

        #region 删除激活记录信息

        /// <summary>
        /// 删除激活记录信息
        /// </summary>
        /// <returns></returns>
        public string Delete(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "ActivateRecord", "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.SpTables).Edit("ActivateRecord", Fields, id);
            }
            spdb.SaveChanges();

            return "success";
        }

        #endregion

        #region 开启

        /// <summary>
        /// 开启
        /// </summary>
        /// <returns></returns>
        public string Open(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "ActivateRecord", "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.SpTables).Edit("ActivateRecord", Fields, id);
            }
            spdb.SaveChanges();
            return "success";
        }

        #endregion

        #region 关闭

        /// <summary>
        /// 关闭
        /// </summary>
        /// <returns></returns>
        public string Close(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "ActivateRecord", "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.SpTables).Edit("ActivateRecord", Fields, id);
            }
            spdb.SaveChanges();
            return "success";
        }

        #endregion

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

            AddSysLog(Id.ToString(), "ActivateRecord", "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];

                spdb.ActivateRecord.Add(new ActivateRecord()
                {
                    CreateDate = DateTime.Now,
                    UpdateDate = DateTime.Now,

                });
                spdb.SaveChanges();
            }
            AddSysLog("0", "ActivateRecord", "Import");
            return "success";
        }
        #endregion

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcel(ActivateRecord data, string ActivateStatusSelect, string ProductTypeSelect)
        {
            Dictionary<string, string> Fields = new Dictionary<string, string>();
            Fields.Add("MerNo", "1"); //商户编号
            Fields.Add("MerName", "1"); //商户名称
            Fields.Add("MerMobile", "1"); //商户手机号
            Fields.Add("SnNo", "1"); //终端机身号
            Fields.Add("ChannelSerial", "1"); //渠道流水号


            string condition = " and Status>-1";
            //激活状态
            if (!string.IsNullOrEmpty(ActivateStatusSelect))
            {
                condition += " and ActivateStatus=" + ActivateStatusSelect;
            }
            //产品类型
            if (!string.IsNullOrEmpty(ProductTypeSelect))
            {
                condition += " and ProductType=" + ProductTypeSelect;
            }


            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.SpTables).IndexData("ActivateRecord", Fields, "Id desc", "0", 1, 20000, condition, "MerNo,MerName,MerMobile,SnNo,MerRegDate,AssessMonth,AssessMonthCount,ActivateStatus,ActivateDate,MerRealName,MerIdcardNo,Remark,AgentNo,ProductType,ChannelSerial", false);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                //激活状态
                string ActivateStatus = dic["ActivateStatus"].ToString();
                if (ActivateStatus == "0") dic["ActivateStatus"] = "未激活";
                if (ActivateStatus == "1") dic["ActivateStatus"] = "激活";
                //产品类型
                string ProductType = dic["ProductType"].ToString();
                if (ProductType == "KYSPOS") dic["ProductType"] = "快益刷";
                if (ProductType == "KPOS") dic["ProductType"] = "快联盟大POS";
                if (ProductType == "SIGNPOS") dic["ProductType"] = "快联盟电签POS";
                if (ProductType == "SUPPOS") dic["ProductType"] = "超级Mpos";
                if (ProductType == "") dic["ProductType"] = "";

            }

            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("MerNo", "商户编号");
            ReturnFields.Add("MerName", "商户名称");
            ReturnFields.Add("MerMobile", "商户手机号");
            ReturnFields.Add("SnNo", "终端机身号");
            ReturnFields.Add("MerRegDate", "注册日期");
            ReturnFields.Add("AssessMonth", "考核月");
            ReturnFields.Add("AssessMonthCount", "考核第几月");
            ReturnFields.Add("ActivateStatus", "激活状态");
            ReturnFields.Add("ActivateDate", "激活日期");
            ReturnFields.Add("MerRealName", "商户真实姓名");
            ReturnFields.Add("MerIdcardNo", "商户身份证号");
            ReturnFields.Add("Remark", "备注");
            ReturnFields.Add("AgentNo", "代理商编号");
            ReturnFields.Add("ProductType", "产品类型");
            ReturnFields.Add("ChannelSerial", "渠道流水号");

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

        #endregion

    }
}
