package cn.iocoder.yudao.module.server.controller.admin.materialshareuser;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.server.controller.admin.materialshareuser.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.materialshareuser.MaterialShareUserDO;
import cn.iocoder.yudao.module.server.service.materialshareuser.MaterialShareUserService;

@Tag(name = "管理后台 - 资料共享用户")
@RestController
@RequestMapping("/server/material-share-user")
@Validated
public class MaterialShareUserController {

    @Resource
    private MaterialShareUserService materialShareUserService;


    /**
     * 保存共享用户
     */

    @RequestMapping("/create")
    @Operation(summary = "创建资料共享用户")
    /*@PreAuthorize("@ss.hasPermission('server:material-share-user:create')")*/
    public CommonResult<Long> createMaterialShareUser(@Valid @RequestBody MaterialShareUserSaveReqVO createReqVO) {
        return success(materialShareUserService.createMaterialShareUser(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新资料共享用户")
    @PreAuthorize("@ss.hasPermission('server:material-share-user:update')")
    public CommonResult<Boolean> updateMaterialShareUser(@Valid @RequestBody MaterialShareUserSaveReqVO updateReqVO) {
        materialShareUserService.updateMaterialShareUser(updateReqVO);
        return success(true);
    }

    @RequestMapping("/delete")
    @Operation(summary = "删除资料共享用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:material-share-user:delete')")
    public CommonResult<Boolean> deleteMaterialShareUser(@RequestParam("id") Long id) {
        materialShareUserService.deleteMaterialShareUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资料共享用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
   /* @PreAuthorize("@ss.hasPermission('server:material-share-user:query')")*/
    public CommonResult<List<MaterialShareUserRespVO>> getMaterialShareUser(@RequestParam("id") Long id) {
        List<MaterialShareUserDO> materialShareUserDOS= materialShareUserService.getMaterialShareUser(id);
        return success(BeanUtils.toBean(materialShareUserDOS, MaterialShareUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资料共享用户分页")
    @PreAuthorize("@ss.hasPermission('server:material-share-user:query')")
    public CommonResult<PageResult<MaterialShareUserRespVO>> getMaterialShareUserPage(@Valid MaterialShareUserPageReqVO pageReqVO) {
        PageResult<MaterialShareUserDO> pageResult = materialShareUserService.getMaterialShareUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialShareUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资料共享用户 Excel")
    @PreAuthorize("@ss.hasPermission('server:material-share-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialShareUserExcel(@Valid MaterialShareUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialShareUserDO> list = materialShareUserService.getMaterialShareUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资料共享用户.xls", "数据", MaterialShareUserRespVO.class,
                        BeanUtils.toBean(list, MaterialShareUserRespVO.class));
    }

}