using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using GameData;
using ObjectModel.Club;
using MrWu;
using MrWu.Debug;
using Server.DB.Sql;
using GameData.Club;

/// <summary>
/// 竞技比赛统计数据存取
/// </summary>
public static class ClubMatchManagerAdo {

    public const dbbase sqldb = dbbase.game2018;
    public static readonly string tb_ClubMatch = "ClubMatch";
    public static readonly string tb_ClubMatchNotes = "ClubMatchNotes";
    public static readonly string tb_ClubMatchPlayWay = "ClubMatchPlayWay";
    public static readonly string tb_ClubMatchScoreNotes = "ClubMatchScoreNotes";
    public static readonly string tb_ClubMatchUserScore = "ClubMatchUserScore";
    public static readonly string tb_ClubMatchPlayWayMaxWinnerScoreRange = "ClubMatchPlayWayMaxWinnerScoreRange";

    public static bool DeleteMatchPlayWayById(int id)
    {
        if (id <= 0) return false;
        var sql = " DELETE  from ClubMatchPlayWay where MatchPlayWayId = @id";
        return sqldb.ExecuteTransaction(t => {
       
            var succ = t.ExecuteNonQuery(sql, new SqlParameter[]{                    
                new SqlParameter("@id",id)
                }) == 1;
            if (!succ)
            {
                throw new Exception("DELETE ClubMatchPlayWay 记录失败！" + sql);
            } 
            return succ;
        });
    }

    //这里需要加个新表

    public static ClubMatch GetClubMatchByClubId(int clubId, out List<ClubMatchUserScore> list, int statusFlag = 1) {
        var statusFlagWhere = statusFlag >= 0 ? $"and statusFlag = {statusFlag} " : string.Empty;
        list = null;
        var sql = new StringBuilder();
        
        //查询比赛 比赛表 连接 玩法表 以及比赛规则  找的是最后一条数据 
        sql.AppendLine($"select p1.*, p2.*, p3.* from");//比赛与玩法及子项，
        sql.AppendLine($"    (select top(1) * from {tb_ClubMatch} where clubId = {clubId} {statusFlagWhere} order by ClubMatchId desc) as p1");
        sql.AppendLine($"left join {tb_ClubMatchPlayWay} as p2");
        sql.AppendLine($"on p1.ClubMatchId = p2.ClubMatchId");
        sql.AppendLine($"left join {tb_ClubMatchPlayWayMaxWinnerScoreRange} as p3");
        sql.AppendLine($"on p2.MatchPlayWayId = p3.MatchPlayWayId");
        sql.AppendLine($"order by p1.ClubMatchId desc, p2.MatchPlayWayId desc");

        sql.AppendLine($"select p2.* from");//参加比赛的玩家分数
        sql.AppendLine($"    (select top(1) * from {tb_ClubMatch} where clubId = {clubId} {statusFlagWhere} order by ClubMatchId desc) as p1");
        sql.AppendLine($"left join {tb_ClubMatchUserScore} as p2");
        sql.AppendLine($"on p1.ClubMatchId = p2.ClubMatchId");
        sql.AppendLine($"where p2.ClubMatchId > 0");
        sql.AppendLine($"order by p2.UserScoreId desc");

        var tabs = sqldb.ExecuteTables(sql.ToString());
        if (tabs.Length > 0 && tabs[0].Rows.Count > 0) {
            var clubMatchs = tabs[0].GetClubMatche();

            list = new List<ClubMatchUserScore>();
            if (tabs.Length > 1 && tabs[1].Rows.Count > 0) {
                foreach (DataRow row in tabs[1].Rows) {
                    var score = row.ToObject<ClubMatchUserScore>();
                    list.Add(score);
                }
            }
            return clubMatchs.Single();
        }
        return null;
    }

    public static bool SaveClubMatch(ClubMatch model) {
        
        Debug.Info($"保存比赛数据:{model.ClubId},{model.StatusFlag},{model.ClubMatchId}");
        
        if (model.ClubMatchId == 0) {
            return AddClubMatch(model);
        } else {
            return UpdateClubMatch(model);
        }
    }

