package db

import (
	"World/club"
	"World/common"
	pb "World/pb"
	"World/utils"
	"common/logger"
	stat "common/statistic"
	"errors"
	"strconv"
	"time"

	b64 "encoding/base64"
)

type NotifyClubGrantFundInfo struct {
	ClubId     uint32
	OperatorId uint32
	TargetId   uint32
	Amount     int64
	GoldAfter  int64
}
type OperateType int

const (
	Add                  = 1
	Subtract OperateType = 2
)

func IsClubNumFull(uid uint32) (bool, error) {
	//SELECT COALESCE(current_clubs,0),COALESCE(clubs_max,0) from dtb_user_main where id = 340
	cmd := "SELECT COALESCE(current_clubs,0),COALESCE(clubs_max,0) from dtb_user_main where id =" + strconv.Itoa(int(uid))
	//	log.Println("IsClubNumFull-cmd:", cmd)
	full, err := IsNumFull(cmd)
	return full, err
}

func IsCreatedAllianceNumFull(clubId uint32) (bool, error) {
	cmd_max_search := "select created_alliance_max from dtb_club where id=" + strconv.Itoa(int(clubId))
	var max int
	var err error
	if max, err = GetSingleIntValue(cmd_max_search); err == nil {
		//SELECT count(id) from dtb_alliance_member where club_id = creater_club_id and club_id =0
		cmd_cur_num := "SELECT count(id) from dtb_alliance_member where creater_club_id =" + strconv.Itoa(int(clubId)) + " and club_id =" + strconv.Itoa(int(clubId))
		var cur int
		cur, err = GetSingleIntValue(cmd_cur_num)

		//俱乐部创建者也放入俱乐部成员表中
		if cur >= max {
			return true, err
		}
	}
	return false, err
}

func IsJoinedAllianceNumFull(clubId uint32) (bool, error) {
	cmd_max_search := "select joined_alliance_max from dtb_club where id=" + strconv.Itoa(int(clubId))
	var max int
	var err error
	if max, err = GetSingleIntValue(cmd_max_search); err == nil {
		//SELECT count(id) from dtb_alliance_member where club_id <> creater_club_id and club_id =0
		cmd_cur_num := "SELECT count(id) from dtb_alliance_member where creater_club_id <>" + strconv.Itoa(int(clubId)) + " and club_id =" + strconv.Itoa(int(clubId))
		var cur int
		cur, err = GetSingleIntValue(cmd_cur_num)

		//俱乐部创建者也放入俱乐部成员表中
		if cur >= max {
			return true, err
		}
	}
	return false, err
}

func IsAdminNumFull(clubId uint32) (bool, error) {
	//SELECT COALESCE(current_admins,0),COALESCE(admins_max,0) from dtb_club where id = 340
	cmd := "SELECT COALESCE(current_admins,0),COALESCE(admins_max,0) from dtb_club where id =" + common.U32toa(clubId)
	result, err := IsNumFull(cmd)
	return result, err
}

func AddClub(cd *club.ClubParams) (uint32, error) {
	//	cmd := "INSERT INTO dtb_club (club_name, club_area,club_img,creater_id,is_private) VALUES ('" + cd.ClubName + "','" + cd.ClubArea + "','" + cd.ClubIcon + "'," + strconv.Itoa(cd.Creater_id) + "," + strconv.Itoa(1) + ")"
	cmd := "INSERT INTO dtb_club (club_name, club_area,club_img,club_thumb,creater_id,is_private,club_level,members_max,current_members,admins_max,created_alliance_max,joined_alliance_max, create_time)	VALUES ('" + cd.ClubName + "','" + cd.ClubArea + "','" + cd.ClubIcon + "','" + cd.ClubIcon + "'," + strconv.Itoa(cd.Creater_id) + "," + strconv.Itoa(1) + "," + strconv.Itoa(cd.ClubLevel) + "," + strconv.Itoa(cd.MemberMax) + "," + strconv.Itoa(cd.CurrentMembers) + "," + strconv.Itoa(cd.AdminsMax) + "," + strconv.Itoa(cd.CreatedAllianceMax) + "," + strconv.Itoa(cd.JoinedAllianceMax) + "," + strconv.Itoa(int(time.Now().Unix())) + ")"

	var clubId uint32 = 0
	err := ExcuteCmd(cmd)
	if err != nil {
		return 0, err
	}

	//	sql := "SELECT LAST_INSERT_ID()"
	sql := "select max(id) from dtb_club"
	clubId, err = GetSingleU32(sql)
	return clubId, err
}

func InsertClubInvitationCode(clubId uint32, code string) error {
	//update dtb_club set invitation_code='LFWLAY' WHERE id=100269;
	cmd := "update dtb_club set invitation_code='" + code + "' WHERE id=" + common.U32toa(clubId)
	err := ExcuteCmd(cmd)
	return err
}

func AddClubMember(clubId uint32, userId uint32) error {
	// INSERT INTO dtb_club_member (club_id, member_id,add_time) VALUES (1,2,1483620096)
	cmd := "INSERT INTO dtb_club_member (club_id, member_id,add_time) VALUES (" + common.U32toa(clubId) + "," + common.U32toa(userId) + "," + strconv.Itoa(int(time.Now().Unix())) + ")"
	//	log.Println("AddClubMember-cmd:", cmd)
	return ExcuteCmd(cmd)
}

func AddApplyMsg(cid uint32, uid uint32, msg string) {
	//INSERT INTO dtb_club_apply(user_id, club_id, apply_msg, add_time) VALUES ('409', '100344', 'hi', '1483620096');
	cmd := "INSERT INTO dtb_club_apply (club_id, user_id,apply_msg,add_time) VALUES ('" + strconv.Itoa(int(cid)) + "','" + strconv.Itoa(int(uid)) + "','" + msg + "'" + "," + strconv.Itoa(int(time.Now().Unix())) + ")"
	ExcuteCmd(cmd)
}

func AddPending(noti pb.NoticeJoinClubToMember) {
	//INSERT INTO t_club_pending (club_id, user_id,chk_status,reason) VALUES (999,888,2,'you win')
	club_id := noti.ClubId
	user_id := noti.Uid
	chk_status := noti.Result
	reason := noti.Reason
	cmd := "INSERT INTO t_club_pending (club_id, user_id,chk_status,reason) VALUES ('" + strconv.Itoa(int(club_id)) + "','" + strconv.Itoa(int(user_id)) + "','" + strconv.Itoa(int(chk_status)) + "','" + reason + "')"
	ExcuteCmd(cmd)
}

func DeleteLeaveallianceNotice(clubAdminId uint32) error {
	cmd := "DELETE FROM t_leavealliance_notice WHERE club_admin_id = " + strconv.Itoa(int(clubAdminId))
	err := ExcuteCmd(cmd)
	return err
}

func AddMsgContent(noti *pb.NoticeSendMsg) {
	msg := noti.Msg

	sEnc := b64.StdEncoding.EncodeToString([]byte(msg))
	//INSERT INTO t_club_msg_content(msg) VALUES ('Hello');
	contentCmd := "INSERT INTO t_club_msg_content(msg) VALUES ('" + sEnc + "')"
	ExcuteCmd(contentCmd)
}

func AddPendingMsg(noti *pb.NoticeSendMsg, memberId uint32) {
	cid := noti.ClubId
	uid := noti.UserId
	time := noti.ChatTime

	sql := "select max(id) from t_club_msg_content"
	if msgId, err := GetSingleIntValue(sql); err == nil {
		cmd := "INSERT INTO t_club_pending_msg (club_id, user_id,receiver_id,chat_time,msg_id) VALUES ('" + strconv.Itoa(int(cid)) + "','" + strconv.Itoa(int(uid)) + "','" + strconv.Itoa(int(memberId)) + "','" + strconv.Itoa(int(time)) + "','" + strconv.Itoa(int(msgId)) + "')"
		ExcuteCmd(cmd)
	}
}

func IsPlayerInClub(cid uint32, uid uint32) (bool, error) {
	// select count(id) from dtb_club_member where club_id = 31 and member_id = 14
	cmd := "select count(id) from dtb_club_member where club_id = " + strconv.Itoa(int(cid)) + " and member_id =" + strconv.Itoa(int(uid))
	result, err := IsMemberExist(cmd)
	return result, err
}

func IsClubInAlliance(AllianceId uint32, cludId uint32) (bool, error) {
	//select count(id) from dtb_alliance_member where alliance_id = 31 and club_id = 14
	cmd := "select count(id) from dtb_alliance_member where alliance_id = " + strconv.Itoa(int(AllianceId)) + " and club_id =" + strconv.Itoa(int(cludId))
	result, err := IsMemberExist(cmd)
	return result, err
}

func IsMemberExist(cmd string) (bool, error) {
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("IsMemberExist Query error:%v cmd:%v", err, cmd)
		return false, err
	}

	for rows.Next() {
		var value string
		err = rows.Scan(&value)
		if err != nil {
			logger.Notic("IsMemberExist Scan error:%v", err)
			return false, err
		}
		count, err := strconv.Atoi(value)
		if count > 0 {
			return true, err
		}
	}
	return false, err
}

func IsClubTotalPlayerFull(cid uint32) (bool, error) {
	cmd_max_search := "select members_max from dtb_club where id=" + strconv.Itoa(int(cid))
	var max int
	var err error
	//	log.Println("IsClubTotalPlayerFull-cmd_max_search:", cmd_max_search)
	if max, err = GetSingleIntValue(cmd_max_search); err == nil {
		//select count(id) from dtb_club_member where club_id=101034
		cmd_cur_num := "select count(id) from dtb_club_member where club_id=" + strconv.Itoa(int(cid))
		var cur int
		//		log.Println("IsClubTotalPlayerFull-cmd_cur_num:", cmd_cur_num)
		cur, err = GetSingleIntValue(cmd_cur_num)

		//俱乐部创建者也放入俱乐部成员表中
		if cur >= max {
			return true, err
		}
	}

	return false, err
}

