/*
 * 兑换机具记录
 */

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

            return View();
        }

        #endregion

        #region 根据条件查询兑换机具记录列表

        /// <summary>
        /// 兑换机具记录列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(StoreSwapSn data, string MakerCode, string RealName, string SnTypeSelect, string SwapStatusSelect, string BizStatusSelect, string RiskStatusSelect, int page = 1, int limit = 30)
        {

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

            Fields.Add("BrandId", "1"); //品牌
            Fields.Add("SnNo", "1"); //SN号
            Fields.Add("StandardDate", "3"); //激活时间
            Fields.Add("SwapSnNo", "1"); //兑换SN号
            Fields.Add("SwapApplyOrderNo", "1"); //兑换申请单号
            Fields.Add("SwapApplyDate", "3"); //兑换时间

            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 + "')";
            }
            //SN类型
            if(!string.IsNullOrEmpty(SnTypeSelect))
            {
                condition += " and SnType=" + SnTypeSelect;
            }
            //兑换状态
            if(!string.IsNullOrEmpty(SwapStatusSelect))
            {
                condition += " and SwapStatus=" + SwapStatusSelect;
            }
            //业务状态
            if(!string.IsNullOrEmpty(BizStatusSelect))
            {
                condition += " and BizStatus=" + BizStatusSelect;
            }
            //风控状态
            if(!string.IsNullOrEmpty(RiskStatusSelect))
            {
                condition += " and RiskStatus=" + RiskStatusSelect;
            }

            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("StoreSwapSn", 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)
            {
                dic["BrandId"] = RelationClass.GetKqProductBrandInfo(int.Parse(dic["BrandId"].ToString()));
                dic["SwapExtProductTypes"] = RelationClass.GetKqProductBrandList(dic["SwapExtProductTypes"].ToString());

                //创客信息
                int UserId = int.Parse(dic["UserId"].ToString());
                Users puser = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
                dic["MakerCode"] = puser.MakerCode;
                dic["MakerName"] = puser.RealName;

                //SN类型
                int SnType = int.Parse(dic["SnType"].ToString());
                if(SnType == 0) dic["SnType"] = "兑换机具";
                if(SnType == 1) dic["SnType"] = "循环机具";

                //兑换状态
                int SwapStatus = int.Parse(dic["SwapStatus"].ToString());
                if(SwapStatus == 0) dic["SwapStatus"] = "未兑换";
                if(SwapStatus == 1) dic["SwapStatus"] = "兑换中";
                if(SwapStatus == 2) dic["SwapStatus"] = "已兑换";
                
                //业务状态
                int BizStatus = int.Parse(dic["BizStatus"].ToString());
                if(BizStatus == 0) dic["BizStatus"] = "正常";
                if(BizStatus == 1) dic["BizStatus"] = "异常";
                
                //风控状态
                int RiskStatus = int.Parse(dic["RiskStatus"].ToString());
                if(RiskStatus == 0) dic["RiskStatus"] = "未风控";
                if(RiskStatus == 1) dic["RiskStatus"] = "已风控";

            }
            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(StoreSwapSn data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();

            Fields.Add("Remark", data.Remark); //备注

            Fields.Add("SeoTitle", data.SeoTitle);
            Fields.Add("SeoKeyword", data.SeoKeyword);
            Fields.Add("SeoDescription", data.SeoDescription);
            int Id = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Add("StoreSwapSn", Fields, 0);
            AddSysLog(data.Id.ToString(), "StoreSwapSn", "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;


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

        #endregion

        #region 修改兑换机具记录

        /// <summary>
        /// 增加或修改兑换机具记录信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Edit(StoreSwapSn data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();

            Fields.Add("Remark", data.Remark); //备注

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

            return "success";
        }

        #endregion

        #region 删除兑换机具记录信息

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

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

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

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcel(StoreSwapSn data, string MakerCode, string RealName, string SnTypeSelect, string SwapStatusSelect, string BizStatusSelect, string RiskStatusSelect)
        {
            Dictionary<string, string> Fields = new Dictionary<string, string>();

            Fields.Add("BrandId", "1"); //品牌
            Fields.Add("SnNo", "1"); //SN号
            Fields.Add("StandardDate", "3"); //激活时间
            Fields.Add("SwapSnNo", "1"); //兑换SN号
            Fields.Add("SwapApplyOrderNo", "1"); //兑换申请单号
            Fields.Add("SwapApplyDate", "3"); //兑换时间

            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 + "')";
            }
            //SN类型
            if(!string.IsNullOrEmpty(SnTypeSelect))
            {
                condition += " and SnType=" + SnTypeSelect;
            }
            //兑换状态
            if(!string.IsNullOrEmpty(SwapStatusSelect))
            {
                condition += " and SwapStatus=" + SwapStatusSelect;
            }
            //业务状态
            if(!string.IsNullOrEmpty(BizStatusSelect))
            {
                condition += " and BizStatus=" + BizStatusSelect;
            }
            //风控状态
            if(!string.IsNullOrEmpty(RiskStatusSelect))
            {
                condition += " and RiskStatus=" + RiskStatusSelect;
            }

            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("StoreSwapSn", Fields, "Id desc", "0", 1, 20000, condition, "UserId,BrandId,SnNo,SnType,StandardDate,PosType,SwapStatus,BizStatus,RiskStatus,SwapExtProductTypes,SwapSnNo,SwapApplyOrderNo,SwapApplyDate,Remark", false);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                dic["BrandId"] = RelationClass.GetKqProductBrandInfo(int.Parse(dic["BrandId"].ToString()));
                dic["SwapExtProductTypes"] = RelationClass.GetKqProductBrandList(dic["SwapExtProductTypes"].ToString());

                //创客信息
                int UserId = int.Parse(dic["UserId"].ToString());
                Users puser = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
                dic["MakerCode"] = puser.MakerCode;
                dic["MakerName"] = puser.RealName;

                //SN类型
                int SnType = int.Parse(dic["SnType"].ToString());
                if(SnType == 0) dic["SnType"] = "兑换机具";
                if(SnType == 1) dic["SnType"] = "循环机具";

                //兑换状态
                int SwapStatus = int.Parse(dic["SwapStatus"].ToString());
                if(SwapStatus == 0) dic["SwapStatus"] = "未兑换";
                if(SwapStatus == 1) dic["SwapStatus"] = "兑换中";
                if(SwapStatus == 2) dic["SwapStatus"] = "已兑换";
                
                //业务状态
                int BizStatus = int.Parse(dic["BizStatus"].ToString());
                if(BizStatus == 0) dic["BizStatus"] = "正常";
                if(BizStatus == 1) dic["BizStatus"] = "异常";
                
                //风控状态
                int RiskStatus = int.Parse(dic["RiskStatus"].ToString());
                if(RiskStatus == 0) dic["RiskStatus"] = "未风控";
                if(RiskStatus == 1) dic["RiskStatus"] = "已风控";

                dic.Remove("UserId");
            }

            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("BrandId", "产品类型");
            ReturnFields.Add("SnNo", "激活SN");
            ReturnFields.Add("SnType", "SN类型");
            ReturnFields.Add("StandardDate", "达标日期");
            ReturnFields.Add("PosType", "POS类型");
            ReturnFields.Add("SwapStatus", "兑换状态");
            ReturnFields.Add("BizStatus", "业务状态");
            ReturnFields.Add("RiskStatus", "风控状态");
            ReturnFields.Add("SwapSnNo", "兑换SN");
            ReturnFields.Add("SwapExtProductTypes", "兑换产品类型");
            ReturnFields.Add("SwapApplyOrderNo", "兑换申请单号");
            ReturnFields.Add("SwapApplyDate", "兑换时间");
            ReturnFields.Add("Remark", "备注");
            result.Add("Fields", ReturnFields);
            AddSysLog("0", "StoreSwapSn", "ExportExcel");
            return Json(result);
        }

        #endregion

    }
}
