using FineUIPro;
using System;
using System.Collections;
using System.Linq;
namespace BLL
{
    public static class InterFaceLogService
    {
        public static Model.SGGLDB db = Funs.DB;
        public const string Type1 = "上报";
        public const string Type2 = "下发";
        public const string Type3 = "回调";
        #region 获取列表
        /// 
        /// 记录数
        /// 
        public static int count
        {
            get;
            set;
        }
        public static IQueryable GetInterFaceLogByModle(Model.InterFaceLog table)
        {
            var q = from x in db.InterFaceLog
                    where
                              (string.IsNullOrEmpty(table.InterFaceLogId) || x.InterFaceLogId.Contains(table.InterFaceLogId)) &&
                              (string.IsNullOrEmpty(table.UserId) || x.UserId.Contains(table.UserId)) &&
                              (string.IsNullOrEmpty(table.UnitId) || x.UnitId.Contains(table.UnitId)) &&
                              (string.IsNullOrEmpty(table.InterFaceName) || x.InterFaceName.Contains(table.InterFaceName)) &&
                              (string.IsNullOrEmpty(table.InterFaceUrl) || x.InterFaceUrl.Contains(table.InterFaceUrl)) &&
                              (string.IsNullOrEmpty(table.InterFaceMehtod) || x.InterFaceMehtod.Contains(table.InterFaceMehtod)) &&
                              (string.IsNullOrEmpty(table.InterFaceBody) || x.InterFaceBody.Contains(table.InterFaceBody)) &&
                              (string.IsNullOrEmpty(table.InterFaceReturnData) || x.InterFaceReturnData.Contains(table.InterFaceReturnData)) &&
                              (string.IsNullOrEmpty(table.InterFaceType) || x.InterFaceType.Contains(table.InterFaceType)) &&
                              (string.IsNullOrEmpty(table.IP) || x.IP.Contains(table.IP))
                    orderby x.InterFaceLogDate descending
                    select x
                  ;
            return q;
        }
        /// 获取分页列表
        /// 
        /// 页码
        /// 每页数量
        /// 
        public static IEnumerable getListData(Model.InterFaceLog table, Grid Grid1)
        {
            var q = GetInterFaceLogByModle(table);
            count = q.Count();
            if (count == 0)
            {
                return null;
            }
            q = q.Skip(Grid1.PageSize * (Grid1.PageIndex)).Take(Grid1.PageSize);
            // q = SortConditionHelper.SortingAndPaging(q, Grid1.SortField, Grid1.SortDirection, Grid1.PageIndex, Grid1.PageSize);
            return (from x in q
                    select new
                    {
                        x.InterFaceLogId,
                        x.UserId,
                        x.UnitId,
                        x.InterFaceName,
                        x.InterFaceUrl,
                        x.InterFaceMehtod,
                        x.InterFaceBody,
                        x.InterFaceReturnData,
                        x.InterFaceLogDate,
                        x.InterFaceType,
                        x.LogSate,
                        x.IP,
                    }).ToList();
        }
        #endregion
        public static Model.InterFaceLog GetInterFaceLogById(string InterFaceLogId)
        {
            return db.InterFaceLog.FirstOrDefault(x => x.InterFaceLogId == InterFaceLogId);
        }
        public static void AddInterFaceLog(Model.InterFaceLog newtable)
        {
            Model.InterFaceLog table = new Model.InterFaceLog
            {
                InterFaceLogId = newtable.InterFaceLogId,
                UserId = newtable.UserId,
                UnitId = newtable.UnitId,
                InterFaceName = newtable.InterFaceName,
                InterFaceUrl = newtable.InterFaceUrl,
                InterFaceMehtod = newtable.InterFaceMehtod,
                InterFaceBody = newtable.InterFaceBody,
                InterFaceReturnData = newtable.InterFaceReturnData,
                InterFaceLogDate = newtable.InterFaceLogDate,
                InterFaceType = newtable.InterFaceType,
                LogSate = newtable.LogSate,
                IP = newtable.IP,
            };
            Funs.DB.InterFaceLog.InsertOnSubmit(table);
            Funs.DB.SubmitChanges();
        }
        public static void WriteInterFaceLog(string InterFaceSetlId, string InterFaceBody, string ReturnData, string InterFaceType, bool isSuccess)
        {
            var InterfaceSetModel = InterFaceSetService.GetInterFaceSetById(InterFaceSetlId);
            Model.InterFaceLog table = new Model.InterFaceLog();
            table.InterFaceLogId = SQLHelper.GetNewID(typeof(Model.InterFaceLog));
            table.UserId = Const.sysglyId;
            table.InterFaceLogDate = DateTime.Now;
            table.IP = "";
            table.InterFaceName = InterfaceSetModel.InterFaceName;
            table.InterFaceBody = InterFaceBody;
            table.InterFaceReturnData = ReturnData;
            table.InterFaceType = InterFaceType;
            table.LogSate = isSuccess;
            switch (InterFaceType)
            {
                case Type1:
                    table.UnitId = InterfaceSetModel.UnitId;
                    table.InterFaceUrl = InterfaceSetModel.InterFaceUrl;
                    table.InterFaceMehtod = InterfaceSetModel.UrlReqMethod;
                    break;
                case Type2:
                    table.UnitId = InterfaceSetModel.AuthUnitIds;
                    table.InterFaceUrl = InterfaceSetModel.InterFaceForUrl;
                    table.InterFaceMehtod = InterfaceSetModel.ForUrlReqMethod;
                    break;
                case Type3:
                    table.UnitId = InterfaceSetModel.UnitId;
                    table.InterFaceUrl = InterfaceSetModel.InterFaceCallBackUrl;
                    table.InterFaceMehtod = InterfaceSetModel.CallBackUrlReqMethod;
                    break;
            }
            Funs.DB.InterFaceLog.InsertOnSubmit(table);
            Funs.DB.SubmitChanges();
        }
        public static void UpdateInterFaceLog(Model.InterFaceLog newtable)
        {
            Model.InterFaceLog table = db.InterFaceLog.FirstOrDefault(x => x.InterFaceLogId == newtable.InterFaceLogId);
            if (table != null)
            {
                table.InterFaceLogId = newtable.InterFaceLogId;
                table.UserId = newtable.UserId;
                table.UnitId = newtable.UnitId;
                table.InterFaceName = newtable.InterFaceName;
                table.InterFaceUrl = newtable.InterFaceUrl;
                table.InterFaceMehtod = newtable.InterFaceMehtod;
                table.InterFaceBody = newtable.InterFaceBody;
                table.InterFaceReturnData = newtable.InterFaceReturnData;
                table.InterFaceLogDate = newtable.InterFaceLogDate;
                table.InterFaceType = newtable.InterFaceType;
                table.LogSate = newtable.LogSate;
                table.IP = newtable.IP;
                Funs.DB.SubmitChanges();
            }
        }
        public static void DeleteInterFaceLogById(string InterFaceLogId)
        {
            Model.InterFaceLog table = Funs.DB.InterFaceLog.FirstOrDefault(x => x.InterFaceLogId == InterFaceLogId);
            if (table != null)
            {
                Funs.DB.InterFaceLog.DeleteOnSubmit(table);
                Funs.DB.SubmitChanges();
            }
        }
    }
}