using System;
using System.Collections.Generic;
using System.Xml.Linq;
using FactorySystemCommon;
using FactorySystemModel.ResponseModel;
using FactorySystemModel.SqlSugarModel;
using SqlSugar;

namespace FactorySystemBll
{
    public class OperateLogBll
    {
        public object GetPageList(Dictionary<string, object> inParam, out int totalCount, string orderBy = "a.FID desc")
        {
            totalCount = 0;
            int pageIndex = 1, pageSize = 10;
            List<string> paramName = new List<string>() { "FDeleted!=1" };
            List<SugarParameter> paramVal = new List<SugarParameter>();
            if (inParam != null && inParam.Count > 0)
            {
                foreach (var item in inParam)
                {
                    if (item.Key == "FPageIndex" || item.Key == "PageIndex")
                    {
                        int.TryParse(item.Value.ToString(), out pageIndex);
                    }
                    else if (item.Key == "FPageSize" || item.Key == "PageSize")
                    {
                        int.TryParse(item.Value.ToString(), out pageSize);
                    }
                    else
                    {
                        try
                        {
                            //检索，全转成字符串
                            paramName.Add(item.Key + "=@" + item.Key);
                            paramVal.Add(new SugarParameter("@" + item.Key, item.Value.ToString()));
                        }
                        catch (Exception) { }
                    }
                }
            }
            SqlSugarClient db = AppSettingsHelper.GetSqlSugar();
            var temp = db.Queryable<TFS_OperateLog, TUser>((a, b) => new JoinQueryInfos(JoinType.Left, a.FAddUser == b.FID))
                .Where(string.Join(" and ", paramName), paramVal).OrderBy(orderBy).Select<dynamic>("a.*,b.FName as FOptUser");
            return temp.ToPageList(pageIndex, pageSize, ref totalCount);
        }

        public List<LogRow> GetLogsByPage(string FName, int FType, int FModule, string dateFrom, string dateTo, int pageNumber, int pageSize, out int totalNumber)
        {
            SqlSugarClient db = AppSettingsHelper.GetSqlSugar();
            totalNumber = 0;
            List<LogRow> logs = db.Queryable<TFS_OperateLog, TUser>((a, b)=> new JoinQueryInfos(JoinType.Left, a.FAddUser == b.FID))
                .WhereIF(!string.IsNullOrEmpty(FName), (a, b) => b.FName.Contains(FName))
                .WhereIF(FType > 0, (a, b) => a.FType == FType)
                .WhereIF(FModule > 0, (a, b) => a.FModule == FModule)
                .WhereIF(!string.IsNullOrEmpty(dateFrom), (a, b) => a.FAddDate >= DateTime.Parse(dateFrom))
                .WhereIF(!string.IsNullOrEmpty(dateTo), (a, b) => a.FAddDate <= DateTime.Parse(dateTo))
                .Select<LogRow>("a.*,b.FName").OrderBy((a) => a.FID, OrderByType.Desc)
                .ToPageList(pageNumber, pageSize, ref totalNumber);

            return logs;
        }

        /// <summary>
        /// 保存日志
        /// </summary>
        /// <param name="teamId">协同ID</param>
        /// <param name="type">来源类型</param>
        /// <param name="desc">描述</param>
        /// <param name="userId">操作人</param>
        public static void Add(int teamId, int type, string desc, int userId)
        {
            var db = AppSettingsHelper.GetSqlSugar();
            db.Insertable(new TFS_OperateLog
            {
                FTeamID = teamId,
                FType = type,
                FDesc = desc,
                FAddUser = userId,
                FAddDate = DateTime.Now
            }).ExecuteCommand();
        }

    }
}
