/*
 * 产品规则配置
 */

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


            string Condition = "";
            
            if (!string.IsNullOrEmpty(Condition))
            { 
                Condition = Condition.TrimEnd(',');
                Condition = ", where: {" + Condition + "}";
            }
            ViewBag.Condition = Condition;
            return View();
        }

        #endregion

        #region 根据条件查询产品规则配置列表

        /// <summary>
        /// 产品规则配置列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(KqProductRuleSet data, int page = 1, int limit = 30)
        {
            
            Dictionary<string, string> Fields = new Dictionary<string, string>();
            Fields.Add("OrgId", "1");

            
            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("KqProductRuleSet", Fields, "Id desc", "0", page, limit, " and Status>-1");
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                dic["StatusName"] = dic["Status"].ToString() == "1" ? "正常" : "关闭";
dic["CycleIsOtherName"] = dic["CycleIsOther"].ToString() == "1" ? "是" : "否";

            }
            return Json(obj);
        }

        #endregion

        #region 增加产品规则配置

        /// <summary>
        /// 增加或修改产品规则配置信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Add(string right, string OrgId)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;
            ViewBag.OrgId = OrgId;


            return View();
        }

        #endregion

        #region 增加产品规则配置

        /// <summary>
        /// 增加或修改产品规则配置信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Add(KqProductRuleSet data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();
            Fields.Add("OrgId", data.OrgId);

            Fields.Add("Deposit", data.Deposit); //押金
Fields.Add("ActiveCount", data.ActiveCount); //活动政策台数
Fields.Add("ActivePrice", data.ActivePrice); //活动政策价格
Fields.Add("MerStaStartDate", data.MerStaStartDate); //活动开始时间
Fields.Add("MerStaEndDate", data.MerStaEndDate); //活动结束时间
Fields.Add("MerStaProfit", data.MerStaProfit); //达标奖励
Fields.Add("MerStaMonths", data.MerStaMonths); //激活后达标月
Fields.Add("MerStaTradeForMonth", data.MerStaTradeForMonth); //激活后每月达标交易
Fields.Add("ActStartDate", data.ActStartDate); //活动开始时间
Fields.Add("ActEndDate", data.ActEndDate); //活动结束时间
Fields.Add("ActDeposit", data.ActDeposit); //激活押金
Fields.Add("ActFirst", data.ActFirst); //激活首刷
Fields.Add("ActFirstDeposit", data.ActFirstDeposit); //激活首刷押金
Fields.Add("ActPrize", data.ActPrize); //激活奖励金额
Fields.Add("ActPosCount", data.ActPosCount); //激活赠机台数
Fields.Add("OpenStartDate", data.OpenStartDate); //活动开始时间
Fields.Add("OpenEndDate", data.OpenEndDate); //活动结束时间
Fields.Add("OpenStaDays", data.OpenStaDays); //达标天数
Fields.Add("OpenStaTrade", data.OpenStaTrade); //达标交易
Fields.Add("OpenStaPrize", data.OpenStaPrize); //达标奖励1
Fields.Add("OpenStaPrize2", data.OpenStaPrize2); //达标奖励2
Fields.Add("CycleStartDate", data.CycleStartDate); //活动开始时间
Fields.Add("CycleEndDate", data.CycleEndDate); //活动结束时间
Fields.Add("CycleIsOther", data.CycleIsOther); //是否允许划拨给他人
Fields.Add("CyclePosDays", data.CyclePosDays); //领机天数
Fields.Add("PullNewStartDate", data.PullNewStartDate); //活动开始时间
Fields.Add("PullNewEndDate", data.PullNewEndDate); //活动结束时间
Fields.Add("PullNewExtendDays", data.PullNewExtendDays); //延长领机天数
Fields.Add("BigTotalAmount", data.BigTotalAmount); //兑换机起始价
Fields.Add("BigExtendCycleDays", data.BigExtendCycleDays); //延长循环周期天数
Fields.Add("SubsidyStartDate", data.SubsidyStartDate); //活动开始时间
Fields.Add("SubsidyEndDate", data.SubsidyEndDate); //活动结束时间
Fields.Add("FlowCardStartDate", data.FlowCardStartDate); //活动开始时间
Fields.Add("FlowCardEndDate", data.FlowCardEndDate); //活动结束时间
Fields.Add("FlowCardFreeDays", data.FlowCardFreeDays); //免收天数
Fields.Add("FlowCardFee", data.FlowCardFee); //费用
Fields.Add("FeeOfDays", data.FeeOfDays); //扣费频率（天）
Fields.Add("FlowCardDirectProfit", data.FlowCardDirectProfit); //直属上级返佣
Fields.Add("HelpStartDate", data.HelpStartDate); //活动开始时间
Fields.Add("HelpEndDate", data.HelpEndDate); //活动结束时间
Fields.Add("HelpMonths", data.HelpMonths); //计算自然月
Fields.Add("HelpFee", data.HelpFee); //扶植期费率
Fields.Add("HelpSteadyFee", data.HelpSteadyFee); //稳定期费率
Fields.Add("FeeBindDays", data.FeeBindDays); //绑定起天数
Fields.Add("FeeFixed", data.FeeFixed); //固定手续费
Fields.Add("FeeRate", data.FeeRate); //费率

            Fields.Add("SeoTitle", data.SeoTitle);
            Fields.Add("SeoKeyword", data.SeoKeyword);
            Fields.Add("SeoDescription", data.SeoDescription);
            int Id = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Add("KqProductRuleSet", Fields, 0);
            AddSysLog(data.Id.ToString(), "KqProductRuleSet", "add");
            db.SaveChanges();
            
            return "success";
        }

        #endregion

        #region 修改产品规则配置

        /// <summary>
        /// 增加或修改产品规则配置信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Edit(string right, string OrgId, int Id = 0)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;
            ViewBag.OrgId = OrgId;


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

        #endregion

        #region 修改产品规则配置

        /// <summary>
        /// 增加或修改产品规则配置信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Edit(KqProductRuleSet data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();
            Fields.Add("OrgId", data.OrgId);

            Fields.Add("Deposit", data.Deposit); //押金