    public static bool AddClubMatch(ClubMatch model) {
        var sql = new StringBuilder();
        sql.Append($"insert into {tb_ClubMatch}(");
        sql.Append($"clubid,ClubMatchName,MatchStartTime,MatchEndTime,DieOutNum,StatusFlag,AutoOpen,HowTopUser,Round,MatchRate,VisibleDesk,isOnlyVisibleMyClub,isVisibleScoreBak,isScoreReset,JsonData) ");
        sql.AppendLine($"values({model.ClubId},@ClubMatchName,@MatchStartTime,@MatchEndTime,{model.DieOutNum},{model.StatusFlag},{model.AutoOpen.SQLValue()},{model.HowTopUser},{model.Round},{model.MatchRate},{model.VisibleDesk},{model.isOnlyVisibleMyClub},{model.isVisibleScoreBak},{model.isScoreReset},@JsonData)");
        sql.AppendLine($"select cast(SCOPE_IDENTITY() as int)");
        return sqldb.ExecuteTransaction(t => {
            var result = t.ExecuteScalar(sql.ToString(), new SqlParameter[]{
                new SqlParameter("@ClubMatchName",model.ClubMatchName),               
                new SqlParameter("@MatchStartTime", model.MatchStartTime),
                new SqlParameter("@MatchEndTime",model.MatchEndTime),
                 new SqlParameter("@JsonData",model.GetJsonData())
            });
            if (result is int id) {
                model.ClubMatchId = id;
            } else {
                throw new Exception("AddClubMatch 增加新记录失败！" + sql);
            }

            if (model.LeagueClubMatch == null) //只有创建比赛的俱乐部才需要保存玩法信息
            {
                if (model.MatchPlayWays != null && model.MatchPlayWays.Count > 0) {
                    foreach (var way in model.MatchPlayWays) {
                        AddOrUpdateClubMatchPlayWay(t, way, model.ClubMatchId);
                    }
                }
            }
            return true;
        });
    }

    public static bool UpdateClubMatch(ClubMatch model) {
        return sqldb.ExecuteTransaction(t => {
            var sql = new StringBuilder();
            sql.Append($"update {tb_ClubMatch} set ");
            sql.Append($"ClubMatchName = @ClubMatchName,");
            sql.Append($"MatchStartTime = @MatchStartTime,");
            sql.Append($"MatchEndTime = @MatchEndTime,");
            sql.Append($"JsonData = @JsonData,");
            sql.Append($"DieOutNum = {model.DieOutNum},");
            sql.Append($"StatusFlag = {model.StatusFlag},");
            sql.Append($"AutoOpen = {model.AutoOpen.SQLValue()},");
            sql.Append($"HowTopUser = {model.HowTopUser},");
            sql.Append($"Round = {model.Round},");
            sql.Append($"isOnlyVisibleMyClub = {model.isOnlyVisibleMyClub},");
            sql.Append($"VisibleDesk = {model.VisibleDesk},");
            sql.Append($"MatchRate = {model.MatchRate},");
            sql.Append($"isVisibleScoreBak={model.isVisibleScoreBak},");
            sql.Append($"isScoreReset={model.isScoreReset} ");
            sql.Append($"where ClubMatchId = {model.ClubMatchId}");

            var succ = t.ExecuteNonQuery(sql.ToString(), new SqlParameter[]{
                    new SqlParameter("@ClubMatchName",model.ClubMatchName),
                    new SqlParameter("@MatchStartTime", model.MatchStartTime),
                    new SqlParameter("@MatchEndTime",model.MatchEndTime),
                    new SqlParameter("@JsonData",model.GetJsonData())
                }) == 1;
            if (!succ) {
                throw new Exception("UpdateClubMatch 更新记录失败！" + sql);
            }

            if (model.LeagueClubMatch == null)
            {
                if (model.MatchPlayWays != null && model.MatchPlayWays.Count > 0) {
                    foreach (var way in model.MatchPlayWays) {
                        AddOrUpdateClubMatchPlayWay(t, way, model.ClubMatchId);
                    }
                }
            }
            return succ;
        });
    }

