kongdeqiang
2024-04-09 fb050c0dafa5363a73540dd9e52b78487e25ba0a
src/main/java/com/boying/controller/ParkController.java
@@ -9,6 +9,7 @@
import com.boying.entity.User;
import com.boying.service.ParkService;
import com.boying.service.UserService;
import com.boying.util.RedisJsonUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
@@ -17,6 +18,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -24,7 +26,7 @@
import java.util.stream.Collectors;
@RestController
@RequestMapping("/park")
@RequestMapping("ffzf/park")
@RequiredArgsConstructor
public class ParkController {
@@ -32,6 +34,7 @@
    private StringRedisTemplate redisTemplate;
    private final ParkService parkService;
    private final UserService userService;
    private final RedisJsonUtil redisJsonUtil;
    @PostMapping("/findPage")
    public Object findPage(Page page) {
@@ -39,7 +42,12 @@
        List<Park> records = page1.getRecords();
        for (Park record : records) {
            String num = redisTemplate.opsForValue().get("car_park_" +  record.getId());
            record.setCarNum(Integer.parseInt(num));
            if(num != null){
                record.setCarNum(Integer.parseInt(num));
            }else {
                record.setCarNum(0);
            }
        }
        page1.setRecords(records);
        return R.ok(page1);
@@ -53,12 +61,18 @@
            redisTemplate.opsForValue().set("car_park_" + park.getId(),"0",30, TimeUnit.DAYS);
            redisTemplate.opsForValue().set("park_up_" + park.getId(),"true",30, TimeUnit.DAYS);
        }
        try {
            redisJsonUtil.set("park-"+park.getId(), park);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok("保存成功");
    }
    @PostMapping("/delete")
    public Object delete(Long id) {
        parkService.removeById(id);
        redisJsonUtil.del("park-"+id);
        return R.ok("删除成功");
    }
@@ -89,26 +103,29 @@
    }
    @PostMapping("/getByUserId")
    public Object getByUserId(Long userId) {
        User byId = userService.getById(userId);
    public Object getByUserId(String parkIds) {
        List<Park> list = new ArrayList<>();
        if(byId == null){
            return R.failed("未查询到用户");
        }else {
            String parkIds = byId.getParkIds();
            if(StringUtils.isBlank(parkIds)){
                return R.failed("该用户未管理停车场");
            }
            List<Long> ids= Arrays.stream(parkIds.split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList());
            for (Long id : ids) {
                Park byId1 = parkService.getById(id);
                String num = redisTemplate.opsForValue().get("car_park_" +  byId1.getId());
                byId1.setCarNum(Integer.parseInt(num));
                list.add(byId1);
            }
            return R.ok(list);
        if(StringUtils.isBlank(parkIds)){
            return R.failed("该用户未管理停车场");
        }
        List<Long> ids= Arrays.stream(parkIds.split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList());
        for (Long id : ids) {
            Park byId1 = parkService.getById(id);
            String num = redisTemplate.opsForValue().get("car_park_" +  byId1.getId());
            try {
                if(StringUtils.isBlank(num)){
                    num = "0";
                }
                byId1.setCarNum(Integer.parseInt(num));
            }catch (Exception e){
                byId1.setCarNum(0);
                e.printStackTrace();
            }
            list.add(byId1);
        }
        return R.ok(list);
    }
    @PostMapping("/getById")
    public Object getById(Long parkId) {
        Park byId = parkService.getById(parkId);
@@ -116,6 +133,10 @@
            return R.failed("未查询到该停车场");
        }else {
            String s = redisTemplate.opsForValue().get("car_park_" + parkId);
            if(StringUtils.isBlank(s)){
                redisTemplate.opsForValue().set("car_park_" + parkId,"0",30, TimeUnit.DAYS);
                s = "0";
            }
            byId.setCarNum(Integer.parseInt(s));
            return R.ok(byId,"查询成功");
        }