package com.example.demo.module.rank;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.CacheMapRoot;
import com.example.demo.common.GlobalRoot;
import com.example.demo.table.RoomGameRecord;
import pers.jc.sql.SQL;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Socket服务-排行
 */
public class RankService {
    public static RankService ins = new RankService();

    public void setRoomNeedCalculatePKScore(String roomKey) {
        CacheMapRoot.roomNeedCalculatePKScore.put(roomKey, true,
            System.currentTimeMillis() + 60 * 60 * 1000);
    }

    public boolean isRoomNeedCalculatePKScore(String roomKey) {
        Boolean res = CacheMapRoot.roomNeedCalculatePKScore.get(roomKey, Boolean.class);
        if (res == null) return false;
        return res;
    }

    public int getDefaultPkScore() {
        return 1500;
    }

    public Integer getGameScore(int gameType, int userID) {
        SQL sql = new SQL(){{
            SELECT("score");
            FROM("game" + gameType + "_score");
            WHERE("userID=" + PARAM(userID));
        }};
        List<JSONObject> list = GlobalRoot.db.select(sql);
        if (list.size() > 0) {
            return list.get(0).getInteger("score");
        }
        return getDefaultPkScore();
    }

    public void updateGameScore(int gameType, int userID, int score) {
        SQL sql = new SQL(){{
            INSERT_INTO("game" + gameType + "_score");
            VALUES("userID", PARAM(userID));
            VALUES("score", PARAM(score));
            VALUES("update_time", PARAM(new Timestamp(System.currentTimeMillis())));
//            ON_DUPLICATE_KEY_UPDATE("score = score + " + PARAM(score));
            ON_DUPLICATE_KEY_UPDATE("score = " + PARAM(score));
        }};
        GlobalRoot.db.insert(sql);
    }

    public boolean insertRoomGameRecord(
            String roomKey, int gameType, int p1ID, int p2ID, int gameResult
    ) {
        RoomGameRecord roomGameRecord = new RoomGameRecord(roomKey, gameType, p1ID, p2ID, gameResult);
        int insertRes = GlobalRoot.db.insertIgnore(roomGameRecord);
        return insertRes > 0;
    }


    //单机默认分数
    public int getDefaultSingleScore(){
        return 0;
    }
    /**
     * 不存在分数则默认返回0，通常用于单机游戏
     * @param gameType
     * @param userID
     * @return
     */
    public Integer getSingleGameScore(int gameType, int userID) {
        SQL sql = new SQL(){{
            SELECT("score");
            FROM("game" + gameType + "_score");
            WHERE("userID=" + PARAM(userID));
        }};
        List<JSONObject> list = GlobalRoot.db.select(sql);
        if (list.size() > 0) {
            return list.get(0).getInteger("score");
        }
        return getDefaultSingleScore();
    }

    /**
     * 处理赛季分数，替换旧的接口
     */
    /**
     * 获取单个的赛季分数，一个列表会存储同一个用户的N个月的数据，用id区分唯一性
     * @param gameType
     * @param userID
     * @param startDateStr
     * @param endDateStr
     * @return
     */
    public Map getSeasonGameScore(int gameType, int userID, String startDateStr, String endDateStr) {
        SQL sql = new SQL(){{
            SELECT("id","score");
            FROM("game_season_" + gameType);
            WHERE("userID=" + PARAM(userID) +" AND create_time >= '"+startDateStr+"' AND create_time <= '" + endDateStr + "'");
        }};
        List<JSONObject> list = GlobalRoot.db.select(sql);
        Map res = new HashMap();
        if (list.size() > 0) {
            res.put("score", list.get(0).getInteger("score"));
            res.put("id", list.get(0).getInteger("id"));
            return res;// list.get(0).getInteger("score");
        }
        res.put("score",getDefaultPkScore());
        res.put("id",0);
        return res;
    }

    /**
     * 单机分数获取，默认返回0
     * @param gameType
     * @param userID
     * @return
     */
    public Map getSingleSeasonGameScore(int gameType, int userID , String startDateStr, String endDateStr) {

        SQL sql = new SQL(){{
            SELECT("id", "score");
            FROM("game_season_" + gameType);
            WHERE("userID=" + PARAM(userID) +" AND create_time >= '"+startDateStr+"' AND create_time <= '" + endDateStr + "'");
        }};
        List<JSONObject> list = GlobalRoot.db.select(sql);
        Map res = new HashMap();
        if (list.size() > 0) {
            res.put("score", list.get(0).getInteger("score"));
            res.put("id",list.get(0).getInteger("id"));
            return res;// list.get(0).getInteger("score");
        }
        res.put("score", getDefaultSingleScore());
        res.put("id",0);
        return res;
    }

    public void updateSeasonGameScore(int gameType, int id, int userID, int score) {
        SQL sql = new SQL(){{
            INSERT_INTO("game_season_" + gameType);
            VALUES("id",PARAM(id));
            VALUES("userID", PARAM(userID));
            VALUES("score", PARAM(score));
            VALUES("update_time", PARAM(new Timestamp(System.currentTimeMillis())));
            ON_DUPLICATE_KEY_UPDATE("score = " + PARAM(score));
        }};
        GlobalRoot.db.insert(sql);
    }

}