    /// <summary>
    /// 只更新ClubMatchModel
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public static bool UpdateClubMatchModel(ClubMatch model) {
        return sqldb.ExecuteTransaction(t => {
            var sql = new StringBuilder();
            sql.Append($"update {tb_ClubMatch} set ");
            sql.Append($"ClubMatchName = @ClubMatchName,");
            sql.Append($"MatchStartTime = @MatchStartTime,");
            sql.Append($"MatchEndTime = @MatchEndTime,");
            sql.Append($"DieOutNum = {model.DieOutNum},");
            sql.Append($"StatusFlag = {model.StatusFlag},");
            sql.Append($"AutoOpen = {model.AutoOpen.SQLValue()},");
            sql.Append($"HowTopUser = {model.HowTopUser},");
            sql.Append($"Round = {model.Round},");
            sql.Append($"MatchRate = {model.MatchRate} ");
            sql.Append($"where ClubMatchId = {model.ClubMatchId}");

            var succ = t.ExecuteNonQuery(sql.ToString(), new SqlParameter[]{
                    new SqlParameter("@ClubMatchName",model.ClubMatchName),
                    new SqlParameter("@MatchStartTime", model.MatchStartTime),
                    new SqlParameter("@MatchEndTime",model.MatchEndTime)
                }) == 1;
            if (!succ) {
                throw new Exception("UpdateClubMatch 更新记录失败！" + sql);
            }
            return succ;
        });
    }


    public static void AddOrUpdateClubMatchPlayWay(SqlTransaction t, MatchPlayWay way, int ClubMatchId) {
        var sql = new StringBuilder();
        if (way.MatchPlayWayId == 0) {
            sql.Append($"insert into {tb_ClubMatchPlayWay} (ClubMatchId,weiyima,MinScore,DeskCost,TypeMode,MinFreeDeskCostNum,IsFirstCalcDeskCost,BuresMinScore,IsBuresMin,IsRealCalcScore,LeagueDeskScore)");
            sql.AppendLine($"values({ClubMatchId},@weiyima,{way.MinScore},{way.DeskCost},{way.TypeMode},{way.MinFreeDeskCostNum},{way.IsFirstCalcDeskCost.SQLValue()},{way.BuresMinScore},{way.IsBuresMin.SQLValue()},{way.IsRealCalcScore.SQLValue()},{way.LeagueDeskScore})");
            sql.AppendLine($"select cast(SCOPE_IDENTITY() as int)");
            var result = t.ExecuteScalar(sql.ToString(), new SqlParameter("@weiyima", way.weiyima));
            if (result is int id) {
                way.MatchPlayWayId = id;
            } else {
                throw new Exception("AddOrUpdateClubMatchPlayWay ClubMatchPlayWay 增加新记录失败！" + sql);
            }
            way.ClubMatchId = ClubMatchId;
        } else {
            sql.Append($"update {tb_ClubMatchPlayWay} set ");
            sql.Append($"weiyima = @weiyima,");
            sql.Append($"MinScore = {way.MinScore},");
            sql.Append($"DeskCost = {way.DeskCost},");
            sql.Append($"TypeMode = {way.TypeMode},");
            sql.Append($"MinFreeDeskCostNum = {way.MinFreeDeskCostNum},");
            sql.Append($"IsFirstCalcDeskCost = {way.IsFirstCalcDeskCost.SQLValue()},");
            sql.Append($"BuresMinScore = {way.BuresMinScore},");
            sql.Append($"IsBuresMin = {way.IsBuresMin.SQLValue()},");
            sql.Append($"IsRealCalcScore = {way.IsRealCalcScore.SQLValue()}, ");
            sql.Append($"LeagueDeskScore = @score ");
            sql.Append($"where MatchPlayWayId = {way.MatchPlayWayId}");
            if (t.ExecuteNonQuery(sql.ToString(), new SqlParameter("@weiyima", way.weiyima), new SqlParameter("@score", way.LeagueDeskScore)) != 1) {
                throw new Exception("AddOrUpdateClubMatchPlayWay ClubMatchPlayWay 更新记录失败！" + sql);
            }
            t.ExecuteNonQuery($"delete {tb_ClubMatchPlayWayMaxWinnerScoreRange} where MatchPlayWayId = {way.MatchPlayWayId}");
        }
        if (way.MaxWinnerScoreRanges != null && way.MaxWinnerScoreRanges.Count > 0) {
            foreach (var item in way.MaxWinnerScoreRanges) {
                AddWayMaxWinnerScoreRange(t, item, way.MatchPlayWayId);
            }
        }
    }

