package com.example.demo.admin.module.user;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.admin.common.ResponseDTO;
import com.example.demo.admin.common.ResponseTable;
import com.example.demo.admin.interceptor.AdminInterceptor;
import com.example.demo.common.GlobalRoot;
import com.example.demo.module.rank.RankService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.jc.sql.SQL;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

/**
 * 后台控制器-PK排行榜
 */
@CrossOrigin
@RestController
@RequestMapping("admin/user")
public class PKRankController {

    @GetMapping("listPKRank")
    public ResponseDTO getRankList(HttpServletRequest request, int page, int limit) {
        //拦截-预处理
        ResponseDTO responseDTO = AdminInterceptor.preHandle(request);
        if (responseDTO != null) return responseDTO;

        //搜索相关
        String searchParams = request.getParameter("searchParams");
        JSONObject searchParamJson = searchParams != null ?  JSONObject.parseObject(searchParams) : new JSONObject();

        ResponseTable responseTable = new ResponseTable();

        Integer gameType = searchParamJson.getInteger("gameType");
        if (gameType == null) gameType = 9;
        String country = searchParamJson.getString("country");
        if (country == null) country = "";
        String state = searchParamJson.getString("state");
        if (state == null) state = "";
        String city = searchParamJson.getString("city");
        if (city == null) city = "";

        SQL sqlUtil = new SQL();
        String tableName = "game" + gameType + "_score";
        String formatScoreSQL = "IFNULL(score," + RankService.ins.getDefaultPkScore() + ") as score";
        String sqlRankList = "SELECT id, nickname, gender, " + formatScoreSQL
                + " FROM user_info LEFT JOIN "
                + tableName + " ON userID = id";

        List<String> limitAreaForCountRows = new LinkedList<>();
        if (country.length() > 0) {
            if (limitAreaForCountRows.size() == 0) sqlRankList += " WHERE";
            else sqlRankList += " AND";
            sqlRankList += " country = " + sqlUtil.PARAM(country);
            limitAreaForCountRows.add("country = " + sqlUtil.PARAM(country));
        }
        if (state.length() > 0) {
            if (limitAreaForCountRows.size() == 0) sqlRankList += " WHERE";
            else sqlRankList += " AND";
            sqlRankList += " state = " + sqlUtil.PARAM(state);
            limitAreaForCountRows.add("state = " + sqlUtil.PARAM(state));
        }
        if (city.length() > 0) {
            if (limitAreaForCountRows.size() == 0) sqlRankList += " WHERE";
            else sqlRankList += " AND";
            sqlRankList += " city = " + sqlUtil.PARAM(city);
            limitAreaForCountRows.add("city = " + sqlUtil.PARAM(city));
        }

        int rowCount = GlobalRoot.db.getRowCount("user_info", new SQL(){{
            LEFT_OUTER_JOIN(tableName + " ON userID = id");
            for (String item: limitAreaForCountRows) {
                WHERE(item);
            }
        }});
        responseTable.setCount(rowCount);

        sqlRankList += " ORDER BY score DESC, update_time ASC, id ASC";
        int startIndex = (page - 1) * limit;
        List<JSONObject> rankList = GlobalRoot.db.select(sqlRankList + " Limit " + startIndex + ", " + limit);
        for (int i = 0; i < rankList.size(); i++) {
            rankList.get(i).put("rankNum", startIndex + i + 1);
        }

        responseTable.setData(rankList);

        return responseTable;
    }
}
