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

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.*;
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.digftp02.vo.*;
import com.trs.digital.module.manager.dal.dataobject.digftp02.Digftpsource02DO;
import com.trs.digital.module.manager.service.digftp02.Digftpsource02Service;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - STF02资源管理")
@RestController
@RequestMapping("/manager/digftpsource02")
@Validated
public class Digftpsource02Controller {

    @Resource
    private Digftpsource02Service digftpsource02Service;

    @PostMapping("/create")
    @Operation(summary = "创建STF02资源管理")
    @PreAuthorize("@ss.hasPermission('manager:digftpsource02:create')")
    public CommonResult<Long> createdigftpsource02(@Valid @RequestBody Digftpsource02SaveReqVO createReqVO) {
        return success(digftpsource02Service.createdigftpsource02(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新STF02资源管理")
    @PreAuthorize("@ss.hasPermission('manager:digftpsource02:update')")
    public CommonResult<Boolean> updatedigftpsource02(@Valid @RequestBody Digftpsource02SaveReqVO updateReqVO) {
        digftpsource02Service.updatedigftpsource02(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得STF02资源管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('manager:digftpsource02:query')")
    public CommonResult<Digftpsource02DO> getdigftpsource02(@RequestParam("id") Long id) {
        Digftpsource02DO digftpsource02 = digftpsource02Service.getdigftpsource02(id);
        return success(digftpsource02);
    }

    @GetMapping("/page")
    @Operation(summary = "获得STF02资源管理分页")
    @PreAuthorize("@ss.hasPermission('manager:digftpsource02:query')")
    public CommonResult<PageResult<Digftpsource02RespVO>> getdigftpsource02Page(@Valid Digftpsource02PageReqVO pageReqVO) {
        PageResult<Digftpsource02DO> pageResult = digftpsource02Service.getdigftpsource02Page(pageReqVO);
        return success(BeanUtils.toBean(pageResult, Digftpsource02RespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出STF02资源管理 Excel")
    @PreAuthorize("@ss.hasPermission('manager:digftpsource02:export')")
    @OperateLog(type = EXPORT)
    public void exportdigftpsource02Excel(@Valid Digftpsource02PageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<Digftpsource02DO> list = digftpsource02Service.getdigftpsource02Page(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "STF02资源管理.xls", "数据", Digftpsource02RespVO.class,
                        BeanUtils.toBean(list, Digftpsource02RespVO.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++) {
            Digftpsource02SaveReqVO digFtpSourceDO = BeanUtils.toBean(list.get(i), Digftpsource02SaveReqVO.class);
            digftpsource02Service.createdigftpsource02(digFtpSourceDO);
        }
        return new CommonResult<>();
    }


}