Fields.Add("ActiveCount", data.ActiveCount); //活动政策台数
Fields.Add("ActivePrice", data.ActivePrice); //活动政策价格
Fields.Add("MerStaStartDate", data.MerStaStartDate); //活动开始时间
Fields.Add("MerStaEndDate", data.MerStaEndDate); //活动结束时间
Fields.Add("MerStaProfit", data.MerStaProfit); //达标奖励
Fields.Add("MerStaMonths", data.MerStaMonths); //激活后达标月
Fields.Add("MerStaTradeForMonth", data.MerStaTradeForMonth); //激活后每月达标交易
Fields.Add("ActStartDate", data.ActStartDate); //活动开始时间
Fields.Add("ActEndDate", data.ActEndDate); //活动结束时间
Fields.Add("ActDeposit", data.ActDeposit); //激活押金
Fields.Add("ActFirst", data.ActFirst); //激活首刷
Fields.Add("ActFirstDeposit", data.ActFirstDeposit); //激活首刷押金
Fields.Add("ActPrize", data.ActPrize); //激活奖励金额
Fields.Add("ActPosCount", data.ActPosCount); //激活赠机台数
Fields.Add("OpenStartDate", data.OpenStartDate); //活动开始时间
Fields.Add("OpenEndDate", data.OpenEndDate); //活动结束时间
Fields.Add("OpenStaDays", data.OpenStaDays); //达标天数
Fields.Add("OpenStaTrade", data.OpenStaTrade); //达标交易
Fields.Add("OpenStaPrize", data.OpenStaPrize); //达标奖励1
Fields.Add("OpenStaPrize2", data.OpenStaPrize2); //达标奖励2
Fields.Add("CycleStartDate", data.CycleStartDate); //活动开始时间
Fields.Add("CycleEndDate", data.CycleEndDate); //活动结束时间
Fields.Add("CycleIsOther", data.CycleIsOther); //是否允许划拨给他人
Fields.Add("CyclePosDays", data.CyclePosDays); //领机天数
Fields.Add("PullNewStartDate", data.PullNewStartDate); //活动开始时间
Fields.Add("PullNewEndDate", data.PullNewEndDate); //活动结束时间
Fields.Add("PullNewExtendDays", data.PullNewExtendDays); //延长领机天数
Fields.Add("BigTotalAmount", data.BigTotalAmount); //兑换机起始价
Fields.Add("BigExtendCycleDays", data.BigExtendCycleDays); //延长循环周期天数
Fields.Add("SubsidyStartDate", data.SubsidyStartDate); //活动开始时间
Fields.Add("SubsidyEndDate", data.SubsidyEndDate); //活动结束时间
Fields.Add("FlowCardStartDate", data.FlowCardStartDate); //活动开始时间
Fields.Add("FlowCardEndDate", data.FlowCardEndDate); //活动结束时间
Fields.Add("FlowCardFreeDays", data.FlowCardFreeDays); //免收天数
Fields.Add("FlowCardFee", data.FlowCardFee); //费用
Fields.Add("FeeOfDays", data.FeeOfDays); //扣费频率（天）
Fields.Add("FlowCardDirectProfit", data.FlowCardDirectProfit); //直属上级返佣
Fields.Add("HelpStartDate", data.HelpStartDate); //活动开始时间
Fields.Add("HelpEndDate", data.HelpEndDate); //活动结束时间
Fields.Add("HelpMonths", data.HelpMonths); //计算自然月
Fields.Add("HelpFee", data.HelpFee); //扶植期费率
Fields.Add("HelpSteadyFee", data.HelpSteadyFee); //稳定期费率
Fields.Add("FeeBindDays", data.FeeBindDays); //绑定起天数
Fields.Add("FeeFixed", data.FeeFixed); //固定手续费
Fields.Add("FeeRate", data.FeeRate); //费率

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

        #endregion

        #region 删除产品规则配置信息

        /// <summary>
        /// 删除产品规则配置信息
        /// </summary>
        /// <returns></returns>
        public string Delete(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id,"KqProductRuleSet","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("KqProductRuleSet", 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,"KqProductRuleSet","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("KqProductRuleSet", 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,"KqProductRuleSet","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("KqProductRuleSet", 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("KqProductRuleSet", Sort, Id);
            
            AddSysLog(Id.ToString(), "KqProductRuleSet", "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.KqProductRuleSet.Add(new KqProductRuleSet()
                {
                    CreateDate = DateTime.Now,
                    UpdateDate = DateTime.Now,
                    
                });
                db.SaveChanges();
            }
            AddSysLog("0", "KqProductRuleSet", "Import");
            return "success";
        }
        #endregion

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcel(KqProductRuleSet data)
        {
            Dictionary<string, string> Fields = new Dictionary<string, string>();
            
            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("KqProductRuleSet", 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["StatusName"] = dic["Status"].ToString() == "1" ? "正常" : "关闭";
dic["CycleIsOtherName"] = dic["CycleIsOther"].ToString() == "1" ? "是" : "否";

            }

            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", "KqProductRuleSet", "ExportExcel");
            return Json(result);
        }

        #endregion

    }
}
