kongdeqiang
2024-04-19 928a66524efbdb2371b7fa6b7ee8b55b910182fa
src/main/java/com/boying/controller/ParkController.java
@@ -5,10 +5,16 @@
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boying.common.R;
import com.boying.entity.EnterPark;
import com.boying.entity.Park;
import com.boying.entity.User;
import com.boying.service.EnterParkService;
import com.boying.service.ParkService;
import com.boying.service.UserService;
import com.boying.util.RedisJsonUtil;
import io.swagger.models.auth.In;
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 org.springframework.data.redis.core.StringRedisTemplate;
@@ -17,6 +23,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,28 +31,38 @@
import java.util.stream.Collectors;
@RestController
@RequestMapping("/park")
@RequestMapping("ffzf/park")
@RequiredArgsConstructor
@Tag(description = "ffzf/park" , name = "停车场接口" )
public class ParkController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    private final ParkService parkService;
    private final UserService userService;
    private final RedisJsonUtil redisJsonUtil;
    private final EnterParkService enterParkService;
    @PostMapping("/findPage")
    @Operation(summary = "分页查询" , description = "分页查询" )
    public Object findPage(Page page) {
        Page page1 = parkService.page(page, new QueryWrapper<Park>().lambda().orderByDesc(Park::getId));
        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);
    }
    @PostMapping("/save")
    @Operation(summary = "保存停车场" , description = "保存停车场" )
    public Object save(Park park) {
        parkService.saveOrUpdate(park);
        String num = redisTemplate.opsForValue().get("car_park_" +  park.getId());
@@ -53,16 +70,24 @@
            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")
    @Operation(summary = "删除停车场" , description = "删除停车场" )
    public Object delete(Long id) {
        parkService.removeById(id);
        redisJsonUtil.del("park-"+id);
        return R.ok("删除成功");
    }
    @PostMapping("findAll")
    @Operation(summary = "查询所有" , description = "查询所有" )
    public Object findAll() {
        List<Park> all = parkService.list();
        for (Park park : all) {
@@ -77,6 +102,7 @@
    }
    @PostMapping("/getCarNum")
    @Operation(summary = "根据id查询车位数" , description = "根据id查询车位数" )
    public Object getCarNum(Long parkId) {
        Park byId = (Park) parkService.getById(parkId);
        String s = redisTemplate.opsForValue().get("car_park_" + parkId);
@@ -89,28 +115,48 @@
    }
    @PostMapping("/getByUserId")
    public Object getByUserId(Long userId) {
        User byId = userService.getById(userId);
    @Operation(summary = "根据用户查询管理停车场车位数" , description = "根据用户查询管理停车场车位数" )
    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")
    @Operation(summary = "根据id查询停车场(APP)" , description = "根据id查询停车场(APP)" )
    public Object getById(Long parkId) {
        Park byId = parkService.getById(parkId);
        if(byId == null){
            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,"查询成功");
        }
    }
    @PostMapping("/editParkCarNum")
    @Operation(summary = "修改车位数" , description = "修改车位数" )
    public Object editParkCarNum(Park park) {
        Park byId = parkService.getById(park.getId());
        if(byId == null){
@@ -124,4 +170,23 @@
        }
    }
    @PostMapping("/getParkCar")
    @Operation(summary = "获取场内车辆" , description = "获取场内车辆" )
    public Object getParkCar(Page page, Integer parkId,String carNo) {
        QueryWrapper<EnterPark> enterParkQueryWrapper = new QueryWrapper<>();
        enterParkQueryWrapper.lambda()
                .eq(parkId != null,EnterPark::getParkId,parkId)
                .like(StringUtils.isNotBlank(carNo),EnterPark::getCarNo,carNo)
                .orderByDesc(EnterPark::getCreateTime);
        Page<EnterPark> page1 = enterParkService.page(page, enterParkQueryWrapper);
        for (EnterPark record : page1.getRecords()) {
            record.setParkName(parkService.getById(record.getParkId()).getName());
            if(record.getImgId() != null){
                record.setImgPath("/ffzf/fileinfo/showImgById/"+record.getImgId());
            }
        }
        return R.ok(page1);
    }
}