func IsAllianceFull(AllianceId uint32) (bool, error) {
	cmd_max_search := "select clubs_max from dtb_alliance where id=" + strconv.Itoa(int(AllianceId))
	var max int
	var err error
	if max, err = GetSingleIntValue(cmd_max_search); err == nil {
		cmd_cur_num := "select count(id) from dtb_alliance_member where alliance_id=" + strconv.Itoa(int(AllianceId))
		var cur int
		cur, err = GetSingleIntValue(cmd_cur_num)

		//俱乐部创建者也放入俱乐部成员表中
		if cur >= max {
			return true, err
		}
	}

	return false, err
}

func IsNewApplicant(cid uint32, uid uint32) (bool, error) {
	//select COALESCE(add_time,0),COALESCE(chk_time,0) from dtb_club_apply  WHERE club_id = 100269 and user_id = 389
	cmd := "select COALESCE(add_time,0),COALESCE(chk_time,0) from dtb_club_apply  WHERE club_id = " + strconv.Itoa(int(cid)) + " and user_id=" + strconv.Itoa(int(uid))
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("IsNewApplicant Query error:%v cmd:%v", err, cmd)
		return false, err
	}

	for rows.Next() {
		var addTime int
		var chkTime int

		err = rows.Scan(&addTime, &chkTime)
		if err != nil {
			logger.Notic("IsNewApplicant Scan error:%v", err)
			return false, err
		}

		return (int(time.Now().Unix()) > chkTime), err
	}
	return false, err
}

func IsNewAllianceApplicant(AllianceId uint32, clubId uint32) (bool, error) {
	//select COALESCE(add_time,0),COALESCE(chk_time,0) from dtb_alliance_apply  WHERE alliance_id = 100269 and club_id = 389
	cmd := "select COALESCE(add_time,0),COALESCE(chk_time,0) from dtb_alliance_apply  WHERE alliance_id =" + strconv.Itoa(int(AllianceId)) + " and club_id =" + strconv.Itoa(int(clubId))
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("IsNewAllianceApplicant Query error:%v cmd:%v", err, cmd)
		return false, err
	}

	for rows.Next() {
		var addTime int
		var chkTime int

		err = rows.Scan(&addTime, &chkTime)
		if err != nil {
			logger.Notic("IsNewAllianceApplicant Scan error:%v", err)
			return false, err
		}

		return (int(time.Now().Unix()) > chkTime), err
	}
	return false, err
}

func IsOtherAdminOperated(cid uint32, uid uint32) (bool, error) {
	//select COALESCE(add_time,0),COALESCE(chk_time,0) from dtb_club_apply  WHERE club_id = 100269 and user_id = 389
	cmd := "select COALESCE(add_time,0),COALESCE(chk_time,0) from dtb_club_apply  WHERE club_id = " + strconv.Itoa(int(cid)) + " and user_id=" + strconv.Itoa(int(uid))
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("IsOtherAdminOperated  Query error:%v cmd:%v", err, cmd)
		return false, err
	}

	for rows.Next() {
		var addTime int
		var chkTime int

		err = rows.Scan(&addTime, &chkTime)
		if err != nil {
			logger.Notic("IsOtherAdminOperated Scan error:%v", err)
			return false, err
		}

		return (chkTime > addTime), err
	}
	return false, err
}

func IsOtherAllianceAdminOperated(allianceId uint32, clubId uint32) (bool, error) {
	//select COALESCE(add_time,0),COALESCE(chk_time,0) from dtb_alliance_apply  WHERE alliance_id = 100269 and club_id = 389
	cmd := "select COALESCE(add_time,0),COALESCE(chk_time,0) from dtb_alliance_apply  WHERE alliance_id =" + strconv.Itoa(int(allianceId)) + " and club_id=" + strconv.Itoa(int(clubId))
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("IsOtherAllianceAdminOperated Query error:%v cmd:%v", err, cmd)
		return false, err
	}

	for rows.Next() {
		var addTime int
		var chkTime int

		err = rows.Scan(&addTime, &chkTime)
		if err != nil {
			logger.Notic("IsOtherAllianceAdminOperated error:%v", err)
			return false, err
		}

		return (chkTime > addTime), err
	}
	return false, err
}

func HasOldApplyMsg(cid uint32, uid uint32) (bool, error) {
	//select count(id) from dtb_club_apply  WHERE club_id = 31 and user_id = 14
	cmd := "select count(id) from dtb_club_apply  WHERE club_id = " + strconv.Itoa(int(cid)) + " and user_id=" + strconv.Itoa(int(uid))
	result, err := IsMemberExist(cmd)
	return result, err
}

func HasOldAllianceApplyMsg(allianceId uint32, clubId uint32) (bool, error) {
	//select count(id) from dtb_alliance_apply  WHERE alliance_id = 31 and club_id = 14
	cmd := "select count(id) from dtb_alliance_apply  WHERE alliance_id =" + strconv.Itoa(int(allianceId)) + " and club_id =" + strconv.Itoa(int(clubId))
	result, err := IsMemberExist(cmd)
	return result, err
}

func IsPlayerAlreadyApply(cid uint32, uid uint32) (bool, error) {
	result, err := HasOldApplyMsg(cid, uid)
	newApplicant, err := IsNewApplicant(cid, uid)
	return (result && !newApplicant), err
}

func IsClubAlreadyApply(AllianceId uint32, clubId uint32) (bool, error) {
	result, err := HasOldAllianceApplyMsg(AllianceId, clubId)
	//	newApplicant, err := IsNewApplicant(cid, uid)
	return (result /*&& !newApplicant*/), err
}

func IsClubPlayerEmpty(cid uint32) (bool, error) {
	cmd := "select count(id) from dtb_club_member where club_id=" + strconv.Itoa(int(cid))
	cur, err := GetSingleIntValue(cmd)
	if cur <= 1 { //创建者算一个
		return true, nil
	}

	return false, err
}

func IsAllianceMemberEmpty(allianceId uint32) (bool, error) {
	cmd := "select count(id) from dtb_alliance_member where alliance_id=" + strconv.Itoa(int(allianceId))
	cur, err := GetSingleIntValue(cmd)
	if cur <= 1 { //创建者算一个
		return true, nil
	}

	return false, err
}

func IsExist(cid uint32, tablename string) (bool, error) {
	// select count(id) from  dtb_club  where id = 34
	cmd := "select count(id) from " + tablename + "  where id =" + strconv.Itoa(int(cid))
	count, err := GetSingleIntValue(cmd)
	if count > 0 {
		return true, err
	}

	return false, err
}

func IsJackpotExist(cid uint32, blindLevel int) (bool, error) {
	// select count(id) from t_jackpot where club_id = 100001 and blind_level = 1
	cmd := "select count(id) from t_jackpot where club_id = " + common.U32toa(cid) + "  and blind_level = " + strconv.Itoa(int(blindLevel))
	count, err := GetSingleIntValue(cmd)
	if count > 0 {
		return true, err
	}

	return false, err
}

//SetJackpotAwardType(clubId int, handLevel int32, percent int32) error {
func IsAwardtypeExist(cid uint32, handLevel int32) (bool, error) {
	// select count(id) from t_jackpot_setting where club_id = 100001 and hand_level = 10
	cmd := "select count(id) from t_jackpot_setting where club_id = " + common.U32toa(cid) + "  and hand_level = " + strconv.Itoa(int(handLevel))
	count, err := GetSingleIntValue(cmd)
	if count > 0 {
		return true, err
	}

	return false, err
}

func IsAward2ClubPercentExist(cid uint32) (bool, error) {
	cmd := "select count(id) from t_jackpot_club where club_id = " + common.U32toa(cid)
	count, err := GetSingleIntValue(cmd)
	if count > 0 {
		return true, err
	}

	return false, err
}

func IsClubNameExist(clubNmae string) (bool, error) {
	//SELECT count(id) from dtb_club WHERE club_name = '阿正俱乐部'
	cmd := "SELECT count(id) from dtb_club WHERE club_name = '" + clubNmae + "'"
	count, err := GetSingleIntValue(cmd)
	if count > 0 {
		return true, err
	}

	return false, err
}

func IsMyself(cid uint32, clubNmae string) (bool, error) {
	//SELECT id from dtb_club WHERE club_name = '阿正俱乐部'
	cmd := "SELECT id from dtb_club WHERE club_name =  '" + clubNmae + "'"
	id, err := GetSingleU32(cmd)
	if id == cid {
		return true, err
	}

	return false, err
}

func IsTargetCreater(tid uint32, ClubId uint32) (bool, error) {
	// select count(id) from dtb_club WHERE id =31 and creater_id = 14
	cmd := "select count(id) from  dtb_club  where id =" + strconv.Itoa(int(ClubId)) + " and creater_id =" + strconv.Itoa(int(tid))
	count, err := GetSingleIntValue(cmd)
	if count > 0 {
		return true, err
	}
	return false, err
}

func GetClubType(ClubId uint32) (int, error) {
	//select COALESCE(is_private,0) from dtb_club WHERE id =31
	cmd := "select COALESCE(is_private,0) from dtb_club WHERE id =" + strconv.Itoa(int(ClubId))
	//	log.Println("GetClubType-cmd:", cmd)
	Type, err := GetSingleIntValue(cmd)
	return Type, err
}

