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

namespace QMAPP.FJC.Web.Controllers
{

    /// <summary>
    /// 模块编号:M9-13
    /// 作    用:不合格品记录查询
    /// 作    者:王济
    /// 编写日期:2015年07月010日
    ///</summary> 
    public class MendRecorderController : QController
    {
        #region 获取列表

        [HandleException]
        public ActionResult List(bool? callBack)
        {
            MendRecorderModel searchmodel = new MendRecorderModel();
            searchmodel.CREATEDATESTART = DateTime.Now.AddDays(-10).ToString("yyyy-MM-dd HH:mm:ss");
            searchmodel.CREATEDATEEND = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss");
            searchmodel.ImmediateSearch = false;
            if (callBack == true)
                TryGetSelectBuffer<MendRecorderModel>(out searchmodel);
            searchmodel.rownumbers = true;
            searchmodel.url = "/MendRecorder/GetList";
            return View("MendRecorderList", searchmodel);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="callBack">是否回调</param>
        /// <returns>列表</returns>
        [HandleException]
        public ActionResult GetList(bool? callBack)
        {
            MendRecorderModel searchmodel = null;
            DataPage page = null;
            ServiceAgent wcfAgent = this.GetServiceAgent();
            MendRecorder condition = null;
            DataResult<DataPage> pageResult = new DataResult<DataPage>();
            DateGridResult<MendRecorder> result = new DateGridResult<MendRecorder>();

            try
            {
                searchmodel = GetModel<MendRecorderModel>();
                //如果类型为空,不执行查询。
                if (string.IsNullOrEmpty(searchmodel.MENDRESULT))
                {
                    //return Content(result.GetJsonSource());
                   // return List(true);
                }
                if (callBack != null)
                {
                    TryGetSelectBuffer<MendRecorderModel>(out searchmodel);
                }
                else
                {
                    //保存搜索条件
                    SetSelectBuffer<MendRecorderModel>(searchmodel);
                }
                page = this.GetDataPage(searchmodel);
                condition = CopyToModel<MendRecorder, MendRecorderModel>(searchmodel);
                //condition.MENDRESULT = "0";
                
                #region wcf服务统一接口
                pageResult = wcfAgent.InvokeServiceFunction<DataResult<DataPage>>(QMAPP.ServicesAgent.B9IPCService.MendRecorderBLL_GetLists.ToString(), condition, page);
                result.Total = pageResult.Result.RecordCount;
                result.Rows = JsonConvertHelper.GetDeserialize<List<MendRecorder>>(pageResult.Result.Result.ToString());
                #endregion
                return Content(result.GetJsonSource());
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        #endregion

        #region 导出excel不合格
        /// <summary>
        /// 导出excel
        /// </summary>
        /// <returns>结果</returns>
        [HttpPost]
        public ActionResult ExportExcelMend()
        {
            MendRecorderModel seachModel = null;
            MendRecorder condition = null;
            ServiceAgent wcfAgent = this.GetServiceAgent();
            DataResult<DataTable> result = new DataResult<DataTable>();
            string selectKey = Request.Form["selectKey"];
            string[] list = selectKey.Split(":".ToCharArray());
            try
            {
                //获取查询对象
                seachModel = GetModel<MendRecorderModel>();
                condition = CopyToModel<MendRecorder, MendRecorderModel>(seachModel);
                //condition.MENDRESULT = null;
                //获取数据
                

                result = wcfAgent.InvokeServiceFunction<DataResult<DataTable>>(QMAPP.ServicesAgent.B9IPCService.MendRecorderBLL_GetExportData.ToString(), condition);


                //根据所选信息进行导出
                if (!String.IsNullOrEmpty(selectKey))
                {
                    DataView dv = new DataView(result.Result);
                    string strWhere = "";
                    //string[] list = selectKey.Split(":".ToCharArray());
                    foreach (string ID in list)
                    {
                        strWhere += " PID='" + ID + "' or";
                    }
                    if (strWhere != "")
                    {
                        strWhere = strWhere.Remove((strWhere.Length - 2), 2);
                    }
                    dv.RowFilter = strWhere;
                    result.Result = dv.ToTable();
                }


                //导出
                QMFrameWork.WebUI.Util.IEFileTool efTool = new QMFrameWork.WebUI.Util.IEFileTool();
                return efTool.GetExcelFileResult("MendRecorderExp", "不合格品处理单.xlsx", result.Result);

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

        #region 导出excel报废
        /// <summary>
        /// 导出excel
        /// </summary>
        /// <returns>结果</returns>
        [HttpPost]
        public ActionResult ExportExcelScrap()
        {
            MendRecorderModel seachModel = null;
            MendRecorder condition = null;
            ServiceAgent wcfAgent = this.GetServiceAgent();
            DataResult<DataTable> result = new DataResult<DataTable>();
            string selectKey = Request.Form["selectKey"];
            try
            {
                //获取查询对象
                seachModel = GetModel<MendRecorderModel>();
                condition = CopyToModel<MendRecorder, MendRecorderModel>(seachModel);
                condition.MENDRESULT = "2";
                condition.PIDS = selectKey;
                //获取数据
                result = wcfAgent.InvokeServiceFunction<DataResult<DataTable>>("MendRecorderBLL_GetExportDataScrap", condition);


                //根据所选信息进行导出
                //if (!String.IsNullOrEmpty(selectKey))
                //{
                //    DataView dv = new DataView(result.Result);
                //    string strWhere = "";
                //    string[] list = selectKey.Split(":".ToCharArray());
                //    foreach (string ID in list)
                //    {
                //        strWhere += " PID='" + ID + "' or";
                //    }
                //    if (strWhere != "")
                //    {
                //        strWhere = strWhere.Remove((strWhere.Length - 2), 2);
                //    }
                //    dv.RowFilter = strWhere;
                //    result.Result = dv.ToTable();
                //}


                //导出
                QMFrameWork.WebUI.Util.IEFileTool efTool = new QMFrameWork.WebUI.Util.IEFileTool();
                return efTool.GetExcelFileResult("ScrapRecordExp", "材料报废单.xlsx", result.Result);

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

        #region 导出excel
        /// <summary>
        /// 导出excel
        /// </summary>
        /// <returns>结果</returns>
        [HandleException]
        public ActionResult ExportExcel()
        {
            MendRecorderModel seachModel = null;
            MendRecorder condition = null;
            DataTable exportDt = new DataTable();
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                //获取查询对象
                seachModel = GetModel<MendRecorderModel>();
                condition = CopyToModel<MendRecorder, MendRecorderModel>(seachModel);
                condition.TableName = "MendRecorderAllExp";
                condition.PIDList = Request.Form["selectKey"];
                //获取数据

                MendRecorderBLL bll = new MendRecorderBLL();

                 exportDt = bll.GetCheckStatic(condition);

                //导出
                QMFrameWork.WebUI.Util.IEFileTool efTool = new QMFrameWork.WebUI.Util.IEFileTool();
                return efTool.GetExcelFileResult("MendRecorderAllExp", "不合格品记录查询.xlsx", exportDt);
            }
            catch (Exception ex)
            {
                LogManager.LogHelper.Error(new LogInfo { ErrorInfo = ex, Info = "导出excel", Tag = "不合格品记录查询" });
                this.SetMessage("导出excel失败");
                return this.List(true);
            }
        }
        #endregion

        #region 加载投料列表
        /// <summary>
        /// 加载投料列表
        /// </summary>
        /// <returns>结果</returns>
        [HandleException]
        public ActionResult GetMendReasonList()
        {
            string pid = Request.Params["PID"];
            ServiceAgent wcfAgent = this.GetServiceAgent();
            List<MenderReason> list = new List<MenderReason>();
            MenderReason condition = new MenderReason();
            #region wcf服务统一接口
            list = wcfAgent.InvokeServiceFunction<List<MenderReason>>(QMAPP.ServicesAgent.B9IPCService.MendRecorderBLL_GetMenderReasonList.ToString(), new MendRecorder() { PPID = pid });

            //通过返回dataResult判断

            List<object> objlist = new List<object>();
            foreach (var o in list)
            {
                objlist.Add(new
                {
                    POSITION = o.POSITION,
                    DEFECTKEY = o.DEFECTKEY,
                    DEFECTVALUE = o.DEFECTVALUE
                });
            }

            return Content(JsonConvertHelper.GetSerializes(objlist));
            #endregion
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns>结果</returns>
        [HttpPost]
        [HandleException]
        public ActionResult Delete()
        {
            string selectKey = Request.Form["selectKey"];
            ServiceAgent wcfAgent = this.GetServiceAgent();
            try
            {
                int result = wcfAgent.InvokeServiceFunction<int>("MendRecorderBLL_Delete", selectKey);
                if (result == 0)
                {
                    SetMessage("删除失败!");
                    return List(true);
                }
                SetMessage(AppResource.DeleteMessage);
                return List(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }

}