//
// 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.Car;
import com.luozhihui.project.mapper.CarShowMapper;
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.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
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;

@RestController
@RequestMapping({"/api"})
@CrossOrigin
public class CarShowController {
    @Resource
    CarShowMapper carShowMapper;

    public CarShowController() {
    }

    @PostMapping({"/AllCar"})
    public ApiResponse AllCar() {
        try {
            List<Car> car = this.carShowMapper.FindAllCar();
            return car != null ? new ApiResponse(car, 200, "查询成功") : new ApiResponse((Object)null, 404, "未找到车辆");
        } catch (Exception var2) {
            Exception e = var2;
            e.printStackTrace();
            return new ApiResponse((Object)null, 500, "服务器内部错误");
        }
    }

    @PostMapping({"/FindCarById"})
    public ApiResponse FindCarById(Car car) {
        try {
            List<Car> cars = this.carShowMapper.FindCarById(car.getCarId());
            return cars != null && !cars.isEmpty() ? new ApiResponse(cars, 200, "查询成功") : new ApiResponse((Object)null, 404, "未找到车辆");
        } catch (Exception var3) {
            Exception e = var3;
            e.printStackTrace();
            return new ApiResponse((Object)null, 500, "服务器内部错误");
        }
    }

    @PostMapping({"/AddCar"})
    public ApiResponse addCar(@RequestBody Car car) {
        try {
            this.carShowMapper.addCar(car);
            return new ApiResponse((Object)null, 200, "车辆添加成功");
        } catch (Exception var3) {
            Exception e = var3;
            e.printStackTrace();
            return new ApiResponse((Object)null, 500, "服务器内部错误");
        }
    }

    @CrossOrigin
    @PostMapping({"/delete_car"})
    public ApiResponse delete_cars(@RequestBody Map<String, Object> requestBody) {
        try {
            String carIdsParam = (String)requestBody.get("carIds");
            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.carShowMapper.DeleteCarById(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({"/upcarinfo"})
    public ApiResponse updateCar(@RequestBody Car car) {
        System.out.println("-----------------------------------------");
        System.out.println(Integer.valueOf(car.getCarId()));

        try {
            System.out.println(Integer.valueOf(car.getCarId()));
            Car existingCarinfo = this.carShowMapper.CarId(Integer.valueOf(car.getCarId()));
            System.out.println(existingCarinfo);
            if (existingCarinfo != null) {
                existingCarinfo.setCarContent(car.getCarContent());
                existingCarinfo.setCarPhoto(car.getCarPhoto());
                this.carShowMapper.update(existingCarinfo);
                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, "更新车辆信息发生异常");
        }
    }

    @GetMapping({"/getCarsInParking"})
    public ApiResponse getCarsInParking(@RequestParam Integer parking_id) {
        if (parking_id == null) {
            return new ApiResponse((Object)null, 400, "停车场ID不能为空");
        } else {
            try {
                List<Car> result = this.carShowMapper.findCarsByParkingId(parking_id);
                return result.isEmpty() ? new ApiResponse((Object)null, 404, "车辆列表为空") : new ApiResponse(result, 200, "获取停车场列表成功");
            } catch (Exception var3) {
                Exception e = var3;
                return new ApiResponse(e.getMessage(), 500, "服务器错误");
            }
        }
    }
}