    /// <summary>
    /// 返回俱乐部的比赛日志
    /// </summary>
    /// <param name="clubId">记录表的Id</param>
    /// <param name="createTime">返回该时间之后的日志 按时间倒序排序</param>
    /// <returns></returns>
    public static List<ClubMatchNotes> GetClubMatchNotesByClubId(int clubId, DateTime createTime,int topCnt) {
        var sql = new StringBuilder();
        sql.AppendLine($"select top {topCnt} * from {tb_ClubMatchNotes} where clubId = {clubId} and createTime >= '{createTime.Date}' order by ClubMatchNotesId desc");
        Debug.Info($"执行Sql:{sql}");
        var list = sqldb.ExecuteEntities(sql.ToString(), p => p.ToObject<ClubMatchNotes>()).ToList();
        return list;
    }

    static void AddWayMaxWinnerScoreRange(SqlTransaction t, MaxWinnerScoreRange maxWinnerScoreRange, int matchPlayWayId) {
        var sql = new StringBuilder();
        sql.Append($"insert into {tb_ClubMatchPlayWayMaxWinnerScoreRange} (matchPlayWayId,MaxWinnerMinScore,DeskScore)");
        sql.AppendLine($"values({matchPlayWayId},{maxWinnerScoreRange.MaxWinnerMinScore},{maxWinnerScoreRange.DeskScore})");
        sql.AppendLine($"select cast(SCOPE_IDENTITY() as int)");
        var result = t.ExecuteScalar(sql.ToString());
        if (result is int id) {
            maxWinnerScoreRange.MaxWinnerScoreRangeId = id;
        } else {
            throw new Exception("AddOrUpdateWayMaxWinnerScoreRange MaxWinnerScoreRange 增加新记录失败！" + sql);
        }
        maxWinnerScoreRange.MatchPlayWayId = matchPlayWayId;
    }

    // public static List<ClubMatch> GetClubMatches(int clubId, DateTime date) {
    //     return GetClubMatches(clubId, $"and MatchStartTime >= '{date.Date}'");
    // }

    // public static List<ClubMatch> GetClubMatches(int clubId, string otherWhere, int size = -1) {
    //     var sql = new StringBuilder();
    //     var topSize = size > 0 ? $" top ({size})" : string.Empty;
    //
    //     sql.AppendLine($"select p1.*, p2.*, p3.* from");
    //     sql.AppendLine($"    (select {topSize} * from {tb_ClubMatch} where clubId = {clubId} {otherWhere}) as p1");
    //     sql.AppendLine($"left join {tb_ClubMatchPlayWay} as p2");
    //     sql.AppendLine($"on p1.ClubMatchId = p2.ClubMatchId");
    //     sql.AppendLine($"left join {tb_ClubMatchPlayWayMaxWinnerScoreRange} as p3");
    //     sql.AppendLine($"on p2.MatchPlayWayId = p3.MatchPlayWayId");
    //     sql.AppendLine($"order by p1.ClubMatchId desc, p2.MatchPlayWayId desc, p3.MaxWinnerScoreRangeId desc");
    //
    //     var table = sqldb.ExecuteTable(sql.ToString());
    //     return table.GetClubMatche().ToList();
    // }

    /// <summary>
    /// 获取比赛数据，不关联其他数据
    /// </summary>
    /// <param name="clubId"></param>
    /// <param name="topSize"></param>
    /// <returns></returns>
    public static List<ClubMatch> GetClubMatches(int clubId,int topSize=5)
    {
        if(topSize<=0) topSize = 5;
        string sql = $"SELECT top {topSize} * from ClubMatch where ClubId={clubId} order by ClubMatchId desc";
        //Debug.ImportantLog($"获取比赛数据，不关联其他数据 sql:{sql}");
        var table = sqldb.ExecuteTable(sql.ToString());        
        return table.GetClubMatche().ToList();
    }

