/*
 * 创客商城提大货SN
 */

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 UserMallLargeSnController : BaseController
    {
        public UserMallLargeSnController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
            OtherMySqlConn.connstr = ConfigurationManager.AppSettings["SqlConnStr"].ToString();
        }

        #region 创客商城提大货SN列表

        /// <summary>
        /// 根据条件查询创客商城提大货SN列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index(UserMallLargeSn data, string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;

            return View();
        }

        #endregion

        #region 根据条件查询创客商城提大货SN列表

        /// <summary>
        /// 创客商城提大货SN列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(UserMallLargeSn data, string MakerCode, string RealName, string SnStatusSelect, string SnTypeSelect, 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("SnShipDate", "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(SnStatusSelect))
            {
                condition += " and SnStatus=" + SnStatusSelect;
            }
            //SN类型
            if(!string.IsNullOrEmpty(SnTypeSelect))
            {
                condition += " and SnType=" + SnTypeSelect;
            }

            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("UserMallLargeSn", 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()));
                //创客
                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;

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

                //SN状态
                int SnStatus = int.Parse(dic["SnStatus"].ToString());
                if(SnStatus == 0) dic["SnStatus"] = "未延期";
                if(SnStatus == 1) dic["SnStatus"] = "已延期";
                if(SnStatus == 2) dic["SnStatus"] = "已退货";
                
                //SN类型
                int SnType = int.Parse(dic["SnType"].ToString());
                if(SnType == 0) dic["SnType"] = "兑换机具";
                if(SnType == 1) dic["SnType"] = "循环机具";
            }
            return Json(obj);
        }

        #endregion

        #region 增加创客商城提大货SN

        /// <summary>
        /// 增加或修改创客商城提大货SN信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Add(string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            return View();
        }

        #endregion

        #region 增加创客商城提大货SN

        /// <summary>
        /// 增加或修改创客商城提大货SN信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Add(UserMallLargeSn 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("UserMallLargeSn", Fields, 0);
            AddSysLog(data.Id.ToString(), "UserMallLargeSn", "add");
            db.SaveChanges();

            return "success";
        }

        #endregion

        #region 修改创客商城提大货SN

        /// <summary>
        /// 增加或修改创客商城提大货SN信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Edit(string right, int Id = 0)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


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

        #endregion

        #region 修改创客商城提大货SN

        /// <summary>
        /// 增加或修改创客商城提大货SN信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Edit(UserMallLargeSn 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("UserMallLargeSn", Fields, data.Id);
            AddSysLog(data.Id.ToString(), "UserMallLargeSn", "update");
            db.SaveChanges();

            return "success";
        }

        #endregion

        #region 删除创客商城提大货SN信息

        /// <summary>
        /// 删除创客商城提大货SN信息
        /// </summary>
        /// <returns></returns>
        public string Delete(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "UserMallLargeSn", "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("UserMallLargeSn", 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, "UserMallLargeSn", "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("UserMallLargeSn", 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, "UserMallLargeSn", "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("UserMallLargeSn", 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("UserMallLargeSn", Sort, Id);

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

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

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcel(UserMallLargeSn data)
        {
            Dictionary<string, string> Fields = new Dictionary<string, string>();
            Fields.Add("UserId", "1"); //创客
            Fields.Add("CreateDate", "3"); //时间
            Fields.Add("BrandId", "1"); //品牌
            Fields.Add("SnStatus", "1"); //SN状态
            Fields.Add("SnNo", "2"); //SN编号
            Fields.Add("SnType", "1"); //SN类型
            Fields.Add("SnShipDate", "3"); //SN发货时间

            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("UserMallLargeSn", 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["StoreId"] = RelationClass.GetStoreHouseInfo(int.Parse(dic["StoreId"].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", "UserMallLargeSn", "ExportExcel");
            return Json(result);
        }

        #endregion

    }
}
