package com.luozhihui.project.controller;
import com.luozhihui.project.common.ApiResponse;
import com.luozhihui.project.common.totalResponse;
import com.luozhihui.project.entity.Brand;
import com.luozhihui.project.mapper.BrandMapper;
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 BrandController {
    @Resource
    BrandMapper brandMapper;
    //查询所有汽车品牌信息
    @PostMapping("/AllBrandpage")
    public totalResponse AllBrand(@RequestParam int pageNumber, @RequestParam int pageSize) {
        try {
            // 查询所有汽车车辆
            List<Brand> brand = brandMapper.FindAllBrandpage((pageNumber - 1) * pageSize, pageSize);
            int total = brandMapper.countBrand(); // 获取总订单数
            if (brand != null) {
                return new totalResponse(brand, 200, "查询成功",total);
            } else {
                return new totalResponse(null, 404, "未找到品牌",null); // 如果未找到车辆，返回适当的状态码和消息
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new totalResponse(e.getMessage(), 500, "服务器内部错误",null); // 捕获任何异常并返回服务器内部错误
        }
    }
    @PostMapping("/AllBrand")
    public ApiResponse AllBrandpage() {
        try {
            // 查询所有汽车车辆
            List<Brand> brand = brandMapper.FindAllBrand();
            int total = brandMapper.countBrand(); // 获取总订单数
            if (brand != null) {
                return new ApiResponse(brand, 200, "查询成功");
            } else {
                return new ApiResponse(null, 404, "未找到品牌"); // 如果未找到车辆，返回适当的状态码和消息
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(e.getMessage(), 500, "服务器内部错误"); // 捕获任何异常并返回服务器内部错误
        }
    }
    //通过id查询信息
    @PostMapping("/FindBrandById")
    public ApiResponse FindBrandById(@RequestParam("id") Long id) {
        try {
            // 查询品牌信息
            Brand brand = (Brand) brandMapper.FindBrandById(String.valueOf(id));

            if (brand != null) {
                return new ApiResponse(brand, 200, "查询成功");
            } else {
                return new ApiResponse(null, 404, "未找到品牌信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(null, 500, "服务器内部错误：" + e.getMessage());
        }
    }
//    // 新增品牌
    @PostMapping("/AddBrand")
    public ApiResponse AddBrand(@RequestBody Brand brand) {
        try {
            // 添加车辆
            brandMapper.addBrand(brand);
            // 返回成功消息
            return new ApiResponse(null, 200, "品牌添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            // 返回内部服务器错误
            return new ApiResponse(e.getMessage(), 500, "服务器内部错误");
        }
    }
    @CrossOrigin
    @PostMapping("/delete_brand")
    public ApiResponse delete_brand(@RequestBody Map<String, Object> requestBody) {
        try {
            String carIdsParam = (String) requestBody.get("ids");
            // 将逗号分隔的字符串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 = brandMapper.DeleteBrandById(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("/upbrandinfo")
    public ApiResponse updateCar(@RequestBody Brand brand) {
        System.out.println(brand.getId());
        System.out.println("-------------------------------");
        try {

            Brand existingBrandinfo = brandMapper.BrandId(Integer.valueOf(brand.getId()));
            System.out.println(existingBrandinfo);
            if (existingBrandinfo != null) {
                existingBrandinfo.setBrand_name_ch(brand.getBrand_name_ch());
                existingBrandinfo.setBrand_name_en(brand.getBrand_name_en());
                existingBrandinfo.setBrand_logo(brand.getBrand_logo());
                existingBrandinfo.setStatus(brand.getStatus());
                brandMapper.update(existingBrandinfo);
                return new ApiResponse(null, 200, "品牌信息更新成功");
            } else {
                return new ApiResponse(null, 404, "品牌不存在");
            }
        } catch (Exception e) {
            return new ApiResponse(e.getMessage(), 500, "更新品牌信息发生异常");
        }
    }

}