func IsClubLevelExpired(ClubId uint32) (bool, error) {
	//select expire_time from dtb_club WHERE id =31
	cmd := "select expire_time from dtb_club WHERE id =" + strconv.Itoa(int(ClubId))
	var err error
	if expireTime, err := GetSingleIntValue(cmd); err == nil && int(time.Now().Unix()) > expireTime {
		return true, err
	}
	return false, err
}

func HasPrivilege(ClubId uint32, ClubUid uint32) bool {
	//	 select count(id) from dtb_club WHERE id =31 and creater_id = 14
	//是不是创建者
	cmd := "select count(id) from  dtb_club  where id =" + strconv.Itoa(int(ClubId)) + " and creater_id =" + strconv.Itoa(int(ClubUid))
	cnt, _ := GetSingleIntValue(cmd)
	if cnt > 0 {
		return true
	}

	//	//是不是管理员
	//	cmd = "select count(id) from  dtb_club_admin where club_id=" + strconv.Itoa(int(ClubId)) + " and admin_id =" + strconv.Itoa(int(ClubUid))
	//	if IsClubAdmin(cmd) {
	//		return true
	//	}

	result := IsAdmin(ClubId, ClubUid)

	return result
}

func IsAdmin(ClubId uint32, ClubUid uint32) bool {
	//是不是管理员
	cmd := "select count(id) from  dtb_club_admin where club_id=" + strconv.Itoa(int(ClubId)) + " and admin_id =" + strconv.Itoa(int(ClubUid))
	if IsClubAdmin(cmd) {
		return true
	}
	return false
}

func IsClubAdmin(cmd string) bool {
	count, _ := GetSingleIntValue(cmd)
	if count > 0 {
		return true
	}

	return false
}

func IsUserInClub(ClubUid uint32, ClubId uint32) (bool, error) {
	//select count(id) from dtb_club_member where club_id=31 and member_id = 14
	//是不是创建者
	var err error = nil
	cmd := "select count(id) from dtb_club_member where club_id=" + strconv.Itoa(int(ClubId)) + " and member_id = " + strconv.Itoa(int(ClubUid))
	cnt, err := GetSingleIntValue(cmd)
	if cnt > 0 {
		return true, err
	}

	return false, err
}

func ExcuteCmd(cmd string) error {
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("ExcuteCmd Query error:%v cmd:%v", err, cmd)
		return err
	}
	return err
}

func UptoAdmin(targetId uint32, clubid uint32) error {
	//INSERT INTO dtb_club_admin (club_id, admin_id, add_time ) VALUES (31,16,1)
	cmd := "INSERT INTO dtb_club_admin (club_id, admin_id, add_time ) VALUES (" + strconv.Itoa(int(clubid)) + "," + strconv.Itoa(int(targetId)) + ",'" + strconv.Itoa(int(time.Now().Unix())) + "')"
	err := ExcuteCmd(cmd)
	return err
}

func DowntoNormal(targetId uint32, clubid uint32) error {
	//DELETE FROM dtb_club_admin WHERE club_id = 31 and admin_id = 16
	cmd := "DELETE FROM dtb_club_admin WHERE club_id =" + strconv.Itoa(int(clubid)) + " and admin_id =" + strconv.Itoa(int(targetId))
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("DowntoNormal Query error:%v cmd:%v", err, cmd)
		return err
	}
	return err
}

func DeleteFromClub(clubid uint32) error {
	//DELETE FROM dtb_club WHERE id = 31
	cmd := "DELETE FROM dtb_club WHERE id = " + strconv.Itoa(int(clubid))
	err := ExcuteCmd(cmd)
	return err
}

func DeleteApplyInfo(clubId uint32, uid uint32) error {
	//DELETE FROM dtb_club_apply WHERE club_id =999 and user_id = 32
	cmd := "DELETE FROM dtb_club_apply WHERE club_id =" + strconv.Itoa(int(clubId)) + " and user_id =" + strconv.Itoa(int(uid))
	err := ExcuteCmd(cmd)
	return err
}

func DeletePending(uid uint32, clubId uint32) error {
	//DELETE FROM t_club_pending WHERE user_id = 888 and club_id = 1
	cmd := "DELETE FROM t_club_pending WHERE user_id = " + strconv.Itoa(int(uid)) + " and club_id =" + strconv.Itoa(int(clubId))
	err := ExcuteCmd(cmd)
	return err
}

func RemoveMember(targetId uint32, clubid uint32) error {
	//DELETE FROM dtb_club_member WHERE club_id =100068 and member_id =303
	cmd := "DELETE FROM dtb_club_member WHERE club_id =" + strconv.Itoa(int(clubid)) + " and member_id =" + strconv.Itoa(int(targetId))
	err := ExcuteCmd(cmd)
	return err
}

func ModifyClubInfo(param *pb.ModifyClubInfoParams) error {
	//UPDATE dtb_club SET club_name = '大众俱乐部1', club_introduce = '大众俱乐部2' WHERE id = 34
	cmd := "UPDATE dtb_club SET club_name = '" + param.ClubName + "',club_introduce = '" + param.ClubDescrption + "' where id =" + strconv.Itoa(int(param.ClubId))
	err := ExcuteCmd(cmd)
	return err
}

func ModifyApplyCheckTime(clubId uint32, uid uint32) error {
	//UPDATE dtb_club_apply SET chk_time = 12345678 WHERE club_id = 100269 and user_id = 389
	cmd := "UPDATE dtb_club_apply SET chk_time = " + strconv.Itoa(int(time.Now().Unix())) + " where club_id =" + strconv.Itoa(int(clubId)) + " and user_id =" + strconv.Itoa(int(uid))
	err := ExcuteCmd(cmd)
	return err
}

func ModifyApplyAddTime(clubId uint32, uid uint32) error {
	//UPDATE dtb_club_apply SET add_time = 12345678 WHERE club_id = 100269 and user_id = 389
	cmd := "UPDATE dtb_club_apply SET add_time = " + strconv.Itoa(int(time.Now().Unix())) + " where club_id =" + strconv.Itoa(int(clubId)) + " and user_id =" + strconv.Itoa(int(uid))
	err := ExcuteCmd(cmd)
	return err
}

func GetUserGoldNum(playerID uint32) (int64, error) {
	//select COALESCE(user_gold,0) from dtb_user_main where id = 34
	cmd := "select COALESCE(user_gold,0) from dtb_user_main where id = " + common.U32toa(playerID)
	//	num, err := GetSingleIntValue(cmd)
	num, err := GetSingleInt64(cmd)
	return num, err
}

func GetUserGameMax(clubId uint32) (int32, error) {
	//select COALESCE(games_max,0) from dtb_user_main where id = 34
	cmd := "select COALESCE(games_max,0) from dtb_user_main where id =" + strconv.Itoa(int(clubId))
	num, err := GetSingleIntValue(cmd)
	return int32(num), err
}

func GetClubLevelExpireTime(clubID uint32) (int, error) {
	//select COALESCE(expire_time,0) from dtb_club where id = 5
	cmd := "select COALESCE(expire_time,0) from dtb_club where id = " + strconv.Itoa(int(clubID))
	num, err := GetSingleIntValue(cmd)
	return num, err
}

func GetCurrentClubLevel(clubId uint32) (int32, error) {
	//select COALESCE(club_level,0) from dtb_club where id = 100105
	cmd := "select COALESCE(club_level,0) from dtb_club where id = " + strconv.Itoa(int(clubId))
	clubLevel, err := GetSingleIntValue(cmd)
	return int32(clubLevel), err
}

func GetExpireTime(clubId uint32) (int, error) {
	//select COALESCE(expire_time,0) from dtb_club where id = 100105
	cmd := "select COALESCE(expire_time,0) from dtb_club where id = " + strconv.Itoa(int(clubId))
	clubLevel, err := GetSingleIntValue(cmd)
	return clubLevel, err
}

func IsUserGoldEnough(uid uint32, amount int64) bool {
	//select user_gold from dtb_user_main where id = 1315
	cmd := "select user_gold from dtb_user_main where id =" + strconv.Itoa(int(uid))
	num, _ := GetSingleInt64(cmd)
	if num >= amount {
		return true
	}

	return false
}

func OperateRequesterGold(playerID uint32, goldNum int64, operate OperateType) (int64, int64, error) {
	if goldNum <= 0 {
		return 0, 0, errors.New("goldNum is not valid!")
	}
	var GoldBefore int64 = 0
	var GoldAfter int64 = 0
	//查请求者拥有的金币
	//select COALESCE(user_gold,0) from dtb_user_main where id = 34
	cmd := "select COALESCE(user_gold,0) from dtb_user_main where id =" + strconv.Itoa(int(playerID))
	origin, err := GetSingleInt64(cmd)
	if err != nil {
		return GoldBefore, GoldAfter, err
	}
	GoldBefore = origin

	var current int64
	if operate == Add {
		current = origin + goldNum
	} else if operate == Subtract {
		current = origin - goldNum
	}

	//扣除花费的金币
	//UPDATE dtb_user_main SET user_gold = current WHERE id = playerID
	cmd = "UPDATE dtb_user_main SET user_gold = '" + common.I64toa(current) + "'where id =" + strconv.Itoa(int(playerID))
	err = ExcuteCmd(cmd)
	if err == nil {
		GoldAfter = current
	}
	return GoldBefore, GoldAfter, err
}

