using DeviceRepair.Api.Common;
using DeviceRepair.Api.CustomAttribute;
using DeviceRepair.DataAccess;
using DeviceRepair.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Http;

namespace DeviceRepair.Api.Controllers
{
    [RoutePrefix("Api/Plan")]
    public class PlanController : CFController
    {
        /// <summary>
        /// 保养计划批量新增数据
        /// </summary>
        /// <param name="lst"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("InsertDatas")]
        [HttpAuthorize]
        public APIResponseData InsertDatas(List<DriveMaintencePlanInfo> lst)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败!" };
            try
            {
                GetParams();
                if (lst == null || lst.Count == 0)
                    throw new Exception("插入的数据集合,不能为空！");

                return PlanAccess.Instance.InsertDatas(lst, OperationInfo);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message.ToString();
            }
            return apiResponseData;
        }


        /// <summary>
        /// 获取当月保养计划弹出窗口数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetPlanItems")]
        [HttpAuthorize]
        public APIResponseData GetPlanItems()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "数据查询失败！" };

            try
            {
                apiResponseData = PlanAccess.Instance.GetCurrentMonthPlanTips();
                return apiResponseData;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 获取当前年月的待保修项数量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HttpAuthorize]
        [Route("GetCurrentMonthPlanTipsCountAsync")]
        public APIResponseData GetCurrentMonthPlanTipsCountAsync()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "数据查询失败！" };
            try
            {
                apiResponseData = PlanAccess.Instance.GetCurrentMonthPlanTipsCountAsync();
                return apiResponseData;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        [HttpGet]
        [HttpAuthorize]
        [Route("GetCurrentYearPlanSchedule")]
        public APIResponseData GetCurrentYearPlanSchedule()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "数据查询失败！" };
            try
            {
                apiResponseData = PlanAccess.Instance.GetCurrentYearPlanSchedule();
                return apiResponseData;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 根据设备主键ID取指定年份计划数据
        /// </summary>
        /// <param name="Year"></param>
        /// <param name="EquipmentAutoID"></param>
        /// <returns></returns>
        [HttpGet]
        [HttpAuthorize]
        [Route("GetPlanByEquipmentPkAndYear")]
        public APIResponseData GetDataByEquipmentAutoIdOnYear(int Year, int EquipmentAutoID)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = $"获取数据失败！" };
            try
            {
                return PlanAccess.Instance.GetDataByEquipmentAutoIdOnYear(Year, EquipmentAutoID);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 获取全部计划信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HttpAuthorize]
        [Route("GetAllPlans")]
        public APIResponseData GetAllPlans()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = $"获取数据失败！" };
            try
            {
                return PlanAccess.Instance.GetAllPlans();
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }


        /// <summary>
        /// 导出EXCEL的数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HttpAuthorize]
        [Route("ExportXlsxDatas")]
        public APIResponseData ExportXlsxDatas(int Year)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            try
            {
                return PlanAccess.Instance.ExportXlsxDatas(Year);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message.ToString();
            }
            return apiResponseData;
        }

        /// <summary>
        /// 删除计划
        /// </summary>
        /// <param name="Year"></param>
        /// <param name="EquipmentAutoID"></param>
        /// <returns></returns>
        [HttpPost]
        [HttpAuthorize]
        [Route("DeleteByYearAndEquipmentPk")]
        public APIResponseData DeleteByYearAndEquipmentPk(int Year, int EquipmentAutoID)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = $"操作失败！" };
            try
            {
                return PlanAccess.Instance.DeleteByYearAndEquipmentPk(Year, EquipmentAutoID);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message.ToString();
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取当前计划是否存的保养记录
        /// </summary>
        /// <param name="EquipmentAutoID"></param>
        /// <param name="Year"></param>
        /// <returns></returns>
        [HttpGet]
        [HttpAuthorize]
        [Route("GetPlanRecordProgress")]
        public APIResponseData GetPlanRecordProgress(int EquipmentAutoID, int Year)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = $"获取数据失败！" };
            try
            {
                return PlanAccess.Instance.GetPlanRecordProgress(EquipmentAutoID, Year);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message.ToString();
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取设备在年度的全部保养信息
        /// </summary>
        /// <param name="EquipmentAutoID"></param>
        /// <param name="Year"></param>
        /// <returns></returns>
        [HttpGet]
        [HttpAuthorize]
        [Route("GetDeviceInformationPlans")]
        public APIResponseData GetDeviceInformationPlans(int EquipmentAutoID,int Year)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = $"获取数据失败！" };
            try
            {
                return PlanAccess.Instance.GetDeviceInformationPlans(EquipmentAutoID, Year);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message.ToString();
            }
            return apiResponseData;
        }
    }
}
