using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QMAPP.Common.Web.Controllers;
using QMAPP.Entity;
using QMAPP.FJC.Entity.Andon;
using QMAPP.FJC.Entity.Basic;
using QMAPP.FJC.Web.Models.Andon;
using QMAPP.FJC.Web.Models.Basic;
using QMAPP.MD.Entity;
using QMAPP.ServicesAgent;
using QMFrameWork.Common.Serialization;
using QMFrameWork.Data;
using QMFrameWork.WebUI.Attribute;
using QMFrameWork.WebUI.DataSource;
using ProcessInfo = QMAPP.FJC.Entity.Basic.ProcessInfo;
using QMFrameWork.Log;
using System.Data;
using QMAPP.FJC.BLL.Andon;

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

        /*
        #region 编辑
        /// <summary>
        /// 编辑载入
        /// </summary>
        /// <returns>处理结果</returns>
        [HandleException]
        public ActionResult Edit()
        {
            CallLogModel model = new CallLogModel();
            string ID = Request.Params["PID"];
            string callType = Request.Params["callType"];
            model.CALL_TYPE = callType;
            CallLogEntity Entity = new CallLogEntity();
            ServiceAgent wcfAgent = this.GetServiceAgent();
            DataResult<CallLogEntity> result = new DataResult<CallLogEntity>();
            try
            {
                if (string.IsNullOrEmpty(ID) == false)
                {
                    //修改获取原数据
                    Entity.PID = ID;
                    result = wcfAgent.InvokeServiceFunction<DataResult<CallLogEntity>>("AndonCallBLL_Get", Entity);
                    if (result.IsSuccess == false)
                    {
                        SetMessage(result.Msg);
                        return View("CallReasonEdit", model);
                    }
                    model = CopyToModel<CallLogModel, CallLogEntity>(result.Result);
                    ////根据工位信息获得工序信息
                    //string kind = model.CALL_TYPE;
                    //var WORKCELL_CODE = "";
                    //ServiceAgent wcfAgent1 = this.GetServiceAgent();
                    //if (string.IsNullOrEmpty(kind) == false)
                    //{
                    //    var dataResult = wcfAgent1.InvokeServiceFunction<List<WorkLoc>>
                    //        ("WorkLocBLL_GetWorkCellFromWorkLocList", new WorkLoc(), kind);
                    //    foreach (WorkLoc item in dataResult)
                    //    {
                    //        WORKCELL_CODE = item.WORKCELL_CODE;
                    //    }
                    //}
                    //model.CALL_TYPE = WORKCELL_CODE;

                }
                return View("CallReasonEdit", model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns>处理结果</returns>
        [HttpPost]
        [HandleException]
        [ValidateInput(false)]
        public ActionResult Save(CallLogModel saveModel)
        {
            CallLogEntity Entity = null;
            ServiceAgent _agent = this.GetServiceAgent();
            DataResult<int> result = new DataResult<int>();
            try
            {
                Entity = CopyToModel<CallLogEntity, CallLogModel>(saveModel);
                if (string.IsNullOrEmpty(Entity.PID) == true)
                {
                    //新增

                    
                    //根据配置文件获得工位,工序
                    string _factoryCode = null;
                    string locationfile = System.Configuration.ConfigurationManager.AppSettings["loactionfile"];
                    if (System.IO.File.Exists(locationfile))
                    {
                        string machinecode = System.IO.File.ReadAllText(locationfile);
                        if (!string.IsNullOrEmpty(machinecode.Trim()))
                        {
                            //设置ip
                            MachineInfo machoneInofModel = new MachineInfo { MACHINECODDE = machinecode.Trim() };
                            var list = _agent.InvokeServiceFunction<List<MachineInfo>>(B9BasicService.MachineInfoBLL_GetAllList.ToString(), machoneInofModel);
                            if (list.Count > 0)
                            {
                                var _worklocCode = list[0].WORKLOC_CODE;
                                var _workcellCode = list[0].WORKCELL_CODE;
                                //根据工序获得工作中心
                                WorkCell workCellModel = new WorkCell { WORKCELL_CODE = _workcellCode };
                                var resultWorkCell = _agent.InvokeServiceFunction<DataResult<WorkCell>>("WorkCellBLL_Get", workCellModel);
                                if (resultWorkCell != null)
                                {
                                    var _workcenterCode = resultWorkCell.Result.WORKCENTER_CODE;
                                    //根据工作中心获得工厂编码
                                    var workCenterModel = new MD.Entity.WorkCenter { WORKCENTER_CODE = _workcenterCode };
                                    var resultWorkCenter = _agent.InvokeServiceFunction<DataResult<MD.Entity.WorkCenter>>("WorkCenterBLL_Get", workCenterModel);
                                    if (resultWorkCenter != null)
                                    {
                                        _factoryCode = resultWorkCenter.Result.FACTORY_CODE;
                                    }
                                }
                            }
                        }
                    }
                    Entity.FACTORY_CODE = _factoryCode;

                    result = _agent.InvokeServiceFunction<DataResult<int>>("AndonCallBLL_InsertCallReason", Entity);
                }
                else
                {
                    //修改
                    result = _agent.InvokeServiceFunction<DataResult<int>>("AndonCallBLL_UpdateCallReason", Entity);

                    //result = wcfAgent.InvokeServiceFunction<DataResult<int>>(QMAPP.ServicesAgent.B9BasicService.CallReasonBLL_Update.ToString(), Entity);
                }
                if (result.IsSuccess == false)
                {
                    SetMessage(result.Msg);
                    return View("CallReasonEdit", 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(CallLogModel saveMode)
        {
            string selectKey = Request.Form["selectKey"];
            ServiceAgent wcfAgent = this.GetServiceAgent();
            DataResult<int> result = new DataResult<int>();
            try
            {
                result = wcfAgent.InvokeServiceFunction<DataResult<int>>("AndonCallBLL_DeleteCallReason", selectKey);
                if (result.IsSuccess == false)
                {
                    SetMessage(result.Msg);
                    return List(true);
                }
                SetMessage(AppResource.DeleteMessage);
                return List(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

         */

        #region 导出excel
        /// <summary>
        /// 导出excel
        /// </summary>
        /// <returns>结果</returns>
        [HandleException]
        public ActionResult ExportExcel()
        {
            DataTable exportDt = new DataTable();
            CallLogModel seachModel = null;
            CallLogEntity condition = null;
            try
            {
                //获取查询对象
                seachModel = GetModel<CallLogModel>();
                condition = CopyToModel<CallLogEntity, CallLogModel>(seachModel);
                condition.TableName = "AndonCallLogExp";

                //获取数据
                //exportDt = this.GetServiceAgent().InvokeServiceFunction<DataTable>("AndonCallBLL_ExportExcelData", condition);
                exportDt = new AndonCallBLL().ExportExcelData(condition);
                //导出
                QMFrameWork.WebUI.Util.IEFileTool efTool = new QMFrameWork.WebUI.Util.IEFileTool();
                return efTool.GetExcelFileResult("AndonCallLogExp", "安东呼叫.xlsx", exportDt);
            }
            catch (Exception ex)
            {
                LogManager.LogHelper.Error(new LogInfo { ErrorInfo = ex, Info = "导出excel", Tag = "安东呼叫" });
                this.SetMessage("导出excel失败");
                return this.List(true);
            }
        }
        #endregion
    }
}