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

namespace QMAPP.FJC.Web.Controllers
{
    public class ProcessRouteController : QController
    {
        /// <summary>
        /// 空数据项显示值 
        /// </summary>
        public string EmptyItemTitle = " ";

        #region 获取列表
        /// <summary>
        /// 加载列表
        /// </summary>
        /// <returns>结果</returns>
        [HandleException]
        public ActionResult List(bool? callBack)
        {
            ProcessRouteModel seachModel = new ProcessRouteModel();
            if (callBack == true)
                TryGetSelectBuffer<ProcessRouteModel>(out seachModel);
            seachModel.rownumbers = false;
            seachModel.url = "/ProcessRoute/GetList";
            return View("ProcessRouteList", seachModel);
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="callBack">是否回调</param>
        /// <returns>列表</returns>
        [HandleException]
        public ActionResult GetList(bool? callBack)
        {
            ProcessRouteModel seachModel = null;
            DataPage page = null;
            ServiceAgent wcfAgent = this.GetServiceAgent();
            ProcessRoute condition = null;
            DataResult<DataPage> pageResult = new DataResult<DataPage>();
            try
            {
                //获取查询对象
                seachModel = GetModel<ProcessRouteModel>();
                #region 获取缓存值
                if (callBack != null)
                {
                    TryGetSelectBuffer<ProcessRouteModel>(out seachModel);
                }
                else
                {
                    //保存搜索条件
                    SetSelectBuffer<ProcessRouteModel>(seachModel);
                }
                #endregion
                //获取前台分页设置信息
                page = this.GetDataPage(seachModel);
                condition = CopyToModel<ProcessRoute, ProcessRouteModel>(seachModel);
                #region wcf服务统一接口
                pageResult = wcfAgent.InvokeServiceFunction<DataResult<DataPage>>("ProcessRouteBLL_GetList", condition, page);
                if (pageResult.IsSuccess == false)
                {
                    SetMessage(pageResult.Msg);
                    return List(true);
                }
                DateGridResult<ProcessRoute> result = new DateGridResult<ProcessRoute>();
                result.Total = pageResult.Result.RecordCount;
                result.Rows = JsonConvertHelper.GetDeserialize<List<ProcessRoute>>(pageResult.Result.Result.ToString());
                #endregion
                return Content(result.GetJsonSource());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 编辑
        /// <summary>
        /// 编辑载入
        /// </summary>
        /// <returns>处理结果</returns>
        [HandleException]
        public ActionResult Edit()
        {
            ProcessRouteModel model = new ProcessRouteModel();
            string ID = Request.Params["PID"];
            ProcessRoute Entity = new ProcessRoute();
            ServiceAgent wcfAgent = this.GetServiceAgent();
            DataResult<ProcessRoute> result = new DataResult<ProcessRoute>();
            try
            {
                if (string.IsNullOrEmpty(ID) == false)
                {
                    //修改获取原数据
                    Entity.PID = ID;
                    result = wcfAgent.InvokeServiceFunction<DataResult<ProcessRoute>>("ProcessRouteBLL_Get", Entity);
                    if (result.IsSuccess == false)
                    {
                        SetMessage(result.Msg);
                        return View("ProcessRouteEdit", model);
                    }
                    model = CopyToModel<ProcessRouteModel, ProcessRoute>(result.Result);
                }
                return View("ProcessRouteEdit", model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns>处理结果</returns>
        [HttpPost]
        [HandleException]
        [ValidateInput(false)]
        public ActionResult Save(ProcessRouteModel saveModel)
        {
            ProcessRoute Entity = null;
            ServiceAgent wcfAgent = this.GetServiceAgent();
            DataResult<int> result = new DataResult<int>();
            try
            {
                Entity = CopyToModel<ProcessRoute, ProcessRouteModel>(saveModel);
                if (string.IsNullOrEmpty(Entity.PID) == true)
                {
                    //新增
                    result = wcfAgent.InvokeServiceFunction<DataResult<int>>("ProcessRouteBLL_Insert", Entity);
                }
                else
                {
                    //修改
                    result = wcfAgent.InvokeServiceFunction<DataResult<int>>("ProcessRouteBLL_Update", Entity);
                }
                if (result.IsSuccess == false)
                {
                    SetMessage(result.Msg);
                    return View("ProcessRouteEdit", saveModel);
                }
                return this.GetJsViewResult(string.Format("parent.List(1);parent.showTitle('{0}');parent.closeAppWindow1();", AppResource.SaveMessge));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns>结果</returns>
        [HttpPost]
        [HandleException]
        public ActionResult Delete(ProcessRouteModel saveMode)
        {
            string selectKey = Request.Form["selectKey"];
            ServiceAgent wcfAgent = this.GetServiceAgent();
            DataResult<int> result = new DataResult<int>();
            try
            {
                result = wcfAgent.InvokeServiceFunction<DataResult<int>>("ProcessRouteBLL_Delete", selectKey);
                if (result.IsSuccess == false)
                {
                    SetMessage(result.Msg);
                    return List(true);
                }
                SetMessage(AppResource.DeleteMessage);
                return List(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 配置工艺路线工序

        /// <summary>
        /// 加载列表
        /// </summary>
        /// <param name="callBack"></param>
        /// <returns></returns>
        public ActionResult ProcessRouteWorkCellList(bool? callBack)
        {
            ProcessRouteWorkCellModel processRouteModel = new ProcessRouteWorkCellModel();
            string ROUTE_PID = Request.Params["ROUTE_PID"];

            if (callBack == true)
                TryGetSelectBuffer<ProcessRouteWorkCellModel>(out processRouteModel);

            processRouteModel.rownumbers = false;
            processRouteModel.url = "/ProcessRoute/GetProcessRouteWorkCellList?ROUTE_PID=" + ROUTE_PID;
            processRouteModel.ROUTE_PID = ROUTE_PID;
            return View("ProcessRouteWorkCellList", processRouteModel);
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="callBack"></param>
        /// <returns></returns>
        public ActionResult GetProcessRouteWorkCellList(bool? callBack)
        {
            string ROUTE_PID = Request.Params["ROUTE_PID"];
            ProcessRouteWorkCellModel seachModel = null;
            DataPage page = null;
            ServiceAgent wcfAgent = this.GetServiceAgent();
            ProcessRouteWorkCell condition = null;
            DataResult<DataPage> pageResult = new DataResult<DataPage>();
            try
            {
                //获取查询对象
                seachModel = GetModel<ProcessRouteWorkCellModel>();
                #region 获取缓存值
                if (callBack != null)
                {
                    TryGetSelectBuffer<ProcessRouteWorkCellModel>(out seachModel);
                }
                else
                {
                    //保存搜索条件
                    SetSelectBuffer<ProcessRouteWorkCellModel>(seachModel);
                }
                #endregion
                //获取前台分页设置信息
                page = this.GetDataPage(seachModel);
                condition = CopyToModel<ProcessRouteWorkCell, ProcessRouteWorkCellModel>(seachModel);

                if (string.IsNullOrEmpty(ROUTE_PID))
                {
                    condition.ROUTE_PID = ROUTE_PID;
                }
                #region wcf服务统一接口
                pageResult = wcfAgent.InvokeServiceFunction<DataResult<DataPage>>
                    ("ProcessRouteBLL_GetProcessRouteWorkCellData", condition, page);
                if (pageResult.IsSuccess == false)
                {
                    SetMessage(pageResult.Msg);
                    return ProcessRouteWorkCellList(true);
                }
                DateGridResult<ProcessRouteWorkCell> result = new DateGridResult<ProcessRouteWorkCell>();
                result.Total = pageResult.Result.RecordCount;
                result.Rows = JsonConvertHelper.GetDeserialize<List<ProcessRouteWorkCell>>(pageResult.Result.Result.ToString());
                #endregion
                return Content(result.GetJsonSource());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 配置添加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult ProcessRouteWorkCellEdit()
        {
            ProcessRouteWorkCellModel model = new ProcessRouteWorkCellModel();
            model = GetModel<ProcessRouteWorkCellModel>();
            model.ROUTE_PID = Request.Params["ROUTE_PID"];

            try
            {
                #region 获取前置工序下拉列表
                model.workCellSeqModel.Editable = true;

                //获取前置工序下拉列表
                model.workCellSeqModel.ComboBoxList = new Dictionary<string, Dictionary<string, string>>();
                Dictionary<string, string> workCells = this.GetWorkCellComboBoxList();
                model.workCellSeqModel.ComboBoxList.Add("PRE_WORKCELL_CODE", workCells);
                #endregion

                return View("ProcessRouteWorkCellEdit", model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取明细数据
        /// </summary>
        /// <returns></returns>
        [HandleException]
        public ActionResult GetDetailList()
        {
            DataGridResult<ProcessRouteWorkCellSeq> result = new DataGridResult<ProcessRouteWorkCellSeq>();
            result.Total = 0;
            result.Rows = SessionHelper.GetSession("ProcessRouteWorkCell_Detail") as List<ProcessRouteWorkCellSeq>;
            SessionHelper.SessionDispose("ProcessRouteWorkCell_Detail");
            if (result.Rows == null)
                result.Rows = new List<ProcessRouteWorkCellSeq>();
            return Content(result.GetJsonSource());
        }
        /// <summary>
        /// 保存工序配置
        /// </summary>
        /// <returns></returns>
        public ActionResult ProcessRouteWorkCellSave(ProcessRouteWorkCellModel saveModel, ProcessRouteWorkCellSeqModel saveInfo)
        {
            ProcessRouteWorkCell entity = null;
            //ProcessRouteWorkCellSeq info = null;
            ServiceAgent wcfAgent = this.GetServiceAgent();
            DataResult<int> result = new DataResult<int>();
            string ROUTE_PID = Request.Params["ROUTE_PID"];
            string WORKCELL_PID = Request.Params["WORKCELL_PID"];
            try
            {
                entity = CopyToModel<ProcessRouteWorkCell, ProcessRouteWorkCellModel>(saveModel);
                entity.Details = JsonConvertHelper.DeserializeObject<List<ProcessRouteWorkCellSeq>>(saveModel.DetailValue);
                //工序顺序信息
                //info = CopyToModel<ProcessRouteWorkCellSeq, ProcessRouteWorkCellSeqModel>(saveInfo);

                //工艺路线主键
                if (string.IsNullOrEmpty(ROUTE_PID))
                {
                    entity.ROUTE_PID = ROUTE_PID;
                }
                //前一工序主键
                //if (string.IsNullOrEmpty(WORKCELL_PID))
                //{
                //    info.PRE_PRW_PID = WORKCELL_PID;
                //}
                if (string.IsNullOrEmpty(entity.PID) == true)
                {
                    //新增
                    result = wcfAgent.InvokeServiceFunction<DataResult<int>>
                        ("ProcessRouteBLL_ProcessRouteWorkCellSave", entity);
                }
                if (result.IsSuccess == false)
                {
                    SetMessage(result.Msg);
                    return View("ProcessRouteWorkCellEdit", saveModel);
                }
                return this.GetJsViewResult(string.Format("parent.List(1);parent.showTitle('{0}');parent.closeAppWindow1();", AppResource.SaveMessge));

            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns>结果</returns>
        [HttpPost]
        [HandleException]
        public ActionResult ProcessRouteWorkCellDelete(ProcessRouteWorkCellModel saveMode)
        {
            string selectKey = Request.Form["selectKey"];
            string ROUTE_PID = Request.Form["ROUTE_PID"];
            ServiceAgent wcfAgent = this.GetServiceAgent();
            DataResult<int> result = new DataResult<int>();
            try
            {
                result = wcfAgent.InvokeServiceFunction<DataResult<int>>("ProcessRouteBLL_ProcessRouteWorkCellDelete", selectKey, ROUTE_PID);
                if (result.IsSuccess == false)
                {
                    SetMessage(result.Msg);
                    return ProcessRouteWorkCellList(true);
                }
                SetMessage(AppResource.DeleteMessage);
                return ProcessRouteWorkCellList(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region 获取当前工艺路线下的工序下拉列表
        /// <summary>
        /// 获取当前工艺路线下的工序下拉列表
        /// </summary>
        /// <returns>数据源</returns>
        [HandleException]
        public ActionResult GetProcessComboxSource()
        {
            List<ProcessRouteWorkCell> list = new List<ProcessRouteWorkCell>();
            ServiceAgent wcfAgent = this.GetServiceAgent();
            string ROUTE_PID = Request.Params["ROUTE_PID"];

            try
            {
                var dataResult = wcfAgent.InvokeServiceFunction<DataResult<List<ProcessRouteWorkCell>>>
                    ("ProcessRouteBLL_GeProcessRouteWorkCellList", new ProcessRouteWorkCell() { ROUTE_PID = ROUTE_PID });

                foreach (ProcessRouteWorkCell item in dataResult.Result)
                {
                    list.Add(item);
                }
                list.Insert(0, new ProcessRouteWorkCell { WORKCELL_PID = "", WORKCELL_NAME = " " });
                return Content(QMFrameWork.Common.Serialization.JsonConvertHelper.GetSerializes(list));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion


        #endregion

        #region 获取工序下拉列表
        /// <summary>
        /// 获取工序下拉列表
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetWorkCellComboBoxList()
        {
            QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
            //获取零件类型下拉列表  
            Dictionary<string, string> listWorkCell = new Dictionary<string, string>();
            List<QMAPP.FJC.Entity.Basic.ProcessInfo> list = agent.InvokeServiceFunction<List<QMAPP.FJC.Entity.Basic.ProcessInfo>>("ProcessInfoBLL_GetProcessInfoList", new QMAPP.FJC.Entity.Basic.ProcessInfo());

            listWorkCell.Add(this.EmptyItemTitle, this.EmptyItemTitle);
            foreach (QMAPP.FJC.Entity.Basic.ProcessInfo item in list)
            {
                listWorkCell.Add(item.PID, item.PROCESSNAME);
            }
            return listWorkCell;
        }
        #endregion

    }
}