From a9862e81851bbe037edc6bb1c7f562c1e55c0d7f Mon Sep 17 00:00:00 2001
From: kongdeqiang <123456>
Date: 星期一, 19 九月 2022 18:44:18 +0800
Subject: [PATCH] 增加日志和redis

---
 src/main/java/com/boying/controller/car/PlateServlet2.java |   90 +++++++++++++++++++++++++++++++++++----------
 1 files changed, 70 insertions(+), 20 deletions(-)

diff --git a/src/main/java/com/boying/controller/car/PlateServlet2.java b/src/main/java/com/boying/controller/car/PlateServlet2.java
index 2b7c455..aa197ed 100755
--- a/src/main/java/com/boying/controller/car/PlateServlet2.java
+++ b/src/main/java/com/boying/controller/car/PlateServlet2.java
@@ -2,12 +2,10 @@
 
 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.Barrier;
-import com.boying.entity.EnterPark;
-import com.boying.entity.OutPark;
-import com.boying.entity.Ticket;
+import com.boying.entity.*;
 import com.boying.service.*;
 import com.google.gson.JsonIOException;
 import com.google.gson.JsonObject;
@@ -15,6 +13,7 @@
 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;
@@ -30,7 +29,9 @@
 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;
 
@@ -41,6 +42,12 @@
 @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;
 
 	/**
 	 * 鍥炲寮�闂�
@@ -107,7 +114,8 @@
 	 */
 	@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 = "";
@@ -153,50 +161,68 @@
 				}
 
 				String serialno = jsonInfoPlate.get("serialno").getAsString();//鎽勫儚鏈哄簭鍒楀彿
-				if( license == null || license == "" ) {
+				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);
 		}
 	}
 
-	protected void WriteTxt( String path, String txt)
+
+	private void writeTxt( String txt)
 	{
+		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
 		try
 		{
-			FileWriter  f = new FileWriter(path);
-	        BufferedWriter bw=new BufferedWriter(f);
-	        bw.write(txt);
-	        bw.close();
+			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)
-        {
-        }
+		catch(Exception e)
+		{
+			System.out.println("鎵撳嵃閿欒");
+		}
 	}
+
 
 	@Autowired
 	private OutParkService outParkService;
@@ -210,7 +236,20 @@
 	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());
@@ -234,6 +273,9 @@
 			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);
@@ -247,13 +289,14 @@
 		outPark.setBarrierId(barrierId);
 		outPark.setCreateTime(new Date());
 		outPark.setCode(System.currentTimeMillis()+"");
-		EnterPark enterPark = enterParkService.findByCarNo(carNo);
+		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;
@@ -266,6 +309,13 @@
 
 		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);

--
Gitblit v1.9.1