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.FJC.Entity.ProduceManage;
using QMAPP.Entity;
using QMFrameWork.WebUI.DataSource;
using QMFrameWork.Common.Serialization;
using System.Data;

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

        [HandleException]
        public ActionResult List(bool? callBack)
        {
            MendRecorderModel searchmodel = new MendRecorderModel();
            searchmodel.CREATEDATESTART = DateTime.Now.AddDays(-10).ToString("yyyy-MM-dd");
            searchmodel.CREATEDATEEND = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
            searchmodel.ImmediateSearch = false;
            if (callBack == true)
                TryGetSelectBuffer<MendRecorderModel>(out searchmodel);
            searchmodel.rownumbers = true;
            searchmodel.url = "/ScrapRecord/GetList";
            return View("ScrapRecordList", 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>();
            try
            {
                searchmodel = GetModel<MendRecorderModel>();
                if (callBack != null)
                {
                    TryGetSelectBuffer<MendRecorderModel>(out searchmodel);
                }
                else
                {
                    //保存搜索条件
                    SetSelectBuffer<MendRecorderModel>(searchmodel);
                }
                page = this.GetDataPage(searchmodel);
                condition = CopyToModel<MendRecorder, MendRecorderModel>(searchmodel);
                #region wcf服务统一接口
                pageResult = wcfAgent.InvokeServiceFunction<DataResult<DataPage>>("ScrapRecordBLL_GetLists", condition, page);
                DateGridResult<MendRecorder> result = new DateGridResult<MendRecorder>();
                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 ExportExcel()
        {
            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);
                //获取数据
                result = wcfAgent.InvokeServiceFunction<DataResult<DataTable>>("ScrapRecordBLL_GetExportData", 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();
                }

                //因为模版只能保存20条记录,所以截取20条。
                //var rows= result.Result.Select().Take(20);
                //DataTable dt20 =  result.Result.Clone();
                //foreach (DataRow row in rows)
                //{
                //    dt20.ImportRow(row);
                //}

                //导出
                QMFrameWork.WebUI.Util.IEFileTool efTool = new QMFrameWork.WebUI.Util.IEFileTool();
                return efTool.GetExcelFileResult("ScrapRecordExp", "材料报废单.xlsx", result.Result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #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>>("ScrapRecordBLL_GetMenderReasonList", 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>("ScrapRecordBLL_Delete", selectKey);
                if (result == 0)
                {
                    SetMessage("删除失败!");
                    return List(true);
                }
                SetMessage(AppResource.DeleteMessage);
                return List(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

    }
}