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/OutParkController.java |   76 +++++++++++++++++++++++++++++++++-----
 1 files changed, 66 insertions(+), 10 deletions(-)

diff --git a/src/main/java/com/boying/controller/OutParkController.java b/src/main/java/com/boying/controller/OutParkController.java
index 73e75fb..b14b7b9 100644
--- a/src/main/java/com/boying/controller/OutParkController.java
+++ b/src/main/java/com/boying/controller/OutParkController.java
@@ -1,6 +1,7 @@
 package com.boying.controller;
 
 import com.boying.common.BaseController;
+import com.boying.common.SystemConfigProperties;
 import com.boying.common.util.DateUtil;
 import com.boying.common.util.StringUtil;
 import com.boying.entity.*;
@@ -11,6 +12,7 @@
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.domain.Sort;
 import org.springframework.data.jpa.domain.Specification;
+import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
@@ -20,8 +22,12 @@
 import javax.persistence.criteria.Predicate;
 import javax.persistence.criteria.Root;
 import java.beans.Transient;
+import java.io.BufferedWriter;
+import java.io.FileWriter;
 import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 
 import static com.boying.common.util.DateUtil.getMinute;
 
@@ -37,9 +43,15 @@
     private BarrierService barrierService;
     @Autowired
     private TicketService ticketService;
+    @Autowired
+    private SystemConfigProperties systemConfigProperties;
 
     @Autowired
     private CostRuleService costRuleService;
+    @Autowired
+    private ParkService parkService;
+    @Autowired
+    private StringRedisTemplate redisTemplate;
 
     @PostMapping("findPage")
     public Object findPage(int page,int pageSize) {
@@ -182,7 +194,20 @@
         Barrier barrier1 = findBarrier(code2);
         barrierId = barrier1.getId();
         parkId = barrier1.getParkId();
-
+        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 "null";
+                }
+            }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());
@@ -206,6 +231,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);
@@ -215,8 +243,7 @@
 
     @PostMapping("outPark2")
     public Object outPark(String carNo,Long barrierId,Long parkId,String code2) {
-        String s = "寮�濮嬫墽琛屽嚭鍦烘帴鍙�------>";
-        System.out.println(s);
+        String s = "寮�濮嬫墽琛屽嚭鍦烘帴鍙�------>\n";
         Barrier barrier1 = findBarrier(code2);
         barrierId = barrier1.getId();
         parkId = barrier1.getParkId();
@@ -226,24 +253,25 @@
         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;
-            System.out.println(s1);
+           s += "鏈彂鐜板叆鍦鸿溅杈嗭細"+carNo+"\n";
+            writeTxt(s);
             return error("鏃犺繘鍦鸿褰曟垨鎵嬫満鍙疯繘鍑鸿緭鍏ヤ笉涓�鑷�",null);
         }else{
-            String s1 = "鍙戠幇鍏ュ満杞﹁締: "+enterPark.getCarNo()+",閬撻椄id涓猴細"+enterPark.getBarrierId()+",鍋滆溅鍦篿d锛�"+enterPark.getParkId()+",杩濈珷鏍囪瘑锛�"+enterPark.getStatus();
-            System.out.println(s1);
+            s += "鍙戠幇鍏ュ満杞﹁締: "+enterPark.getCarNo()+",閬撻椄id涓猴細"+enterPark.getBarrierId()+",鍋滆溅鍦篿d锛�"+enterPark.getParkId()+",杩濈珷鏍囪瘑锛�"+enterPark.getStatus()+"\n";
             outPark.setEnterTime(enterPark.getCreateTime());
         }
+        String redis = redisTemplate.opsForValue().get("car_park_" + parkId);
         long l = outPark.getCreateTime().getTime() - enterPark.getCreateTime().getTime();
-        System.out.println("鍦哄唴鏃堕暱涓猴細"+l+"姣,鍚堣涓�: "+l/(1000*60)+"绉�");
+        s+= "鍦哄唴鏃堕暱涓猴細"+l+"姣,鍚堣涓�: "+l/(1000*60)+"绉抃n";
         outPark.setTime(l/(1000*60));
         double money = 0;
         try {
             money = costRuleService.getMoney(parkId, enterPark.getCreateTime(), outPark.getCreateTime(), 1);
-            System.out.println("閲戦涓猴細"+money);
+            s+="閲戦涓猴細"+money+"\n";
         } catch (ParseException e) {
             e.printStackTrace();
         }
@@ -251,13 +279,24 @@
 
         //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);
+        }else {
+            barrier.setType2(0);
         }
         barrierService.save(barrier);
+        s += "\n";
+        writeTxt(s);
         return success("璇锋眰鎴愬姛",outPark);
     }
 
@@ -305,4 +344,21 @@
 //        }
 //        return success("淇濆瓨鎴愬姛");
 //    }
+
+    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("鎵撳嵃閿欒");
+        }
+    }
 }

--
Gitblit v1.9.1