//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.luozhihui.project.controller;

import com.luozhihui.project.common.ApiResponse;
import com.luozhihui.project.entity.Parking;
import com.luozhihui.project.mapper.ParkingMapper;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/api"})
@CrossOrigin
@RestController
public class ParkingController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Resource
    ParkingMapper parkingMapper;

    public ParkingController() {
    }

    @PostMapping({"/webParking"})
    @CrossOrigin
    public ApiResponse webParking() {
        String sql = "SELECT `parking`.id, parkingName, coord, COUNT(`car`.`parking_id`) as carsNumber FROM `parking`, `car` WHERE parking.enable = 1 GROUP BY parking.id";
        List<Map<String, Object>> result = this.jdbcTemplate.queryForList(sql);
        if (result.isEmpty()) {
            return new ApiResponse((Object)null, 404, "停车场为空");
        } else {
            Iterator var3 = result.iterator();

            while(var3.hasNext()) {
                Map<String, Object> row = (Map)var3.next();
                String parkingId = row.get("id").toString();
                String countSql = "SELECT COUNT(parking_id) as carsNumber FROM car WHERE parking_id = ?";
                int carsNumber = (Integer)this.jdbcTemplate.queryForObject(countSql, Integer.class, new Object[]{parkingId});
                row.put("carsNumber", carsNumber);
            }

            return new ApiResponse(result, 200, "停车场获取成功");
        }
    }

    @PostMapping({"/AllParking"})
    public ApiResponse AllParking() {
        try {
            List<Parking> parking = this.parkingMapper.FindAllParking();
            return parking != null ? new ApiResponse(parking, 200, "查询成功") : new ApiResponse((Object)null, 404, "未找到停车场");
        } catch (Exception var2) {
            Exception e = var2;
            e.printStackTrace();
            return new ApiResponse(e.getMessage(), 500, "服务器内部错误");
        }
    }

    @PostMapping({"/FindParkingById"})
    public ApiResponse FindParkingById(@RequestParam("id") Long id) {
        try {
            Parking parking = (Parking)this.parkingMapper.FindParkingById(String.valueOf(id));
            return parking != null ? new ApiResponse(parking, 200, "查询成功") : new ApiResponse((Object)null, 404, "未找到停车场信息");
        } catch (Exception var3) {
            Exception e = var3;
            e.printStackTrace();
            return new ApiResponse((Object)null, 500, "服务器内部错误：" + e.getMessage());
        }
    }

    @PostMapping({"/AddParking"})
    public ApiResponse AddBrand(@RequestBody Parking parking) {
        try {
            this.parkingMapper.addParking(parking);
            return new ApiResponse((Object)null, 200, "停车场添加成功");
        } catch (Exception var3) {
            Exception e = var3;
            e.printStackTrace();
            return new ApiResponse(e.getMessage(), 500, "服务器内部错误");
        }
    }

    @CrossOrigin
    @PostMapping({"/delete_parking"})
    public ApiResponse delete_parking(@RequestBody Map<String, Object> requestBody) {
        try {
            String carIdsParam = (String)requestBody.get("ids");
            List<String> carIdStrings = Arrays.asList(carIdsParam.split(","));
            List<Long> carIds = (List)carIdStrings.stream().map(Long::parseLong).collect(Collectors.toList());
            int deletedCount = 0;
            Iterator var6 = carIds.iterator();

            while(var6.hasNext()) {
                Long carId = (Long)var6.next();
                boolean isDeleted = this.parkingMapper.DeleteParkingById(String.valueOf(carId));
                if (isDeleted) {
                    ++deletedCount;
                }
            }

            if (deletedCount == carIds.size()) {
                return new ApiResponse((Object)null, 200, "删除成功");
            } else if (deletedCount == 0) {
                return new ApiResponse((Object)null, 404, "未找到信息");
            } else {
                return new ApiResponse((Object)null, 207, "部分删除成功");
            }
        } catch (Exception var9) {
            Exception e = var9;
            e.printStackTrace();
            return new ApiResponse((Object)null, 500, "服务器内部错误");
        }
    }

    @PostMapping({"/upparkinginfo"})
    public ApiResponse updateCar(@RequestBody Parking parking) {
        System.out.println(parking.getId());
        System.out.println("-------------------------------");

        try {
            Parking existingParkinginfo = this.parkingMapper.ParkingId(parking.getId());
            System.out.println(existingParkinginfo);
            if (existingParkinginfo != null) {
                existingParkinginfo.setAddress(parking.getAddress());
                existingParkinginfo.setCarsNumber(parking.getCarsNumber());
                existingParkinginfo.setCoord(parking.getCoord());
                existingParkinginfo.setEnable(parking.getEnable());
                existingParkinginfo.setType(parking.getType());
                existingParkinginfo.setParkingName(parking.getParkingName());
                this.parkingMapper.update(existingParkinginfo);
                return new ApiResponse((Object)null, 200, "停车场信息更新成功");
            } else {
                return new ApiResponse((Object)null, 404, "品牌不存在");
            }
        } catch (Exception var3) {
            Exception e = var3;
            return new ApiResponse(e.getMessage(), 500, "更新信息发生异常");
        }
    }
}
