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

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

        /// <summary>
        /// 批量插入数据
        /// </summary>
        /// <param name="lst"></param>
        /// <returns></returns>
        public APIResponseData Insertable(List<DeviceRepairRecordModel> lst)
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                lst.ForEach(x => { x.OperationUser = GlobalInfo.CurrentUser.AutoID; });

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

                    default:
                        result = DeviceRepairRecordAccess.Instance.Insertable(lst, new HeaderModel
                        {
                            ClientMac = ComputerHelper.GetMacAddress,
                            ClientName = ComputerHelper.GetComputerName,
                            IPAddress = ComputerHelper.GetIPAddress,
                            Operator = GlobalInfo.CurrentUser.AutoID,
                            OperatorName = GlobalInfo.CurrentUser.RealName,
                        });
                        break;
                }
                return result;
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 查询设备维修信息
        /// </summary>
        /// <param name="filtervalue"></param>
        /// <returns></returns>
        public APIResponseData Filter(string filtervalue, DateTime starttime, DateTime endtime)
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "查询失败！" };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        result = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRoute.DeviceRepairRecordFilter,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { filtervalue, starttime, endtime })
                        });
                        break;

                    default:
                        result = DeviceRepairRecordAccess.Instance.Filter(filtervalue, starttime, endtime);
                        break;
                }
                return result;
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 设备维修信息修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public APIResponseData Editable(DeviceRepairRecordModel entity)
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        result = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRoute.DeviceRepairEditable,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(entity.ToArrayAndCompress())
                        });
                        break;

                    default:
                        result = DeviceRepairRecordAccess.Instance.Editable(entity, new HeaderModel
                        {
                            ClientMac = ComputerHelper.GetMacAddress,
                            ClientName = ComputerHelper.GetComputerName,
                            IPAddress = ComputerHelper.GetIPAddress,
                            Operator = GlobalInfo.CurrentUser.AutoID,
                            OperatorName = GlobalInfo.CurrentUser.RealName,
                        });
                        break;
                }

                return result;
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
            }
            return result;
        }
    }
}