    static IEnumerable<ClubMatch> GetClubMatche(this DataTable table) {
        var clubMatchs = new Dictionary<int, ClubMatch>();
        foreach (DataRow row in table.Rows) {
            var newWay = row.ToObject<MatchPlayWay>();
            if (newWay.MatchPlayWayId > 0) {
                newWay.MaxWinnerScoreRanges = new List<MaxWinnerScoreRange>();
                if (!clubMatchs.TryGetValue(newWay.ClubMatchId, out var clubMatch)) {
                    clubMatch = row.ToObject<ClubMatch>();
                    clubMatch.InitOtherSetting();
                    clubMatch.MatchPlayWays = new List<MatchPlayWay>();
                    clubMatchs.Add(clubMatch.ClubMatchId, clubMatch);
                }
                var way = clubMatch.MatchPlayWays.SingleOrDefault(p => p.MatchPlayWayId == newWay.MatchPlayWayId);
                if (way == null) {
                    clubMatch.MatchPlayWays.Add(newWay);
                    way = newWay;
                }
                var maxWinnerScoreRange = row.ToObject<MaxWinnerScoreRange>();
                if (maxWinnerScoreRange.MaxWinnerScoreRangeId != 0) {
                    way.MaxWinnerScoreRanges.Add(maxWinnerScoreRange);
                }
            } else {
                //玩法为空的比赛
                var clubMatch = row.ToObject<ClubMatch>();
                clubMatch.InitOtherSetting();
                if (clubMatch.ClubMatchId > 0 && !clubMatchs.Keys.Contains(clubMatch.ClubMatchId)) {
                    clubMatch.MatchPlayWays = new List<MatchPlayWay>();
                    clubMatchs.Add(clubMatch.ClubMatchId, clubMatch);
                }
            }
        }
        return clubMatchs.Values;
    }

    // public static List<ClubMatch> GetTopOneClubMatch(int clubId, int top) {
    //     return GetClubMatches(clubId, "order by ClubMatchId desc", top);
    // }

    // public static void SaveClubMatchScoreNotes(ClubMatchScoreNotes model) {
    //     sqldb.ExecuteTransaction(t => SaveClubMatchScoreNote(t, model));
    // }

    // static bool SaveClubMatchScoreNote(SqlTransaction t, ClubMatchScoreNotes model) {
    //
    //     var sql = new StringBuilder();
    //     sql.Append($"insert into {tb_ClubMatchScoreNotes} (clubId,ClubMatchId,UserId,TypeId,Score,DeskScore,NewScore)");
    //     sql.Append($"values({model.ClubId},{model.ClubMatchId},{model.UserId},{model.TypeId},@Score,@DeskScore,@NewScore)");
    //     if (t.ExecuteNonQuery(sql.ToString(), new SqlParameter("@Score", model.Score), new SqlParameter("@DeskScore", model.DeskScore)
    //         , new SqlParameter("@NewScore", model.NewScore)) != 1) {
    //         throw new Exception("SaveClubMatchScoreNotes ClubMatchScoreNotes 增加新记录失败！" + sql);
    //     }
    //     return true;
    // }

    public static List<ClubMatchScoreNotes> GetClubMatchScoreNotes(int clubId, int clubMatchId, int userId,int cnt) {
        var sql = new StringBuilder();
        sql.Append($"select top {cnt} * from {tb_ClubMatchScoreNotes} where clubId = {clubId} and clubMatchId = {clubMatchId} and userId = {userId} order by ScoreNotesId desc");
        Debug.Info(sql);
        var list = sqldb.ExecuteEntities(sql.ToString(), p => p.ToObject<ClubMatchScoreNotes>()).ToList();
        return list;
    }

    // /// <summary>
    // /// 批量保存比赛日志
    // /// </summary>
    // /// <param name="model"></param>
    // public static void SaveClubMatchNotes(List<ClubMatchNotes> model)
    // {
    //     if (model == null || model.Count <= 0) return;
    //     var transaction = GameDB.Instance.BeginTransaction(dbbase.game2018);
    //     foreach (var item in model)
    //     {
    //         var sql = $"insert into {tb_ClubMatchNotes} (TypeId,clubId,ClubMatchId,Content) values({(int)item.TypeId},{item.ClubId},{item.ClubMatchId},'{item.Content}')";
    //         if (1 != GameDB.Instance.TransactionAddSql(transaction, sql))
    //         {
    //             GameDB.Instance.Rollback(transaction);
    //             return;
    //         }
    //     }
    //     GameDB.Instance.SubTransaction(transaction);
    // }

    /// <summary>
    /// 返回俱乐部的操作日志
    /// </summary>
    /// <returns></returns>
    public static List<ClubMatchNotes> GetClubMatchNotesByClubIdType(int clubId,int type, DateTime createTime)
    {
        var sql = new StringBuilder();
        sql.AppendLine($"select * from {tb_ClubMatchNotes} where clubId = {clubId} and createTime >= '{createTime.Date}' and TypeId={type} order by ClubMatchNotesId desc");
        var list = sqldb.ExecuteEntities(sql.ToString(), p => p.ToObject<ClubMatchNotes>()).ToList();
        return list;
    }

