using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using QMAPP.Common.Web.Controllers;
using QMFrameWork.WebUI.Attribute;
using QMAPP.MD.Entity;
using QMAPP.Entity;
using QMAPP.FJC.Entity.Basic;
using QMAPP.ServicesAgent;
using QMFrameWork.WebUI.DataSource;
using QMAPP.MESReport.Entity.LineQTY;
using QMAPP.MESReport.Entity.Tables;
using ProcessRoute = QMAPP.FJC.Entity.Basic.ProcessRoute;
using WorkCenter = QMAPP.MD.Entity.WorkCenter;

namespace QMAPP.MESReport.Web.Controllers
{
    /// <summary>
    /// 公共类
    /// 于子清
    /// 2017-10-11
    /// </summary>
    public class DictController : QController
    {
        public string EmptyItemTitle = " ";

        #region 获取工艺路线下拉列表
        /// <summary>
        /// 获取工艺路线下拉列表
        /// </summary>
        /// <returns>数据源</returns>
        [HandleException]
        public ActionResult GetProcessRouteComboxSource()
        {
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                var dataResult = wcfAgent.InvokeServiceFunction<DataResult<List<ProcessRoute>>>
                    ("ProcessRouteBLL_GetProcessRouteList", new ProcessRoute());
                ComboboxResult model = new ComboboxResult();
                model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });
                foreach (ProcessRoute item in dataResult.Result)
                {
                    model.Add(new ComboboxItem { ID = item.ROUTE_CODE, Text = item.ROUTE_NAME });
                }
                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取工序下拉列表(联动)
        /// <summary>
        /// 获取工序下拉列表
        /// </summary>
        /// <returns>数据源</returns>
        [HandleException]
        public ActionResult GetWorkCellBasicWithTypeComboxSource(string routecode)
        {
            List<WorkCell> list = new List<WorkCell>();
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                if (string.IsNullOrEmpty(routecode) == false)
                {
                    var dataResult = wcfAgent.InvokeServiceFunction<List<WorkCell>>
                        ("WorkCellBLL_GetNewWorkCellList", new WorkCell(), routecode);
                    foreach (WorkCell item in dataResult)
                    {
                        item.WORKCELL_NAME = item.WORKCELL_NAME;
                        list.Add(item);
                    }
                }
                list.Insert(0, new WorkCell() { WORKCELL_CODE = "", WORKCELL_NAME = this.EmptyItemTitle });
                return Content(QMFrameWork.Common.Serialization.JsonConvertHelper.GetSerializes(list));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取工序下拉列表(联动)排序
        /// <summary>
        /// 获取工序下拉列表
        /// </summary>
        /// <returns>数据源</returns>
        [HandleException]
        public ActionResult GetWorkCellSeqComboxSource(string routecode)
        {
            List<WorkCell> list = new List<WorkCell>();
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                if (string.IsNullOrEmpty(routecode) == false)
                {
                    list = wcfAgent.InvokeServiceFunction<List<WorkCell>>
                        ("WorkCellBLL_GetNewWorkCellListSeq", routecode);
                    //foreach (WorkCell item in dataResult)
                    //{
                    //    //item.WORKCELL_NAME = item.WORKCELL_NAME;
                    //    list.Add(item);
                    //}
                }
                list.Insert(0, new WorkCell() { WORKCELL_CODE = "", WORKCELL_NAME = this.EmptyItemTitle });
                return Content(QMFrameWork.Common.Serialization.JsonConvertHelper.GetSerializes(list));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 工序下拉框
        /// <summary>
        /// 获取工序下拉列表
        /// </summary>
        /// <returns>数据源</returns>
        [HandleException]
        public ActionResult GetWorkCellComboxSource()
        {
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                var dataResult = wcfAgent.InvokeServiceFunction<List<WorkCell>>
                    ("WorkCellBLL_GetWorkCellList", new WorkCell());
                ComboboxResult model = new ComboboxResult();
                model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });
                foreach (WorkCell item in dataResult)
                {
                    model.Add(new ComboboxItem { ID = item.WORKCELL_CODE, Text = item.WORKCELL_NAME });
                }
                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取班次下拉框

        /// <summary>
        /// 获取班次下拉框
        /// </summary>
        /// <returns>数据源</returns>
        [HandleException]
        public ActionResult GetShiftList()
        {
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                var dataResult = wcfAgent.InvokeServiceFunction<DataResult<List<T_QT_SHIFT>>>
                    ("LineShiftQTYCountBLL_GetTQTSHIFTList");
                ComboboxResult model = new ComboboxResult();
                model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });
                foreach (T_QT_SHIFT item in dataResult.Result)
                {
                    model.Add(new ComboboxItem { ID = item.SHIFT_CODE, Text = item.SHIFT_NAME });
                }
                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region 获取EQ

