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.UserDataRoleExcelVO;
import cn.ey.biz.module.system.service.permission.UserDataRoleService;
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-data-role")
@Validated
public class UserDataRoleController {

    @Resource
    private UserDataRoleService userDataRoleService;

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

    @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<UserDataRoleExcelVO> list = ExcelUtils.read(file, UserDataRoleExcelVO.class);
        userDataRoleService.importUserDataRoleList(list);
        return success(true);
    }

}
