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.MD.Web.Models;
using QMAPP.MD.Entity;
using QMAPP.ServicesAgent;
using QMFrameWork.Data;
using QMAPP.Entity;
using QMFrameWork.WebUI.DataSource;
using QMFrameWork.Common.Serialization;
using QMAPP.FJC.Entity.Basic;
using QMAPP.Entity.Sys;
using QMAPP.MD.Web.Models.Sys;

namespace QMAPP.MD.Web.Controllers
{
    /// <summary>
    /// 模   块:逻辑矩阵
    /// 作   者:郭兆福
    /// 时   间:2018年04月20日
    /// </summary>
    public class UserWithMachineController : QController
    {

        #region 获取列表
        /// <summary>
        /// 加载列表
        /// </summary>
        /// <returns>结果</returns>
        [HandleException]
        public ActionResult List(bool? callBack)
        {
            UserWithMachineModel seachModel = new UserWithMachineModel();
            if (callBack == true)
                TryGetSelectBuffer<UserWithMachineModel>(out seachModel);
            seachModel.rownumbers = false;
            seachModel.url = "/UserWithMachine/GetList";
            return View("UserWithMachineList", seachModel);
        }

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

        #region 编辑载入
        /// <summary>
        /// 编辑载入
        /// </summary>
        /// <returns>处理结果</returns>
        [HandleException]
        public ActionResult Edit()
        {
            UserWithMachineModel model = new UserWithMachineModel();

            string ID = Request.Params["USERID"];

            UserWithMachine Entity = new UserWithMachine();
            ServiceAgent wcfAgent = this.GetServiceAgent();
            DataResult<UserWithMachine> result = new DataResult<UserWithMachine>();
            try
            {
                if (string.IsNullOrEmpty(ID) == false)
                {
                    //修改获取原数据
                    Entity.USERID = ID;
                    result = wcfAgent.InvokeServiceFunction<DataResult<UserWithMachine>>("UserWithMachineBLL_Get", Entity);
                    if (result.IsSuccess == false)
                    {
                        SetMessage(result.Msg);
                        return View("UserWithMachineEdit", model);
                    }
                    model = CopyToModel<UserWithMachineModel, UserWithMachine>(result.Result);

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

        #region 保存添加
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns>处理结果</returns>
        [HttpPost]
        [HandleException]
        [ValidateInput(false)]
        public ActionResult UserWithMachineSave(UserWithMachineModel saveModel)
        {

            MachineInfo condition = new MachineInfo();
            UserWithMachine Entity = new UserWithMachine();
            DataResult<int> result = new DataResult<int>();
            ServiceAgent wcfAgent = this.GetServiceAgent();

            //if (string.IsNullOrEmpty(saveModel.EDITFLAG) == false)
            //{
            //    UserWithMachineModel newuserInfo = new UserWithMachineModel();
            //    newuserInfo.USERID = saveModel.USERID;
            //    newuserInfo.EDITFLAG = saveModel.EDITFLAG;
            //    this.EditDelete(newuserInfo);
            //}

            string machineid = "('" + saveModel.MachineIds.Replace(";", "','") + "')";

            condition.PIDS = machineid;

            //获取设备信息
            List<MachineInfo> machinelist = wcfAgent.InvokeServiceFunction<List<MachineInfo>>("UserWithMachineBLL_GetMachineList", condition);

            //用户ID
            Entity.USERID = saveModel.USERID;

            result = wcfAgent.InvokeServiceFunction<DataResult<int>>("UserWithMachineBLL_InsertUserWithMachine", Entity, machinelist);



            return this.GetJsViewResult(string.Format("parent.List(1);parent.showTitle('{0}');parent.closeAppWindow1();", AppResource.SaveMessge));
        }
        #endregion

        #region 主列表界面点击删除
        /// <summary>
        /// 主列表界面点击删除
        /// </summary>
        /// <returns>结果</returns>
        [HttpPost]
        [HandleException]
        public ActionResult Delete(UserWithMachineModel deleteMode)
        {
            string ids = Request.Form["selectKey"];
            ServiceAgent wcfAgent = this.GetServiceAgent();
            DataResult<int> result = new DataResult<int>();
            deleteMode.USERID = ids;
            DataResult<UserWithMachine> Searcchresult = new DataResult<UserWithMachine>();
            Searcchresult = wcfAgent.InvokeServiceFunction<DataResult<UserWithMachine>>("UserWithMachineBLL_Get", deleteMode);
            if (Searcchresult.Result != null)
            {
                deleteMode = CopyToModel<UserWithMachineModel, UserWithMachine>(Searcchresult.Result);

                try
                {
                    result = wcfAgent.InvokeServiceFunction<DataResult<int>>("UserWithMachineBLL_Delete", deleteMode.USERID);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                if (result.IsSuccess == false)
                {
                    SetMessage(result.Msg);
                    return List(true);
                }
            }
            SetMessage(AppResource.DeleteMessage);
            return List(true);
        }
        #endregion

        #region 编辑时删除&编辑后点击保存按钮时删除
        /// <summary>
        /// 编辑时删除&编辑后点击保存按钮时删除
        /// </summary>
        /// <returns>结果</returns>
        [HttpPost]
        [HandleException]
        public ActionResult EditDelete(UserWithMachineModel saveMode)
        {
            ServiceAgent wcfAgent = this.GetServiceAgent();
            DataResult<int> result = new DataResult<int>();
            try
            {
                result = wcfAgent.InvokeServiceFunction<DataResult<int>>("UserWithMachineBLL_EditDelete", saveMode);
                if (result.IsSuccess == false)
                {
                    SetMessage(result.Msg);
                    return Edit();
                }
                SetMessage(AppResource.DeleteMessage);
                return Edit();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取用户信息列表
        /// <summary>
        /// 获取用户信息列表
        /// </summary>
        /// <param name="callBack">是否回调</param>
        /// <returns>用户信息列表</returns>
        [HandleException]
        public ActionResult GetUserList(bool? callBack)
        {
            UserModel seachModel = new UserModel();
            DataPage page = new DataPage();
            ServiceAgent wcfAgent = this.GetServiceAgent();
            User condition = null;
            try
            {
                //获取查询对象
                seachModel.ActivateFlg = "1";
                seachModel = GetModel<UserModel>(seachModel);

                #region 获取缓存值
                if (callBack != null)
                {
                    TryGetSelectBuffer<UserModel>(out seachModel);
                }
                else
                {
                    //保存搜索条件
                    SetSelectBuffer<UserModel>(seachModel);
                }

                #endregion

                //获取前台分页控件的分页大小
                page = this.GetDataPage(seachModel);

                //获取数据
                condition = CopyToModel<User, UserModel>(seachModel);
                QMAPP.ServicesAgent.ServiceAgent agent = this.GetServiceAgent();
                page = agent.InvokeServiceFunction<DataPage>("UserManageBLL_GetList", condition, page);

                //转换结果格式
                DataGridResult<User> result = new DataGridResult<User>();
                result.Rows = JsonConvertHelper.GetDeserialize<List<User>>(page.Result.ToString());
                return Content(result.GetJsonSource());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion 

        #region 点击设备下拉界面保存(提交)
        /// <summary>
        /// 编辑界面填充设备信息
        /// </summary>
        /// <returns></returns>
        [HandleException]
        public ActionResult AddMachineDetail()
        {

            string machineID = Request["MACHINEID"].ToString();
            //物料发货明细实体
            MachineInfo detail = new MachineInfo()
            {
                PID = machineID,
            };
            try
            {
                //实例化服务
                ServiceAgent wcfAgent = this.GetServiceAgent();

                //通过服务调用ProductOffLineBLL中的CheckRfid方法校验填写的RFID是否合法
                MachineInfo dataResult = wcfAgent.InvokeServiceFunction<MachineInfo>("UserWithMachineBLL_GetMachineInfo", detail);

                return Json(dataResult, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        #endregion

        #region 编辑界面填充设备信息

        /// <summary>
        /// 编辑界面填充设备信息
        /// </summary>
        /// <param name="callBack">是否回调</param>
        /// <returns>列表</returns>
        [HandleException]
        public ActionResult GetMachineInfoList(bool? callBack)
        {
            MachineInfoModel seachModel = null;
            DataResult<DataPage> pageResult = new DataResult<DataPage>();
            DataPage page = new DataPage();
            ServiceAgent wcfAgent = this.GetServiceAgent();
            MachineInfo condition = null;
            try
            {
                //获取查询对象
                seachModel = GetModel<MachineInfoModel>();
                #region 获取缓存值
                if (callBack != null)
                {
                    TryGetSelectBuffer<MachineInfoModel>(out seachModel);
                }
                else
                {
                    //保存搜索条件
                    SetSelectBuffer<MachineInfoModel>(seachModel);
                }
                #endregion
                //获取前台分页设置信息
                page = this.GetDataPage(seachModel);
                page.PageSize = 100;
                condition = CopyToModel<MachineInfo, MachineInfoModel>(seachModel);
                if (seachModel.MachineIds != null)
                {
                    string machineid = "('" + seachModel.MachineIds.Replace(";", "','") + "')";
                    condition.PIDS = machineid;
                }


                #region wcf服务统一接口
                pageResult = wcfAgent.InvokeServiceFunction<DataResult<DataPage>>(QMAPP.ServicesAgent.B9BasicService.MachineInfoBLL_GetList.ToString(), condition, page);
                if (pageResult.IsSuccess == false)
                {
                    SetMessage(pageResult.Msg);
                    return List(true);
                }
                DateGridResult<MachineInfo> result = new DateGridResult<MachineInfo>();
                result.Total = pageResult.Result.RecordCount;
                result.Rows = JsonConvertHelper.GetDeserialize<List<MachineInfo>>(pageResult.Result.Result.ToString());
                #endregion
                return Content(result.GetJsonSource());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

             #endregion

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="callBack">是否回调</param>
        /// <returns>列表</returns>
        [HandleException]
        public ActionResult GetListForEdit(bool? callBack)
        {
            UserWithMachineModel seachModel = null;
            DataPage page = null;
            ServiceAgent wcfAgent = this.GetServiceAgent();
            UserWithMachine condition = null;
            DataResult<DataPage> pageResult = new DataResult<DataPage>();
            try
            {
                //获取查询对象
                seachModel = GetModel<UserWithMachineModel>();
                #region 获取缓存值
                if (callBack != null)
                {
                    TryGetSelectBuffer<UserWithMachineModel>(out seachModel);
                }
                else
                {
                    //保存搜索条件
                    SetSelectBuffer<UserWithMachineModel>(seachModel);
                }
                #endregion
                //获取前台分页设置信息
                page = this.GetDataPage(seachModel);
                page.PageSize = 100;
                condition = CopyToModel<UserWithMachine, UserWithMachineModel>(seachModel);
                #region wcf服务统一接口
                pageResult = wcfAgent.InvokeServiceFunction<DataResult<DataPage>>("UserWithMachineBLL_GetList", condition, page);
                if (pageResult.IsSuccess == false)
                {
                    SetMessage(pageResult.Msg);
                    return List(true);
                }
                DateGridResult<UserWithMachine> result = new DateGridResult<UserWithMachine>();
                result.Total = pageResult.Result.RecordCount;
                result.Rows = JsonConvertHelper.GetDeserialize<List<UserWithMachine>>(pageResult.Result.Result.ToString());
                #endregion
                return Content(result.GetJsonSource());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}