package com.luozhihui.project.controller;
import com.luozhihui.project.common.ApiResponse;
import com.luozhihui.project.entity.Repair;
import com.luozhihui.project.mapper.RepairShowMapper;
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 RepairShowController {
    @Resource
    RepairShowMapper repairShowMapper;

    //查询所有车辆信息
    @PostMapping("/AllRepair")
    public ApiResponse AllCar() {
        try {
            // 查询所有车辆
            List<Repair> repair = repairShowMapper.FindAllRepair();
            if (repair != null) {
                return new ApiResponse(repair, 200, "查询成功");
            } else {
                return new ApiResponse(null, 404, "未找到车辆"); // 如果未找到车辆，返回适当的状态码和消息
            }
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常堆栈信息
            return new ApiResponse(null, 500, "服务器内部错误：" + e.getMessage());// 捕获任何异常并返回服务器内部错误
        }
    }
    // 新增维修信息
    @PostMapping("/AddRepair")
    public ApiResponse addRepair(@RequestBody Repair repair) {
        try {
            // 添加维修信息到 repair 表
            repairShowMapper.addRepair(repair);

            // 更新 car 表中对应车辆的 carstatus 字段为 2（维修状态）
            repairShowMapper.updateCarStatusToRepair(repair.getCarId());

            // 返回成功消息
            return new ApiResponse(null, 200, "维修信息添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            // 返回内部服务器错误
            return new ApiResponse(null, 500, "服务器内部错误");
        }
    }
    @CrossOrigin
    //删除维修
    @PostMapping("/delete_repair")
    public ApiResponse delete_repairs(@RequestBody Map<String, Object> requestBody) {
        try {
            String repairIdsInt = (String) requestBody.get("repairIds");
            String repairIdsParam = repairIdsInt;
            // 将逗号分隔的字符串 repairIdsParam 拆分为 repairId 列表
            List<String> repairIdStrings = Arrays.asList(repairIdsParam.split(","));
            List<Long> repairIds = repairIdStrings.stream().map(Long::parseLong).collect(Collectors.toList());

            int deletedCount = 0;
            for (Long repairId : repairIds) {
                boolean isDeleted = repairShowMapper.DeleteRepairById(String.valueOf(repairId));
                if (isDeleted) {
                    deletedCount++;
                }
            }

            if (deletedCount == repairIds.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(e.getMessage(), 500, "服务器内部错误");
        }
    }
    // 更新维修
    @PostMapping("/uprepair")
    public ApiResponse updateCar(@RequestBody Repair repair) {
        try {
            int repairId = repair.getRepairId(); // Assuming carId is an integer, no need for conversion
            System.out.println(repairId);

            Repair existingRepairinfo = repairShowMapper.FindRepairById(String.valueOf(repairId)); // Assuming FindRepairById returns a single Repair object
            System.out.println(existingRepairinfo);
            if (existingRepairinfo != null) {
                // 更新维修费用
                existingRepairinfo.setRepairMoney(repair.getRepairMoney());
                // 更新维修原因
                existingRepairinfo.setRepairReason(repair.getRepairReason());
                // 更新维修状态
                existingRepairinfo.setRepairStatus(repair.getRepairStatus());
                repairShowMapper.update(existingRepairinfo);


                // 如果维修状态为1，则更新 car 表的 carStatus 为 0
                if (repair.getRepairStatus() == 1) {
                    // 执行更新操作，假设 updateCarStatusToAvailable 是更新 carStatus 的方法
                    repairShowMapper.updateCarStatusToAvailable(repair.getCarId());
                    System.out.println("-----------------");
                    System.out.println(repair.getRepairStatus());
                    System.out.println(repair.getRepairStatus());
                    System.out.println(repair.getRepairStatus());
                    System.out.println(repair.getRepairStatus());
                } if (repair.getRepairStatus() == 0) {
                    // 执行更新操作，假设 updateCarStatusToAvailable 是更新 carStatus 的方法
                    repairShowMapper.updateCarStatusToRepair(repair.getCarId());
                    System.out.println("0000000000000000000");
                    System.out.println(repair.getRepairStatus());
                    System.out.println(repair.getRepairStatus());
                    System.out.println(repair.getRepairStatus());
                    System.out.println(repair.getRepairStatus());
                }

                return new ApiResponse(null, 200, "维修信息更新成功");
            } else {
                return new ApiResponse(null, 404, "车辆不存在");
            }
        } catch (Exception e) {
            return new ApiResponse(e.getMessage(), 500, "更新车辆信息发生异常");
        }
    }


}

