//
// 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.User;
import com.luozhihui.project.mapper.UserShowMapper;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/api"})
@CrossOrigin
public class UserShowController {
    @Resource
    UserShowMapper userShowMapper;

    public UserShowController() {
    }

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

    @CrossOrigin
    @PostMapping({"/FindUserById"})
    public ApiResponse FindUserById(User user) {
        try {
            List<User> users = this.userShowMapper.FindUserById(user.getUserId());
            return users != null && !users.isEmpty() ? new ApiResponse(users, 200, "查询成功") : new ApiResponse((Object)null, 404, "未找到用户信息");
        } catch (Exception var3) {
            Exception e = var3;
            e.printStackTrace();
            return new ApiResponse((Object)null, 500, "服务器内部错误");
        }
    }

    @CrossOrigin
    @PostMapping({"/delete_user"})
    public ApiResponse delete_user(@RequestBody Map<String, Object> requestBody) {
        try {
            String userIdsParam = (String)requestBody.get("userIds");
            List<String> userIdStrings = Arrays.asList(userIdsParam.split(","));
            List<Long> userIds = (List)userIdStrings.stream().map(Long::parseLong).collect(Collectors.toList());
            int deletedCount = 0;
            Iterator var6 = userIds.iterator();

            while(var6.hasNext()) {
                Long userId = (Long)var6.next();
                boolean isDeleted = this.userShowMapper.DeleteUserById(String.valueOf(userId));
                if (isDeleted) {
                    ++deletedCount;
                }
            }

            if (deletedCount == userIds.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, "服务器内部错误");
        }
    }

    @CrossOrigin
    @PostMapping({"/add_user"})
    public ApiResponse addUser(@RequestBody User user) {
        try {
            if (user != null && this.isValidUser(user)) {
                if (this.isUserExists(user)) {
                    return new ApiResponse((Object)null, 409, "用户已存在");
                } else {
                    this.userShowMapper.adduser(user);
                    return new ApiResponse((Object)null, 200, "用户添加成功");
                }
            } else {
                return new ApiResponse((Object)null, 400, "无效的用户数据");
            }
        } catch (Exception var3) {
            return new ApiResponse((Object)null, 500, "内部服务器错误");
        }
    }

    private boolean isValidUser(User user) {
        return user != null;
    }

    private boolean isUserExists(User user) {
        try {
            User existingUser = (User)this.userShowMapper.FindUserByuserName(user.getUserName());
            return existingUser != null;
        } catch (Exception var3) {
            return false;
        }
    }
}
