You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

86 lines
3.9 KiB

package app.services.db;
import app.entities.db.AdminInfo;
import app.entities.db.Permition;
import app.entities.other.SteamID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Service
public class PermitionService {
@Autowired
@Qualifier("jt_rw")
JdbcTemplate jdbcTemplate;
public Permition getPermition(SteamID steamID){
return jdbcTemplate.query("SELECT id, flags, immunity, status, amount, extract(epoch from reg_date) as utime FROM sm_admins WHERE identity LIKE ?",
new Object[]{steamID.steam2},
(rs, n) -> new Permition(rs))
.stream().findFirst().orElse(null);
}
public boolean CheckMorePowerfull(SteamID user_1, SteamID user_2) {
if (user_1.is(user_2)) return true;
Permition permition_1 = getPermition(user_1);
if (permition_1 == null) return false;
Permition permition_2 = getPermition(user_2);
if (permition_2 == null) return true;
return permition_1.getImmunity() > permition_2.getImmunity();
}
public int addPermition(SteamID steamID, Integer amount, String flags, Integer immunity_level, String status, String comment) {
if (getPermition(steamID) != null) return 0;
return jdbcTemplate.update("INSERT INTO sm_admins (authtype, identity, password, flags, name, immunity, comment, status, reg_date, amount) VALUES ('steam', ?, NULL, ?, '', ?, ?, ?, CURRENT_TIMESTAMP, ?)",
steamID.steam2, flags, immunity_level, comment, status, amount);
}
public boolean removePermition(SteamID steamID, String status) {
if (getPermition(steamID) == null) return true;
return jdbcTemplate.update("DELETE FROM sm_admins WHERE identity = ? AND status LIKE ?",
steamID.steam2, status) > 0;
}
public int extendPermition(SteamID steamID, Integer amount, String status) {
return jdbcTemplate.update("UPDATE sm_admins SET amount=amount+? WHERE identity LIKE ? AND status LIKE ?",
amount, steamID.steam2, status);
}
public int addFreeVIP(SteamID steamID, Integer amount) {
return addPermition(steamID, amount, "oat", 20, "VIP", "f13bot.FreeVIP");
}
public int addVIP(SteamID steamID, Integer amount) {
//лишняя проверка не будет лишним
Permition permition = getPermition(steamID);
if (permition == null) return addPermition(steamID, amount, "oa", 20, "VIP", "f13bot.User");
else if (permition.getStatus().equals("VIP")) return -1 * extendPermition(steamID, amount, "VIP");
else return 0;
}
public boolean removeVIP(SteamID steamID) {
if (getPermition(steamID) == null) return true;
return removePermition(steamID, "VIP");
}
public AdminInfo getAdminInfo(SteamID steamID) {
//Так слушай меня ваня, ты ебанько ибо перепутал тут steam2 и steam3, d в бд steam3 хотя по факту там 2
List<AdminInfo> result = jdbcTemplate.query("SELECT id, vk_id, discord_id, discord_name, steam3, permition FROM bot_admins WHERE steam3 LIKE ?",
new Object[]{steamID.steam2},
(rs, n) -> new AdminInfo(rs));
//fix если модератор снят делаем бан пельменю
if(result.isEmpty()) result = jdbcTemplate.query("SELECT id, vk_id, discord_id, discord_name, steam3, permition FROM bot_admins WHERE steam3 LIKE ?",
new Object[]{"STEAM_0:0:90763498"},
(rs, n) -> new AdminInfo(rs));
return result.stream().findFirst().orElse(null);
}
}