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

namespace PageAdmin.Web.Areas.Admin.Controllers
{
    public class AttachmentDataAdminController : CustomDataBaseController
    {
        public AttachmentDataAdminController() : base("", "System/Table/AttachmentTable/")
        {
            base.MvcContext.TableType = TableTypeEnum.CustomAttachmentTable;
            base.ListDataExecuting = GetWhere;
            base.CheckTable();
        }

        //构造查询条件
        public void GetWhere()
        {
            var form = Request.QueryString;
            string where="";
            string currentState = form["state"];
            if (!string.IsNullOrEmpty(currentState))
            {
                if (StringHelper.IsInt(currentState))
                {
                    where += "[state]=" + currentState;
                }
            }

            if (!string.IsNullOrEmpty(form["thedate"]))
            {
                string[] thedateParams = form["thedate"].Split(',');
                if (thedateParams.Length == 2 && StringHelper.IsDate(thedateParams[0]) && StringHelper.IsDate(thedateParams[1]))
                {
                    if (where != "")
                    {
                        where += " and ";
                    }
                    where += "thedate between '" + thedateParams[0] + "' and '" + thedateParams[1] + "'";
                }
            }
            if (!string.IsNullOrEmpty(form["username"]))
            {
                if (where != "")
                {
                    where += " and ";
                }
                where += "[username] like '%" + StringHelper.SqlFormat(form["username"]) + "%'";
            }
            if (!string.IsNullOrEmpty(form["path"]))
            {
                if (where != "")
                {
                    where += " and ";
                }
                where += "[path] like '%" + StringHelper.SqlFormat(form["path"]) + "%'";
            }
            base.MvcContext.SqlCondition = where;
        }


        public override JsonResult Delete(FormCollection form)
        {
            AttachmentDataBLL bll = new AttachmentDataBLL();
            MvcContext.Data = form;
            int id = StringHelper.Format<int>(base.MvcContext.Data["id"]);
            bll.Drop(base.MvcContext.Table, id);
            var tipsInfo = MvcContext.TipsInfo;
            return base.Json(tipsInfo);
        }


        /// <summary>
        /// 清理无效的附件
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [AjaxOnly]
        [HttpPost]
        public JsonResult ClearData(FormCollection form)
        {
            base.MvcContext.Data = form;
            AttachmentDataBLL bll = new AttachmentDataBLL();
            int state = StringHelper.Format<int>(Request.QueryString["state"]);
            if (state <= 0)
            {
                return Json(bll.ClearData(base.MvcContext.Table, state));
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 统计附件表的记录
        /// </summary>
        /// <returns></returns>
        [AjaxOnly]
        [HttpGet]
        public JsonResult Count()
        {
            AttachmentDataBLL bll = new AttachmentDataBLL();
            return Json(bll.Count(base.MvcContext.Table), JsonRequestBehavior.AllowGet);
        }
    }
}

