using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QMAPP.Common.Web.Controllers;
using QMAPP.Entity;
using QMAPP.FJC.Entity.Andon;
using QMAPP.FJC.Entity.Basic;
using QMAPP.FJC.Entity.Equipment;
using QMAPP.FJC.Web.Models.Andon;
using QMAPP.FJC.Web.Models.Basic;
using QMAPP.FJC.Web.Models.Equipment;
using QMAPP.MD.Entity;
using QMAPP.ServicesAgent;
using QMFrameWork.Common.Serialization;
using QMFrameWork.Data;
using QMFrameWork.WebUI.Attribute;
using QMFrameWork.WebUI.DataSource;
using ProcessInfo = QMAPP.FJC.Entity.Basic.ProcessInfo;

namespace QMAPP.FJC.Web.Controllers
{
    public class TempCallLogController : QController
    {
        #region 获取列表

        /// <summary>
        /// 加载列表
        /// </summary>
        /// <returns>结果</returns>
        [HandleException]
        public ActionResult List(bool? callBack)
        {
            TempCallLogModel seachModel = new TempCallLogModel();
            string CALL_TYPE = Request["CALL_TYPE"];
            seachModel.CALL_TYPE = CALL_TYPE;

            if (callBack == true)
                TryGetSelectBuffer<TempCallLogModel>(out seachModel);
            seachModel.rownumbers = false;
            seachModel.url = "/TempCallLog/GetList?CALL_TYPE=" + CALL_TYPE;
            return View("TempCallLogList", seachModel);
        }

        //public ActionResult List()
        //{
        //    return View();
        //}
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="callBack">是否回调</param>
        /// <returns>列表</returns>
        [HandleException]
        public ActionResult GetList(bool? callBack)
        {
            TempCallLogModel seachModel = null;
            DataPage page = null;
            CallLogEntity condition = null;
            DataResult<DataPage> pageResult = new DataResult<DataPage>();

            ServiceAgent wcfAgent = this.GetServiceAgent();

            try
            {
                //获取查询对象
                seachModel = GetModel<TempCallLogModel>();

                string CALL_TYPE = Request.Params["CALL_TYPE"];
                string isFirst = Request.Params["isFirst"];
                
                seachModel.CALL_TYPE = CALL_TYPE;

                if (callBack != null)
                {
                    TryGetSelectBuffer<TempCallLogModel>(out seachModel);
                }
                else
                {
                    //保存搜索条件
                    SetSelectBuffer<TempCallLogModel>(seachModel);
                }
                //获取前台分页设置信息
                page = this.GetDataPage(seachModel);
                condition = CopyToModel<CallLogEntity, TempCallLogModel>(seachModel);

                #region wcf服务统一接口

                var callLogList = wcfAgent.InvokeServiceFunction("AndonCallBLL_GetCallLogCount", condition);

                #endregion

                return Content(callLogList);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        [HandleException]
        public ActionResult GetListNew(bool? callBack)
        {
            CallLogEntity condition = new CallLogEntity();
            ServiceAgent wcfAgent = this.GetServiceAgent();

            try
            {


                #region wcf服务统一接口

                var callLogList = wcfAgent.InvokeServiceFunction("AndonCallBLL_GetCallLogCountNew", condition);

                #endregion

                return Content(callLogList);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        [HandleException]
        public ActionResult GetDivStyle(bool? callBack)
        {
            ServiceAgent wcfAgent = this.GetServiceAgent();

            try
            {
                string type = Request.Params["type"];

                #region wcf服务统一接口

                var divs = wcfAgent.InvokeServiceFunction("AndonCallBLL_GetDivStyle",type);

                #endregion

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


        /// <summary>
        /// 统计
        /// </summary>
        /// <returns>结果</returns>
        [HandleException]
        public ActionResult Count(bool? callBack)
        {
            CallLogModel seachModel = new CallLogModel();
            if (callBack == true)
                TryGetSelectBuffer<CallLogModel>(out seachModel);
            string ids = Request.Params["PID"];
            seachModel.rownumbers = false;
            seachModel.url = "/TempCallLog/GetCountList?PID=" + ids;
            return View("CountCallLogList", seachModel);
        }

        /// <summary>
        /// 获取统计列表
        /// </summary>
        /// <param name="callBack">是否回调</param>
        /// <returns>列表</returns>
        [HandleException]
        public ActionResult GetCountList(bool? callBack)
        {
            CallLogModel seachModel = null;
            DataResult<DataPage> pageResult = new DataResult<DataPage>();
            DataPage page = new DataPage();
            ServiceAgent wcfAgent = this.GetServiceAgent();
            CallLogEntity condition = null;
            try
            {
                //获取查询对象
                seachModel = GetModel<CallLogModel>();

                #region 获取缓存值

                if (callBack != null)
                {
                    TryGetSelectBuffer<CallLogModel>(out seachModel);
                }
                else
                {
                    //保存搜索条件
                    SetSelectBuffer<CallLogModel>(seachModel);
                }

                #endregion

                //获取前台分页设置信息
                page = this.GetDataPage(seachModel);
                condition = CopyToModel<CallLogEntity, CallLogModel>(seachModel);
                string ids = Request.Params["PID"];
                var callType = ids.Substring(0,1);
                var mc = ids.Substring(1);
                condition.CALL_TYPE = callType;
                condition.MACHINECODDE = mc;

                #region wcf服务统一接口

                pageResult = wcfAgent.InvokeServiceFunction<DataResult<DataPage>>("AndonCallBLL_GetCallLogListWeb",
                    condition, page);
                if (pageResult.IsSuccess == false)
                {
                    SetMessage(pageResult.Msg);
                    return List(true);
                }
                DateGridResult<CallLogEntity> result = new DateGridResult<CallLogEntity>();
                result.Total = pageResult.Result.RecordCount;
                result.Rows = JsonConvertHelper.GetDeserialize<List<CallLogEntity>>(pageResult.Result.Result.ToString());

                #endregion

                return Content(result.GetJsonSource());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}