package com.luozhihui.project.controller;
import com.luozhihui.project.common.ApiResponse;
import com.luozhihui.project.entity.Car;
import com.luozhihui.project.mapper.CarShowMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

    //查询所有车辆信息
    @PostMapping("/AllCar")
    public ApiResponse AllCar() {
        try {
            // 查询所有车辆
            List<Car> car = carShowMapper.FindAllCar();
            if (car != null) {
                return new ApiResponse(car, 200, "查询成功");
            } else {
                return new ApiResponse(null, 404, "未找到车辆"); // 如果未找到车辆，返回适当的状态码和消息
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(null, 500, "服务器内部错误"); // 捕获任何异常并返回服务器内部错误
        }
    }
    //通过carid查询信息
    @PostMapping("/FindCarById")
    public ApiResponse FindCarById(Car car) {
        try {
            // 查询所有车辆
            List<Car> cars = carShowMapper.FindCarById(car.getCarId());

            if (cars != null && !cars.isEmpty()) {
                return new ApiResponse(cars, 200, "查询成功");
            } else {
                return new ApiResponse(null, 404, "未找到车辆"); // 如果未找到车辆，返回适当的状态码和消息
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(null, 500, "服务器内部错误"); // 捕获任何异常并返回服务器内部错误
        }
    }
    // 新增车辆
    @PostMapping("/AddCar")
    public ApiResponse addCar(@RequestBody Car car) {
        try {
            // 添加车辆
            carShowMapper.addCar(car);
            // 返回成功消息
            return new ApiResponse(null, 200, "车辆添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            // 返回内部服务器错误
            return new ApiResponse(null, 500, "服务器内部错误");
        }
    }
    @CrossOrigin
    //删除车辆信息
    @PostMapping("/delete_car")
    public ApiResponse delete_cars(@RequestBody Map<String, Object> requestBody) {
        try {
            String carIdsParam = (String) requestBody.get("carIds");
            // 将逗号分隔的字符串carIdsParam拆分为carId列表
            List<String> carIdStrings = Arrays.asList(carIdsParam.split(","));
            List<Long> carIds = carIdStrings.stream().map(Long::parseLong).collect(Collectors.toList());

            int deletedCount = 0;
            for (Long carId : carIds) {
                boolean isDeleted = carShowMapper.DeleteCarById(String.valueOf(carId));
                if (isDeleted) {
                    deletedCount++;
                }
            }
            if (deletedCount == carIds.size()) {
                return new ApiResponse(null, 200, "删除成功");
            } else if (deletedCount == 0) {
                return new ApiResponse(null, 404, "未找到车辆信息");
            } else {
                return new ApiResponse(null, 207, "部分车辆删除成功"); // 207 - Partial Content
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(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 = carShowMapper.CarId(Integer.valueOf(car.getCarId()));
        System.out.println(existingCarinfo);
        if (existingCarinfo != null) {
            existingCarinfo.setCarModel(car.getCarModel());
            existingCarinfo.setCarColour(car.getCarColour());
            existingCarinfo.setCarContent(car.getCarContent());
            existingCarinfo.setCarLicense(car.getCarLicense());
            existingCarinfo.setCarDayMoney(car.getCarDayMoney());
            existingCarinfo.setCarHoursMoney(car.getCarHoursMoney());
            existingCarinfo.setCarPhoto(car.getCarPhoto());
            existingCarinfo.setCarSecurityDeposit(car.getCarSecurityDeposit());
            existingCarinfo.setParking_id(car.getParking_id());
            existingCarinfo.setBrand_id(car.getBrand_id());
            existingCarinfo.setEnergy(car.getEnergy());
            existingCarinfo.setEmass(car.getEmass());
            existingCarinfo.setOmass(car.getOmass());
            existingCarinfo.setKm(car.getKm());
            carShowMapper.update(existingCarinfo);
            return new ApiResponse(null, 200, "车辆信息更新成功");
        } else {
            return new ApiResponse(null, 404, "车辆不存在");
        }
    } catch (Exception e) {
        return new ApiResponse(e.getMessage(), 500, "更新车辆信息发生异常");
    }
}

    @GetMapping("/getCarsInParking")
    public ApiResponse getCarsInParking(Car car) {
        try {
            List<Map<String, Object>> result = carShowMapper.findCarsByParkingId(car.getParking_id());
            if (result.isEmpty()) {
                return new ApiResponse(null, 404, "车辆列表为空");
            } else {
                for (Map<String, Object> item : result) {
                    String carImage = (String) item.get("car_image");
                    String brandLogo = (String) item.get("brand_logo");
                    item.put("car_image", carImage);
                    item.put("brand_logo", brandLogo);
                }
                return new ApiResponse(result, 200, "获取车辆列表成功");
            }
        } catch (Exception e) {
            return new ApiResponse(e.getMessage(), 500, "服务器错误");
        }
    }
}
