package com.unis.module.coding.controller.admin.bank;

import com.unis.module.coding.controller.admin.bank.vo.BankPageReqVO;
import com.unis.module.coding.controller.admin.bank.vo.BankRespVO;
import com.unis.module.coding.controller.admin.bank.vo.BankSaveReqVO;
import com.unis.module.coding.dal.dataobject.bank.BankDO;
import com.unis.module.coding.service.bank.BankService;
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.unis.framework.common.pojo.PageParam;
import com.unis.framework.common.pojo.PageResult;
import com.unis.framework.common.pojo.CommonResult;
import com.unis.framework.common.util.object.BeanUtils;
import static com.unis.framework.common.pojo.CommonResult.success;

import com.unis.framework.excel.core.util.ExcelUtils;

import com.unis.framework.apilog.core.annotation.ApiAccessLog;
import static com.unis.framework.apilog.core.enums.OperateTypeEnum.*;



@Tag(name = "管理后台 - 银行已选")
@RestController
@RequestMapping("/delivery/bank")
@Validated
public class BankController {

    @Resource
    private BankService bankService;

    @PostMapping("/create")
    @Operation(summary = "创建银行已选")
    @PreAuthorize("@ss.hasPermission('delivery:bank:create')")
    public CommonResult<Long> createBank(@Valid @RequestBody BankSaveReqVO createReqVO) {
        return success(bankService.createBank(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新银行已选")
    @PreAuthorize("@ss.hasPermission('delivery:bank:update')")
    public CommonResult<Boolean> updateBank(@Valid @RequestBody BankSaveReqVO updateReqVO) {
        bankService.updateBank(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除银行已选")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('delivery:bank:delete')")
    public CommonResult<Boolean> deleteBank(@RequestParam("id") Long id) {
        bankService.deleteBank(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得银行已选")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('delivery:bank:query')")
    public CommonResult<BankRespVO> getBank(@RequestParam("id") Long id) {
        BankDO bank = bankService.getBank(id);
        return success(BeanUtils.toBean(bank, BankRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得银行已选分页")
    @PreAuthorize("@ss.hasPermission('delivery:bank:query')")
    public CommonResult<PageResult<BankRespVO>> getBankPage(@Valid BankPageReqVO pageReqVO) {
        PageResult<BankDO> pageResult = bankService.getBankPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BankRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出银行已选 Excel")
    @PreAuthorize("@ss.hasPermission('delivery:bank:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBankExcel(@Valid BankPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BankDO> list = bankService.getBankPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "银行已选.xls", "数据", BankRespVO.class,
                        BeanUtils.toBean(list, BankRespVO.class));
    }

}