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 specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) { List list = new ArrayList(); 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 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 specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) { List list = new ArrayList(); 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 all = userService.findAll(specification); if(all.size()>0){ User user = all.get(0); if(!StringUtil.isNullOrEmpty(lng)&&!StringUtil.isNullOrEmpty(lat)){ List 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 specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) { List list = new ArrayList(); 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 all = userService.findAll(specification); if(all.size()>0){ User user = all.get(0); user.setPassword(newPassword); userService.save(user); return success("请求成功"); } return error("旧密码不正确"); } }