package com.boying.controller.car;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.boying.common.SystemConfigProperties;
|
import com.boying.common.util.DateUtil;
|
import com.boying.common.util.StringUtil;
|
import com.boying.entity.*;
|
import com.boying.service.*;
|
import com.google.gson.JsonIOException;
|
import com.google.gson.JsonObject;
|
import com.google.gson.JsonParser;
|
import com.google.gson.JsonSyntaxException;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.jpa.domain.Specification;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import javax.persistence.criteria.CriteriaBuilder;
|
import javax.persistence.criteria.CriteriaQuery;
|
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Root;
|
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.*;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
import java.util.concurrent.TimeUnit;
|
|
import static com.boying.common.util.DateUtil.getMinute;
|
|
/**
|
* Servlet implementation class PlateServlet
|
*/
|
@Controller
|
@RequestMapping("/car")
|
public class PlateServlet2 extends HttpServlet {
|
private static final long serialVersionUID = 1L;
|
@Autowired
|
private SystemConfigProperties systemConfigProperties;
|
@Autowired
|
private StringRedisTemplate redisTemplate;
|
@Autowired
|
private ParkService parkService;
|
|
/**
|
* 回复开闸
|
*/
|
protected void open(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
response.setContentType("text/json");
|
PrintWriter out = response.getWriter();
|
out.println("{\"Response_AlarmInfoPlate\":{\"info\":\"ok\",\"content\":\"...\",\"is_pay\":\"true\"}}");
|
out.flush();
|
out.close();
|
}
|
|
//转码
|
public static String deCode(String str) {
|
try {
|
byte[] b = str.getBytes("UTF-8");//����
|
String sa = new String(b);//����:��ʲô�ַ����������ʲô�ַ�������
|
//String sa = new String(str.getBytes());
|
|
return sa;
|
} catch (Exception e) {
|
e.printStackTrace();
|
return "";
|
}
|
}
|
|
protected void noOpen(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
response.setContentType("text/json");
|
PrintWriter out = response.getWriter();
|
out.println("{\"Response_AlarmInfoPlate\":{\"info\":\"error\",\"content\":\"...\",\"is_pay\":\"false\"}}");
|
out.flush();
|
out.close();
|
}
|
|
@PostMapping("/status")
|
public void heart(String serialno,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
|
|
Barrier barrier = barrierService.findByCode(serialno);
|
|
if(barrier!=null){
|
if(barrier.getType2()==1){
|
open(request,response);
|
barrier.setType2(0);
|
barrierService.save(barrier);
|
return;
|
}
|
|
barrier.setStatus(0);
|
barrier.setCreateTime(new Date());
|
barrierService.save(barrier);
|
|
OutPark outPark = outParkService.count4(barrier.getId());
|
if(outPark!=null){
|
open(request,response);
|
outPark.setStatus2(1);
|
outParkService.save(outPark);
|
}else{
|
noOpen(request,response);
|
}
|
}
|
}
|
/**
|
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
|
*/
|
@PostMapping("/PlateServlet")
|
protected void info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
String s= "";
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
|
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));
|
String str = "";
|
String lineStr = "";
|
while((str = reader.readLine()) != null){
|
lineStr += str;
|
}
|
System.out.println(lineStr);
|
|
JSONObject jsonObject1 = JSON.parseObject(lineStr);
|
JSONObject alarmInfoPlate = jsonObject1.getJSONObject("AlarmInfoPlate");
|
JSONObject result = alarmInfoPlate.getJSONObject("result");
|
JSONObject plateResult = result.getJSONObject("PlateResult");
|
String carNo = plateResult.get("license").toString();
|
String code = alarmInfoPlate.get("serialno").toString();
|
System.out.println(carNo+"===="+code);
|
|
try {
|
JsonParser parser=new JsonParser();
|
do {
|
JsonObject jsonObject=(JsonObject) parser.parse(lineStr);
|
if( jsonObject == null || jsonObject.isJsonNull() ) {
|
break;
|
}
|
|
JsonObject jsonInfoPlate = jsonObject.get("AlarmInfoPlate").getAsJsonObject();
|
if( jsonInfoPlate == null || jsonInfoPlate.isJsonNull() ) {
|
break;
|
}
|
|
JsonObject jsonResult = jsonInfoPlate.get("result").getAsJsonObject();
|
if( jsonResult == null || jsonResult.isJsonNull() ) {
|
break;
|
}
|
|
JsonObject jsonPlateResult = jsonResult.get("PlateResult").getAsJsonObject();
|
if( jsonPlateResult == null || jsonPlateResult.isJsonNull() ) {
|
break;
|
}
|
|
String license = jsonPlateResult.get("license").getAsString();//车牌号
|
if( license == null || license == "") {
|
break;
|
}
|
|
String serialno = jsonInfoPlate.get("serialno").getAsString();//摄像机序列号
|
if( serialno == null || serialno == "" ) {
|
break;
|
}
|
|
System.out.println("编号:"+serialno+"设备识别到了:"+license);
|
String format = sdf.format(new Date());
|
s+= format+",车牌号为:"+license+",serialno为:"+code+"。 ";
|
|
Barrier barrier = barrierService.findByCode(code);
|
if(barrier==null){
|
s+="查询道闸为空\n";
|
}else{
|
if(barrier.getType()==0){
|
outPark(license, barrier.getId(), barrier.getParkId());
|
s+=license+"-保存出场记录\n";
|
}else{
|
open(request, response);//抬杆
|
enterPark(license,barrier.getId(),barrier.getParkId());
|
s+=license+"-保存入场记录\n";
|
}
|
}
|
writeTxt(s);
|
}while(false);
|
}
|
|
catch (JsonIOException e) {
|
System.out.println("无入场记录");
|
s+=" ,查询场内记录异常";
|
e.printStackTrace();
|
writeTxt(s);
|
}
|
catch (JsonSyntaxException e) {
|
System.out.println("无入场记录");
|
s+=" ,查询场内记录异常";
|
e.printStackTrace();
|
writeTxt(s);
|
}
|
catch (Exception e) {
|
System.out.println("无入场记录");
|
s+=" ,查询场内记录异常";
|
writeTxt(s);
|
}
|
}
|
|
|
private void writeTxt( String txt)
|
{
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
try
|
{
|
FileWriter f = new FileWriter(systemConfigProperties.getLogPath()+sdf.format(new Date())+".txt",true);
|
BufferedWriter bw=new BufferedWriter(f);
|
bw.write(txt);
|
bw.newLine();
|
bw.close();
|
}
|
catch(Exception e)
|
{
|
System.out.println("打印错误");
|
}
|
}
|
|
|
@Autowired
|
private OutParkService outParkService;
|
@Autowired
|
private EnterParkService enterParkService;
|
@Autowired
|
private BarrierService barrierService;
|
@Autowired
|
private TicketService ticketService;
|
@Autowired
|
private CostRuleService costRuleService;
|
|
public void enterPark(String carNo,Long barrierId,Long parkId) {
|
Park park = (Park)parkService.findById(parkId);
|
int num = 0;
|
String s = redisTemplate.opsForValue().get("car_park_" + parkId);
|
if(park != null){
|
num = park.getNum();
|
if(s !=null){
|
if(Integer.parseInt(s) > num){
|
return;
|
}
|
}else {
|
s= "0";
|
redisTemplate.opsForValue().set("car_park_" + parkId,s,30, TimeUnit.DAYS);
|
}
|
}
|
enterParkService.deleteByCarNo(carNo,parkId);
|
EnterPark enterPark = new EnterPark();
|
enterPark.setCreateTime(new Date());
|
enterPark.setCarNo(carNo);
|
enterPark.setBarrierId(barrierId);
|
enterPark.setParkId(parkId);
|
|
Specification<Ticket> specification2 = new Specification<Ticket>() {
|
@Override
|
public Predicate toPredicate(Root<Ticket> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
|
List<Predicate> list = new ArrayList<Predicate>();
|
list.add(cb.equal(root.get("carNo").as(String.class), enterPark.getCarNo()));
|
list.add(cb.notEqual(root.get("payStatus"), 1));
|
Predicate[] arr = new Predicate[list.size()];
|
cq.where(list.toArray(arr));
|
return null;
|
}
|
};
|
List<Ticket> tickets = ticketService.findAll(specification2);
|
if(tickets.size()>0){
|
enterPark.setStatus(1);//发现有违章
|
}
|
enterParkService.save(enterPark);
|
int i = Integer.parseInt(s);
|
i++;
|
redisTemplate.opsForValue().set("car_park_" + parkId,Integer.toString(i),30, TimeUnit.DAYS);
|
|
Barrier barrier = (Barrier) barrierService.findById(barrierId);
|
barrier.setType2(1);
|
barrierService.save(barrier);
|
}
|
|
public double outPark(String carNo,Long barrierId,Long parkId) {
|
OutPark outPark = new OutPark();
|
outPark.setCarNo(carNo);
|
outPark.setParkId(parkId);
|
outPark.setBarrierId(barrierId);
|
outPark.setCreateTime(new Date());
|
outPark.setCode(System.currentTimeMillis()+"");
|
EnterPark enterPark = enterParkService.findByCarNo(carNo).get(0);
|
|
if(enterPark==null){
|
String s1 = "未发现入场车辆:"+carNo;
|
}else{
|
outPark.setEnterTime(enterPark.getCreateTime());
|
}
|
String redis = redisTemplate.opsForValue().get("car_park_" + parkId);
|
long l = outPark.getCreateTime().getTime() - enterPark.getCreateTime().getTime();
|
outPark.setTime(l/(1000*60));
|
double money = 0;
|
try {
|
money = costRuleService.getMoney(parkId, enterPark.getCreateTime(), outPark.getCreateTime(), 1);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
outPark.setPrice(Double.valueOf(String.format("%.1f", money)));
|
|
outPark.setStatus3(findTicket(carNo));
|
outParkService.save(outPark);
|
int i = Integer.parseInt(redis);
|
i--;
|
if(i<0){
|
redisTemplate.opsForValue().set("car_park_" + parkId,"0",30, TimeUnit.DAYS);
|
}else {
|
redisTemplate.opsForValue().set("car_park_" + parkId,Integer.toString(i),30, TimeUnit.DAYS);
|
}
|
|
Barrier barrier = (Barrier) barrierService.findById(barrierId);
|
barrier.setCarNo(carNo);
|
if(outPark.getPrice()==0&&outPark.getStatus3()==0){
|
barrier.setType2(1);
|
}
|
barrierService.save(barrier);
|
return money;
|
}
|
|
public int findTicket(String carNo){
|
Specification<Ticket> specification2 = new Specification<Ticket>() {
|
@Override
|
public Predicate toPredicate(Root<Ticket> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
|
List<Predicate> list = new ArrayList<Predicate>();
|
list.add(cb.equal(root.get("carNo").as(String.class), carNo));
|
list.add(cb.notEqual(root.get("payStatus"), 1));
|
Predicate[] arr = new Predicate[list.size()];
|
cq.where(list.toArray(arr));
|
return null;
|
}
|
};
|
List<Ticket> tickets = ticketService.findAll(specification2);
|
if(tickets.size()>0){
|
return 1;
|
}else{
|
return 0;
|
}
|
}
|
}
|