/*
 * 仓库库存变动记录
 */

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

            return View();
        }

        #endregion

        #region 根据条件查询仓库库存变动记录列表

        /// <summary>
        /// 仓库库存变动记录列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(StoreStockChange data, string StoreIdStoreNo, string StoreIdStoreName, string BrandIdSelect, string TransTypeSelect, string StockOpDirectSelect, string SnStatusSelect, string BindStatusSelect, string BindMerchantMerchantNo, string BindMerchantMerchantName, string ActiveStatusSelect, string BrandTypeSelect, string CreateDateData, int page = 1, int limit = 30)
        {

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

            Fields.Add("SnNo", "1"); //SN编号


            string condition = " and Status>-1";
            //仓库编号
            if (!string.IsNullOrEmpty(StoreIdStoreNo))
            {
                condition += " and StoreId in (select StoreId from StoreForCode where Code='" + StoreIdStoreNo + "')";
            }
            //仓库名称
            if (!string.IsNullOrEmpty(StoreIdStoreName))
            {
                condition += " and StoreId in (select StoreId from StoreForName where Name='" + StoreIdStoreName + "')";
            }
            //产品类型
            if (!string.IsNullOrEmpty(BrandIdSelect))
            {
                condition += " and BrandId=" + BrandIdSelect;
            }
            //交易类型
            if (!string.IsNullOrEmpty(TransTypeSelect))
            {
                condition += " and TransType=" + TransTypeSelect;
            }
            //库存操作方向
            if (!string.IsNullOrEmpty(StockOpDirectSelect))
            {
                condition += " and StockOpDirect=" + StockOpDirectSelect;
            }
            //SN状态
            if (!string.IsNullOrEmpty(SnStatusSelect))
            {
                condition += " and SnStatus=" + SnStatusSelect;
            }
            //绑定状态
            if (!string.IsNullOrEmpty(BindStatusSelect))
            {
                condition += " and BindStatus=" + BindStatusSelect;
            }
            //绑定商户商户编号
            if (!string.IsNullOrEmpty(BindMerchantMerchantNo))
            {
                condition += " and BindMerchant in (select BindMerchant from MerchantForCode where Code='" + BindMerchantMerchantNo + "')";
            }
            //绑定商户商户姓名
            if (!string.IsNullOrEmpty(BindMerchantMerchantName))
            {
                condition += " and BindMerchant in (select BindMerchant from MerchantForName where Name='" + BindMerchantMerchantName + "')";
            }
            //激活状态
            if (!string.IsNullOrEmpty(ActiveStatusSelect))
            {
                condition += " and ActiveStatus=" + ActiveStatusSelect;
            }
            //品牌类型
            if (!string.IsNullOrEmpty(BrandTypeSelect))
            {
                condition += " and BrandType=" + BrandTypeSelect;
            }
            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("StoreStockChange", 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 StoreId = int.Parse(function.CheckInt(dic["StoreId"].ToString()));
                StoreHouse storeid_StoreHouse = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
                dic["StoreIdStoreNo"] = storeid_StoreHouse.StoreNo;
                dic["StoreIdStoreName"] = storeid_StoreHouse.StoreName;
                dic.Remove("StoreId");
                //产品类型
                dic["BrandId"] = RelationClass.GetKqProductBrandInfo(int.Parse(dic["BrandId"].ToString()));
                //交易类型
                int TransType = int.Parse(dic["TransType"].ToString());
                if (TransType == 1) dic["TransType"] = "调拨";
                if (TransType == 2) dic["TransType"] = "出货";
                if (TransType == 3) dic["TransType"] = "退货";
                if (TransType == 0) dic["TransType"] = "采购";
                if (TransType == 4) dic["TransType"] = "驳回";
                if (TransType == 5) dic["TransType"] = "回仓退款";
                //库存操作方向
                int StockOpDirect = int.Parse(dic["StockOpDirect"].ToString());
                if (StockOpDirect == 0) dic["StockOpDirect"] = "入库";
                if (StockOpDirect == 1) dic["StockOpDirect"] = "出库";
                //SN状态
                int SnStatus = int.Parse(dic["SnStatus"].ToString());
                if (SnStatus == 0) dic["SnStatus"] = "未拨出";
                if (SnStatus == 1) dic["SnStatus"] = "已拨出";
                //绑定状态
                int BindStatus = int.Parse(dic["BindStatus"].ToString());
                if (BindStatus == 0) dic["BindStatus"] = "未绑定";
                if (BindStatus == 1) dic["BindStatus"] = "已绑定";
                //绑定商户
                int BindMerchant = int.Parse(function.CheckInt(dic["BindMerchant"].ToString()));
                MerchantInfo bindmerchant_MerchantInfo = db.MerchantInfo.FirstOrDefault(m => m.Id == BindMerchant) ?? new MerchantInfo();
                dic["BindMerchantMerchantNo"] = "";
                dic["BindMerchantMerchantName"] = bindmerchant_MerchantInfo.Name;
                dic.Remove("BindMerchant");
                //激活状态
                int ActiveStatus = int.Parse(dic["ActiveStatus"].ToString());
                if (ActiveStatus == 0) dic["ActiveStatus"] = "未激活";
                if (ActiveStatus == 1) dic["ActiveStatus"] = "已激活";
                //品牌类型
                string BrandType = dic["BrandType"].ToString();
                if (BrandType == "KysSignPos") dic["BrandType"] = "快益刷电签POS";
                if (BrandType == "KssSignPos") dic["BrandType"] = "快闪刷电签POS";
                if (BrandType == "") dic["BrandType"] = "";

            }
            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(StoreStockChange 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("StoreStockChange", Fields, 0);
            AddSysLog(data.Id.ToString(), "StoreStockChange", "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;


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

        #endregion

        #region 修改仓库库存变动记录

        /// <summary>
        /// 增加或修改仓库库存变动记录信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Edit(StoreStockChange 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("StoreStockChange", Fields, data.Id);
            AddSysLog(data.Id.ToString(), "StoreStockChange", "update");
            db.SaveChanges();

            return "success";
        }

        #endregion

        #region 删除仓库库存变动记录信息

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

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

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

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcel(StoreStockChange data, string StoreIdStoreNo, string StoreIdStoreName, string BrandIdSelect, string TransTypeSelect, string StockOpDirectSelect, string SnStatusSelect, string BindStatusSelect, string BindMerchantMerchantNo, string BindMerchantMerchantName, string ActiveStatusSelect, string BrandTypeSelect)
        {
            Dictionary<string, string> Fields = new Dictionary<string, string>();
            Fields.Add("CreateDate", "3"); //时间
            Fields.Add("SnNo", "1"); //SN编号


            string condition = " and Status>-1";
            //仓库编号
            if (!string.IsNullOrEmpty(StoreIdStoreNo))
            {
                condition += " and StoreId in (select StoreId from StoreForCode where Code='" + StoreIdStoreNo + "')";
            }
            //仓库名称
            if (!string.IsNullOrEmpty(StoreIdStoreName))
            {
                condition += " and StoreId in (select StoreId from StoreForName where Name='" + StoreIdStoreName + "')";
            }
            //产品类型
            if (!string.IsNullOrEmpty(BrandIdSelect))
            {
                condition += " and BrandId=" + BrandIdSelect;
            }
            //交易类型
            if (!string.IsNullOrEmpty(TransTypeSelect))
            {
                condition += " and TransType=" + TransTypeSelect;
            }
            //库存操作方向
            if (!string.IsNullOrEmpty(StockOpDirectSelect))
            {
                condition += " and StockOpDirect=" + StockOpDirectSelect;
            }
            //SN状态
            if (!string.IsNullOrEmpty(SnStatusSelect))
            {
                condition += " and SnStatus=" + SnStatusSelect;
            }
            //绑定状态
            if (!string.IsNullOrEmpty(BindStatusSelect))
            {
                condition += " and BindStatus=" + BindStatusSelect;
            }
            //绑定商户商户编号
            if (!string.IsNullOrEmpty(BindMerchantMerchantNo))
            {
                condition += " and BindMerchant in (select BindMerchant from MerchantForCode where Code='" + BindMerchantMerchantNo + "')";
            }
            //绑定商户商户姓名
            if (!string.IsNullOrEmpty(BindMerchantMerchantName))
            {
                condition += " and BindMerchant in (select BindMerchant from MerchantForName where Name='" + BindMerchantMerchantName + "')";
            }
            //激活状态
            if (!string.IsNullOrEmpty(ActiveStatusSelect))
            {
                condition += " and ActiveStatus=" + ActiveStatusSelect;
            }
            //品牌类型
            if (!string.IsNullOrEmpty(BrandTypeSelect))
            {
                condition += " and BrandType=" + BrandTypeSelect;
            }


            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("StoreStockChange", Fields, "Id desc", "0", 1, 20000, condition, "StoreId,BrandId,TransType,SnNo,StockOpDirect,SnStatus,BindStatus,BindMerchant,ActiveStatus,ExpressNo,BrandType", false);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                //仓库
                int StoreId = int.Parse(function.CheckInt(dic["StoreId"].ToString()));
                StoreHouse storeid_StoreHouse = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
                dic["StoreIdStoreNo"] = storeid_StoreHouse.StoreNo;
                dic["StoreIdStoreName"] = storeid_StoreHouse.StoreName;
                dic.Remove("StoreId");
                //产品类型
                dic["BrandId"] = RelationClass.GetKqProductBrandInfo(int.Parse(dic["BrandId"].ToString()));
                //交易类型
                int TransType = int.Parse(dic["TransType"].ToString());
                if (TransType == 1) dic["TransType"] = "调拨";
                if (TransType == 2) dic["TransType"] = "出货";
                if (TransType == 3) dic["TransType"] = "退货";
                if (TransType == 0) dic["TransType"] = "采购";
                //库存操作方向
                int StockOpDirect = int.Parse(dic["StockOpDirect"].ToString());
                if (StockOpDirect == 0) dic["StockOpDirect"] = "入库";
                if (StockOpDirect == 1) dic["StockOpDirect"] = "出库";
                //SN状态
                int SnStatus = int.Parse(dic["SnStatus"].ToString());
                if (SnStatus == 0) dic["SnStatus"] = "未拨出";
                if (SnStatus == 1) dic["SnStatus"] = "已拨出";
                //绑定状态
                int BindStatus = int.Parse(dic["BindStatus"].ToString());
                if (BindStatus == 0) dic["BindStatus"] = "未绑定";
                if (BindStatus == 1) dic["BindStatus"] = "已绑定";
                //绑定商户
                int BindMerchant = int.Parse(function.CheckInt(dic["BindMerchant"].ToString()));
                MerchantInfo bindmerchant_MerchantInfo = db.MerchantInfo.FirstOrDefault(m => m.Id == BindMerchant) ?? new MerchantInfo();
                dic["BindMerchantMerchantNo"] = "";
                dic["BindMerchantMerchantName"] = bindmerchant_MerchantInfo.Name;
                dic.Remove("BindMerchant");
                //激活状态
                int ActiveStatus = int.Parse(dic["ActiveStatus"].ToString());
                if (ActiveStatus == 0) dic["ActiveStatus"] = "未激活";
                if (ActiveStatus == 1) dic["ActiveStatus"] = "已激活";
                //品牌类型
                string BrandType = dic["BrandType"].ToString();
                if (BrandType == "KysSignPos") dic["BrandType"] = "快益刷电签POS";
                if (BrandType == "KssSignPos") dic["BrandType"] = "快闪刷电签POS";
                if (BrandType == "") dic["BrandType"] = "";

            }

            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("StoreIdStoreNo", "仓库编号");
            ReturnFields.Add("StoreIdStoreName", "仓库名称");
            ReturnFields.Add("BrandId", "产品类型");
            ReturnFields.Add("TransType", "交易类型");
            ReturnFields.Add("SnNo", "SN编号");
            ReturnFields.Add("StockOpDirect", "库存操作方向");
            ReturnFields.Add("SnStatus", "SN状态");
            ReturnFields.Add("BindStatus", "绑定状态");
            ReturnFields.Add("BindMerchantMerchantNo", "绑定商户编号");
            ReturnFields.Add("BindMerchantMerchantName", "绑定商户姓名");
            ReturnFields.Add("ActiveStatus", "激活状态");
            ReturnFields.Add("ExpressNo", "快递单号");
            ReturnFields.Add("BrandType", "品牌类型");
            ReturnFields.Add("CreateDate", "变动时间");

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

        #endregion

    }
}
