mirror of
				https://github.com/ZSCNetSupportDept/WechatTicketSystem.git
				synced 2025-10-31 10:26:19 +08:00 
			
		
		
		
	tickettracker feature
This commit is contained in:
		| @@ -28,7 +28,7 @@ import java.util.List; | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
| @WebServlet(name = "TicketLookup", urlPatterns = "/api/admin/ticketlookup", loadOnStartup = 24) | ||||
| @WebServlet(name = "TicketLookup", urlPatterns = "/api/admin/ticketlookup", loadOnStartup = 33) | ||||
| public class TicketLookup extends HttpServlet { | ||||
|  | ||||
| 	private Gson gson = SQLCore.gson; | ||||
|   | ||||
| @@ -0,0 +1,70 @@ | ||||
| package love.sola.netsupport.api.admin; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import love.sola.netsupport.api.Response; | ||||
| import love.sola.netsupport.sql.SQLCore; | ||||
| import love.sola.netsupport.sql.TableTicket; | ||||
| import love.sola.netsupport.util.Checker; | ||||
| import love.sola.netsupport.util.ParseUtil; | ||||
| import love.sola.netsupport.wechat.Command; | ||||
| import me.chanjar.weixin.common.session.WxSession; | ||||
| import org.hibernate.HibernateException; | ||||
|  | ||||
| import javax.servlet.ServletException; | ||||
| import javax.servlet.annotation.WebServlet; | ||||
| import javax.servlet.http.HttpServlet; | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
| import javax.servlet.http.HttpServletResponse; | ||||
| import java.io.IOException; | ||||
| import java.io.PrintWriter; | ||||
|  | ||||
| /** | ||||
|  * *********************************************** | ||||
|  * Created by Sola on 2015/12/18. | ||||
|  * Don't modify this source without my agreement | ||||
|  * *********************************************** | ||||
|  */ | ||||
|  | ||||
| @WebServlet(name = "TicketTrack", urlPatterns = "/api/admin/tickettrack", loadOnStartup = 34) | ||||
| public class TicketTrack extends HttpServlet{ | ||||
|  | ||||
| 	private Gson gson = SQLCore.gson; | ||||
|  | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	} | ||||
|  | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
| 		request.setCharacterEncoding("utf-8"); | ||||
| 		response.setCharacterEncoding("utf-8"); | ||||
| 		response.addHeader("Content-type", "application/json;charset=utf-8"); | ||||
| 		PrintWriter out = response.getWriter(); | ||||
| 		String json = gson.toJson(track(request)); | ||||
| 		out.println(ParseUtil.parseJsonP(request, json)); | ||||
| 		out.close(); | ||||
| 	} | ||||
|  | ||||
| 	private Response track(HttpServletRequest request) { | ||||
| 		String tid = request.getParameter("id"); | ||||
| 		if (tid == null) { | ||||
| 			return new Response(Response.ResponseCode.PARAMETER_REQUIRED); | ||||
| 		} | ||||
| 		WxSession session = Checker.isAuthorized(request, Command.LOGIN); | ||||
| 		if (session == null) { | ||||
| 			return new Response(Response.ResponseCode.UNAUTHORIZED); | ||||
| 		} | ||||
| 		try { | ||||
| 			return new Response(Response.ResponseCode.OK, TableTicket.track(Integer.parseInt(tid))); | ||||
| 		} catch (NumberFormatException e) { | ||||
| 			return new Response(Response.ResponseCode.ILLEGAL_PARAMETER); | ||||
| 		} catch (HibernateException e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.DATABASE_ERROR, e.getMessage()); | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 			return new Response(Response.ResponseCode.INTERNAL_ERROR, e.getMessage()); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|  | ||||
| } | ||||
| @@ -2,14 +2,20 @@ package love.sola.netsupport.sql; | ||||
|  | ||||
| import com.google.gson.*; | ||||
| import com.google.gson.annotations.Expose; | ||||
| import com.google.gson.reflect.TypeToken; | ||||
| import com.google.gson.stream.JsonReader; | ||||
| import com.google.gson.stream.JsonWriter; | ||||
| import love.sola.netsupport.enums.ISP; | ||||
| import org.hibernate.Hibernate; | ||||
| import org.hibernate.SessionFactory; | ||||
| import org.hibernate.boot.MetadataSources; | ||||
| import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||||
| import org.hibernate.proxy.HibernateProxy; | ||||
| import org.hibernate.service.ServiceRegistry; | ||||
|  | ||||
| import javax.naming.InitialContext; | ||||
| import javax.sql.DataSource; | ||||
| import java.io.IOException; | ||||
| import java.util.Date; | ||||
|  | ||||
| /** | ||||
| @@ -50,6 +56,7 @@ public class SQLCore { | ||||
| 			.registerTypeAdapter(Date.class, (JsonSerializer<Date>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getTime())) | ||||
| 			.registerTypeAdapter(ISP.class, (JsonDeserializer<ISP>) (json, typeOfT, context) -> ISP.fromId(json.getAsJsonPrimitive().getAsInt())) | ||||
| 			.registerTypeAdapter(ISP.class, (JsonSerializer<ISP>) (src, typeOfSrc, context) -> new JsonPrimitive(src.id)) | ||||
| 			.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY) | ||||
| 			.create(); | ||||
| 	public static SessionFactory sf; | ||||
| 	public static ServiceRegistry sr; | ||||
| @@ -69,4 +76,43 @@ public class SQLCore { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public static class HibernateProxyTypeAdapter extends TypeAdapter<HibernateProxy> { | ||||
|  | ||||
| 		public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() { | ||||
| 			@Override | ||||
| 			@SuppressWarnings("unchecked") | ||||
| 			public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | ||||
| 				return (HibernateProxy.class.isAssignableFrom(type.getRawType()) ? (TypeAdapter<T>) new HibernateProxyTypeAdapter(gson) : null); | ||||
| 			} | ||||
| 		}; | ||||
| 		private final Gson context; | ||||
|  | ||||
| 		private HibernateProxyTypeAdapter(Gson context) { | ||||
| 			this.context = context; | ||||
| 		} | ||||
|  | ||||
| 		@Override | ||||
| 		public HibernateProxy read(JsonReader in) throws IOException { | ||||
| 			throw new UnsupportedOperationException("Not supported"); | ||||
| 		} | ||||
|  | ||||
| 		@SuppressWarnings({"rawtypes", "unchecked"}) | ||||
| 		@Override | ||||
| 		public void write(JsonWriter out, HibernateProxy value) throws IOException { | ||||
| 			if (value == null) { | ||||
| 				out.nullValue(); | ||||
| 				return; | ||||
| 			} | ||||
| 			// Retrieve the original (not proxy) class | ||||
| 			Class<?> baseType = Hibernate.getClass(value); | ||||
| 			// Get the TypeAdapter of the original class, to delegate the serialization | ||||
| 			TypeAdapter delegate = context.getAdapter(TypeToken.get(baseType)); | ||||
| 			// Get a filled instance of the original class | ||||
| 			Object unproxiedValue = ((HibernateProxy) value).getHibernateLazyInitializer() | ||||
| 					.getImplementation(); | ||||
| 			// Serialize the value | ||||
| 			delegate.write(out, unproxiedValue); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -7,6 +7,9 @@ import org.hibernate.Session; | ||||
| import org.hibernate.criterion.Order; | ||||
| import org.hibernate.criterion.Projections; | ||||
| import org.hibernate.criterion.Restrictions; | ||||
| import org.hibernate.envers.AuditReader; | ||||
| import org.hibernate.envers.AuditReaderFactory; | ||||
| import org.hibernate.envers.query.AuditEntity; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| @@ -70,4 +73,21 @@ public class TableTicket extends SQLCore { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@SuppressWarnings("unchecked") | ||||
| 	public static List<Object[]> track(int tid) { | ||||
| 		try (Session s = SQLCore.sf.openSession()) { | ||||
| 			AuditReader reader = getAuditReader(s); | ||||
| 			return reader.createQuery() | ||||
| 					.forRevisionsOfEntity(Ticket.class, false, true) | ||||
| 					.addOrder(AuditEntity.revisionNumber().desc()) | ||||
| 					.add(AuditEntity.id().eq(tid)) | ||||
| 					.getResultList() | ||||
| 					; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	protected static AuditReader getAuditReader(Session session) { | ||||
| 		return AuditReaderFactory.get(session); | ||||
| 	} | ||||
|  | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Sola
					Sola