    // public static void SaveClubMatchNotes(ClubMatchNotes model) {
    //     var sql = new StringBuilder();
    //     sql.Append($"insert into {tb_ClubMatchNotes} (TypeId,clubId,ClubMatchId,Content)");
    //     sql.Append($"values({(int)model.TypeId},{model.ClubId},{model.ClubMatchId},@content)");
    //     if (sqldb.ExecuteNonQuery(sql.ToString(), new SqlParameter("@Content", model.Content)) != 1) {
    //         Debug.Fatal("SaveClubMatchNotes ClubMatchNotes 增加新记录失败！" + sql);
    //     }
    // }

    // public static List<ClubMatchNotes> GetClubMatchNotes(int clubId, int clubMatchId) {
    //     var sql = new StringBuilder();
    //     sql.Append($"select * from {tb_ClubMatchNotes} where clubId = {clubId} and clubMatchId = {clubMatchId} order by ClubMatchNotesId desc");
    //     var list = sqldb.ExecuteEntities(sql.ToString(), p => p.ToObject<ClubMatchNotes>()).ToList();
    //     return list;
    // }

    // public static void SaveClubMatchScoreNotesList(List<ClubMatchScoreNotes> model) {
    //     sqldb.ExecuteTransaction(t => model.All(p => SaveClubMatchScoreNote(t, p)));
    // }

    public static ClubMatchUserScore GetClubMatchUserScore(int clubId, int clubMatchId, int userId) {
        var sql = new StringBuilder();
        sql.Append($"select * from {tb_ClubMatchUserScore} where clubId = {clubId} and clubMatchId = {clubMatchId} and userId = {userId}");
        var item = sqldb.ExecuteEntities(sql.ToString(), p => p.ToObject<ClubMatchUserScore>()).SingleOrDefault();
        return item;
    }

    public static bool SaveClubMatchUserScore(ClubMatchUserScore model) {
        return sqldb.ExecuteTransaction(t => SaveClubMatchUserScore(t, model));
    }

    public static bool SaveClubMatchUserScores(List<ClubMatchUserScore> models) {
        return sqldb.ExecuteTransaction(t => models.All(model => SaveClubMatchUserScore(t, model)));
    }

    public static bool SaveClubMatchUserScore(SqlTransaction t, ClubMatchUserScore model) {
        var sql = new StringBuilder();
        var parameters = new[] { new SqlParameter("@Score", model.Score), new SqlParameter("@deskScore", model.DeskScore)
        , new SqlParameter("@ScoreBak", model.ScoreBak)};
        if (model.UserScoreId == 0) {
            sql.Append($"insert into {tb_ClubMatchUserScore} (clubId,ClubMatchId,UserId,Score,DeskScore,ScoreBak)");
            sql.AppendLine($"values({model.ClubId},{model.ClubMatchId},{model.UserId},@Score,@DeskScore,@ScoreBak)");
            sql.Append($"select cast(SCOPE_IDENTITY() as int)");
            var result = t.ExecuteScalar(sql.ToString(), parameters);
            if (result is int id) {
                model.UserScoreId = id;
            } else {
                throw new Exception($"SaveClubMatchUserScore ClubMatchUserScore 增加记录失败！" + sql);
            }
        } else {
            sql.AppendLine($"update {tb_ClubMatchUserScore} set score = @Score, deskScore = @deskScore,ScoreBak=@ScoreBak where UserScoreId = {model.UserScoreId}");
            if (t.ExecuteNonQuery(sql.ToString(), parameters) != 1) {
                throw new Exception("SaveClubMatchUserScore ClubMatchUserScore 更新记录失败！" + sql);
            }
        }
        return true;
    }

    public static List<ClubMatchUserScore> GetClubMatchUserScores(int clubId, int clubMatchId) {
        var sql = new StringBuilder();
        sql.Append($"select * from {tb_ClubMatchUserScore} where clubId = {clubId} and clubMatchId = {clubMatchId} order by UserScoreId desc");
        var list = sqldb.ExecuteEntities(sql.ToString(), p => p.ToObject<ClubMatchUserScore>()).ToList();
        return list;
    }
}
