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

import com.trs.digital.framework.common.enums.CommonStatusEnum;
import com.trs.digital.module.manager.dal.dataobject.DigExcelVO;
import com.trs.digital.module.system.controller.admin.user.vo.user.UserImportExcelVO;
import com.trs.digital.module.system.controller.admin.user.vo.user.UserImportRespVO;
import com.trs.digital.module.system.dal.dataobject.user.AdminUserDO;
import com.trs.digital.module.system.enums.common.SexEnum;
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.digftp.vo.*;
import com.trs.digital.module.manager.dal.dataobject.digftp.DigFtpSourceDO;
import com.trs.digital.module.manager.service.digftp.DigFtpSourceService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - STF资源管理")
@RestController
@RequestMapping("/manager/dig-ftp-source")
@Validated
public class DigFtpSourceController {

    @Resource
    private DigFtpSourceService digFtpSourceService;

    @PostMapping("/create")
    @Operation(summary = "创建STF资源管理")
    @PreAuthorize("@ss.hasPermission('manager:dig-ftp-source:create')")
    public CommonResult<Long> createDigFtpSource(@Valid @RequestBody DigFtpSourceSaveReqVO createReqVO) {
        return success(digFtpSourceService.createDigFtpSource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新STF资源管理")
    @PreAuthorize("@ss.hasPermission('manager:dig-ftp-source:update')")
    public CommonResult<Boolean> updateDigFtpSource(@Valid @RequestBody DigFtpSourceSaveReqVO updateReqVO) {
        digFtpSourceService.updateDigFtpSource(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得STF资源管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('manager:dig-ftp-source:query')")
    public CommonResult<DigFtpSourceDO> getDigFtpSource(@RequestParam("id") Long id) {
        DigFtpSourceDO digFtpSource = digFtpSourceService.getDigFtpSource(id);
        return success(digFtpSource);
    }

    @GetMapping("/page")
    @Operation(summary = "获得STF资源管理分页")
    @PreAuthorize("@ss.hasPermission('manager:dig-ftp-source:query')")
    public CommonResult<PageResult<DigFtpSourceRespVO>> getDigFtpSourcePage(@Valid DigFtpSourcePageReqVO pageReqVO) {
        PageResult<DigFtpSourceDO> pageResult = digFtpSourceService.getDigFtpSourcePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DigFtpSourceRespVO.class));
    }

    @RequestMapping("/export-excel")
    @Operation(summary = "导出STF资源管理 Excel")
    @PreAuthorize("@ss.hasPermission('manager:dig-ftp-source:export')")
    @OperateLog(type = EXPORT)
    public void exportDigFtpSourceExcel(DigFtpSourcePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DigFtpSourceDO> list = digFtpSourceService.getDigFtpSourcePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "STF资源管理.xls", "数据", DigFtpSourceRespVO.class,
                        BeanUtils.toBean(list, DigFtpSourceRespVO.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++) {
            DigFtpSourceSaveReqVO digFtpSourceDO = BeanUtils.toBean(list.get(i), DigFtpSourceSaveReqVO.class);
            digFtpSourceService.createDigFtpSource(digFtpSourceDO);
        }
        return new CommonResult<>();
    }


}