kongdeqiang
2022-09-19 a9862e81851bbe037edc6bb1c7f562c1e55c0d7f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.boying.controller;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.boying.common.BaseController;
import com.boying.common.SystemConfigProperties;
import com.boying.common.util.DateUtil;
import com.boying.common.util.HttpUtil;
import com.boying.common.util.StringUtil;
import com.boying.entity.Barrier;
import com.boying.entity.EnterPark;
import com.boying.entity.Park;
import com.boying.entity.User;
import com.boying.service.BarrierService;
import com.boying.service.EnterParkService;
import com.boying.service.ParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
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.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@RestController
@RequestMapping("barrier")
public class BarrierController extends BaseController {
 
    @Autowired
    private BarrierService barrierService;
    @Autowired
    private SystemConfigProperties systemConfigProperties;
 
    @PostMapping("findPage")
    public Object findPage(int page,int pageSize,String parkId) {
        Pageable pageable = PageRequest.of(page-1,pageSize, Sort.Direction.DESC,"id");
 
        Specification<Barrier> specification = new Specification<Barrier>() {
            @Override
            public Predicate toPredicate(Root<Barrier> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtil.isNullOrEmpty(parkId)) {
                    list.add(cb.equal(root.get("parkId").as(Long.class), parkId));
                }
                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        };
 
        Page<Barrier> pages = barrierService.findPage(pageable,specification);
 
        for(Barrier barrier:pages.getContent()){
            long l = new Date().getTime() - barrier.getCreateTime().getTime();
            System.out.println(l);
            if(l>10000){
                barrier.setStatus(1);
            }
        }
        return success("",pages);
    }
 
    @PostMapping("save")
    public Object save(Barrier barrier) {
        barrierService.save(barrier);
        return success("保存成功");
    }
 
    @PostMapping("delete")
    public Object delete(Long id) {
        barrierService.delete(id);
        return success("删除成功");
    }
 
    @PostMapping("findAll")
    public Object findAll(Long parkId) {
        if (parkId==null) {
            return error("未发现该停车场");
        }
        Specification<Barrier> specification = new Specification<Barrier>() {
            @Override
            public Predicate toPredicate(Root<Barrier> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                list.add(cb.equal(root.get("parkId").as(Long.class), parkId));
                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        };
        return success("请求成功",barrierService.findAll(specification));
    }
 
//    @PostMapping("open")
//    public Object open(Long barrierId) {
//        if (barrierId==null) {
//            return error("未找到该道闸");
//        }
//        Barrier b= (Barrier) barrierService.findById(barrierId);
//        String res = HttpUtil.get(systemConfigProperties.getIp3()+"?barrierCode="+b.getCode());
//        JSONObject jsonObject = JSON.parseObject(res);
//        if(jsonObject.get("result").equals("true")){
//            return success("请求成功");
//        }else{
//            return error("抬杆失败");
//        }
//    }
 
    @PostMapping("open")
    public Object open(Long barrierId) {
        Barrier b= (Barrier) barrierService.findById(barrierId);
        if (b==null) {
            return error("未找到该道闸");
        }
        b.setType2(1);
        barrierService.save(b);
        return success("请求成功");
    }
}