package com.luozhihui.project.controller;
import com.luozhihui.project.common.ApiResponse;
import com.luozhihui.project.entity.User;
import com.luozhihui.project.mapper.UserShowMapper;
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 UserShowController {
    @Resource
    UserShowMapper userShowMapper;

    //查询所有用户信息
    @CrossOrigin
    @PostMapping("/AllUser")
    public ApiResponse AllCar() {
        try {
            // 查询所有订单
            List<User> user = userShowMapper.FindAllUser();
            if (user != null) {
                return new ApiResponse(user, 200, "查询成功");
            } else {
                return new ApiResponse(null, 404, "未找到"); // 如果未找到订单，返回适当的状态码和消息
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(null, 500, "服务器内部错误"); // 捕获任何异常并返回服务器内部错误
        }
    }

    @CrossOrigin
    //通过userid查询信息
    @PostMapping("/FindUserById")
    public ApiResponse FindUserById(User user) {
        try {

            List<User> users = userShowMapper.FindUserById(user.getUserId());

            if (users != null && !users.isEmpty()) {
                return new ApiResponse(users, 200, "查询成功");
            } else {
                return new ApiResponse(null, 404, "未找到用户信息"); // 如果未找到订单，返回适当的状态码和消息
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(null, 500, "服务器内部错误"); // 捕获任何异常并返回服务器内部错误
        }
    }

    @CrossOrigin
    @PostMapping("/delete_user")
    public ApiResponse delete_user(@RequestBody Map<String, Object> requestBody) {
        try {
            String userIdsParam = (String) requestBody.get("userIds");
            // 将逗号分隔的字符串userIdsParam拆分为userId列表
            List<String> userIdStrings = Arrays.asList(userIdsParam.split(","));
            List<Long> userIds = userIdStrings.stream().map(Long::parseLong).collect(Collectors.toList());

            int deletedCount = 0;
            for (Long userId : userIds) {
                boolean isDeleted = userShowMapper.DeleteUserById(String.valueOf(userId));
                if (isDeleted) {
                    deletedCount++;
                }
            }
            if (deletedCount == userIds.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, "服务器内部错误");
        }
    }


    @CrossOrigin
    @PostMapping("/add_user")
    public ApiResponse addUser(@RequestBody User user) {
        try {
            if (user == null || !isValidUser(user)) {
                return new ApiResponse(null, 400, "无效的用户数据");
            }

            if (isUserExists(user)) {
                return new ApiResponse(null, 409, "用户已存在");
            }

            userShowMapper.adduser(user);

            return new ApiResponse(null, 200, "用户添加成功");
        } catch (Exception e) {
//            logger.error("添加用户时出错: " + e.getMessage(), e);
            return new ApiResponse(null, 500, "内部服务器错误");
        }
    }

    private boolean isValidUser(User user) {
        if (user == null) {
            return false;
        }
        // 在这里执行其他验证逻辑，例如检查用户名、密码、邮箱等是否符合要求
        return true;
    }

    private boolean isUserExists(User user) {
        try {
            User existingUser = (User) userShowMapper.FindUserByuserName(user.getUserName());
            return existingUser != null;
        } catch (Exception e) {
//            logger.error("检查用户是否已存在时出错: " + e.getMessage(), e);
            return false; // 或者根据需要采取其他措施
        }
    }




}


