using CsharpHttpHelper;
using DevExpress.Xpo.DB;
using DeviceRepairAndOptimization.Data;
using DeviceRepairAndOptimization.Models;
using DeviceRepairAndOptimization.Models.History;
using DeviceRepairAndOptimization.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using static DevExpress.XtraEditors.Mask.MaskSettings;

namespace DeviceRepairAndOptimization.Business
{
    public class HistoryManager
    {
        private static HistoryManager manager;
        public static HistoryManager Instance
        {
            get
            {
                if (manager == null)
                    manager = new HistoryManager();
                return manager;
            }
        }

        public APIResponseData WriteLog<T>(string url, T model) where T : class
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "日志写入失败！" };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        result = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = url,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(model.ToArrayAndCompress())
                        });
                        break;

                    default:
                        result = HistoryAccess.Instance.AddLog(model);
                        break;
                }
                return result;
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 获取用户历史密码
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public APIResponseData GetUserPwdHis(int UserID)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            //try
            //{
            //    switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
            //    {
            //        case "api":
            //            apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
            //            {
            //                URL = ServiceRoute.GetUserPwdHis,
            //                Method = "Post",
            //                ContentType = "application/json;charset=utf-8",
            //                Postdata = JsonConvert.SerializeObject(new { userid = UserID })
            //            });
            //            break;

            //        default:
            //            apiResponseData = HistoryAccess.Instance.GetPwdHis(UserID);
            //            break;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    apiResponseData.Code = -1;
            //    apiResponseData.Message = ex.Message;
            //}

            return apiResponseData;
        }


        #region new

        /// <summary>
        /// 设备信息日志写入
        /// </summary>
        /// <param name="header"></param>
        /// <param name="PrimaryKey"></param>
        /// <param name="OperationType"></param>
        /// <returns></returns>
        public APIResponseData DeviceHistoryWrite(string EquipmentID, string OperationType)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = HistoryRoute.DeviceHistoryWrite,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { equipmentid = EquipmentID, operationtype = OperationType })
                        });
                        break;

                    default:
                        apiResponseData = HistoryAccess.Instance.DeviceHistoryWrite(new HeaderModel
                        {
                            ClientMac = ComputerHelper.GetMacAddress,
                            ClientName = ComputerHelper.GetComputerName,
                            IPAddress = ComputerHelper.GetIPAddress,
                            Operator = GlobalInfo.CurrentUser.AutoID,
                            OperatorName = GlobalInfo.CurrentUser.RealName,
                        }, EquipmentID, OperationType);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 设备保养日志写入
        /// </summary>
        /// <param name="header"></param>
        /// <param name="PrimaryKey"></param>
        /// <param name="OperationType"></param>
        /// <returns></returns>
        public APIResponseData DeviceMaintenanceHistoryWrite(int PrimaryKey, string OperationType)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = HistoryRoute.DeviceMaintenanceHistoryWrite,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { primarykey = PrimaryKey, operationtype = OperationType })
                        });
                        break;

                    default:
                        apiResponseData = HistoryAccess.Instance.DeviceMaintenanceHistoryWrite(new HeaderModel
                        {
                            ClientMac = ComputerHelper.GetMacAddress,
                            ClientName = ComputerHelper.GetComputerName,
                            IPAddress = ComputerHelper.GetIPAddress,
                            Operator = GlobalInfo.CurrentUser.AutoID,
                            OperatorName = GlobalInfo.CurrentUser.RealName,
                        }, PrimaryKey, OperationType);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 点检表日志写入
        /// </summary>
        /// <param name="header"></param>
        /// <param name="PrimaryKey"></param>
        /// <param name="OperationType"></param>
        /// <returns></returns>
        public APIResponseData FormHistoryWrite(int PrimaryKey, string OperationType)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = HistoryRoute.FormHistoryWrite,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { primarykey = PrimaryKey, operationtype = OperationType })
                        });
                        break;

                    default:
                        apiResponseData = HistoryAccess.Instance.FormHistoryWrite(new HeaderModel
                        {
                            ClientMac = ComputerHelper.GetMacAddress,
                            ClientName = ComputerHelper.GetComputerName,
                            IPAddress = ComputerHelper.GetIPAddress,
                            Operator = GlobalInfo.CurrentUser.AutoID,
                            OperatorName = GlobalInfo.CurrentUser.RealName,
                        }, PrimaryKey, OperationType);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 设备保养计划日志写入
        /// </summary>
        /// <param name="header"></param>
        /// <param name="PrimaryKey"></param>
        /// <param name="OperationType"></param>
        /// <returns></returns>
        public APIResponseData PlanHistoryWrite(int PrimaryKey, string OperationType)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = HistoryRoute.PlanHistoryWrite,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { primarykey = PrimaryKey, operationtype = OperationType })
                        });
                        break;

                    default:
                        apiResponseData = HistoryAccess.Instance.PlanHistoryWrite(new HeaderModel
                        {
                            ClientMac = ComputerHelper.GetMacAddress,
                            ClientName = ComputerHelper.GetComputerName,
                            IPAddress = ComputerHelper.GetIPAddress,
                            Operator = GlobalInfo.CurrentUser.AutoID,
                            OperatorName = GlobalInfo.CurrentUser.RealName,
                        }, PrimaryKey, OperationType);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 角色权限日志写入
        /// </summary>
        /// <param name="header"></param>
        /// <param name="his"></param>
        /// <param name="OperationType"></param>
        /// <returns></returns>
        public APIResponseData RoleAuthHistoryWrite(List<RoleAuthHistory> his)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                if (his == null || his.Count == 0)
                    throw new Exception("对象集合为为空值！");

                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = HistoryRoute.RoleAuthHistoryWrite,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(his.ToArrayAndCompress())
                        });
                        break;

                    default:
                        apiResponseData = HistoryAccess.Instance.RoleAuthHistoryWrite(new HeaderModel
                        {
                            ClientMac = ComputerHelper.GetMacAddress,
                            ClientName = ComputerHelper.GetComputerName,
                            IPAddress = ComputerHelper.GetIPAddress,
                            Operator = GlobalInfo.CurrentUser.AutoID,
                            OperatorName = GlobalInfo.CurrentUser.RealName,
                        }, his);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 角色信息变更日志写入
        /// </summary>
        /// <param name="header"></param>
        /// <param name="PrimaryKey"></param>
        /// <param name="OperationType"></param>
        /// <param name="OperationContent"></param>
        /// <returns></returns>
        public APIResponseData RoleInfoChangeHistoryWrite(int PrimaryKey, string OperationType, string OperationContent)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = HistoryRoute.RoleInfoChangeHistoryWrite,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { primarykey = PrimaryKey, operationtype = OperationType, operationcontent = OperationContent })
                        });
                        break;

                    default:
                        apiResponseData = HistoryAccess.Instance.RoleInfoChangeHistoryWrite(new HeaderModel
                        {
                            ClientMac = ComputerHelper.GetMacAddress,
                            ClientName = ComputerHelper.GetComputerName,
                            IPAddress = ComputerHelper.GetIPAddress,
                            Operator = GlobalInfo.CurrentUser.AutoID,
                            OperatorName = GlobalInfo.CurrentUser.RealName,
                        }, PrimaryKey, OperationType, OperationContent);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 用户信息变更日志写入
        /// </summary>
        /// <param name="header"></param>
        /// <param name="PrimaryKey"></param>
        /// <param name="OperationType"></param>
        /// <param name="OperationContent"></param>
        /// <returns></returns>
        public APIResponseData UserInfoChangeHistoryWrite(int PrimaryKey, string OperationType, string OperationContent)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = HistoryRoute.UserInfoChangeHistoryWrite,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { primarykey = PrimaryKey, operationtype = OperationType, operationcontent = OperationContent })
                        });
                        break;

                    default:
                        apiResponseData = HistoryAccess.Instance.UserInfoChangeHistoryWrite(new HeaderModel
                        {
                            ClientMac = ComputerHelper.GetMacAddress,
                            ClientName = ComputerHelper.GetComputerName,
                            IPAddress = ComputerHelper.GetIPAddress,
                            Operator = GlobalInfo.CurrentUser.AutoID,
                            OperatorName = GlobalInfo.CurrentUser.RealName,
                        }, PrimaryKey, OperationType, OperationContent);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 用户账户 锁定/解锁 日志写入
        /// </summary>
        /// <param name="header"></param>
        /// <param name="PrimaryKey"></param>
        /// <param name="OperationType"></param>
        /// <returns></returns>
        public APIResponseData UserLockHistoryWrite(int PrimaryKey, string OperationType)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = HistoryRoute.UserLockHistoryWrite,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { primarykey = PrimaryKey, operationtype = OperationType })
                        });
                        break;

                    default:
                        apiResponseData = HistoryAccess.Instance.UserLockHistoryWrite(new HeaderModel
                        {
                            ClientMac = ComputerHelper.GetMacAddress,
                            ClientName = ComputerHelper.GetComputerName,
                            IPAddress = ComputerHelper.GetIPAddress,
                            Operator = GlobalInfo.CurrentUser.AutoID,
                            OperatorName = GlobalInfo.CurrentUser.RealName,
                        }, PrimaryKey, OperationType);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 用户登录/登出 日志写入
        /// </summary>
        /// <param name="header"></param>
        /// <param name="PrimaryKey"></param>
        /// <param name="OperationType"></param>
        /// <returns></returns>
        public APIResponseData UserLoginHistoryWrite(int PrimaryKey, string OperationType)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = HistoryRoute.UserLoginHistoryWrite,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { primarykey = PrimaryKey, operationtype = OperationType })
                        });
                        break;

                    default:
                        apiResponseData = HistoryAccess.Instance.UserLoginHistoryWrite(new HeaderModel
                        {
                            ClientMac = ComputerHelper.GetMacAddress,
                            ClientName = ComputerHelper.GetComputerName,
                            IPAddress = ComputerHelper.GetIPAddress,
                            Operator = GlobalInfo.CurrentUser.AutoID,
                            OperatorName = GlobalInfo.CurrentUser.RealName,
                        }, PrimaryKey, OperationType);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 用户密码修改 日志写入
        /// </summary>
        /// <param name="header"></param>
        /// <param name="PrimaryKey"></param>
        /// <param name="OperationType"></param>
        /// <param name="PwdOld"></param>
        /// <returns></returns>
        public APIResponseData UserPassChangeHistoryWrite(int PrimaryKey, string OperationType, string PwdOld)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = HistoryRoute.UserPassChangeHistoryWrite,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { primarykey = PrimaryKey, operationtype = OperationType, pwdold = PwdOld })
                        });
                        break;

                    default:
                        apiResponseData = HistoryAccess.Instance.UserPassChangeHistoryWrite(new HeaderModel
                        {
                            ClientMac = ComputerHelper.GetMacAddress,
                            ClientName = ComputerHelper.GetComputerName,
                            IPAddress = ComputerHelper.GetIPAddress,
                            Operator = GlobalInfo.CurrentUser.AutoID,
                            OperatorName = GlobalInfo.CurrentUser.RealName,
                        }, PrimaryKey, OperationType, PwdOld);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 用户密码错误日志写入
        /// </summary>
        /// <param name="header"></param>
        /// <param name="PrimaryKey"></param>
        /// <param name="OperationType"></param>
        /// <returns></returns>
        public APIResponseData UserPwdErrorHistoryWrite(int PrimaryKey, string OperationType)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = HistoryRoute.UserPwdErrorHistoryWrite,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { primarykey = PrimaryKey, operationtype = OperationType })
                        });
                        break;

                    default:
                        apiResponseData = HistoryAccess.Instance.UserPwdErrorHistoryWrite(new HeaderModel
                        {
                            ClientMac = ComputerHelper.GetMacAddress,
                            ClientName = ComputerHelper.GetComputerName,
                            IPAddress = ComputerHelper.GetIPAddress,
                            Operator = GlobalInfo.CurrentUser.AutoID,
                            OperatorName = GlobalInfo.CurrentUser.RealName,
                        }, PrimaryKey, OperationType);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 用户角色日志写入
        /// </summary>
        /// <param name="header"></param>
        /// <param name="PrimaryKey"></param>
        /// <param name="OperationType"></param>
        /// <param name="OperationContent"></param>
        /// <returns></returns>
        public APIResponseData UserRoleHistoryWrite(int PrimaryKey, string OperationType, string OperationContent)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = HistoryRoute.UserRoleHistoryWrite,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { primarykey = PrimaryKey, operationtype = OperationType, operationcontent = OperationContent })
                        });
                        break;

                    default:
                        apiResponseData = HistoryAccess.Instance.UserRoleHistoryWrite(new HeaderModel
                        {
                            ClientMac = ComputerHelper.GetMacAddress,
                            ClientName = ComputerHelper.GetComputerName,
                            IPAddress = ComputerHelper.GetIPAddress,
                            Operator = GlobalInfo.CurrentUser.AutoID,
                            OperatorName = GlobalInfo.CurrentUser.RealName,
                        }, PrimaryKey, OperationType, OperationContent);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        #endregion
    }
}