func DelNotifyClubGrantFundInfo(targetId uint32) error {
	logger.Info("DelNotifyClubGrantFundInfo")
	cmd := "DELETE FROM dtb_grant_gold_notify_record where target_id = " + strconv.Itoa(int(targetId))
	err := ExcuteCmd(cmd)
	if err != nil {
		logger.Info("DelNotifyClubGrantFundInfo failed err:%v", err)
	}
	return nil
}

func GetNotifyClubGrantFundInfo(targetId uint32) ([]*NotifyClubGrantFundInfo, error) {
	logger.Info("GetNotifyClubGrantFundInfo")
	cmd := "select COALESCE(club_id,''),COALESCE(operator_id,0),COALESCE(amount,0),COALESCE(current_gold,0)" +
		"from dtb_grant_gold_notify_record where target_id = " + strconv.Itoa(int(targetId))

	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetNotifyClubGrantFundInfo Query error:%v cmd:%v", err, cmd)
		return nil, err
	}

	var list []*NotifyClubGrantFundInfo
	for rows.Next() {
		var club_id int
		var operator_id int
		var amount int
		var gold_after int
		err = rows.Scan(&club_id, &operator_id, &amount, &gold_after)
		info := new(NotifyClubGrantFundInfo)
		info.TargetId = targetId
		info.ClubId = uint32(club_id)
		info.OperatorId = uint32(operator_id)
		info.Amount = int64(amount)
		info.GoldAfter = int64(gold_after)
		if err != nil {
			logger.Notic("GetNotifyClubGrantFundInfo Scan error:%v", err)
			return nil, err
		}
		list = append(list, info)
	}
	return list, nil
}

func GetClubInfo(clubId uint32) (string, uint32, error) {
	//select COALESCE(club_name,''),COALESCE(creater_id,0) from dtb_club where id = 34
	cmd := "select COALESCE(club_name,''),COALESCE(creater_id,0) from dtb_club where id = " + common.U32toa(clubId)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetClubInfo Query error:%v cmd:%v", err, cmd)
		return "", 0, err
	}

	for rows.Next() {
		var club_name string
		var createrId uint32
		err = rows.Scan(&club_name, &createrId)
		if err != nil {
			logger.Notic("GetClubInfo Scan error:%v", err)
			return "", 0, err
		}

		return club_name, createrId, err
	}
	return "", 0, err
}

func RecordClubGrantFundNotify(clubId uint32, targetId uint32, amount int64, operatorId uint32, goldAfter int64) {
	logger.Info("RecordClubGrantFundNotify targetId:%v amount:%v operatorId:%v goldAfter:%v",
		targetId, amount, operatorId, goldAfter)

	cmd := "INSERT INTO dtb_grant_gold_notify_record (club_id,target_id,operator_id,amount,current_gold)" +
		"VALUES (" + strconv.Itoa(int(clubId)) + "," + strconv.Itoa(int(targetId)) + "," + strconv.Itoa(int(operatorId)) + "," +
		strconv.Itoa(int(amount)) + "," + strconv.Itoa(int(goldAfter)) + ")"

	err := ExcuteCmd(cmd)
	if err != nil {
		logger.Info("RecordClubGrantFundNotify failed err:%v", err)
	}

	logger.Info("RecordClubGrantFundNotify success err:%v", err)
}

func RecordAddTargetFund(clubId uint32, targetId uint32, amount int64, operatorId uint32) error {
	var club_name string
	var creater_id uint32
	var err error
	logger.Info("receiveRecordAddTargetFund cludId:%v targetId:%v amount:%v operatorId:%v", clubId, targetId, amount, operatorId)
	if club_name, creater_id, err = GetClubInfo(clubId); err == nil {
		//需要查询操作者昵称
		op_info, op_err := GetUserInfo(operatorId)
		if op_err != nil {
			logger.Info("RecordAddTargetFundGetUserInfo failed err:%v", op_err)
			return op_err
		}
		//需要查询目标昵称
		tar_info, tar_err := GetUserInfo(targetId)
		if tar_err != nil {
			logger.Info("RecordAddTargetFundGetUserInfo failed err:%v", tar_err)
			return tar_err
		}

		cmd := "INSERT INTO dtb_grant_gold_record (club_id, club_name,creater_id,target_id,operator_id,amount,grant_time,operator_name,target_name) " +
			"VALUES (" + strconv.Itoa(int(clubId)) + ",'" + club_name + "'," + strconv.Itoa(int(creater_id)) + "," + strconv.Itoa(int(targetId)) +
			"," + strconv.Itoa(int(operatorId)) + "," + strconv.Itoa(int(amount)) + "," + strconv.Itoa(int(time.Now().Unix())) + ",'" +
			op_info.UserName + "','" + tar_info.UserName + "')"

		err = ExcuteCmd(cmd)

		if err != nil {
			logger.Info("RecordAddTargetFund failed err:%v", err)
		}
	} else {
		logger.Info("RecordAddTargetFundGetClubInfo failed clubId:%v", clubId)
	}

	logger.Info("RecordAddTargetFund success err:%v", err)
	return err
}

func GetClubGrantToList(uid uint32) ([]*pb.TransferToOtherInfo, error) {
	var toList []*pb.TransferToOtherInfo

	cmd := "SELECT club_id,operator_id,target_id,amount,grant_time,operator_name,target_name from" +
		" dtb_grant_gold_record where operator_id = " + strconv.Itoa(int(uid)) + " order by grant_time DESC"
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetClubGrantToList Query error:%v cmd:%v", err, cmd)
		return nil, err
	}

	for rows.Next() {
		var club_id string
		var operator_id string
		var target_id string
		var grant_time string
		var operator_name string
		var target_name string
		info := new(pb.TransferToOtherInfo)

		err = rows.Scan(&club_id, &operator_id, &target_id, &info.Amount, &grant_time, &operator_name, &target_name)
		if err != nil {
			logger.Notic("GetClubGrantToList Scan error:%v", err)
			return nil, err
		}
		clubId, _ := strconv.Atoi(club_id)
		operatorId, _ := strconv.Atoi(operator_id)
		targetId, _ := strconv.Atoi(target_id)
		grantTime, _ := strconv.Atoi(grant_time)
		info.ClubId = uint32(clubId)
		info.OperatorId = uint32(operatorId)
		info.TargerId = uint32(targetId)
		info.GrantTime = int32(grantTime)
		info.OperatorName = operator_name
		info.TargetName = target_name

		toList = append(toList, info)
	}

	return toList, nil
}

func GetClubGrantGetList(uid uint32) ([]*pb.TransferToOtherInfo, error) {
	var getList []*pb.TransferToOtherInfo
	info := new(pb.TransferToOtherInfo)
	cmd := "SELECT club_id,operator_id,target_id,amount,grant_time from dtb_grant_gold_record where target_id = " +
		strconv.Itoa(int(uid)) + " order by grant_time DESC"
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetClubGrantGetList Query error:%v cmd:%v", err, cmd)
		return nil, err
	}

	for rows.Next() {
		var club_id string
		var operator_id string
		var target_id string
		var grant_time string

		err = rows.Scan(&club_id, &operator_id, &target_id, &info.Amount, &grant_time)
		if err != nil {
			logger.Notic("GetClubGrantGetList Scan error:%v", err)
			return nil, err
		}
		clubId, _ := strconv.Atoi(club_id)
		operatorId, _ := strconv.Atoi(operator_id)
		targetId, _ := strconv.Atoi(target_id)
		grantTime, _ := strconv.Atoi(grant_time)
		info.ClubId = uint32(clubId)
		info.OperatorId = uint32(operatorId)
		info.TargerId = uint32(targetId)
		info.GrantTime = int32(grantTime)

		getList = append(getList, info)
	}

	return getList, nil
}

func GetClubLevelInfo(typeId int) (*club.ClubLevelInfo, error) {
	//select COALESCE(club_level,0),COALESCE(members_max,0),COALESCE(admins_max,0),COALESCE(expire_time,0) from dtb_goods where goods_type=3 and club_level = 7
	cmd := "select COALESCE(members_max,0),COALESCE(admins_max,0),COALESCE(expire_time,0),COALESCE(created_alliance_max,0),COALESCE(joined_alliance_max,0) from dtb_goods where goods_type=3 and club_level = " + strconv.Itoa(typeId)
	//	log.Println("GetClubLevelInfo-cmd:", cmd)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetClubLevelInfo  Query error:%v", err)
		return nil, err
	}

	for rows.Next() {
		var members_max int
		var admins_max int
		var expire_time int
		var created_alliance_max int
		var joined_alliance_max int
		err = rows.Scan(&members_max, &admins_max, &expire_time, &created_alliance_max, &joined_alliance_max)
		if err != nil {
			logger.Notic("GetClubLevelInfo Scan error:%v", err)
			return nil, err
		}

		Info := new(club.ClubLevelInfo)
		Info.ClubLevel = int(typeId)

		Info.MembersMax = int(members_max)
		Info.AdminsMax = int(admins_max)
		Info.Span = int(expire_time) //目前数据库中存的是2592000=30*24*3600
		Info.CreatedAllianceMax = int(created_alliance_max)
		Info.JoinedAllianceMax = int(joined_alliance_max)

		//		log.Println("GetClubLevelInfo-Info.MembersMax:", Info.MembersMax)

		return Info, err
	}
	return nil, err
}

func GetClubGameMax(typeId int) (int32, error) {
	//select COALESCE(games_max,0) from dtb_goods where goods_type=3 and club_level = 3
	cmd := "select COALESCE(games_max,0) from dtb_goods where goods_type=3 and club_level = " + strconv.Itoa(typeId)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	var gameMax int32
	if err != nil {
		logger.Notic("GetClubGameMax Query error:%v cmd:%v", err, cmd)
		return -1, err
	}

	for rows.Next() {
		err = rows.Scan(&gameMax)
		if err != nil {
			return gameMax, err
		}

		return gameMax, err
	}
	return gameMax, err
}

