using DeviceRepair.Api.Common;
using DeviceRepair.Api.CustomAttribute;
using DeviceRepair.Api.Models;
using DeviceRepair.DataAccess;
using DeviceRepair.Models;
using NLog;
using System;
using System.Web.Http;

namespace DeviceRepair.Api.Controllers
{
    /// <summary>
    /// 设备维修
    /// </summary>
    [RoutePrefix("Api/Maintenance")]
    public class MaintenanceController : CFController
    {
        private static readonly Logger log = LogManager.GetCurrentClassLogger();

        /// <summary>
        /// 查询设备维修单
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        [HttpPost, HttpAuthorize, Route("GetDatas")]
        public APIResponseData GetDatas(DeviceWarrantyRequestFormFilter FilterInfo)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                GetParams();
                apiResponseData = MaintenanceAccess.Instance.GetDatas(FilterInfo, OperationInfo.OperatorCode);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 设备维修
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        [HttpPost, HttpAuthorize, Route("DeviceMaintion")]
        public APIResponseData DeviceMaintion(DeviceWarrantyRequestMaintaionView Entity)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                base.GetParams();
                return MaintenanceAccess.Instance.DeviceMaintion(Entity.TodbModel(), Entity.AccessoriesItems, OperationInfo);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 停机单申请
        /// </summary>
        /// <param name="Form"></param>
        /// <returns></returns>
        [HttpPost, HttpAuthorize, Route("DeviceDownFormAdd")]
        public APIResponseData DeviceDownFormAdd(DeviceWarrantyRequestForm Form)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                base.GetParams();
                return MaintenanceAccess.Instance.DeviceDownFormAdd(Form, OperationInfo);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 停机单 - 评估
        /// </summary>
        /// <param name="AuotID"></param>
        /// <param name="AssessmentCode"></param>
        /// <returns></returns>
        [HttpPost, HttpAuthorize, Route("DownFormAssessment")]
        public APIResponseData DownFormAssessment(DeviceWarrantyEvaluatorInfo Evaluator)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                base.GetParams();
                return MaintenanceAccess.Instance.DownFormAssessment(Evaluator, OperationInfo);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 修改停机单 停机状态
        /// </summary>
        /// <param name="AutoID"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        [HttpPost, HttpAuthorize, Route("ChangeDownStatus")]
        public APIResponseData ChangeDownStatus(int AutoID, int Status)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                base.GetParams();
                return MaintenanceAccess.Instance.ChangeDownStatus(AutoID, Status > 0, OperationInfo);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 双重确认
        /// </summary>
        /// <param name="AutoID"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpPost, HttpAuthorize, Route("ValidateMaintenance")]
        public APIResponseData ValidateMaintenance(int AutoID, int t)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                base.GetParams();
                return MaintenanceAccess.Instance.ValidateMaintenance(AutoID, t, OperationInfo);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 生产部门设备恢复确认
        /// </summary>
        /// <param name="AutoID"></param>
        /// <returns></returns>
        [HttpPost, HttpAuthorize, Route("DeviceResumptionComfirm")]
        public APIResponseData DeviceResumptionComfirm(int AutoID)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                base.GetParams();
                return MaintenanceAccess.Instance.DeviceResumptionComfirm(AutoID, OperationInfo);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取生产批号信息
        /// </summary>
        /// <param name="Batch"></param>
        /// <returns></returns>
        [HttpGet, HttpAuthorize, Route("GetBatchInfoToStaff")]
        public APIResponseData GetBatchInfoToStaff(string Batch)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "获取失败！" };
            try
            {
                return TsSFCAccess.Instance.GetBatchInfoToStaff(Batch);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取当前批次的QE PE邮箱
        /// </summary>
        /// <param name="Batch"></param>
        /// <returns></returns>
        [HttpGet, HttpAuthorize, Route("CurrentBatchManagerEmail")]
        public APIResponseData CurrentBatchManagerEmail(string Batch)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "获取失败！" };
            try
            {
                return TsSFCAccess.Instance.CurrentBatchManagerEmail(Batch);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 判断维修单是否提交
        /// </summary>
        /// <param name="AutoID"></param>
        /// <returns></returns>
        [HttpGet, HttpAuthorize, Route("MaintionDataIsSubmit")]
        public APIResponseData MaintionDataIsSubmit(int AutoID)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "查询失败！" };
            try
            {
                return MaintenanceAccess.Instance.MaintionDataIsSubmit(AutoID);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 判断当前账户是否PE QE
        /// </summary>
        /// <param name="UserCode"></param>
        /// <returns></returns>
        [HttpGet, HttpAuthorize, Route("CurrentIsManager")]
        public APIResponseData CurrentIsManager(string UserCode)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "查询失败！" };
            try
            {
                return TsSFCAccess.Instance.CurrentIsManager(UserCode);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 导出到excel
        /// </summary>
        /// <param name="FilterInfo"></param>
        /// <returns></returns>
        [HttpPost, HttpAuthorize, Route("GetXlsxData")]
        public APIResponseData GetXlsxData(DeviceWarrantyRequestFormFilter FilterInfo)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                GetParams();
                apiResponseData = MaintenanceAccess.Instance.GetXlsxData(FilterInfo, OperationInfo.OperatorCode);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }
    }
}