        /// <summary>
        /// 获取工作中心下拉列表
        /// </summary>
        /// <returns>数据源</returns>
        [HandleException]
        public ActionResult GetAllEquipmentComboxSource()
        {
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                var dataResult = wcfAgent.InvokeServiceFunction<DataResult<List<Equipment>>>
                    ("EquipmentBLL_GetEquipmentList", new Equipment());
                ComboboxResult model = new ComboboxResult();
                model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });
                foreach (Equipment item in dataResult.Result)
                {
                    model.Add(new ComboboxItem { ID = item.EQUIPMENT_CODE, Text = item.EQUIPMENT_CODE + "/" + item.EQUIPMENT_NAME });
                }
                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取物料产品列表

        /// <summary>
        /// 获取物料产品列表
        /// </summary>
        /// <returns>数据源</returns>
        [HandleException]
        public ActionResult GetWList()
        {
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                var dataResult = wcfAgent.InvokeServiceFunction<DataResult<List<ShipmentCountConditionModel>>>
                    ("ShipmentCountBLL_GetWList");
                ComboboxResult model = new ComboboxResult();
                model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });
                foreach (ShipmentCountConditionModel item in dataResult.Result)
                {
                    model.Add(new ComboboxItem { ID = item.MATERIAL_CODE, Text = item.MATERIAL_NAME });
                }
                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region 获取设备列表

        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <returns>数据源</returns>
        [HandleException]
        public ActionResult GetSList()
        {
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                var dataResult = wcfAgent.InvokeServiceFunction<DataResult<List<T_BD_MACHINEINFO>>>
                    ("EqueUseCountBLL_GetSList");
                ComboboxResult model = new ComboboxResult();
                model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });
                foreach (T_BD_MACHINEINFO item in dataResult.Result)
                {
                    model.Add(new ComboboxItem { ID = item.MACHINECODDE, Text = item.MACHINENAME });
                }
                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region 获取日周月下拉菜单
        /// <summary>
        /// 获取日周月下拉菜单
        /// </summary>
        /// <returns></returns>
        [HandleException]
        public ActionResult GetTJList()
        {

            try
            {
                ComboboxResult model = new ComboboxResult();
                model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });

                model.Add(new ComboboxItem { ID = "DAY", Text = "日"});

                model.Add(new ComboboxItem { ID = "WEEK", Text = "周" });

                model.Add(new ComboboxItem { ID = "MONTH", Text = "月" });

                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region 获取日月年下拉菜单
        /// <summary>
        /// 获取日月年下拉菜单
        /// </summary>
        /// <returns></returns>
        [HandleException]
        public ActionResult GetYMDList()
        {
            try
            {
                ComboboxResult model = new ComboboxResult();
                model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });

                model.Add(new ComboboxItem { ID = "DAY", Text = "日" });

                model.Add(new ComboboxItem { ID = "MONTH", Text = "月" });

                model.Add(new ComboboxItem { ID = "YEAR", Text = "年" });

                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region 获取呼叫类型
        /// <summary>
        /// 获取呼叫类型
        /// </summary>
        /// <returns></returns>
        [HandleException]
        public ActionResult GetCALLTYPE()
        {
            try
            {
                ComboboxResult model = new ComboboxResult();
                model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });

                model.Add(new ComboboxItem { ID = "0", Text = "维修呼叫" });

                model.Add(new ComboboxItem { ID = "1", Text = "质量呼叫" });

                model.Add(new ComboboxItem { ID = "2", Text = "物料呼叫" });

                model.Add(new ComboboxItem { ID = "3", Text = "工程呼叫" });

                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取工作中心-写死

        [HandleException]
        public ActionResult GetSendPlanWorkCenter()
        {
            {
                try
                {
                    ComboboxResult model = new ComboboxResult();
                    //model.Add(new ComboboxItem { ID = "DPLFY/DPRFY", Text = "门板发运" });
                    model.Add(new ComboboxItem { ID = "IPFY01", Text = "东厂仪表板发运" });

                    model.Add(new ComboboxItem { ID = "IPFY02", Text = "西厂仪表板发运" });

                    return Content(model.ToString());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
           
        }

        #endregion 

        #region 获取呼叫原因
        /// <summary>
        /// 获取呼叫原因
        /// </summary>
        /// <returns></returns>
        [HandleException]
        public ActionResult GetCALLREA()
        {
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                var dataResult = wcfAgent.InvokeServiceFunction<DataResult<List<T_BD_MACHINEINFO>>>
                    ("EqueUseCountBLL_GetSList");
                ComboboxResult model = new ComboboxResult();
                model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });
                foreach (T_BD_MACHINEINFO item in dataResult.Result)
                {
                    model.Add(new ComboboxItem { ID = item.MACHINECODDE, Text = item.MACHINENAME });
                }
                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取全部工作中心下拉列表
        /// <summary>
        /// 获取工作中心下拉列表
        /// </summary>
        /// <returns>数据源</returns>
        [HandleException]
        public ActionResult GetAllWorkCenterComboxSource()
        {
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                var centerType = "0";
                var dataResult = wcfAgent.InvokeServiceFunction<DataResult<List<WorkCenter>>>
                    ("WorkCenterBLL_GetWorkCenterList", new WorkCenter
                {
                    CENTER_TYPE = centerType
                });
                ComboboxResult model = new ComboboxResult();
                model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });
                foreach (WorkCenter item in dataResult.Result)
                {
                    model.Add(new ComboboxItem { ID = item.WORKCENTER_CODE, Text = item.WORKCENTER_NAME });
                }
                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        [HandleException]
        public ActionResult GetWorkcenterComboxSource()
        {
            try
            {
                ComboboxResult model = new ComboboxResult();
                //model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });

                model.Add(new ComboboxItem { ID = "INJECTION_AREA", Text = "注塑工作中心" });

                //model.Add(new ComboboxItem { ID = "1", Text = "宝来门板装配中心" });

                //model.Add(new ComboboxItem { ID = "2", Text = "宝来仪表板装配工作中心" });

                model.Add(new ComboboxItem { ID = "SLUSH_AREA", Text = "搪塑工作中心" });

                model.Add(new ComboboxItem { ID = "VW371_FOAMAREA", Text = "浇注工作中心" });

                model.Add(new ComboboxItem { ID = "BC316_ASSLINE", Text = "BC316仪表" });

                model.Add(new ComboboxItem { ID = "VW371_IPASSYLINE", Text = "VW371仪表" });

                model.Add(new ComboboxItem { ID = "VW371_DPASSYLINE", Text = "VW371门板" });

                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        #endregion

        #region 获取设备(MACHINE)下拉列表
        /// <summary>
        /// 获取设备(MACHINE)下拉列表
        /// </summary>
        /// <returns>工作中心</returns>
        [HandleException]
        public ActionResult GetMachine(string workcenter)
        {
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                var dataResult = wcfAgent.InvokeServiceFunction<DataResult<List<T_BD_MACHINEINFO>>>
                    ("EqueUseCountBLL_GetMachine", workcenter);
                ComboboxResult model = new ComboboxResult();
                model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });
                foreach (T_BD_MACHINEINFO item in dataResult.Result)
                {
                    model.Add(new ComboboxItem { ID = item.MACHINECODDE, Text = item.MACHINENAME });
                }
                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取全部设备下拉列表
        /// <summary>
        /// 获取全部工序下拉列表
        /// </summary>
        /// <returns>数据源</returns>
        [HandleException]
        public ActionResult GetMachineListComboxSource()
        {
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                var machineList = wcfAgent.InvokeServiceFunction<List<MachineInfo>>
                    ("MachineInfoBLL_GetMachineList");
                ComboboxResult model = new ComboboxResult();
                model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });
                foreach (MachineInfo item in machineList)
                {
                    model.Add(new ComboboxItem { ID = item.MACHINECODDE, Text = item.MACHINECODDE + "/" + item.MACHINENAME });
                }
                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region  获取项目号

        [HandleException]
        public ActionResult GetProjectList()
        {
            ComboboxResult model = new ComboboxResult();
            model.Add(new ComboboxItem { ID = "VW371 IP", Text = "VW371仪表板" });

            model.Add(new ComboboxItem { ID = "VW371 DP", Text = "VW371门板" });
            model.Add(new ComboboxItem { ID = "BC316 IP", Text = "BC316仪表板" });

            return Content(model.ToString());
        }

        #endregion

        #region 配置项下拉获取

        public ActionResult GetFixedComboxSource()
        {
            string kind = Request.Params["kind"];
            //单雨春 是否带空项 2014年9月26日
            bool isWithEmpty = string.IsNullOrEmpty(Request.Params["WithEmpty"]) ? true : false;
            Dictionary<string, string> dicts = null;
            try
            {
                ComboboxResult model = new ComboboxResult();

                //单雨春 是否带空项 2014年9月26日
                if (isWithEmpty)
                {
                    model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });
                }

                QMFrameWork.Common.Util.ModelDictionaryHandler.TryGetModelDictionary(kind.ToString(), out dicts);

                foreach (string key in dicts.Keys)
                {
                    model.Add(new ComboboxItem { ID = key, Text = dicts[key] });
                }

                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion 

        #region  获取零件号

        public ActionResult GetMaterialForCount()
        {

            string projectCode = Request.Params["PROJECTCODE"];
            string hbtype = Request.Params["HBTYPE"];
            string color = Request.Params["COLOR"];

            List<Material> list = new List<Material>();
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                var dataResult = wcfAgent.InvokeServiceFunction<List<Material>>
                        ("MaterialBLL_GetMaterialList", new Material()
                        {
                            PROJECTCODE = projectCode,
                            HBTYPE = hbtype,
                            COLOR = color,
                            MATERIAL_ATTRIBUTE = "2"
                        });
                foreach (Material item in dataResult)
                {
                    list.Add(item);
                }

                list.Insert(0, new Material() { MATERIAL_CODE = "", MATERIAL_NAME = this.EmptyItemTitle });
                return Content(QMFrameWork.Common.Serialization.JsonConvertHelper.GetSerializes(list));
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        #endregion

        #region 获取物料信息下拉列表(所有)
        /// <summary>
        /// 获取物料信息下拉列表(所有)
        /// </summary>
        /// <returns>数据源</returns>
        [HandleException]
        public ActionResult GetALLMaterialComboxSource()
        {
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                var result = wcfAgent.InvokeServiceFunction<List<Material>>
                    ("MaterialBLL_GetMaterialList", new Material());
                ComboboxResult model = new ComboboxResult();
                model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });
                foreach (Material item in result)
                {
                    model.Add(new ComboboxItem { ID = item.MATERIAL_CODE, Text = item.MATERIAL_NAME });
                }
                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion


        #region 获取物料信息下拉列表
        /// <summary>
        /// 获取物料信息下拉列表
        /// </summary>
        /// <returns>数据源</returns>
        [HandleException]
        public ActionResult GetMaterialTree()
        {
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                var materialResult = wcfAgent.InvokeServiceFunction<List<Material>>
                    ("MaterialBLL_GetMaterialList", new Material());
                var classresult = wcfAgent.InvokeServiceFunction<DataResult<List<MaterialClass>>>
                    ("MaterialBLL_GetMaterialClassList", new MaterialClass()).Result;
                List<CombotreeItem> trees = new List<CombotreeItem>();
                trees.Add(new CombotreeItem { id = "", text = "  " });

                foreach (MaterialClass item in classresult.Where(p => string.IsNullOrWhiteSpace(p.UP_MATERIAL_TYPE_CODE)).OrderBy(p => p.SEQ_NUM))
                {
                    var subnode = new CombotreeItem { id = item.MATERIAL_TYPE_CODE, text = item.MATERIAL_TYPE_NAME, children = new List<CombotreeItem>() };
                    AppendTree(subnode, classresult, materialResult);
                    if (subnode.children.Count > 0)
                    {
                        trees.Add(subnode);
                    }
                }

                return Json(trees);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 追加树节点
        /// </summary>
        /// <param name="node"></param>
        /// <param name="mclasslist"></param>
        /// <param name="materiallist"></param>
        private void AppendTree(CombotreeItem node, List<MaterialClass> mclasslist, List<Material> materiallist)
        {
            foreach (var mclass in mclasslist.Where(p => string.Equals(node.id, p.UP_MATERIAL_TYPE_CODE)).OrderBy(p => p.SEQ_NUM))
            {
                var subnode = new CombotreeItem { id = mclass.MATERIAL_TYPE_CODE, text = mclass.MATERIAL_TYPE_NAME, children = new List<CombotreeItem>() };
                AppendTree(subnode, mclasslist, materiallist);
                if (subnode.children.Count > 0)
                {
                    node.children.Add(subnode);
                }
            }
            foreach (var material in materiallist.Where(p => string.Equals(node.id, p.MATERIAL_TYPE_CODE)).OrderBy(p => p.MATERIAL_CODE))
            {
                var subnode = new CombotreeItem { id = material.MATERIAL_CODE, text = material.MATERIAL_CODE + "/" + material.MATERIAL_NAME };
                node.children.Add(subnode);
            }
        }

        #endregion

        #region 获取呼叫类型

        /// <summary>
        /// 获取呼叫类型
        /// </summary>
        /// <returns>数据源</returns>
        [HandleException]
        public ActionResult GetConfigDetailComboxSource()
        {
            string kind = Request.Params["kind"];

            bool isWithEmpty = string.IsNullOrEmpty(Request.Params["WithEmpty"]) ? true : false;
            Dictionary<string, string> dicts = null;
            try
            {
                ComboboxResult model = new ComboboxResult();

                if (isWithEmpty)
                {
                    model.Add(new ComboboxItem { ID = "", Text = this.EmptyItemTitle });
                }
                //执行查询
                List<ConfigDetail> list = new List<ConfigDetail>();
                ServiceAgent wcfAgent = this.GetServiceAgent();
                list = wcfAgent.InvokeServiceFunction<List<ConfigDetail>>("ConfigDetailBLL_GetAllList", kind);

                foreach (ConfigDetail item in list)
                {
                    //item.DetailTypeName = item.DetailTypeCode + "/" + item.DetailTypeName;
                    model.Add(new ComboboxItem { ID = item.DetailTypeCode, Text = item.DetailTypeName });
                }

                return Content(model.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion


    }
}