using System;
using System.Collections.Generic;
using System.Data;
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 module, int teamId, int type, string desc, int userId)
        {
            var db = AppSettingsHelper.GetSqlSugar();
            db.Insertable(new TFS_OperateLog
            {
                FModule = module,
                FTeamID = teamId,
                FType = type,
                FDesc = desc,
                FAddUser = userId,
                FAddDate = DateTime.Now
            }).ExecuteCommand();
        }

        public static DataTable GetLogs(int limit)
        {
            SqlSugarClient db = AppSettingsHelper.GetSqlSugar();
            string strSql = @"
                select TOP(" + limit + @")
	                CONVERT(varchar(100), a.FAddDate, 120) as '操作时间',
	                b.FName as '操作人',
	                CASE a.FModule
		                when 1 THEN '待办事项'
		                WHEN 2 THEN '协同路线'
		                WHEN 3 THEN '物料路线'
		                WHEN 4 THEN '试验号变更'
		                WHEN 5 THEN '清单管理'
		                WHEN 6 THEN '系统设置'
		                ELSE ''
	                END AS '模块',
	                a.FTeamID as '协同ID',
	                c.FName as '来源类型',
	                LEFT(a.FDesc, 100) as '操作描述'
                from
	                TFS_OperateLog a
                left join TUser b on
	                a.FAddUser = b.FID
                left join TBasicCode c on
	                a.FType = c.FCode 
                WHERE c.FType = 33
                ORDER by
	                a.FAddDate desc
                ";

            DataTable data = db.Ado.GetDataTable(strSql);
            return data;
        }

    }
}
