package org.ta.pddserver.controller;

import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import jakarta.annotation.security.RolesAllowed;
import jakarta.servlet.http.HttpServletRequest;
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;
import org.ta.pdd.entity.UserEntity;
import org.ta.pdd.entity.UserRoleEntity;
import org.ta.pddserver.model.api.request.AuthRequest;
import org.ta.pddserver.model.api.request.UserRequest;
import org.ta.pddserver.service.impl.UserImpl;
import org.ta.pddserver.service.impl.UserRoleImpl;
import org.ta.pddserver.utils.JsonResponse;

import java.util.List;

@RestController
@RequestMapping("/hj-user")
@RolesAllowed("ADMIN")
public class UserController {
    @Resource
    private UserImpl userService;
    @Resource
    private UserRoleImpl userRoleService;

    /*用户列表*/
    @PostMapping("/list")
    public JSONObject list(HttpServletRequest request, @RequestBody UserRequest userRequest) {
        return JsonResponse.generateLocalCommonSuccessResponse(userService.getListByRequest(userRequest), "");
    }

    /*用户新增*/
    @PostMapping("/add")
    public JSONObject add(HttpServletRequest request, @RequestBody UserEntity userEntity) {
        UserEntity result =  userService.add(userEntity);
        if (result != null) {
            return JsonResponse.generateLocalCommonSuccessResponse(result, "");
        } else {
            return JsonResponse.generateOpenCommonFailedResponse(-1, "添加用户失败");
        }
    }

    /*用户修改*/
    @PostMapping("/edit")
    public JSONObject edit(HttpServletRequest request, @RequestBody UserEntity userEntity) {
        if (this.userService.updateById(userEntity)) {
            return JsonResponse.generateLocalCommonSuccessResponse(userEntity, "编辑用户成功");
        }
        return JsonResponse.generateOpenCommonFailedResponse(-1, "编辑用户失败");
    }

    /*用户删除*/
    @PostMapping("/delete")
    public JSONObject delete(HttpServletRequest request, @RequestBody UserEntity userEntity) {
        if (this.userService.removeById(userEntity.getUid())) {
            return JsonResponse.generateLocalCommonSuccessResponse(userEntity, "删除成功");
        }
        this.userRoleService.removeRoleByUserId(userEntity.getUid());
        return JsonResponse.generateOpenCommonFailedResponse(-1, "删除用户失败");
    }

    /*用户删除*/
    @PostMapping("/auth")
    public JSONObject auth(HttpServletRequest request, @RequestBody AuthRequest authRequest) {
        UserEntity userEntity = userService.getById(authRequest.getUid());
        if (userEntity == null) {
            return JsonResponse.generateOpenCommonFailedResponse(-1, "用户不存在");
        }
        List<UserRoleEntity> roles = userRoleService.getByUserId(userEntity.getUid());
        for (UserRoleEntity role : roles) {
            if (role.getRole().equals(authRequest.getRole())) {
                return JsonResponse.generateOpenCommonFailedResponse(-1, "已存在该权限");
            }
        }
        UserRoleEntity roleEntity = new UserRoleEntity();
        roleEntity.setRole(authRequest.getRole());
        roleEntity.setUserId(userEntity.getUid());
        if (!userRoleService.save(roleEntity)) {
            return JsonResponse.generateOpenCommonFailedResponse(-1, "授权失败");
        }
        return JsonResponse.generateLocalCommonSuccessResponse(roleEntity, "授权成功");
    }
}
