
using PageAdmin.BLL;
using PageAdmin.Common;
using PageAdmin.Utils.ORM;
using PageAdmin.Utils;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace PageAdmin.Web.Areas.E.Views.SubData
{

    //控制器辅助类
    public class SubData
    {
        private MvcControllerContext _MvcConetxt;
        public SubData(MvcControllerContext mvcConetxt)
        {
            _MvcConetxt = mvcConetxt;
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="parentTable"></param>
        /// <param name="parentField"></param>
        /// <param name="parentGuid"></param>
        /// <param name="parentId"></param>
        /// <param name="order"></param>
        /// <param name="pageInfo"></param>
        /// <returns></returns>
        public IEnumerable<dynamic> ListData()
        {
            NameValueCollection form = _MvcConetxt.Data;
            int parentId = StringHelper.Format<int>(form["parentId"]);
            string parentGuid = StringHelper.Format<string>(form["parentGuid"]);
            int pageSize = StringHelper.Format<int>(form["pagesize"]);
            int defaultPageSize = StringHelper.Format<int>(form["_pagesize"]);
            if (pageSize == 0)
            {
                pageSize = defaultPageSize;
            }
            int page = StringHelper.Format<int>(form["page"]);
            string order = form["order"];
            string defaultOrder = form["_order"];
            if (string.IsNullOrEmpty(order))
            {
                order = defaultOrder;
            }
            MvcControllerContext mvcContext = _MvcConetxt;
            string sqlCondition = "parentId=@parentId";
            mvcContext.DbContext.AddDbParamter("@parentId", parentId);
            if (parentId==0)
            {
                sqlCondition += " and parentGuid=@parentGuid";
                mvcContext.DbContext.AddDbParamter("@parentGuid",parentGuid);
            }
            string userName = StringHelper.Format<string>(form["username"]);
            if (!string.IsNullOrEmpty(userName))
            {
                sqlCondition += " and username=@username";
                mvcContext.DbContext.AddDbParamter("@username", userName);
            }
            mvcContext.SqlCondition = sqlCondition;
            mvcContext.ListOrder = order;
            PageInfo pageInfo = mvcContext.PageInfo;
            IPageListBLL listBll = new ListBLL();
            pageInfo.CurrentPage = page;
            pageInfo.PageSize = pageSize;
            return listBll.PageList(mvcContext);
        }

        #region 附属信息的添加
        public TipsInfo Add()
        {
            IAddBLL addBll = new AddBLL();
            return addBll.Add(_MvcConetxt);
        }
        #endregion

        #region 附属信息的删除
        public TipsInfo Delete()
        {
            NameValueCollection form = _MvcConetxt.Data;
            string parentGuid = StringHelper.Format<string>(form["parentGuid"]);
            _MvcConetxt.DbContext.SetDbParamter("parentGuid", parentGuid);
            _MvcConetxt.SqlCondition = "parentGuid=@parentGuid";
            DeleteBLL deleteBll = new DeleteBLL();
            return deleteBll.Delete(_MvcConetxt);
        }
        #endregion


        #region 获取附属信息内容页数据
        public dynamic Detail()
        {

            IDetailBLL detailBll = new DetailBLL();
            dynamic detailData= detailBll.Detail(_MvcConetxt);
            var form = _MvcConetxt.Data;
            string parentGuid = detailData.ParentGuid;
            string parentGuidQueryString = form["parentGuid"];
            if(!parentGuidQueryString.Equals(parentGuid))
            {
                throw new System.Exception("无效的parentGuid参数！");
            }
            return detailBll.Detail(_MvcConetxt);
        }

        #endregion

        #region 附属信息的修改

        public TipsInfo Edit()
        {
            IEditBLL editBll = new EditBLL();
            return editBll.Edit(_MvcConetxt);
        }

        #endregion

    }

}