func GetUserInfo(userId uint32) (*club.UserInfo, error) {
	//select COALESCE(nick_name,''),COALESCE(gender,0),COALESCE(diamond_num,0),COALESCE(user_gold,0),COALESCE(avatar_thumb,'') from dtb_user_main where id = 35
	cmd := "select COALESCE(nick_name,''),COALESCE(gender,0),COALESCE(diamond_num,0),COALESCE(user_gold,0),COALESCE(avatar_thumb,''), COALESCE(user_marks,'') from dtb_user_main where id =" + common.U32toa(userId)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetUserInfo error:%v cmd:%v", err, cmd)
		return nil, err
	}

	Info := new(club.UserInfo)
	for rows.Next() {
		var nick_name string
		var gender int32
		var diamond_num uint64
		var avatar_thumb string
		var user_marks string

		err = rows.Scan(&nick_name, &gender, &diamond_num, &Info.UserGold, &avatar_thumb, &user_marks)
		if err != nil {
			logger.Notic("GetUserInfo Scan error:%v", err)
			return nil, err
		}

		Info.UserId = userId
		Info.UserName = string(nick_name)
		Info.Gender = gender
		Info.DiamondNum = uint64(diamond_num)
		Info.AvatarThumb = avatar_thumb
		Info.Marks = string(user_marks)

		return Info, err
	}
	return Info, err
}

func AddTargetFund(clubId uint32, targetId uint32, amount int64) (int64, error) {
	//查发放对象拥有的基金
	//select user_gold from dtb_user_main where id = 5
	cmd := "select COALESCE(user_gold,0) from dtb_user_main where id =" + strconv.Itoa(int(targetId))

	origin, err := GetSingleInt64(cmd)
	if err != nil {
		return 0, nil
	}

	current := origin + amount

	//向发放对象拥有的基金
	//UPDATE dtb_user_main SET user_gold = current WHERE id = targetId
	cmd = "UPDATE dtb_user_main SET user_gold =  '" + common.I64toa(current) + "'where id =" + strconv.Itoa(int(targetId))

	err = ExcuteCmd(cmd)
	if err == nil {
		utils.SendCurrencyReport(targetId, clubId,
			stat.Currency_Channel_GrantClubFund, stat.Currency_Type_Money,
			int(amount), int(origin), int(current), 0, "")
	}
	return int64(current), err
}

func AddJackpotAmount(clubId uint32, blindLevel int, amount int64) (int64, error) {
	//查jackpot拥有的基金
	//select amount from t_jackpot where club_id = 100001 and blind_level = 2
	cmd := "select  COALESCE(amount,0) from t_jackpot where club_id = " + strconv.Itoa(int(clubId)) + " and blind_level = " + strconv.Itoa(int(blindLevel))
	origin, err := GetSingleInt64(cmd)
	if err != nil {
		return 0, err
	}

	var current int64 = origin + amount

	//向jackpot注入金额
	//UPDATE t_jackpot SET amount = 1100 WHERE club_id = 100001 and blind_level = 2
	cmd = "UPDATE t_jackpot SET amount =" + common.I64toa(current) + " where club_id =" + strconv.Itoa(int(clubId)) + " and blind_level = " + strconv.Itoa(int(blindLevel))

	err = ExcuteCmd(cmd)
	return current, err
}

func UpdateJackpotAmount(clubId uint32, blindLevel int, amount int64) error {
	//向jackpot注入金额
	//UPDATE t_jackpot SET amount = 1100 WHERE club_id = 100001 and blind_level = 2
	cmd := "UPDATE t_jackpot SET amount =" + common.I64toa(amount) + " where club_id =" + strconv.Itoa(int(clubId)) + " and blind_level = " + strconv.Itoa(int(blindLevel))
	err := ExcuteCmd(cmd)
	return err
}

func ModifyClubNum(createrId uint32, num int) error {
	//select COALESCE(current_clubs,0) from dtb_user_main where id = 35
	cmd := "select COALESCE(current_clubs,0) from dtb_user_main where id =" + strconv.Itoa(int(createrId))
	origin, err := GetSingleIntValue(cmd)
	if err != nil {
		return err
	}

	current := origin + (int(num))

	//UPDATE dtb_user_main SET current_clubs = 1 WHERE id = 35
	cmd = "UPDATE dtb_user_main SET current_clubs =  '" + strconv.Itoa(current) + "'where id =" + strconv.Itoa(int(createrId))

	err = ExcuteCmd(cmd)
	return err
}

func ModifyClubCurrentMemberNum(clubId uint32, num int) error {
	//select COALESCE(current_members,0) from dtb_club where id = 100107
	cmd := "select COALESCE(current_members,0) from dtb_club where id = " + strconv.Itoa(int(clubId))
	//	log.Println("1 ModifyClubCurrentMemberNum-cmd:", cmd)
	origin, err := GetSingleIntValue(cmd)
	if err != nil {
		return err
	}

	current := origin + (int(num))

	//UPDATE dtb_club SET current_members = 1 WHERE id = 100107
	cmd = "UPDATE dtb_club SET current_members =  '" + strconv.Itoa(current) + "'where id =" + strconv.Itoa(int(clubId))
	//	log.Println("2 ModifyClubCurrentMemberNum-cmd:", cmd)
	err = ExcuteCmd(cmd)
	return err
}

func ModifyClubCurrentAdminNum(clubId uint32, num int) error {
	//select COALESCE(current_admins,0) from dtb_club where id = 100258
	cmd := "select COALESCE(current_admins,0) from dtb_club where id = " + strconv.Itoa(int(clubId))
	origin, err := GetSingleIntValue(cmd)
	if err != nil {
		return err
	}

	var current int
	current = origin + (int(num))

	//1 WHERE id = 100258
	cmd = "UPDATE dtb_club SET current_admins =   '" + strconv.Itoa(current) + "'where id =" + strconv.Itoa(int(clubId))

	err = ExcuteCmd(cmd)
	return err
}

func GetClubCreaterId(clubid uint32) (uint32, error) {
	//select creater_id from dtb_club WHERE id = 31
	cmd := "select creater_id from dtb_club WHERE id = " + common.U32toa(clubid)
	createrId, err := GetSingleU32(cmd)
	return createrId, err
}

func IsNeedOperateGold(clubid uint32) bool {
	//select oprate_gold from  dtb_club where id = 100269
	bOperate := true
	cmd := "select oprate_gold from  dtb_club where id = " + common.U32toa(clubid)
	if operateval, err := GetSingleIntValue(cmd); err == nil {
		if operateval == 2 {
			bOperate = false
		}
	}

	return bOperate
}

func GetCreatedClubs(uid uint32) ([]uint32, error) {
	//select id from  dtb_club where creater_id = 14
	cmd := "select id from  dtb_club where creater_id = " + common.U32toa(uid)
	clubIds, err := GetU32List(cmd)
	return clubIds, err
}

func GetAttendedClubs(uid uint32) ([]uint32, error) {
	//select club_id from dtb_club_member where member_id = 14
	cmd := "select club_id from dtb_club_member where member_id =" + strconv.Itoa(int(uid))
	clubIds, err := GetU32List(cmd)
	return clubIds, err
}

func GetAllianceIds(clubId uint32) ([]uint32, error) {
	//select alliance_id from dtb_alliance_member where club_id = 14
	cmd := "select alliance_id from dtb_alliance_member where club_id = " + strconv.Itoa(int(clubId))
	AllianceIds, err := GetU32List(cmd)
	return AllianceIds, err
}

func GetPlublicClubs(uid uint32) ([]uint32, error) {
	//select id from  dtb_club where is_private = 0
	cmd := "select id from  dtb_club where is_private = 0"
	clubIds, err := GetU32List(cmd)
	return clubIds, err
}

func GetAdminClubs(uid uint32) ([]uint32, error) {
	//select club_id from  dtb_club_admin where admin_id = 15
	cmd := "select club_id from  dtb_club_admin where admin_id =" + strconv.Itoa(int(uid))
	clubIds, err := GetU32List(cmd)
	return clubIds, err
}

func GetAdminAllianceIds(clubId uint32) ([]uint32, error) {
	//select alliance_id from  dtb_alliance_member where club_id = creater_club_id and club_id = clubId
	cmd := "select alliance_id from  dtb_alliance_member where club_id = creater_club_id and club_id = " + strconv.Itoa(int(clubId))
	allianceIds, err := GetU32List(cmd)
	return allianceIds, err
}

func GetClubAdmins(clubid uint32) ([]uint32, error) {
	//select admin_id from  dtb_club_admin where club_id=31
	cmd := "select admin_id from  dtb_club_admin where club_id=" + common.U32toa(clubid)
	adminIds, err := GetU32List(cmd)
	return adminIds, err
}

func GetUserClubIds(playId uint32) ([]uint32, error) {
	//select club_id from  dtb_club_member  where member_id = 35
	cmd := "select club_id from  dtb_club_member  where member_id =" + common.U32toa(playId)
	clubIds, err := GetU32List(cmd)
	return clubIds, err
}

func GetAllClubs() ([]uint32, error) {
	//select id from  dtb_club
	cmd := "select id from  dtb_club"
	clubIds, err := GetU32List(cmd)
	return clubIds, err
}

func GetIntList(cmd string) ([]int, error) {
	var ids []int
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetIntList Query error:%v cmd:%v", err, cmd)
		return ids, err
	}
	for rows.Next() {
		var id int
		err = rows.Scan(&id)
		if err != nil {
			logger.Notic("GetIntList error:%v", err)
			return ids, err
		}
		ids = append(ids, id)
	}
	return ids, err
}

