package com.boying.controller; import cn.hutool.core.date.DateUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.boying.common.R; import com.boying.common.SystemConfigProperties; import com.boying.entity.*; import com.boying.service.*; import com.boying.util.DateUtilOther; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.data.jpa.domain.Specification; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import java.io.BufferedWriter; import java.io.FileWriter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.concurrent.TimeUnit; @RestController @RequestMapping("ffzf/outpark") @RequiredArgsConstructor @Tag(description = "ffzf/outpark" , name = "出场表接口" ) public class OutParkController { private final OutParkService outParkService; private final EnterParkService enterParkService; private final BarrierService barrierService; private final TicketService ticketService; private final SystemConfigProperties systemConfigProperties; private final UserService userService; @Autowired private CostRuleService costRuleService; @Autowired private ParkService parkService; @Autowired private StringRedisTemplate redisTemplate; @PostMapping("/findPage") @Operation(summary = "分页查询" , description = "分页查询" ) public Object findPage(Page page, String carNo,Long parkId,String payCode,String date) throws ParseException { List list = outParkService.getList(page.getCurrent(), page.getSize(), carNo,parkId,payCode, date); System.out.println(list); long count = outParkService.getCount(carNo,parkId,payCode, date); for (OutPark record : list) { record.setParkName(parkService.getById(record.getParkId()).getName()); if(record.getEnterTime() != null){ record.setTimeStr(DateUtilOther.millisToDayHrMinSec(new Double(record.getTime()).longValue())); }else { record.setTimeStr("未发现入场记录"); } if(record.getImgId() != null){ record.setImgPath("/ffzf/fileinfo/showImgById/"+record.getImgId()); } } page.setRecords(list); page.setTotal(count); return R.ok(page); } //getById @PostMapping("/getById") @Operation(summary = "根据id查询" , description = "根据id查询" ) public Object getById(Long id) { return R.ok(outParkService.getById(id)); } //道闸code @PostMapping("/findByBarrierCode") @Operation(summary = "根据道闸code查询出场(已废弃)" , description = "根据道闸code查询出场(已废弃)" ) public Object findById(String code) { if(StringUtils.isBlank(code)){ return R.failed(null,"参数错误"); } QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda() .eq(Barrier::getCode2,code); System.out.println("code :" +code); List all = barrierService.list(wrapper); if(all.size()==0){ return R.failed("未找到该设备"); }else{ Barrier barrier = all.get(0); String carNo = barrier.getCarNo(); OutPark outPark = outParkService.findByCarNoAndBarrierId(carNo,barrier.getId()); if(outPark==null){ return R.failed("未识别到车牌号"); }else{ outPark.setParkName(barrier.getName()); if(outPark.getTime() > 0){ outPark.setTimeStr(DateUtilOther.millisToDayHrMinSec(new Double(outPark.getTime()).longValue())); }else { outPark.setTimeStr("不足一分钟"); } return R.ok(outPark); } } } //道闸code @PostMapping("/findByBarrierCode2") @Operation(summary = "根据道闸code查询出场" , description = "根据道闸code查询出场" ) public Object findById2(String code2) { if(StringUtils.isBlank(code2)){ return R.failed(null,"参数错误"); } System.out.println("code2 :"+code2); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda() .eq(Barrier::getCode2,code2); List all = barrierService.list(wrapper); if(all.size()==0){ return R.failed("未找到该设备"); }else{ Barrier barrier = all.get(0); String carNo = barrier.getCarNo(); OutPark outPark = outParkService.findByCarNoAndBarrierId(carNo,barrier.getId()); if(outPark==null){ return R.failed("未识别到车牌号"); }else{ outPark.setParkName(barrier.getName()); if(outPark.getTime() > 0){ outPark.setTimeStr(DateUtilOther.millisToDayHrMinSec(new Double(outPark.getTime()).longValue())); }else { outPark.setTimeStr("不足一分钟"); } return R.ok(outPark); } } } @PostMapping("/delete") @Operation(summary = "删除出场" , description = "删除出场" ) public Object delete(Long id) { outParkService.removeById(id); return R.ok("删除成功"); } @PostMapping("enterPark2") @Operation(summary = "新增入场(废弃)" , description = "新增入场(废弃)" ) public Object enterPark(String carNo,Integer barrierId,Integer parkId,String code2) { Barrier barrier1 = findBarrier(code2); barrierId = barrier1.getId(); parkId = barrier1.getParkId(); Park park = parkService.getById(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){ redisTemplate.opsForValue().set("park_up_" + parkId,"false",30, TimeUnit.DAYS); return "false"; } }else { s= "0"; redisTemplate.opsForValue().set("car_park_" + parkId,s,30, TimeUnit.DAYS); redisTemplate.opsForValue().set("park_up_" + parkId,"true",30, TimeUnit.DAYS); } } enterParkService.deleteByCarNo(carNo,parkId); EnterPark enterPark = new EnterPark(); enterPark.setCreateTime(LocalDateTime.now()); enterPark.setCarNo(carNo); enterPark.setBarrierId(barrierId); enterPark.setParkId(parkId); enterParkService.saveOrUpdate(enterPark); int i = Integer.parseInt(s); i++; redisTemplate.opsForValue().set("car_park_" + parkId,Integer.toString(i),30, TimeUnit.DAYS); redisTemplate.opsForValue().set("park_up_" + parkId,"true",30, TimeUnit.DAYS); Barrier barrier =barrierService.getById(barrierId); barrier.setType2(1); barrier.setUpdateTime(LocalDateTime.now()); barrierService.saveOrUpdate(barrier); return R.ok("请求成功"); } @PostMapping("outPark2") @Operation(summary = "新增出场(废弃)" , description = "新增出场(废弃)" ) public Object outPark(String carNo,Integer barrierId,Integer parkId,String code2) { String s = "开始执行出场接口------>\n"; Barrier barrier1 = findBarrier(code2); barrierId = barrier1.getId(); parkId = barrier1.getParkId(); OutPark outPark = new OutPark(); outPark.setCarNo(carNo); outPark.setParkId(parkId); outPark.setBarrierId(barrierId); outPark.setCreateTime(LocalDateTime.now()); outPark.setCode(System.currentTimeMillis()+""); EnterPark enterPark = null; List byCarNo = enterParkService.findByCarNo(carNo,parkId); if(byCarNo.size() > 0){ enterPark = byCarNo.get(0); } if(enterPark==null){ s += "未发现入场车辆:"+carNo+"\n"; writeTxt(s); return R.failed("无进场记录或手机号进出输入不一致",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().toInstant(ZoneOffset.of("+8")).toEpochMilli() - enterPark.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();; 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(); } outPark.setPrice(Double.valueOf(String.format("%.1f", money))); //outPark.setStatus3(findTicket(carNo)); outPark.setUpdateTime(LocalDateTime.now()); outParkService.saveOrUpdate(outPark); int i = Integer.parseInt(redis); i--; if(i<0){ redisTemplate.opsForValue().set("car_park_" + parkId,"0",30, TimeUnit.DAYS); redisTemplate.opsForValue().set("park_up_" + parkId,"true",30, TimeUnit.DAYS); }else { redisTemplate.opsForValue().set("car_park_" + parkId,Integer.toString(i),30, TimeUnit.DAYS); redisTemplate.opsForValue().set("park_up_" + parkId,"true",30, TimeUnit.DAYS); } Barrier barrier = barrierService.getById(barrierId); barrier.setCarNo(carNo); if(outPark.getPrice()==0&&outPark.getStatus3()==0){ barrier.setType2(1); }else { barrier.setType2(0); } barrier.setUpdateTime(LocalDateTime.now()); barrierService.saveOrUpdate(barrier); s += "\n"; writeTxt(s); return R.ok(outPark); } @GetMapping("/statisticParkOrder/count") @Operation(summary = "统计用户管理的停车场订单数量" , description = "统计用户管理的停车场订单数量" ) public R statisticParkOrder(String parkIds) { Map resultMap = new HashMap<>(); if(StringUtils.isNotBlank(parkIds)){ String[] split = parkIds.split(","); long[] array = Arrays.stream(split).mapToLong(Long::parseLong).toArray(); List parkIdList = new ArrayList<>(); for (long i : array) { parkIdList.add(i); } QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda() .between(OutPark::getCreateTime, DateUtil.beginOfDay(new Date()),DateUtil.endOfDay(new Date())) .eq(OutPark::getStatus,1) .in(OutPark::getParkId,parkIdList); wrapper.select("IFNULL(ROUND(SUM(price)),0) as num"); Map map = outParkService.getMap(wrapper); String a = map.get("num").toString(); if(a != null){ resultMap.put("money",Double.parseDouble(a)); }else { resultMap.put("money",0.0); } wrapper.clear(); wrapper.lambda() .between(OutPark::getCreateTime, DateUtil.beginOfDay(new Date()),DateUtil.endOfDay(new Date())) .eq(OutPark::getStatus,1) .in(OutPark::getParkId,parkIdList); long count = outParkService.count(wrapper); resultMap.put("count",count); return R.ok(resultMap); }else { return R.failed(null,"该用户未管理停车场"); } } @GetMapping("/isJS") @Operation(summary = "僵尸车查询" , description = "僵尸车查询" ) public Object isJS(String carNo ,Integer parkId) { List byCarNo = enterParkService.findByCarNo(carNo, parkId); if(byCarNo.size() > 0){ return R.ok(byCarNo.get(0),"true"); }else { return R.ok(null,"false"); } } @GetMapping("/getByCarAndPark") @Operation(summary = "根据车牌和停车场获取最新出场" , description = "根据车牌和停车场获取最新出场" ) public Object getByCarAndPark(String carNo ,Integer parkId) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda() .eq(OutPark::getCarNo,carNo) .eq(OutPark::getParkId,parkId) .orderByDesc(OutPark::getCreateTime); List list = outParkService.list(wrapper); if(list.size() > 0){ OutPark outPark = list.get(0); if(outPark.getTime() > 0){ outPark.setTimeStr(DateUtilOther.millisToDayHrMinSec(new Double(outPark.getTime()).longValue())); }else { outPark.setTimeStr("不足一分钟"); } return R.ok(outPark,"找到数据"); }else { return R.failed(null,"未找到数据"); } } @GetMapping("/editEnterPark") @Operation(summary = "修改入场信息" , description = "修改入场信息" ) public Object editEnterPark(String carNo ,Integer parkId,String time) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda() .eq(Barrier::getParkId,parkId) .eq(Barrier::getType,1); List list = barrierService.list(wrapper); EnterPark enterPark = new EnterPark(); enterPark.setCreateTime(LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); enterPark.setCarNo(carNo); enterPark.setParkId(parkId); enterPark.setBarrierId(list.get(0).getId()); enterPark.setStatus(0); enterPark.setIsAdd(1); enterPark.setIsPay(0); enterParkService.saveOrUpdate(enterPark); OutPark outPark = null; QueryWrapper wrapper1 = new QueryWrapper<>(); wrapper1.lambda() .eq(OutPark::getParkId,parkId) .eq(OutPark::getCarNo,carNo) .ge(OutPark::getCreateTime,enterPark.getCreateTime()) .ne(OutPark::getStatus,1); List list1 = outParkService.list(wrapper1); if(list1 !=null&&list1.size()>0){ outPark = list1.get(0); outPark.setEnterTime(enterPark.getCreateTime()); long l = outPark.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli() - enterPark.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli(); outPark.setTime(l/(1000*60)); try { outPark.setPrice(costRuleService.getMoney(parkId, outPark.getEnterTime(), outPark.getCreateTime(), 1)); }catch (Exception e){ e.printStackTrace(); } outParkService.saveOrUpdate(outPark); return R.ok(null,"修改出场记录成功"); }else { return R.ok(null,"新增入场记录成功"); } } public Barrier findBarrier(String code2) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda() .eq(Barrier::getCode2,code2); List all = barrierService.list(wrapper); if(all.size()==0){ return null; }else{ Barrier barrier = all.get(0); return barrier; } } 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("打印错误"); } } }