using DeviceRepairAndOptimization.Data.Plan;
using DeviceRepairAndOptimization.Models;
using DeviceRepairAndOptimization.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace DeviceRepairAndOptimization.Data
{
    public class PlanMaintenance
    {
        private static PlanMaintenance manager;

        public static PlanMaintenance Instance
        {
            get
            {
                if (manager == null)
                    manager = new PlanMaintenance();
                return manager;
            }
        }

        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        public List<DrivePlanModel> GetAll()
        {
            List<DrivePlanModel> lst = null;

            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        APIResponseData result = ApiHelper.Instance.SendMessage(new CsharpHttpHelper.HttpItem
                        {
                            URL = ServiceRoute.GetAllPlans,
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8"
                        });

                        if (result.Code == 1)
                        {
                            lst = JsonConvert.DeserializeObject<List<DrivePlanModel>>(result.Data + "");
                        }
                        else
                        {
                            throw new Exception(result.Message);
                        }

                        #endregion
                        break;
                    case "sql":
                        #region sql

                        lst = PlanAccess.Instance().GetAll();

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return lst;
        }

        public APIResponseData Single(int AutoID)
        {
            APIResponseData api = new APIResponseData { Code = -1, Message = "获取数据失败！" };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        api = ApiHelper.Instance.SendMessage(new CsharpHttpHelper.HttpItem
                        {
                            URL = ServiceRoute.GetSinglePlan,
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { autoid = AutoID }),
                        });

                        #endregion
                        break;
                    case "sql":
                        #region sql

                        api = PlanAccess.Instance().Single(AutoID);

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return api;
        }

        /// <summary>
        /// 获取本月保养计划
        /// </summary>
        /// <returns></returns>
        public List<DrivePlanModel> GetTipsPlan()
        {
            List<DrivePlanModel> lst = null;

            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        APIResponseData result = ApiHelper.Instance.SendMessage(new CsharpHttpHelper.HttpItem
                        {
                            URL = ServiceRoute.GetCurrentMonthPlanByYear,
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8"
                        });

                        if (result.Code == 1)
                        {
                            lst = JsonConvert.DeserializeObject<List<DrivePlanModel>>(result.Data + "");
                        }
                        else
                        {
                            throw new Exception(result.Message);
                        }

                        #endregion
                        break;
                    case "sql":
                        #region sql

                        lst = PlanAccess.Instance().GetTipsPlan();

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return lst;
        }

        /// <summary>
        /// 批量插入数据
        /// </summary>
        /// <param name="lst"></param>
        /// <returns></returns>
        public int InsertDatas(List<DrivePlanModel> lst)
        {
            int Count = 0;
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        string postData = JsonConvert.SerializeObject(new
                        {
                            datas = lst
                        });
                        APIResponseData result = ApiHelper.Instance.PostWebRequest(ServiceRoute.InsertDataBulk, postData, GlobalInfo.token);

                        if (result.Code == 1)
                        {
                            int.TryParse(result.Data + "", out Count);
                        }
                        else
                        {
                            throw new Exception(result.Message);
                        }

                        break;
                    case "sql":

                        Count = PlanAccess.Instance().InsertDatas(lst);

                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return Count;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="lst"></param>
        /// <returns></returns>
        public int Delete(int Year, int EquipmentAutoID)
        {
            int Count = 0;
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        APIResponseData result = ApiHelper.Instance.SendMessage(
                            new CsharpHttpHelper.HttpItem
                            {
                                URL = ServiceRoute.DeletePlanByPrimaryKey,
                                Method = "Post",
                                ContentType = "application/json; charset=utf-8",
                                Postdata = JsonConvert.SerializeObject(new { Year = Year, EquipmentAutoID = EquipmentAutoID })
                            }
                        );

                        if (result.Code == 1)
                        {
                            int.TryParse(result.Data + "", out Count);
                        }
                        else
                        {
                            throw new Exception(result.Message);
                        }

                        break;
                    case "sql":

                        Count = PlanAccess.Instance().Delete(Year, EquipmentAutoID);

                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return Count;
        }

        /// <summary>
        /// 根据设备主键ID取指定年份计划数据
        /// </summary>
        /// <param name="Year"></param>
        /// <param name="EquipmentAutoID"></param>
        /// <returns></returns>
        public View_AnnualMaintenancePlan GetDataByEquipmentAutoIdOnYear(int Year, int EquipmentAutoID)
        {
            View_AnnualMaintenancePlan view = null;

            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        APIResponseData result = ApiHelper.Instance.SendMessage(
                            new CsharpHttpHelper.HttpItem
                            {
                                URL = ServiceRoute.GetDataByEquipmentAutoIdOnYear,
                                Method = "Post",
                                ContentType = "application/json; charset=utf-8",
                                Postdata = JsonConvert.SerializeObject(new { Year = Year, EquipmentAutoID = EquipmentAutoID })
                            }
                        );

                        if (result.Code == 1)
                        {
                            view = JsonConvert.DeserializeObject<View_AnnualMaintenancePlan>(result.Data + "");
                        }
                        else
                        {
                            throw new Exception(result.Message);
                        }

                        break;
                    case "sql":

                        view = PlanAccess.Instance().GetDataByEquipmentAutoIdOnYear(Year, EquipmentAutoID);

                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return view;
        }

        /// <summary>
        /// 获取所有年计划
        /// </summary>
        /// <returns></returns>
        public List<View_AnnualMaintenancePlan> GetList()
        {
            List<View_AnnualMaintenancePlan> lst = null;

            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        APIResponseData result = ApiHelper.Instance.SendMessage(new CsharpHttpHelper.HttpItem
                        {
                            URL = ServiceRoute.GetAllPlans2,
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8"
                        });

                        if (result.Code == 1)
                        {
                            lst = JsonConvert.DeserializeObject<List<View_AnnualMaintenancePlan>>(result.Data + "");
                        }
                        else
                        {
                            throw new Exception(result.Message);
                        }

                        #endregion
                        break;
                    case "sql":
                        #region sql

                        lst = PlanAccess.Instance().GetList();

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return lst;
        }

        /// <summary>
        /// 导出EXCEL的数据
        /// </summary>
        /// <returns></returns>
        public List<View_YearsMaintenancePlansExport> ExportXlsxDatas(int Year)
        {
            List<View_YearsMaintenancePlansExport> lst = null;

            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        APIResponseData result = ApiHelper.Instance.SendMessage(new CsharpHttpHelper.HttpItem
                        {
                            URL = ServiceRoute.GetExcelDatas,
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",//返回类型    可选项有默认值
                            Postdata = JsonConvert.SerializeObject(new { Year = Year })
                        });

                        if (result.Code == 1)
                        {
                            lst = JsonConvert.DeserializeObject<List<View_YearsMaintenancePlansExport>>(result.Data + "");
                        }
                        else
                        {
                            throw new Exception(result.Message);
                        }

                        #endregion
                        break;
                    case "sql":
                        #region sql

                        lst = PlanAccess.Instance().ExportXlsxDatas();

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return lst;
        }




    }
}