func GetU32List(cmd string) ([]uint32, error) {
	var ids []uint32
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetIntList Query error:%v cmd:%v", err, cmd)
		return ids, err
	}
	for rows.Next() {
		var id uint32
		err = rows.Scan(&id)
		if err != nil {
			logger.Notic("GetIntList error:%v", err)
			return ids, err
		}
		ids = append(ids, id)
	}
	return ids, err
}

func GetClubSnapshotInfo(clubId uint32, PlayerID uint32) (*pb.ClubSnapshotListParams, error) {
	//	select COALESCE(id,0),COALESCE(club_name,''),COALESCE(club_area,''),COALESCE(club_img,''),COALESCE(members_max,0),COALESCE(current_members,0),COALESCE(is_private,0),COALESCE(creater_id,0),COALESCE(club_gold,0),
	//	COALESCE(club_introduce,''),COALESCE(create_time,0),COALESCE(club_gold,0),COALESCE(club_level,0) from dtb_club where id = 100014
	cmd := "select COALESCE(id,0),COALESCE(club_name,''),COALESCE(club_area,''),COALESCE(club_img,''),COALESCE(members_max,0),COALESCE(current_members,0),COALESCE(is_private,0),COALESCE(creater_id,0),COALESCE(club_introduce,''),COALESCE(create_time,0),COALESCE(club_level,0),COALESCE(expire_time,0),COALESCE(invitation_code,'') from dtb_club where id = " + common.U32toa(clubId)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetClubSnapshotInfo Query error:%v cmd:%v", err, cmd)
		return nil, err
	}
	for rows.Next() {
		clubinfo := new(pb.ClubSnapshotListParams)

		err = rows.Scan(&clubinfo.ClubId, &clubinfo.ClubName, &clubinfo.ClubArea, &clubinfo.ClubIcon, &clubinfo.ClubMemberMax,
			&clubinfo.ClubMemberCount, &clubinfo.ClubType, &clubinfo.ClubOwner, &clubinfo.ClubDescrption,
			&clubinfo.ClubCreateTime, &clubinfo.ClubLevel, &clubinfo.ExpireTime, &clubinfo.InvitationCode)
		if err != nil {
			logger.Notic("GetClubSnapshotInfo Scan error:%v", err)

			return nil, err
		}

		//clubinfo.ClubMemberCount = int32(current_members)
		ids, e := GetClubMemberList(clubId)
		//ids = common.RemoveDuplicates(ids)
		var num int
		if e == nil {
			num = len(ids)
			clubinfo.ClubMemberCount = int32(num)
		}

		if clubinfo.ClubType == int32(common.Enum_Club_Public) {
			if bInClub, er := IsUserInClub(PlayerID, clubId); er == nil && bInClub {
				clubinfo.IsPublicMember = common.Enum_In_Club
			} else {
				clubinfo.IsPublicMember = common.Enum_Not_In_Club
			}
		}

		if IsAdmin(clubId, PlayerID) {
			clubinfo.IsManager = 1
		} else {
			clubinfo.IsManager = 2
		}

		cmd2 := "select COALESCE(blind_level,0) from t_jackpot  WHERE club_id = " + common.U32toa(clubId)
		levels, e := GetIntList(cmd2)
		if e == nil {
			for _, level := range levels {
				clubinfo.OpenedBlindlevels = append(clubinfo.OpenedBlindlevels, int32(level))
			}
		}

		return clubinfo, err
	}

	return nil, err
}

func GetNickName(createPlayerId uint32) (string, error) {
	//select COALESCE(nick_name,'') from dtb_user_main where id = 6
	cmd := "select COALESCE(nick_name,'') from dtb_user_main where id = " + common.U32toa(createPlayerId)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetNickName Query error:%v cmd:%v", err, cmd)
		return "", err
	}
	for rows.Next() {
		var nick_name string
		err = rows.Scan(&nick_name)
		if err != nil {
			logger.Notic("GetNickName Scan error:%v", err)
			return "", err
		}

		return nick_name, err
	}
	return "", err
}

func GetSubCreaterInfo(createrId uint32) (*pb.NoticeClubCreaterInfo, error) {
	cmd := "select COALESCE(avatar,''),COALESCE(nick_name,'') from dtb_user_main where id =" + common.U32toa(createrId)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetSubCreaterInfo Query error:%v cmd:%v", err, cmd)
		return nil, err
	}

	for rows.Next() {
		var nick_name string
		var avatar string

		err = rows.Scan(&avatar, &nick_name)
		if err != nil {
			logger.Notic("GetSubCreaterInfo Scan error:%v", err)
			return nil, err
		}

		info := new(pb.NoticeClubCreaterInfo)
		info.CreatePlayerName = string(nick_name)
		info.CreatePlayerThumb = string(avatar) // to do 需换成字符串

		return info, err
	}
	return nil, err
}

func GetApplyInfoList(clubId uint32) ([]*pb.JoinClubParams, error) {
	//select COALESCE(user_id,0),COALESCE(apply_msg,'') from dtb_club_apply where club_id = 9
	//cmd := "select COALESCE(user_id,0),COALESCE(apply_msg,'') from dtb_club_apply where club_id = " + strconv.Itoa(clubId)
	cmd := "select COALESCE(user_id,0),COALESCE(apply_msg,'') from dtb_club_apply where club_id = " + common.U32toa(clubId) + " and add_time > chk_time"
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetApplyInfoList Query error:%v cmd:%v", err, cmd)
		return nil, err
	}

	var list []*pb.JoinClubParams
	for rows.Next() {
		var user_id uint32
		var msg string

		applyInfo := new(pb.JoinClubParams)
		err = rows.Scan(&applyInfo.ClubUid, &msg)
		if err != nil {
			logger.Notic("GetApplyInfoList Scan error:%v", err)
			return nil, err
		}

		applyInfo.ClubId = clubId
		applyInfo.ClubMessage = string(msg)

		if len(list) > 0 {
			bExist := false
			for _, info := range list {
				if user_id == info.ClubUid {
					bExist = true
				}
			}
			if !bExist {
				list = append(list, applyInfo)
			}
		} else {
			list = append(list, applyInfo)
		}

	}
	return list, err
}

func GetPendingInfoList(uid uint32) ([]*pb.NoticeJoinClubToMember, error) {
	//select COALESCE(user_id,0),COALESCE(club_id,0),COALESCE(chk_status,0),COALESCE(reason,'') from t_club_pending where user_id = 888
	cmd := "select COALESCE(user_id,0),COALESCE(club_id,0),COALESCE(chk_status,0),COALESCE(reason,'') from t_club_pending where user_id =" + common.U32toa(uid)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetPendingInfoList Query error:%v cmd:%v", err, cmd)
		return nil, err
	}

	var list []*pb.NoticeJoinClubToMember
	for rows.Next() {
		var club_id uint32
		var chk_status int
		var reason string

		Info := new(pb.NoticeJoinClubToMember)
		err = rows.Scan(&Info.Uid, &Info.ClubId, &chk_status, &reason)
		if err != nil {
			logger.Notic("GetPendingInfoList Scan error:%v", err)
			return nil, err
		}

		if clubName, _, err := GetClubInfo(club_id); err == nil {
			Info.ClubName = clubName
		}

		Info.Result = int32(chk_status)
		Info.Reason = string(reason)

		list = append(list, Info)
	}
	return list, err
}

func GetClubMemberAuth(memberId uint32, clubId uint32) (int, error) {
	cmd := "select count(id) from  dtb_club_admin where club_id=" + strconv.Itoa(int(clubId)) + " and admin_id =" + strconv.Itoa(int(memberId))
	var auth int
	if ok, _ := IsTargetCreater(memberId, clubId); ok {
		auth = 2
	} else if IsClubAdmin(cmd) {
		auth = 1
	} else {
		auth = 0
	}
	return auth, nil
}

func GetClubMemberSnapshot(memberId uint32, clubId uint32) (*pb.ClubMemberSnapshot, error) {
	//select COALESCE(id,0),COALESCE(avatar_thumb,''),COALESCE(nick_name,''),COALESCE(diamond_num,0),COALESCE(user_gold,0),COALESCE(last_login,0) from dtb_user_main where id = 10
	cmd := "select COALESCE(id,0),COALESCE(avatar_thumb,''),COALESCE(nick_name,''),COALESCE(diamond_num,0),COALESCE(user_gold,0),COALESCE(last_login,0) from dtb_user_main where id = " + common.U32toa(memberId)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetClubMemberSnapshot Query error:%v cmd:%v", err, cmd)
		return nil, err
	}
	for rows.Next() {
		var avatar_thumb string

		var nick_name string
		var last_login int
		var diamond_num int

		memberinfo := new(pb.ClubMemberSnapshot)
		err = rows.Scan(&memberinfo.MemberUid, &avatar_thumb, &nick_name, &diamond_num, &memberinfo.UserGold, &last_login)
		if err != nil {
			logger.Notic("GetClubMemberSnapshot Scan error:%v", err)
			return nil, err
		}

		memberinfo.MemberIcon = avatar_thumb
		memberinfo.MemberName = nick_name

		memberinfo.MemberLastLoginTime = int32(last_login)
		auth, _ := GetClubMemberAuth(memberinfo.MemberUid, clubId)
		memberinfo.MemberAuth = int32(auth)

		return memberinfo, err
	}
	return nil, err
}

