/*
 * 小仓库
 */

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


            return View();
        }

        #endregion

        #region 根据条件查询小仓库列表

        /// <summary>
        /// 小仓库列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(SmallStoreHouse data, string CreateDateData, int page = 1, int limit = 30)
        {

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

            Fields.Add("StoreNo", "1"); //仓库编号
            Fields.Add("StoreName", "1"); //仓库名称
            Fields.Add("Mobile", "1"); //仓库归属人手机号


            string condition = " and Status>-1";
            //创建时间
            if(!string.IsNullOrEmpty(CreateDateData))
            {
                string[] datelist = CreateDateData.Split(new string[] { " - " }, StringSplitOptions.None);
                string start = datelist[0];
                string end = datelist[1];
                condition += " and CreateDate>='" + start + " 00:00:00' and CreateDate<='" + end + " 23:59:59'";
            }


            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("SmallStoreHouse", 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)
            {
                //出货仓库
                int FromStoreId = int.Parse(function.CheckInt(dic["FromStoreId"].ToString()));
                StoreHouse fromstoreid_StoreHouse = db.StoreHouse.FirstOrDefault(m => m.Id == FromStoreId) ?? new StoreHouse();
                dic["FromStoreIdStoreNo"] = fromstoreid_StoreHouse.StoreNo;
                dic["FromStoreIdStoreName"] = fromstoreid_StoreHouse.StoreName;
                dic.Remove("FromStoreId");

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

            Fields.Add("StoreNo", data.StoreNo); //仓库编号
            Fields.Add("StoreName", data.StoreName); //仓库名称
            Fields.Add("UserId", data.UserId); //仓库归属人
            Fields.Add("BrandId", data.BrandId); //产品类型
            Fields.Add("TotalNum", data.TotalNum); //总库存数
            Fields.Add("LaveNum", data.LaveNum); //剩余库存数
            Fields.Add("Remark", data.Remark); //备注
            Fields.Add("Mobile", data.Mobile); //仓库归属人手机号
            Fields.Add("FromStoreId", data.FromStoreId); //出货仓库

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


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

        #endregion

        #region 修改小仓库

        /// <summary>
        /// 增加或修改小仓库信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Edit(SmallStoreHouse data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();

            Fields.Add("StoreNo", data.StoreNo); //仓库编号
            Fields.Add("StoreName", data.StoreName); //仓库名称
            Fields.Add("UserId", data.UserId); //仓库归属人
            Fields.Add("BrandId", data.BrandId); //产品类型
            Fields.Add("TotalNum", data.TotalNum); //总库存数
            Fields.Add("LaveNum", data.LaveNum); //剩余库存数
            Fields.Add("Remark", data.Remark); //备注
            Fields.Add("Mobile", data.Mobile); //仓库归属人手机号
            Fields.Add("FromStoreId", data.FromStoreId); //出货仓库

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

            return "success";
        }

        #endregion

        #region 删除小仓库信息

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

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

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

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcel(SmallStoreHouse data, string UserIdSelect, string FromStoreIdStoreNo, string FromStoreIdStoreName)
        {
            Dictionary<string, string> Fields = new Dictionary<string, string>();
            Fields.Add("StoreNo", "1"); //仓库编号
            Fields.Add("CreateDate", "3"); //时间
            Fields.Add("StoreName", "1"); //仓库名称
            Fields.Add("BrandId", "2"); //产品类型
            Fields.Add("Mobile", "1"); //仓库归属人手机号


            string condition = " and Status>-1";
            //仓库归属人
            if (!string.IsNullOrEmpty(UserIdSelect))
            {
                condition += " and UserId=" + UserIdSelect;
            }
            //出货仓库编号
            if (!string.IsNullOrEmpty(FromStoreIdStoreNo))
            {
                condition += " and FromStoreId in (select FromStoreId from StoreHouseForStoreNo where StoreNo='" + FromStoreIdStoreNo + "')";
            }
            //出货仓库名称
            if (!string.IsNullOrEmpty(FromStoreIdStoreName))
            {
                condition += " and FromStoreId in (select FromStoreId from StoreHouseForStoreName where StoreName='" + FromStoreIdStoreName + "')";
            }


            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("SmallStoreHouse", Fields, "Id desc", "0", 1, 20000, condition, "StoreNo,StoreName,UserId,BrandId,TotalNum,LaveNum,Mobile,FromStoreId", false);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                //出货仓库
                int FromStoreId = int.Parse(function.CheckInt(dic["FromStoreId"].ToString()));
                StoreHouse fromstoreid_StoreHouse = db.StoreHouse.FirstOrDefault(m => m.Id == FromStoreId) ?? new StoreHouse();
                dic["FromStoreIdStoreNo"] = fromstoreid_StoreHouse.StoreNo;
                dic["FromStoreIdStoreName"] = fromstoreid_StoreHouse.StoreName;
                dic.Remove("FromStoreId");

            }

            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("StoreNo", "仓库编号");
            ReturnFields.Add("StoreName", "仓库名称");
            ReturnFields.Add("UserId", "仓库归属人");
            ReturnFields.Add("BrandId", "产品类型");
            ReturnFields.Add("TotalNum", "总库存数");
            ReturnFields.Add("LaveNum", "剩余库存数");
            ReturnFields.Add("Mobile", "仓库归属人手机号");
            ReturnFields.Add("FromStoreIdStoreNo", "出货仓库编号");
            ReturnFields.Add("FromStoreIdStoreName", "出货仓库名称");

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

        #endregion

    }
}
