new operator entity

This commit is contained in:
Sola
2015-12-07 13:43:16 +08:00
parent bdf3cfe74e
commit b0ba46cc77
16 changed files with 212 additions and 145 deletions

View File

@@ -1,6 +1,5 @@
package love.sola.netsupport.api;
import love.sola.netsupport.enums.Block;
import love.sola.netsupport.enums.ISP;
import love.sola.netsupport.pojo.User;
import love.sola.netsupport.sql.TableUser;
@@ -38,7 +37,7 @@ public class Register extends HttpServlet {
}
ISP isp = checkISP(request.getParameter("isp"));
Block block = checkBlock(request.getParameter("block"));
int block = checkBlock(request.getParameter("block"));
String result = register(
checkStudentId(request.getParameter("sid")),
request.getParameter("name"),
@@ -60,13 +59,13 @@ public class Register extends HttpServlet {
Redirect.message(response, -1, "Illegal_Request");
}
private String register(long sid, String name, ISP isp, String netAccount, Block block, int room, long phone, String wechat) {
private String register(long sid, String name, ISP isp, String netAccount, int block, int room, long phone, String wechat) {
if (wechat == null) return "Illegal_Request";
if (sid == -1) return "Invalid_Student_Id";
if (name == null) return "Invalid_Name";
if (isp == null) return "Invalid_ISP";
if (netAccount == null) return "Invalid_Account";
if (block == null) return "Invalid_Block";
if (block == -1) return "Invalid_Block";
if (room == -1) return "Invalid_Room";
if (phone == -1) return "Invalid_Phone_Number";
User user = TableUser.getUserById(sid);
@@ -122,17 +121,17 @@ public class Register extends HttpServlet {
return account;
}
private Block checkBlock(String block) {
if (block == null) return null;
private int checkBlock(String block) {
if (block == null) return -1;
try {
return Block.fromId(Integer.parseInt(block));
return Integer.parseInt(block);
} catch (NumberFormatException ignored) {
}
return null;
return -1;
}
private int checkRoom(String room, Block block) {
if (block == null) return -1;
private int checkRoom(String room, int block) {
if (block == -1) return -1;
if (room == null) return -1;
try {
Integer i = Integer.parseInt(room);

View File

@@ -0,0 +1,10 @@
package love.sola.netsupport.api;
/**
* ***********************************************
* Created by Sola on 2015/12/6.
* Don't modify this source without my agreement
* ***********************************************
*/
public class TicketCancel {
}

View File

@@ -9,6 +9,7 @@ import love.sola.netsupport.wechat.Command;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import javax.servlet.ServletException;
@@ -64,6 +65,7 @@ public class TicketQuery extends HttpServlet {
int limit = request.getParameter("limit") == null ? 5 : Integer.parseInt(request.getParameter("limit"));
c.setFirstResult(first);
c.setMaxResults(limit);
c.addOrder(Order.desc(Ticket.PROPERTY_SUBMIT_TIME));
c.add(Restrictions.eq(Ticket.PROPERTY_USER, u));
if (request.getParameter("status") != null) {
c.add(Restrictions.eq(Ticket.PROPERTY_STATUS, Integer.parseInt(request.getParameter("status"))));

View File

@@ -1,6 +1,7 @@
package love.sola.netsupport.api;
package love.sola.netsupport.api.admin;
import com.google.gson.Gson;
import love.sola.netsupport.api.Response;
import love.sola.netsupport.pojo.User;
import love.sola.netsupport.sql.SQLCore;
import love.sola.netsupport.sql.TableUser;

View File

@@ -1,4 +1,4 @@
package love.sola.netsupport.api;
package love.sola.netsupport.api.test;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

View File

@@ -1,106 +1,56 @@
package love.sola.netsupport.enums;
import com.google.gson.annotations.SerializedName;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import static love.sola.netsupport.config.Lang.lang;
/**
* ***********************************************
* Created by Sola on 2015/11/30.
* Don't modify this source without my agreement
* ***********************************************
*/
public enum Block {
public class Block {
@SerializedName("10")
QT_18(10),
@SerializedName("11")
QT_19(11),
@SerializedName("12")
QT_16(12),
@SerializedName("13")
QT_17(13),
@SerializedName("20")
BM_7(20),
@SerializedName("21")
BM_8(21),
@SerializedName("22")
BM_9(22),
@SerializedName("23")
BM_10(23),
@SerializedName("24")
BM_11(24),
@SerializedName("30")
DM_12(30),
@SerializedName("31")
DM_13(31),
@SerializedName("32")
DM_14(32),
@SerializedName("33")
DM_15(33),
@SerializedName("40")
XH_A(40),
@SerializedName("41")
XH_B(41),
@SerializedName("42")
XH_C(42),
@SerializedName("43")
XH_D(43),
@SerializedName("50")
FX_1(50),
@SerializedName("51")
FX_2(51),
@SerializedName("52")
FX_3(52),
@SerializedName("53")
FX_4(53),
@SerializedName("54")
FX_5(54),
@SerializedName("55")
FX_6(55),
@SerializedName("60")
DM_20(60),
@SerializedName("61")
DM_21(61),
;
public static final int QT_18 = 10;
public static final int QT_19 = 11;
public static final int QT_16 = 12;
public static final int QT_17 = 13;
public static final int BM_7 = 20;
public static final int BM_8 = 21;
public static final int BM_9 = 22;
public static final int BM_10 = 23;
public static final int BM_11 = 24;
public static final int DM_12 = 30;
public static final int DM_13 = 31;
public static final int DM_14 = 32;
public static final int DM_15 = 33;
public static final int XH_A = 40;
public static final int XH_B = 41;
public static final int XH_C = 42;
public static final int XH_D = 43;
public static final int FX_1 = 50;
public static final int FX_2 = 51;
public static final int FX_3 = 52;
public static final int FX_4 = 53;
public static final int FX_5 = 54;
public static final int FX_6 = 55;
public static final int DM_20 = 60;
public static final int DM_21 = 61;
private static final Map<String, Block> NAME_MAP = new HashMap<>();
private static final Map<Integer, Block> ID_MAP = new HashMap<>();
public static final Map<Integer, String> inverseMap = new HashMap<>();
static {
for (Block type : values()) {
if (type.name != null) {
NAME_MAP.put(type.name.toLowerCase(), type);
}
if (type.id > 0) {
ID_MAP.put(type.id, type);
static{
System.out.println("Loading Blocks...");
for (Field field : Block.class.getDeclaredFields()) {
if (field.getType().isAssignableFrom(Integer.TYPE)) {
try {
inverseMap.put((Integer) field.get(null), field.getName());
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
public final String name;
public final int id;
Block(int id) {
this.name = lang("BLOCK_" + name());
this.id = id;
}
public static Block fromName(String name) {
if (name == null) {
return null;
}
return NAME_MAP.get(name.toLowerCase());
}
public static Block fromId(int id) {
return ID_MAP.get(id);
}
@Override
public String toString() { return name; }
}

View File

@@ -1,25 +0,0 @@
package love.sola.netsupport.enums;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter
public class BlockConverter implements AttributeConverter<Block, Integer> {
@Override
public Integer convertToDatabaseColumn(Block attribute) {
if (attribute == null) {
return null;
}
return attribute.id;
}
@Override
public Block convertToEntityAttribute(Integer dbData) {
if (dbData == null) {
return null;
}
return Block.fromId(dbData);
}
}

View File

@@ -0,0 +1,33 @@
package love.sola.netsupport.enums;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
* ***********************************************
* Created by Sola on 2015/12/6.
* Don't modify this source without my agreement
* ***********************************************
*/
public class Status {
public static final int UNCHECKED = 0;
public static final int UNSOLVED = 9;
public static final Map<Integer, String> inverseMap = new HashMap<>();
static{
System.out.println("Loading Status...");
for (Field field : Status.class.getDeclaredFields()) {
if (field.getType().isAssignableFrom(Integer.TYPE)) {
try {
inverseMap.put((Integer) field.get(null), field.getName());
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}

View File

@@ -0,0 +1,39 @@
package love.sola.netsupport.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* ***********************************************
* Created by Sola on 2015/12/6.
* Don't modify this source without my agreement
* ***********************************************
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "operators")
public class Operator {
@Id
@Column(name = "id", nullable = false, insertable = false, updatable = false)
private Integer id;
@Column(name = "name", nullable = false, insertable = false, updatable = false)
private String name;
@Column(name = "access", nullable = false, insertable = false, updatable = false)
private Integer access;
@Column(name = "wechat", nullable = false, insertable = false, updatable = false)
private String wechat;
private Integer block;
private Integer week;
private String password;
}

View File

@@ -23,6 +23,7 @@ public class Ticket {
public static final String PROPERTY_USER = "user";
public static final String PROPERTY_STATUS = "status";
public static final String PROPERTY_SUBMIT_TIME = "submitTime";
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -31,13 +32,13 @@ public class Ticket {
@JoinColumn(name = TableTicket.COLUMN_SID)
private User user;
private String description;
@Column(name = TableTicket.COLUMN_SUBMIT_TIME, insertable = false)
@Column(name = TableTicket.COLUMN_SUBMIT_TIME, insertable = false, updatable = false)
private Date submitTime;
private String remark;
private Date updateTime;
@ManyToOne(optional = true)
@JoinColumn(name = TableTicket.COLUMN_OPSID)
private User operator;
private Operator operator;
private Integer status;
}

View File

@@ -3,8 +3,6 @@ package love.sola.netsupport.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import love.sola.netsupport.enums.Block;
import love.sola.netsupport.enums.BlockConverter;
import love.sola.netsupport.enums.ISP;
import love.sola.netsupport.enums.ISPConverter;
@@ -23,6 +21,10 @@ import javax.persistence.*;
@Table(name = "users")
public class User {
public static final String PROPERTY_NAME = "name";
public static final String PROPERTY_WECHAT = "wechatId";
public static final String PROPERTY_BLOCK = "block";
@Id
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@@ -34,8 +36,7 @@ public class User {
private String netAccount;
@Column(name = "wechat")
private String wechatId;
@Convert(converter = BlockConverter.class)
private Block block;
private Integer block;
private Integer room;
private Long phone;

View File

@@ -1,5 +1,13 @@
package love.sola.netsupport.sql;
import love.sola.netsupport.enums.Status;
import love.sola.netsupport.pojo.Ticket;
import love.sola.netsupport.pojo.User;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import java.util.List;
/**
* ***********************************************
* Created by Sola on 2015/12/6.
@@ -17,4 +25,24 @@ public class TableTicket extends SQLCore {
public static final String COLUMN_OPSID = "opsid";
public static final String COLUMN_STATUS = "status";
public static Ticket queryLastOpen(User u) {
try (Session s = SQLCore.sf.openSession()) {
return (Ticket) s.createCriteria(Ticket.class)
.add(Restrictions.eq(Ticket.PROPERTY_USER, u))
.add(Restrictions.eq(Ticket.PROPERTY_STATUS, Status.UNCHECKED))
.uniqueResult();
}
}
@SuppressWarnings("unchecked")
public static List<Ticket> queryUnsolvedByBlock(int b) {
try (Session s = SQLCore.sf.openSession()) {
return s.createCriteria(Ticket.class)
.createCriteria(Ticket.PROPERTY_USER)
.add(Restrictions.between(User.PROPERTY_BLOCK, b * 10, (b + 1) * 10 - 1))
.list();
}
}
}

View File

@@ -1,7 +1,6 @@
package love.sola.netsupport.sql;
import love.sola.netsupport.pojo.User;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
@@ -26,29 +25,20 @@ public class TableUser extends SQLCore {
public static User getUserById(long id) {
try (Session s = sf.openSession()) {
return s.get(User.class, id);
} catch (HibernateException e) {
e.printStackTrace();
}
return null;
}
public static User getUserByWechat(String wechat) {
try (Session s = sf.openSession()) {
return (User) s.createCriteria(User.class).add(Restrictions.eq(COLUMN_WECHAT, wechat)).uniqueResult();
} catch (HibernateException e) {
e.printStackTrace();
return (User) s.createCriteria(User.class).add(Restrictions.eq(User.PROPERTY_WECHAT, wechat)).uniqueResult();
}
return null;
}
public static User getUserByName(String name) {
try (Session s = sf.openSession()) {
return (User) s.createCriteria(User.class).add(Restrictions.eq(COLUMN_NAME, name)).uniqueResult();
} catch (HibernateException e) {
e.printStackTrace();
return (User) s.createCriteria(User.class).add(Restrictions.eq(User.PROPERTY_NAME, name)).uniqueResult();
}
return null;
}
public static int updateUser(User user) {
@@ -57,10 +47,7 @@ public class TableUser extends SQLCore {
s.update(user);
s.getTransaction().commit();
return 1;
} catch (HibernateException e) {
e.printStackTrace();
}
return -1;
}
}

View File

@@ -0,0 +1,21 @@
package love.sola.netsupport.util;
import org.mindrot.jbcrypt.BCrypt;
/**
* ***********************************************
* Created by Sola on 2015/12/6.
* Don't modify this source without my agreement
* ***********************************************
*/
public class Crypt {
public static String hash(String pw) {
return BCrypt.hashpw(pw, BCrypt.gensalt());
}
public static boolean check(String plain, String hash) {
return BCrypt.checkpw(plain, hash);
}
}

View File

@@ -39,6 +39,7 @@
<!-- Names the annotated entity class -->
<mapping class="love.sola.netsupport.pojo.User"/>
<mapping class="love.sola.netsupport.pojo.Ticket"/>
<mapping class="love.sola.netsupport.pojo.Operator"/>
</session-factory>

View File

@@ -0,0 +1,19 @@
package love.sola.netsupport.wechat;
import love.sola.netsupport.enums.Block;
import org.junit.Test;
/**
* ***********************************************
* Created by Sola on 2015/12/6.
* Don't modify this source without my agreement
* ***********************************************
*/
public class TestReflection {
@Test
public void testBlock() {
System.out.println(Block.inverseMap);
}
}