func GetSubClubInfo(clubId uint32) (*club.SubGameInfo, error) {
	//select COALESCE(club_name,''),COALESCE(club_thumb,'') from dtb_club WHERE id = 31
	cmd := "select COALESCE(club_name,''),COALESCE(club_thumb,'') from dtb_club WHERE id =" + common.U32toa(clubId)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetSubClubInfo Query error:%v", err)
		return nil, err
	}
	for rows.Next() {
		var club_name string
		var club_thumb string

		err = rows.Scan(&club_name, &club_thumb)
		if err != nil {
			logger.Notic("GetSubClubInfo Scan error:%v", err)
			return nil, err
		}

		info := new(club.SubGameInfo)
		info.ClubName = club_name
		info.PicPath = club_thumb
		return info, err
	}
	return nil, err
}

func GetJackpotClubInfo(clubId uint32) (string, string, string, error) {
	//select COALESCE(club_name,''),COALESCE(club_thumb,''),COALESCE(club_area,'') from dtb_club where id = 100105
	cmd := "select COALESCE(club_name,''),COALESCE(club_thumb,''),COALESCE(club_area,'') from dtb_club where id =" + common.U32toa(clubId)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetJackpotClubInfo Query error:%v", err)
		return "", "", "", err
	}
	for rows.Next() {
		var club_name string
		var club_thumb string
		var club_area string

		err = rows.Scan(&club_name, &club_thumb, &club_area)
		if err != nil {
			logger.Notic("GetJackpotClubInfo Scan error:%v", err)
			return "", "", "", err
		}

		return club_name, club_thumb, club_area, err
	}
	return "", "", "", err
}

func GetSubGameInfo(createPlayerId uint32) (*club.SubGameInfo, error) {
	//select COALESCE(avatar_thumb,'') from dtb_user_main where id = 6
	cmd := "select COALESCE(avatar_thumb,'') from dtb_user_main where id = " + common.U32toa(createPlayerId)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetSubGameInfo Query error:%v cmd:%d", err, cmd)
		return nil, err
	}
	for rows.Next() {
		var avatar_thumb string

		err = rows.Scan(&avatar_thumb)
		if err != nil {
			logger.Notic("GetSubGameInfo Scan error:%v", err)
			return nil, err
		}

		info := new(club.SubGameInfo)
		info.PicPath = avatar_thumb
		return info, err
	}
	return nil, err
}

func GetSubUserInfo(createPlayerId uint32) (*club.SubUserInfo, error) {
	//select COALESCE(nick_name,''),COALESCE(avatar_thumb,'') from dtb_user_main where id = 6
	cmd := "select COALESCE(nick_name,''),COALESCE(avatar_thumb,'') from dtb_user_main where id = " + common.U32toa(createPlayerId)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetSubUserInfo Query error:%v", err)
		return nil, err
	}
	for rows.Next() {
		var nick_name string
		var avatar_thumb string

		err = rows.Scan(&nick_name, &avatar_thumb)
		if err != nil {
			logger.Notic("GetSubUserInfo Scan error:%v", err)
			return nil, err
		}

		info := new(club.SubUserInfo)
		info.Name = nick_name
		info.Thumb = avatar_thumb

		return info, err
	}
	return nil, err
}

func GetClubMemberList(clubid uint32) ([]uint32, error) {
	cmd := "select member_id from dtb_club_member WHERE club_id =" + common.U32toa(clubid)
	memberIds, err := GetU32List(cmd)

	return memberIds, err
}

func IsNumFull(cmd string) (bool, error) {
	var cur int
	var max int
	full := false
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("IsNumFull Query error:%v cmd:%v", err, cmd)
		return full, err
	}
	for rows.Next() {
		err = rows.Scan(&cur, &max)
		if err != nil {
			logger.Notic("IsNumFull Scan error:%v", err)
			return full, err
		}
		if cur >= max {
			full = true
		}
	}

	return full, err
}

func GetSingleIntValueList(cmd string) ([]int, error) {
	rows, err := m_game_db.Query(cmd)
	var valList []int
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetSingleIntValueList Query error:%d cmd:%v", err, cmd)
		return valList, err
	}

	for rows.Next() {
		var value string
		err = rows.Scan(&value)
		if err != nil {
			logger.Notic("GetSingleIntValueList Scan error:%v", err)
			return valList, err
		}
		val, _ := strconv.Atoi(value)
		valList = append(valList, val)
	}
	return valList, err
}

func GetSingleValue(cmd string) (string, error) {
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	var value string
	if err != nil {
		logger.Notic("GetSingleValue Query error:%v cmd:%v", err, cmd)
		return value, err
	}

	for rows.Next() {
		err = rows.Scan(&value)
		if err != nil {
			logger.Notic("GetSingleValue Scan error:%v", err)
			return value, err
		}
		return value, err
	}

	return value, err
}

func GetSingleInt(cmd string) (int, error) {
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	var value int
	if err != nil {
		logger.Notic("GetSingleInt Query error:%v cmd:%v", err, cmd)
		return value, err
	}

	for rows.Next() {
		err = rows.Scan(&value)
		if err != nil {
			logger.Notic("GetSingleInt Scan error:%v", err)
			return value, err
		}
		return value, err
	}

	return value, err
}

func GetSingleU32(cmd string) (uint32, error) {
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	var value uint32
	if err != nil {
		logger.Notic("GetSingleInt Query error:%v cmd:%v", err, cmd)
		return value, err
	}

	for rows.Next() {
		err = rows.Scan(&value)
		if err != nil {
			logger.Notic("GetSingleInt Scan error:%v", err)
			return value, err
		}
		return value, err
	}

	return value, err
}

func GetSingleIntValue(cmd string) (int, error) {
	var val int
	var value string
	var err error
	if value, err = GetSingleValue(cmd); err == nil {
		val, err = strconv.Atoi(value)
	}

	return val, err
}

func GetSingleUint64(cmd string) (uint64, error) {
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	var val uint64
	if err != nil {
		logger.Notic("GetSingleUint64 Query error:%v cmd:%v", err, cmd)
		return val, err
	}

	for rows.Next() {
		err = rows.Scan(&val)
		if err != nil {
			logger.Notic("GetSingleUint64 Scan error:%v", err)
			return val, err
		}
		return val, err
	}

	return val, err
}

func GetSingleInt64(cmd string) (int64, error) {
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	var val int64
	if err != nil {
		logger.Notic("GetSingleUint64 Query error:%v cmd:%v", err, cmd)
		return val, err
	}

	for rows.Next() {
		err = rows.Scan(&val)
		if err != nil {
			logger.Notic("GetSingleUint64 Scan error:%v", err)
			return val, err
		}
		return val, err
	}

	return val, err
}

func GetMaxRoomId() (uint64, error) {
	sql := "select max(id) from t_room_id"
	Id, err := GetSingleUint64(sql)
	return Id, err
}

func AddJackpot(clubId uint32, blindLevel int, profitScale int, drawinAmout int) error {
	// INSERT INTO t_jackpot (club_id, blind_level,scale,drawin_amount) VALUES (100001,1,50,1)
	cmd := "INSERT INTO t_jackpot (club_id, blind_level,scale,drawin_amount) VALUES (" + common.U32toa(clubId) + "," + strconv.Itoa(blindLevel) + "," + strconv.Itoa(profitScale) + "," + strconv.Itoa(drawinAmout) + ")"
	err := ExcuteCmd(cmd)
	return err
}

func AddJackpotRecord(clubId uint32, room_creater_uid uint32, room_uintid int, card_uintid int, blindLevel int, hand_level int, award_amount int, award_uid int) error {
	//INSERT INTO t_jackpot_record (club_id, room_creater_uid, room_uintid, card_uintid, time, blind_level, hand_level, award_amount, award_uid) VALUES (1, 2, 3, 4, 5, 6, 7, 8, 9);
	cmd := "INSERT INTO t_jackpot_record (club_id, room_creater_uid, room_uintid, card_uintid, time, blind_level, hand_level, award_amount, award_uid) VALUES (" + common.U32toa(clubId) + "," + common.U32toa(room_creater_uid) + "," + strconv.Itoa(room_uintid) + "," + strconv.Itoa(card_uintid) + "," + strconv.Itoa(int(time.Now().Unix())) + "," + strconv.Itoa(blindLevel) + "," + strconv.Itoa(hand_level) + "," + strconv.Itoa(award_amount) + "," + strconv.Itoa(award_uid) + ")"
	err := ExcuteCmd(cmd)
	return err
}

func UpdateJackpot(clubId uint32, blindLevel int, profitScale int, drawinAmout int) error {
	//UPDATE t_jackpot SET scale = 40,drawin_amount = 2 WHERE club_id = 100324 and blind_level = 1
	cmd := "UPDATE t_jackpot SET scale = " + strconv.Itoa(profitScale) + " ,drawin_amount = " + strconv.Itoa(drawinAmout) + " WHERE club_id = " + common.U32toa(clubId) + " and blind_level = " + strconv.Itoa(blindLevel)
	err := ExcuteCmd(cmd)
	return err
}

func AddJackpotAwardtype(clubId uint32, handLevel int, rewardPercent int) error {
	//INSERT INTO t_jackpot_setting (club_id, hand_level,reward_percent) VALUES (100001,10,50)
	cmd := "INSERT INTO t_jackpot_setting (club_id, hand_level,reward_percent) VALUES (" + common.U32toa(clubId) + "," + strconv.Itoa(handLevel) + "," + strconv.Itoa(rewardPercent) + ")"
	err := ExcuteCmd(cmd)
	return err
}

