package cn.ey.biz.module.system.controller.admin.permission;

import cn.ey.biz.common.pojo.R;
import cn.ey.biz.framework.excel.core.util.ExcelUtils;
import cn.ey.biz.framework.operatelog.core.annotations.OperateLog;
import cn.ey.biz.module.system.controller.admin.permission.vo.userRole.UserRoleExcelVO;
import cn.ey.biz.module.system.service.permission.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import static cn.ey.biz.common.pojo.R.success;
import static cn.ey.biz.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Api(tags = "管理后台 - 用户角色关联表")
@RestController
@RequestMapping("/system/user-role")
@Validated
public class UserRoleController {

    @Resource
    private UserRoleService userRoleService;

    @GetMapping("/export-excel")
    @ApiOperation("导出用户角色关联数据Excel")
    @OperateLog(type = EXPORT)
    public void exportUserRole(HttpServletResponse response) throws IOException {
        List<UserRoleExcelVO> userRoleExcelVOList=userRoleService.getListUserRoleSql();
        ExcelUtils.write(response, "用户角色关系数据.xls", "数据", UserRoleExcelVO.class, userRoleExcelVOList);
    }

    @PostMapping("/import")
    @ApiOperation("导入用户角色关联数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "Excel文件", required = true, dataTypeClass = MultipartFile.class)})
    public R<Boolean> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<UserRoleExcelVO> list = ExcelUtils.read(file, UserRoleExcelVO.class);
        userRoleService.importUserRoleList(list);
        return success(true);
    }
}
