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

namespace QMAPP.FJC.Web.Controllers
{
    public class FISPhraseController : QController
    {
        #region 获取信息
        /// <summary>
        /// 加载列表
        /// </summary>
        /// <returns>结果</returns>
        [HandleException]
        public ActionResult List(bool? callback)
        {
            FISAssemblyModel seachModel = new FISAssemblyModel();
            seachModel.checkbox = false;
            seachModel.pagination = false;
            seachModel.singleSelect = true;
            seachModel.ImmediateSearch = false;
            if (callback == true)
                TryGetSelectBuffer<FISAssemblyModel>(out seachModel);
            seachModel.url = "/FISPhrase/GetList";
            return View("List", seachModel);
        }
        #endregion

        #region 获取列表
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="callBack">是否回调</param>
        /// <returns>列表</returns>
        [HandleException]
        public ActionResult GetList(bool? callBack)
        {
               
            FISAssemblyModel seachModel = null;
            ServiceAgent wcfAgent = this.GetServiceAgent();

            //wcfAgent.InvokeServiceFunction<int>("FISPhraseBLL_PhraseSync");
             
            FISAssembly condition = null;
            try
            {
                //获取查询对象
                seachModel = GetModel<FISAssemblyModel>();
                #region 获取缓存值
                if (callBack != null)
                {
                    TryGetSelectBuffer<FISAssemblyModel>(out seachModel);
                }
                else
                {
                    //保存搜索条件
                    SetSelectBuffer<FISAssemblyModel>(seachModel);
                }
                #endregion
                condition = CopyToModel<FISAssembly, FISAssemblyModel>(seachModel);
                #region wcf服务统一接口
                var list = wcfAgent.InvokeServiceFunction<List<FISAssembly>>("FISPhraseBLL_GetAssemblyList", condition);
                
                DateGridResult<FISAssembly> result = new DateGridResult<FISAssembly>();
                result.Total = list.Count;
                result.Rows = list;
                #endregion
                return Content(result.GetJsonSource());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 编辑
        /// <summary>
        /// 编辑载入
        /// </summary>
        /// <returns>处理结果</returns>
        [HandleException]
        public ActionResult Edit()
        {
            FISAssemblyModel model = new FISAssemblyModel();
            string ID = Request.Params["PID"];
            FISAssembly Entity = new FISAssembly();
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                if (string.IsNullOrEmpty(ID) == false)
                {
                    //修改获取原数据
                    Entity.Id = ID;
                    Entity = wcfAgent.InvokeServiceFunction<FISAssembly>("FISPhraseBLL_GetAssembly", Entity);
                    
                    model = CopyToModel<FISAssemblyModel, FISAssembly>(Entity);
                }
                return View("Edit", model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns>处理结果</returns>
        [HttpPost]
        [HandleException]
        [ValidateInput(false)]
        public ActionResult Save(FISAssemblyModel saveModel)
        {
            FISAssembly Entity = null;
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                Entity = CopyToModel<FISAssembly, FISAssemblyModel>(saveModel);

                //修改
                var result = wcfAgent.InvokeServiceFunction<int>("FISPhraseBLL_UpdateAssembly", Entity);
                if (result == 0)
                {
                    SetMessage("保存失败!");
                    return View("Edit", saveModel);
                }

                return this.GetJsViewResult(string.Format("parent.List(1);parent.showTitle('{0}');parent.closeAppWindow1();", AppResource.SaveMessge));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion


        /// <summary>
        /// 同步数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns>处理结果</returns>
        [HttpPost]
        [HandleException]
        [ValidateInput(false)]
        public ActionResult Sync()
        {
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {

                var result = wcfAgent.InvokeServiceFunction<int>("FISPhraseBLL_PhraseSync");
                SetMessage("同步数据完成");
                return List(true);
            }
            catch (Exception ex)
            {
                SetMessage("数据同步失败:"+ex.Message);
                return List(true);
                
            }
        }
    }
}