using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QMFrameWork.WebUI.Attribute;
using QMFrameWork.WebUI.DataSource;
using QMFrameWork.Data;
using QMAPP.Entity.Sys;
using QMAPP.Web.Models.Sys;
using QMAPP.Common.Web.Controllers;
using QMFrameWork.Common.Serialization;

namespace QMAPP.Web.Controllers
{
    ///</summary>
    /// 作    用:文件管理控制层
    /// 作    者:王丹丹
    /// 编写日期:2015年03月17日
    ///</summary>
    public class FileManageController : QController
    {
        #region 文件类别管理

        #region 编辑
        /// <summary>
        /// 编辑载入
        /// </summary>
        /// <returns>处理结果</returns>
        [HandleException]
        public ActionResult FileTypeEdit()
        {
            FileTypeModel model = new FileTypeModel();
            string ID = Request.Params["TYPENO"];
            FileType entity = new FileType();
            try
            {
                if (string.IsNullOrEmpty(ID) == false)
                {
                    //修改获取原数据
                    entity.TYPENO = ID; 

                    #region 调用服务
                    QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
                    entity = agent.InvokeServiceFunction<FileType>(QMAPP.ServicesAgent.SystemService.FileTypeBLL_Get.ToString(), entity);
                    #endregion  
                     
                    model = CopyToModel<FileTypeModel, FileType>(entity);
                }
                return View(model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns>处理结果</returns>
        [HttpPost]
        [HandleException]
        public ActionResult Save(FileTypeModel saveModel)
        {
            try
            {
                FileType entity = new FileType(); ;
                int count = 0;//是否保存成功
                TryUpdateModel<FileTypeModel>(saveModel);
                //获取保存信息
                entity = CopyToModel<FileType, FileTypeModel>(saveModel);
                try
                {
                    //新增
                    if (string.IsNullOrEmpty(entity.TYPENO) == true)
                    {
                        #region 调用服务
                        QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
                        count = agent.InvokeServiceFunction<int>(QMAPP.ServicesAgent.SystemService.FileTypeBLL_Insert.ToString(), entity);
                        #endregion  
                    }
                    //修改
                    else
                    {
                        #region 调用服务
                        QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
                        //修改
                        count = agent.InvokeServiceFunction<int>(QMAPP.ServicesAgent.SystemService.FileTypeBLL_Update.ToString(), entity);
                        #endregion  
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                if (count == -1)
                {
                    return this.GetJsViewResult(string.Format("parent.Search();parent.showTitle('{0}');parent.closeAppWindow1();", AppResource.FileTypeIsHave));
                }
                else
                {
                    return this.GetJsViewResult(string.Format("parent.Search();parent.showTitle('{0}');parent.closeAppWindow1();", AppResource.SaveMessge));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns>结果</returns>
        [HttpPost]
        [HandleException]
        public ActionResult Delete()
        {
            FileType file = new FileType();
            IList<FileInfo> listModel = new List<FileInfo>();
            string selectKey = Request.Params["ids"];
            int count = 0;
            try
            {
                #region 调用服务
                QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
                count = agent.InvokeServiceFunction<int>(QMAPP.ServicesAgent.SystemService.FileTypeBLL_Delete.ToString(), selectKey);
                #endregion 

                SetMessage(AppResource.DeleteMessage); 

                return List();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取列表
        /// <summary>
        /// 加载列表
        /// </summary>
        /// <returns>结果</returns>
        [HandleException]
        public ActionResult List()
        {
            FileTypeModel seachModel = new FileTypeModel();
            TryGetSelectBuffer<FileTypeModel>(out seachModel);
            seachModel.rownumbers = false;
            seachModel.url = "/FileType/GetList";
            return View("FileType", seachModel);
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="callBack">是否回调</param>
        /// <returns>列表</returns>
        [HandleException]
        public ActionResult GetList(bool? callBack)
        {
            FileTypeModel seachModel = null;
            DataPage page = null;
            FileType condition = null;
            try
            {
                //获取查询对象
                seachModel = GetModel<FileTypeModel>();
                #region 获取缓存值
                if (callBack != null)
                {
                    TryGetSelectBuffer<FileTypeModel>(out seachModel);
                }
                else
                {
                    //保存搜索条件
                    SetSelectBuffer<FileTypeModel>(seachModel);
                }
                #endregion
                //获取前台分页设置信息
                page = this.GetDataPage(seachModel);
                condition = CopyToModel<FileType, FileTypeModel>(seachModel);
                 
                #region 调用服务
                QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
                page = agent.InvokeServiceFunction<DataPage>(QMAPP.ServicesAgent.SystemService.FileTypeBLL_GetList.ToString(), condition, page);
                #endregion 
                  
                DataGridResult<FileType> result = new DataGridResult<FileType>();
                result.Total = page.RecordCount;
                result.Rows = JsonConvertHelper.GetDeserialize<List<FileType>>(page.Result.ToString());
                 
                return Content(result.GetJsonSource());

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #endregion

        #region 获取文件类型树形控件
        /// <summary>
        /// 获取文件类型树形控件
        /// </summary>
        /// <returns></returns>
        public ActionResult GetTreeData()
        {
            List<TreeNodeResult> list = new List<TreeNodeResult>();
            List<FileType> menus = new List<FileType>();

            try
            { 
                #region 调用服务
                QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
                menus = agent.InvokeServiceFunction <List<FileType>>(QMAPP.ServicesAgent.SystemService.FileTypeBLL_GetAllList.ToString());
                #endregion
                 
                if (menus != null && menus.Count != 0)
                {
                    for (int i = 0; i < menus.Count; i++)
                    {
                        TreeNodeResult treenode = new TreeNodeResult();
                        treenode.Tid = menus[i].TYPENO.ToString();
                        treenode.Ttext = menus[i].TYPENAME.ToString();
                        if (menus[i].Orgas != null)
                        {
                            for (int j = 0; j < menus[i].Orgas.Count; j++)
                            {
                                TreeNodeResult cTreeNode = new TreeNodeResult();
                                cTreeNode.Tid = menus[i].Orgas[j].TYPENO.ToString();
                                cTreeNode.Ttext = menus[i].Orgas[j].TYPENAME.ToString();
                                treenode.AddchildNode(cTreeNode);
                            }
                        }
                        list.Add(treenode);
                    }
                    list[0].TChecked = true;
                }
                return Content(TreeNodeResult.GetResultJosnS(list.ToArray()));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

    }
}