func UpdateJackpotAwardtype(clubId uint32, handLevel int, rewardPercent int) error {
	//UPDATE t_jackpot_setting SET reward_percent = 15 WHERE club_id = 100351 and hand_level = 8
	cmd := "UPDATE t_jackpot_setting SET reward_percent =" + strconv.Itoa(rewardPercent) + " WHERE club_id = " + common.U32toa(clubId) + " and hand_level= " + strconv.Itoa(handLevel)
	err := ExcuteCmd(cmd)
	return err
}

//
func AddAward2ClubPercent(clubId uint32, rewardPercent int) error {
	cmd := "INSERT INTO t_jackpot_club (club_id, reward_percent) VALUES (" + common.U32toa(clubId) + "," + strconv.Itoa(rewardPercent) + ")"
	err := ExcuteCmd(cmd)
	return err
}

func UpdateAward2ClubPercent(clubId uint32, rewardPercent int) error {
	cmd := "UPDATE t_jackpot_club SET reward_percent =" + strconv.Itoa(rewardPercent) + " WHERE club_id = " + common.U32toa(clubId)
	err := ExcuteCmd(cmd)
	return err
}

func GetJackpotList(clubId uint32) ([]*pb.Jackpot, error) {
	//select COALESCE(blind_level,0),COALESCE(amount,0) from t_jackpot where club_id = 100001
	cmd := "select COALESCE(blind_level,0),COALESCE(amount,0) from t_jackpot where club_id = " + common.U32toa(clubId)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetJackpotList query error:%v cmd:%v", err, cmd)
		return nil, err
	}

	var list []*pb.Jackpot
	for rows.Next() {
		Info := new(pb.Jackpot)
		err = rows.Scan(&Info.BlindLevel, &Info.Amount)
		if err != nil {
			logger.Notic("GetJackpotList Scan error:%v", err)
			return nil, err
		}

		list = append(list, Info)
	}
	return list, err
}

func GetJackpotAmount(clubId uint32, blindLevel int) (int64, error) {
	//select COALESCE(amount,0) from t_jackpot where club_id = 100001 and blind_level = 1
	cmd := "select COALESCE(amount,0) from t_jackpot where club_id = " + common.U32toa(clubId) + " and blind_level = " + strconv.Itoa(blindLevel)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetJackpotAmount query error:%v cmd:%v", err, cmd)
		return -1, err
	}

	var amount int64
	for rows.Next() {

		err = rows.Scan(&amount)
		if err != nil {
			logger.Notic("GetJackpotAmount scan error:%v", err)
			return amount, err
		}
		return amount, err
	}
	return amount, err
}

func GetJackpotAwardList(clubId uint32, blindLevel int) ([]*pb.AwardInfo, error) {
	// 待排序 待请求50条记录
	//select COALESCE(award_uid,0),COALESCE(hand_level,0),COALESCE(award_amount,0),COALESCE(time,0) from t_jackpot_record where club_id = 100001 and blind_level = 10
	cmd := "select COALESCE(award_uid,0),COALESCE(hand_level,0),COALESCE(award_amount,0),COALESCE(time,0) from t_jackpot_record where club_id = " + common.U32toa(clubId) + " and blind_level = " + strconv.Itoa(blindLevel) + " order by id desc LIMIT 0,12"
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetJackpotAwardList query error:%v cmd:%v", err, cmd)
		return nil, err
	}

	var list []*pb.AwardInfo
	for rows.Next() {
		var hand_level int32

		Info := new(pb.AwardInfo)
		err = rows.Scan(&Info.PlayerId, &hand_level, &Info.AwardAmount, &Info.AwardTime)
		if err != nil {
			logger.Notic("GetJackpotAwardList Scan error:%v", err)
			return nil, err
		}

		Info.HandLevel = hand_level

		if userInfo, err := GetSubUserInfo(Info.PlayerId); err == nil {
			if userInfo != nil {
				Info.PlayerName = userInfo.Name
			}
		}
		list = append(list, Info)
	}
	return list, err
}

func GetLuckyJackpotAward(clubId uint32, blindLevel int) (*pb.AwardInfo, error) {
	// 待排序 找到award最大的一条记录
	//select award_uid,hand_level,award_amount,time from t_jackpot_record where club_id = 100403 and blind_level = 1  and award_amount= (select max(award_amount) from t_jackpot_record where club_id = 100403 and blind_level = 1 )
	cmd := "select award_uid,hand_level,award_amount,time from t_jackpot_record where club_id =" + common.U32toa(clubId) + " and blind_level = " + strconv.Itoa(blindLevel) + " and award_amount= (select max(award_amount) from t_jackpot_record where club_id =" + common.U32toa(clubId) + " and blind_level = " + strconv.Itoa(blindLevel) + ")"
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetLuckyJackpotAward query error:%v cmd:%v", err, cmd)
		return nil, err
	}

	var Info *pb.AwardInfo
	for rows.Next() {

		Info = new(pb.AwardInfo)
		err = rows.Scan(&Info.PlayerId, &Info.HandLevel, &Info.AwardAmount, &Info.AwardTime)
		if err != nil {
			logger.Notic("GetLuckyJackpotAward scan error:%v", err)
			return nil, err
		}

		if userInfo, err := GetSubUserInfo(Info.PlayerId); err == nil {
			if userInfo != nil {
				Info.PlayerName = userInfo.Name
			}
		}
		break
	}
	return Info, err
}

func GetJackpotInfoList(clubId uint32) ([]*pb.JackpotInfo, error) {
	//select COALESCE(blind_level,0),COALESCE(amount,0),COALESCE(scale,0),COALESCE(drawin_amount,0) from t_jackpot where club_id = 100001
	cmd := "select COALESCE(blind_level,0),COALESCE(amount,0),COALESCE(scale,0),COALESCE(drawin_amount,0) from t_jackpot where club_id = " + common.U32toa(clubId)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetJackpotInfoList query error:%v cmd:%v", err, cmd)
		return nil, err
	}

	var list []*pb.JackpotInfo
	for rows.Next() {
		var blind_level int32
		Info := new(pb.JackpotInfo)
		err = rows.Scan(&blind_level, &Info.Amount, &Info.ProfitScale, &Info.DrawinAmout)
		if err != nil {
			logger.Notic("GetJackpotInfoList scan error:%v", err)
			return nil, err
		}

		Info.BlindLevel = blind_level

		list = append(list, Info)
	}
	return list, err
}

func GetJackpotInfo(clubId uint32, blindLevel int) (*pb.JackpotInfo, error) {
	//select COALESCE(blind_level,0),COALESCE(amount,0),COALESCE(scale,0),COALESCE(drawin_amount,0) from t_jackpot where club_id = 100001 and blind_level = 1
	cmd := "select COALESCE(blind_level,0),COALESCE(amount,0),COALESCE(scale,0),COALESCE(drawin_amount,0) from t_jackpot where club_id = " + common.U32toa(clubId) + " and blind_level = " + strconv.Itoa(blindLevel)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetJackpotInfo query error:%v cmd:%v", err, cmd)
		return nil, err
	}

	var info *pb.JackpotInfo = nil
	for rows.Next() {
		var blind_level int32
		var amount uint64

		info = new(pb.JackpotInfo)
		err = rows.Scan(&blind_level, &amount, &info.ProfitScale, &info.DrawinAmout)
		if err != nil {
			logger.Notic("GetJackpotInfo scan error:%v", err)
			return nil, err
		}

		break
	}
	return info, err
}

func GetAward2ClubPercent(clubId uint32) (int, bool, error) {
	var bExist bool = false
	cmd := "select COALESCE(reward_percent,0) from t_jackpot_club where club_id = " + common.U32toa(clubId)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	} else {
		logger.Notic("GetAward2ClubPercent query error:%d cmd:%v", err, cmd)
		return -1, bExist, err
	}

	var reward_percent int
	for rows.Next() {
		err = rows.Scan(&reward_percent)
		if err != nil {
			logger.Notic("GetAward2ClubPercent scan error:%v", err)
			return reward_percent, bExist, err
		}

		bExist = true

		break
	}
	return reward_percent, bExist, err
}

func GetAwardTypeList(clubId uint32) ([]*pb.AwardType, error) {
	//select COALESCE(hand_level,0),COALESCE(reward_percent,0) from t_jackpot_setting where club_id = 100001
	cmd := "select COALESCE(hand_level,0),COALESCE(reward_percent,0) from t_jackpot_setting where club_id = " + common.U32toa(clubId)
	rows, err := m_game_db.Query(cmd)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Notic("GetAwardTypeList query error:%v", err)
		return nil, err
	}

	var list []*pb.AwardType
	for rows.Next() {
		var hand_level int32
		var reward_percent int32

		err = rows.Scan(&hand_level, &reward_percent)
		if err != nil {
			logger.Notic("GetAwardTypeList scan error:", err)
			return nil, err
		}

		Info := new(pb.AwardType)
		Info.HandLevel = hand_level
		Info.AwardPercent = reward_percent
		list = append(list, Info)
	}
	return list, err
}

func GetClubOpenedJackpots(clubId uint32) ([]int, error) {
	//select blind_level from t_jackpot where club_id = 100351
	cmd := "select blind_level from t_jackpot where club_id = " + common.U32toa(clubId)
	JackpotIds, err := GetIntList(cmd)
	return JackpotIds, err
}

func DeleteSpecialBlindlevels(clubId uint32, blindLevel int) error {
	//DELETE from t_jackpot WHERE club_id = 100001 and blind_level= 1
	cmd := "DELETE from t_jackpot WHERE club_id = " + common.U32toa(clubId) + " and blind_level= " + strconv.Itoa(blindLevel)
	err := ExcuteCmd(cmd)
	return err
}
