/*
 * 创客机具延迟变动明细表
 */

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

            return View();
        }

        #endregion

        #region 根据条件查询创客机具延迟变动明细表列表

        /// <summary>
        /// 创客机具延迟变动明细表列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(UserSnDelayChange data, string MakerCode, string RealName, string SnTypeSelect, string SourceSnTypeSelect, 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("SourceSnNo", "1"); //源SN编号
            Fields.Add("SourceSnShipDate", "3"); //源SN发货时间

            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;
            }
            //源SN类型
            if(!string.IsNullOrEmpty(SourceSnTypeSelect))
            {
                condition += " and SourceSnType=" + SourceSnTypeSelect;
            }

            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("UserSnDelayChange", 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["ActivityRuleId"] = RelationClass.GetProfitObjectsActivesInfo(int.Parse(dic["ActivityRuleId"].ToString()));

                //创客
                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 RecUserId = int.Parse(dic["RecUserId"].ToString());
                Users ruser = db.Users.FirstOrDefault(m => m.Id == RecUserId) ?? new Users();
                dic["RecMakerCode"] = ruser.MakerCode;
                dic["RecRealName"] = ruser.RealName;
                
                //发货仓库
                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;

                //源SN发货仓库
                int SourceStoreId = int.Parse(dic["SourceStoreId"].ToString());
                StoreHouse sstore = db.StoreHouse.FirstOrDefault(m => m.Id == SourceStoreId) ?? new StoreHouse();
                dic["SourceStoreNo"] = sstore.StoreNo;
                dic["SourceStoreName"] = sstore.StoreName;

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

                //源SN类型
                int SourceSnType = int.Parse(dic["SourceSnType"].ToString());
                if(SourceSnType == 0) dic["SourceSnType"] = "兑换机具";
                if(SourceSnType == 1) dic["SourceSnTypeSelect"] = "循环机具";
            }
            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(UserSnDelayChange 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("UserSnDelayChange", Fields, 0);
            AddSysLog(data.Id.ToString(), "UserSnDelayChange", "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;


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

        #endregion

        #region 修改创客机具延迟变动明细表

        /// <summary>
        /// 增加或修改创客机具延迟变动明细表信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Edit(UserSnDelayChange 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("UserSnDelayChange", Fields, data.Id);
            AddSysLog(data.Id.ToString(), "UserSnDelayChange", "update");
            db.SaveChanges();

            return "success";
        }

        #endregion

        #region 删除创客机具延迟变动明细表信息

        /// <summary>
        /// 删除创客机具延迟变动明细表信息
        /// </summary>
        /// <returns></returns>
        public string Delete(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "UserSnDelayChange", "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("UserSnDelayChange", 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, "UserSnDelayChange", "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("UserSnDelayChange", 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, "UserSnDelayChange", "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("UserSnDelayChange", 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("UserSnDelayChange", Sort, Id);

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

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

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcel(UserSnDelayChange data)
        {
            Dictionary<string, string> Fields = new Dictionary<string, string>();
            Fields.Add("CreateDate", "3"); //时间
            Fields.Add("BrandId", "1"); //品牌
            Fields.Add("SnNo", "2"); //SN编号
            Fields.Add("SnType", "1"); //SN机具类型
            Fields.Add("SourceSnNo", "2"); //源SN编号
            Fields.Add("SourceSnType", "1"); //源SN类型
            Fields.Add("SourceSnShipDate", "3"); //源SN发货时间

            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("UserSnDelayChange", Fields, "Id desc", "0", 1, 20000, "", "", false);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                dic["UserId"] = RelationClass.GetUsersInfo(int.Parse(dic["UserId"].ToString()));
                dic["BrandId"] = RelationClass.GetKqProductBrandInfo(int.Parse(dic["BrandId"].ToString()));
                dic["SnStoreId"] = RelationClass.GetStoreHouseInfo(int.Parse(dic["SnStoreId"].ToString()));
                dic["RecUserId"] = RelationClass.GetUsersInfo(int.Parse(dic["RecUserId"].ToString()));
                dic["ActivityRuleId"] = RelationClass.GetProfitObjectsActivesInfo(int.Parse(dic["ActivityRuleId"].ToString()));
                dic["SourceStoreId"] = RelationClass.GetStoreHouseInfo(int.Parse(dic["SourceStoreId"].ToString()));

            }

            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>();

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

        #endregion

    }
}
