package com.trs.digital.module.manager.controller.admin.oracle;

import com.trs.digital.module.manager.controller.admin.digftp.vo.DigFtpSourceSaveReqVO;
import com.trs.digital.module.manager.dal.dataobject.DigExcelVO;
import io.swagger.v3.oas.annotations.Parameters;
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 com.trs.digital.framework.common.pojo.PageParam;
import com.trs.digital.framework.common.pojo.PageResult;
import com.trs.digital.framework.common.pojo.CommonResult;
import com.trs.digital.framework.common.util.object.BeanUtils;
import static com.trs.digital.framework.common.pojo.CommonResult.success;

import com.trs.digital.framework.excel.core.util.ExcelUtils;

import com.trs.digital.framework.operatelog.core.annotations.OperateLog;
import static com.trs.digital.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.trs.digital.module.manager.controller.admin.oracle.vo.*;
import com.trs.digital.module.manager.dal.dataobject.oracle.OracleResourceDO;
import com.trs.digital.module.manager.service.oracle.OracleResourceService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - oracle资源库管理")
@RestController
@RequestMapping("/manager/oracle-resource")
@Validated
public class OracleResourceController {

    @Resource
    private OracleResourceService oracleResourceService;

    @PostMapping("/create")
    @Operation(summary = "创建oracle资源库管理")
    @PreAuthorize("@ss.hasPermission('manager:oracle-resource:create')")
    public CommonResult<Long> createOracleResource(@Valid @RequestBody OracleResourceSaveReqVO createReqVO) {
        return success(oracleResourceService.createOracleResource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新oracle资源库管理")
    @PreAuthorize("@ss.hasPermission('manager:oracle-resource:update')")
    public CommonResult<Boolean> updateOracleResource(@Valid @RequestBody OracleResourceSaveReqVO updateReqVO) {
        oracleResourceService.updateOracleResource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除oracle资源库管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('manager:oracle-resource:delete')")
    public CommonResult<Boolean> deleteOracleResource(@RequestParam("id") Long id) {
        oracleResourceService.deleteOracleResource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得oracle资源库管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('manager:oracle-resource:query')")
    public CommonResult<OracleResourceDO> getOracleResource(@RequestParam("id") Long id) {
        OracleResourceDO oracleResource = oracleResourceService.getOracleResource(id);
        return success(oracleResource);
    }

    @GetMapping("/page")
    @Operation(summary = "获得oracle资源库管理分页")
    @PreAuthorize("@ss.hasPermission('manager:oracle-resource:query')")
    public CommonResult<PageResult<OracleResourceRespVO>> getOracleResourcePage(@Valid OracleResourcePageReqVO pageReqVO) {
        PageResult<OracleResourceDO> pageResult = oracleResourceService.getOracleResourcePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OracleResourceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出oracle资源库管理 Excel")
    @PreAuthorize("@ss.hasPermission('manager:oracle-resource:export')")
    @OperateLog(type = EXPORT)
    public void exportOracleResourceExcel(@Valid OracleResourcePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OracleResourceDO> list = oracleResourceService.getOracleResourcePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "oracle资源库管理.xls", "数据", OracleResourceRespVO.class,
                        BeanUtils.toBean(list, OracleResourceRespVO.class));
    }

    @PostMapping("/import")
    @Operation(summary = "导入")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<DigExcelVO> importExcel(@RequestParam("file") MultipartFile file,
                                                @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<DigExcelVO> list = ExcelUtils.read(file, DigExcelVO.class);
        for (int i = 0; i < list.size(); i++) {
            OracleResourceSaveReqVO digFtpSourceDO = BeanUtils.toBean(list.get(i), OracleResourceSaveReqVO.class);
            oracleResourceService.createOracleResource(digFtpSourceDO);
        }
        return new CommonResult<>();
    }


}