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
129
130
131
132
133
134
135
136
137
138
package com.boying.controller;
 
import com.boying.common.BaseController;
import com.boying.common.util.LngLatUtil;
import com.boying.common.util.StringUtil;
import com.boying.entity.Park;
import com.boying.entity.User;
import com.boying.service.ParkService;
import com.boying.service.UserService;
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.List;
 
@RestController
@RequestMapping("user")
public class UserController extends BaseController {
 
    @Autowired
    private UserService userService;
    @Autowired
    private ParkService parkService;
 
    @PostMapping("findPage")
    public Object findPage(String name,int page,int pageSize) {
        Pageable pageable = PageRequest.of(page-1,pageSize, Sort.Direction.DESC,"id");
        Specification<User> specification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtil.isNullOrEmpty(name)) {
                    list.add(cb.like(root.get("name").as(String.class), "%"+name+"%"));
                }
                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        };
        Page<User> pages = userService.findPage(pageable,specification);
        return success("",pages);
    }
 
    @PostMapping("findAll")
    public Object findAll() {
        return success("",userService.findAll());
    }
 
 
    @PostMapping("save")
    public Object save(User user) {
        if(user.getId()==null){
            user.setPassword(user.getLoginName());
        }
        userService.save(user);
        return success("保存成功");
    }
 
    @PostMapping("delete")
    public Object delete(Long id) {
        userService.delete(id);
        return success("删除成功");
    }
 
    @PostMapping("login")
    public Object login(String loginName,String password,String lng,String lat) {
        if(StringUtil.isNullOrEmpty(loginName)||StringUtil.isNullOrEmpty(password)){
            return error("用户名和密码必填");
        }
 
        Specification<User> specification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                list.add(cb.equal(root.get("loginName").as(String.class), loginName));
                list.add(cb.equal(root.get("password").as(String.class), password));
                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        };
 
        List<User> all = userService.findAll(specification);
        if(all.size()>0){
            User user = all.get(0);
 
            if(!StringUtil.isNullOrEmpty(lng)&&!StringUtil.isNullOrEmpty(lat)){
                List<Park> all1 = parkService.findAll();
                for(Park p:all1){
                    double distance =  LngLatUtil.getDistance(lat,lng,p.getLat(),p.getLng());
                    if(distance<500){
                        user.setParkId(p.getId());
                    }
                }
                user.setLng(lng);
                user.setLat(lat);
                userService.save(user);
            }
            return success("请求成功",user);
        }
        return error("用户名或密码不正确");
    }
 
    @PostMapping("updatePassword")
    public Object updatePassword(Long userId,String oldPassword,String newPassword) {
        Specification<User> specification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                list.add(cb.equal(root.get("id").as(Long.class), userId));
                list.add(cb.equal(root.get("password").as(String.class), oldPassword));
                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        };
 
        List<User> all = userService.findAll(specification);
        if(all.size()>0){
            User user = all.get(0);
            user.setPassword(newPassword);
            userService.save(user);
            return success("请求成功");
        }
        return error("旧密码不正确");
    }
}