kongdeqiang
2022-09-19 a9862e81851bbe037edc6bb1c7f562c1e55c0d7f
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,6 +243,7 @@
    @PostMapping("outPark2")
    public Object outPark(String carNo,Long barrierId,Long parkId,String code2) {
        String s = "开始执行出场接口------>\n";
        Barrier barrier1 = findBarrier(code2);
        barrierId = barrier1.getId();
        parkId = barrier1.getParkId();
@@ -224,18 +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){
           s += "未发现入场车辆:"+carNo+"\n";
            writeTxt(s);
            return error("无进场记录或手机号进出输入不一致",null);
        }else{
            s += "发现入场车辆: "+enterPark.getCarNo()+",道闸id为:"+enterPark.getBarrierId()+",停车场id:"+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();
        s+= "场内时长为:"+l+"毫秒,合计为: "+l/(1000*60)+"秒\n";
        outPark.setTime(l/(1000*60));
        double money = 0;
        try {
            money = costRuleService.getMoney(parkId, enterPark.getCreateTime(), outPark.getCreateTime(), 1);
            s+="金额为:"+money+"\n";
        } catch (ParseException e) {
            e.printStackTrace();
        }
@@ -243,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);
    }
@@ -297,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("打印错误");
        }
    }
}