/*
 * 绑定记录
 */

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 BindRecordController : BaseController
    {
        public BindRecordController(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(BindRecord data, string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            return View();
        }

        #endregion

        #region 根据条件查询绑定记录列表

        /// <summary>
        /// 绑定记录列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(BindRecord data, string MerOperateTypeSelect, string MerStatusSelect, string ProductTypeSelect, string CreateTimeData, int page = 1, int limit = 30)
        {

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

            Fields.Add("AgentName", "1"); //机构名称
            Fields.Add("MerNo", "1"); //商户编号
            Fields.Add("MerName", "1"); //商户名称
            Fields.Add("MerSnNo", "1"); //终端机身号
            Fields.Add("MerNewSnNo", "1"); //新终端机身号
            // Fields.Add("CreateTime", "3"); //创建时间
            Fields.Add("ChannelSerial", "1"); //渠道流水号


            string condition = " and Status>-1";
            //操作类型
            if (!string.IsNullOrEmpty(MerOperateTypeSelect))
            {
                condition += " and MerOperateType=" + MerOperateTypeSelect;
            }
            //商户状态
            if (!string.IsNullOrEmpty(MerStatusSelect))
            {
                condition += " and MerStatus=" + MerStatusSelect;
            }
            //产品类型
            if (!string.IsNullOrEmpty(ProductTypeSelect))
            {
                condition += " and ProductType=" + ProductTypeSelect;
            }
            //绑定时间
            if (!string.IsNullOrEmpty(CreateTimeData))
            {
                string[] datelist = CreateTimeData.Split(new string[] { " - " }, StringSplitOptions.None);
                string start = datelist[0];
                string end = datelist[1];
                condition += " and CreateTime>='" + start + " 00:00:00' and CreateTime<='" + end + " 23:59:59'";
            }


            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.SpTables).IndexData("BindRecord", 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"] = "";
                //产品类型
                dic["ProductType"] = RelationClass.GetKqProductBrandList(dic["ProductType"].ToString());
            }
            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(BindRecord 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("BindRecord", Fields, 0);
            AddSysLog(data.Id.ToString(), "BindRecord", "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;


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

        #endregion

        #region 修改绑定记录

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

            return "success";
        }

        #endregion

        #region 删除绑定记录信息

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

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

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

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcel(BindRecord data, string MerOperateTypeSelect, string MerStatusSelect, string ProductTypeSelect)
        {
            Dictionary<string, string> Fields = new Dictionary<string, string>();
            Fields.Add("AgentName", "1"); //机构名称
            Fields.Add("MerNo", "1"); //商户编号
            Fields.Add("MerName", "1"); //商户名称
            Fields.Add("MerSnNo", "1"); //终端机身号
            Fields.Add("MerNewSnNo", "1"); //新终端机身号
            Fields.Add("CreateTime", "3"); //创建时间
            Fields.Add("ChannelSerial", "1"); //渠道流水号


            string condition = " and Status>-1";
            //操作类型
            if (!string.IsNullOrEmpty(MerOperateTypeSelect))
            {
                condition += " and MerOperateType=" + MerOperateTypeSelect;
            }
            //商户状态
            if (!string.IsNullOrEmpty(MerStatusSelect))
            {
                condition += " and MerStatus=" + MerStatusSelect;
            }
            //产品类型
            if (!string.IsNullOrEmpty(ProductTypeSelect))
            {
                condition += " and ProductType=" + ProductTypeSelect;
            }


            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.SpTables).IndexData("BindRecord", Fields, "Id desc", "0", 1, 20000, condition, "AgentName,MerNo,MerName,MerSnNo,MerOperateDate,MerOperateType,MerStatus,ProductType,MerNewSnNo,Remark,CreateTime,UpdateTime,ChannelSerial", false);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {

            }

            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("AgentName", "机构名称");
            ReturnFields.Add("MerNo", "商户编号");
            ReturnFields.Add("MerName", "商户名称");
            ReturnFields.Add("MerSnNo", "终端机身号");
            ReturnFields.Add("MerOperateDate", "操作时间");
            ReturnFields.Add("MerOperateType", "操作类型");
            ReturnFields.Add("MerStatus", "商户状态");
            ReturnFields.Add("ProductType", "产品类型");
            ReturnFields.Add("MerNewSnNo", "新终端机身号");
            ReturnFields.Add("Remark", "注释");
            ReturnFields.Add("CreateTime", "创建时间");
            ReturnFields.Add("UpdateTime", "更新时间");
            ReturnFields.Add("ChannelSerial", "渠道流水号");

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

        #endregion

    }
}
