using PageAdmin.BLL;
using PageAdmin.Common;using PageAdmin.Utils.ORM;using PageAdmin.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using PageAdmin.Web.Filter;

namespace PageAdmin.Web.Areas.Admin.Controllers
{
    public class FieldController : DefaultBaseController
    {
        FieldBLL _FieldBll;
        public FieldController() : base("pa_field", "System/Table/")
        {

            base.ListDataExecuting = new Action(OnListDataExecuting);
            base.UpdateListExecuting = new Action(SetUpdateListDic);

            base.AddExecuting = OnAddExecuting;
            base.AddViewExecuting = GetDbType;

            base.EditViewExecuting = GetDbType;

            base.AddExecuting += new Action(ExexuceAction);
            base.AddExecuted = UpdateCache;

            base.EditExecuting = new Action(ExexuceAction);
            base.DeleteExecuting = new Action(ExexuceAction);
            base.DeleteExecuted = UpdateCache;
        }

        private void GetDbType()
        {
            ViewBag.DbType = base.MvcContext.DbContext.DbType;
        }
        private void OnAddExecuting()
        {
            string name = base.MvcContext.Data["name"].Trim();
            base.MvcContext.Data["name"] = name;
            _FieldBll = new FieldBLL();
            _FieldBll.CheckExists(base.MvcContext);
        }

        private void ExexuceAction()
        {
            FieldBLL fieldBll = new FieldBLL();

            string actionName = RouteData.Values["action"].ToString();
            switch (actionName)
            {
                case "Add":
                    fieldBll.Add(base.MvcContext);
                    break;
                case "Edit":
                    fieldBll.Edit(base.MvcContext);
                    break;
                case "Delete":
                    base.MvcContext.SqlCondition = "sysField=0";
                    fieldBll.Delete(base.MvcContext, StringHelper.Format<int>(base.MvcContext.Data["id"]));
                    break;
            }
        }

        private void OnListDataExecuting()
        {
            base.MvcContext.ListOrder = "xuhao asc";
            string tableName = System.Web.HttpContext.Current.Request.QueryString["tableName"];
            if (!StringHelper.IsStr(tableName))
            {
                throw new Exception("无效的tableName参数");
            }
            base.MvcContext.SqlCondition = "tableName=@tableName";
            base.MvcContext.DbContext.AddDbParamter("@tableName", tableName);
        }

        //设置更新字典集合
        private void SetUpdateListDic()
        {
            var form = Request.Form;
            DbKeyValueList dic = new DbKeyValueList();
            dic.Add("Xuhao", StringHelper.Format<int>(form["Xuhao"]));
            dic.Add("AdminCanAdd", StringHelper.Format<int>(form["AdminCanAdd"]));
            dic.Add("AdminCanEdit", StringHelper.Format<int>(form["AdminCanEdit"]));
            dic.Add("MemberCanAdd", StringHelper.Format<int>(form["MemberCanAdd"]));
            dic.Add("MemberCanEdit", StringHelper.Format<int>(form["MemberCanEdit"]));
            dic.Add("AnonymousCanAdd", StringHelper.Format<int>(form["AnonymousCanAdd"]));
            dic.Add("AnonymousCanEdit", StringHelper.Format<int>(form["AnonymousCanEdit"]));
            base.UpdateDbKeyValues = dic;
        }

        //更新缓存
        private void UpdateCache()
        {
            CacheHelper.Remove("pa_attachment_table");
            CacheHelper.Remove("pa_sub_table");
        }

    }
}