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

namespace QMAPP.FJC.Web.Controllers
{
    public class YieldTrendController : QController
    {
        /// <summary>
        /// 模块编号:M10-2
        /// 作    用:产量趋势分析控制层
        /// 作    者:王庆男
        /// 编写日期:2015年07月06日
        ///</summary> 
        ///

        #region 初始化
        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns>结果</returns>
        [HandleException]
        public ActionResult Init()
        {
            YieldTrendModel seachModel = new YieldTrendModel();
            return View("YieldTrend", seachModel);
        }
        #endregion
        #region 获取列表
        /// <summary>
        /// 加载列表
        /// </summary>
        /// <returns>结果</returns>
        [HandleException]
        public ActionResult List()
        {
            YieldTrendModel seachModel = null;
            DataResult<List<MainOperation>> listResult = new DataResult<List<MainOperation>>();
            DataPage page = new DataPage();
            ServiceAgent wcfAgent = this.GetServiceAgent();
            MainOperation condition = null;
            try
            {
                //获取查询对象
                seachModel = GetModel<YieldTrendModel>();
                condition = CopyToModel<MainOperation, YieldTrendModel>(seachModel);

                #region wcf服务统一接口
                listResult = wcfAgent.InvokeServiceFunction<DataResult<List<MainOperation>>>(QMAPP.ServicesAgent.B9BasicService.MainOperationBLL_GetListGroupBy.ToString(), condition);
                #endregion

                if (listResult.IsSuccess == false)
                {
                    SetMessage(listResult.Msg);
                    return View("YieldTrend", seachModel);
                }
                var machines = from l in listResult.Result
                               group l by new { l.MACHINECODDE, l.MACHINENAME } into g
                               select new
                                 {
                                     CODE = g.Key.MACHINECODDE,
                                     NAME = g.Key.MACHINENAME
                                 };
                var query = listResult.Result.GroupBy(m => new { m.PRODUCESHIFTTCODE, m.PRODUCESHIFTNAME });
                string arr = "[";
                foreach (var petGroup in query)
                {
                    if (arr.Length > 1)
                    {
                        arr += ",";
                    }
                    arr += "{ \"CODE\":" + "\"" + petGroup.Key.PRODUCESHIFTTCODE + "\"";
                    arr += ",\"NAME\":" + "\"" + petGroup.Key.PRODUCESHIFTNAME + "\"";
                    foreach (MainOperation mainOperation in petGroup)
                    {
                        arr += ",\"" + mainOperation.MACHINECODDE + "\":\"" + mainOperation.Counts + "\"";
                    }
                    arr += "}";
                }
                arr += "]";

                seachModel.results = JsonConvertHelper.GetSerializes(machines);
                seachModel.arr = arr;
                if (listResult.Result.Count == 0)
                {
                    SetMessage("未查到相关数据!");
                } 
                return View("YieldTrend", seachModel);

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

        #region 导出excel
        /// <summary>
        /// 导出excel
        /// </summary>
        /// <returns>结果</returns>
        [HttpPost]
        public ActionResult ExportExcel()
        {
            YieldTrendModel seachModel = null;
            MainOperation condition = null;
            ServiceAgent wcfAgent = this.GetServiceAgent();
            DataResult<List<MainOperation>> listResult = new DataResult<List<MainOperation>>();
            try
            {
                //获取查询对象
                seachModel = GetModel<YieldTrendModel>();
                condition = CopyToModel<MainOperation, YieldTrendModel>(seachModel);
                //获取数据
                listResult = wcfAgent.InvokeServiceFunction<DataResult<List<MainOperation>>>(QMAPP.ServicesAgent.B9BasicService.MainOperationBLL_GetListGroupBy.ToString(), condition);
                //导出
            QMFrameWork.WebUI.Util.IEFileTool efTool = new QMFrameWork.WebUI.Util.IEFileTool();
            DataTable dt = JsonConvertHelper.GetDeserialize<DataTable>(JsonConvertHelper.GetSerializes(listResult.Result));
            return efTool.GetExcelFileResult("YieldTrendExp", "产量趋势分析.xlsx", dt);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion



        #region 获取设备编号下拉列表
        /// <summary>
        /// 获取设备编号下拉列表
        /// </summary>
        /// <returns></returns>
        public ContentResult GetMachineInfoComboxSource()
        {
            List<MachineInfo> listMachineInfo = new List<MachineInfo>();
            ComboboxResult model = new ComboboxResult();
            ServiceAgent wcfAgent = this.GetServiceAgent();
            MachineInfo entity = new MachineInfo();
            try
            {
                //获取设备信息
                listMachineInfo = wcfAgent.InvokeServiceFunction<List<MachineInfo>>(QMAPP.ServicesAgent.FeedInManage.InjectionRecorderBLL_GetMachineInfoList.ToString(), entity);

                foreach (var item in listMachineInfo)
                {
                    if (model.Items.Count == 0)
                    {
                        model.Add(new ComboboxItem { ID = "", Text = new DictController().EmptyItemTitle });
                        model.Add(new ComboboxItem { ID = item.MACHINECODDE, Text = item.MACHINECODDE });
                    }
                    else
                    {
                        model.Add(new ComboboxItem { ID = item.MACHINECODDE, Text = item.MACHINECODDE });
                    }
                }

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

        #region 获取班次编号下拉列表
        /// <summary>
        /// 获取班次编号下拉列表
        /// </summary>
        /// <returns></returns>
        public ContentResult GetProductShiftComboxSource()
        {
            List<ProduceShift> listProductShiftInfo = new List<ProduceShift>();
            ComboboxResult model = new ComboboxResult();
            ServiceAgent wcfAgent = this.GetServiceAgent();
            ProduceShift entity = new ProduceShift();
            try
            {
                //获取班次信息
                listProductShiftInfo = wcfAgent.InvokeServiceFunction<List<ProduceShift>>(QMAPP.ServicesAgent.FeedInManage.InjectionRecorderBLL_GetProduceShiftList.ToString(), entity);

                foreach (var item in listProductShiftInfo)
                {
                    if (model.Items.Count == 0)
                    {
                        model.Add(new ComboboxItem { ID = "", Text = new DictController().EmptyItemTitle });
                        model.Add(new ComboboxItem { ID = item.PRODUCESHIFTTCODE, Text = item.PRODUCESHIFTTCODE });
                    }
                    else
                    {
                        model.Add(new ComboboxItem { ID = item.PRODUCESHIFTTCODE, Text = item.PRODUCESHIFTTCODE });
                    }
                }

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