package easydo.technology.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import easydo.technology.annotation.WebLog;
import easydo.technology.system.domain.User;
import easydo.technology.system.domain.UsersRoles;
import easydo.technology.system.service.UserService;
import easydo.technology.system.service.UsersRolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户角色关联 前端控制器
 * </p>
 *
 * @author hewj
 * @since 2020-10-13
 */
@RestController
@RequestMapping("/sysUsersRoles")
public class SysUsersRolesController {

    @Autowired
    private UsersRolesService sysUsersRolesService;

    @Autowired
    private UserService sysUserService;

    @WebLog
    @RequestMapping(value = "/getUserListByRoleId")
    public ResponseEntity<Object> getUserListByRoleId(@RequestBody Long roleId) throws Exception {

        Map<String, Object> result = new HashMap<>();

        QueryWrapper<UsersRoles> wrapper = Wrappers.query();
        wrapper.eq("role_id",roleId);
        List<UsersRoles> usersRolesList = sysUsersRolesService.list(wrapper);
        List<Long> userIds = new ArrayList<>();
        for (UsersRoles useRole : usersRolesList){
            userIds.add(useRole.getUserId());
        }

        QueryWrapper<User> userWrapper = Wrappers.query();
        userWrapper.in("user_id",userIds);
        List<User> userList = sysUserService.list(userWrapper);

        result.put("userList",userList);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
}
