package com.yc.ship.module.ota.controller.admin.otaRegion;

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.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

import com.yc.ship.framework.excel.core.util.ExcelUtils;

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

import com.yc.ship.module.ota.controller.admin.otaRegion.vo.*;
import com.yc.ship.module.ota.dal.dataobject.otaRegion.OtaRegionDO;
import com.yc.ship.module.ota.service.otaRegion.OtaRegionService;

@Tag(name = "管理后台 - 地区编码")
@RestController
@RequestMapping("/ota/region")
@Validated
public class OtaRegionController {

    @Resource
    private OtaRegionService regionService;

    @PostMapping("/create")
    @Operation(summary = "创建地区编码")
    @PreAuthorize("@ss.hasPermission('ota:region:create')")
    public CommonResult<String> createRegion(@Valid @RequestBody OtaRegionSaveReqVO createReqVO) {
        return success(regionService.createRegion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新地区编码")
    @PreAuthorize("@ss.hasPermission('ota:region:update')")
    public CommonResult<Boolean> updateRegion(@Valid @RequestBody OtaRegionSaveReqVO updateReqVO) {
        regionService.updateRegion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除地区编码")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ota:region:delete')")
    public CommonResult<Boolean> deleteRegion(@RequestParam("id") String id) {
        regionService.deleteRegion(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得地区编码")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ota:region:query')")
    public CommonResult<OtaRegionRespVO> getRegion(@RequestParam("id") String id) {
        OtaRegionDO region = regionService.getRegion(id);
        return success(BeanUtils.toBean(region, OtaRegionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得地区编码分页")
    @PreAuthorize("@ss.hasPermission('ota:region:query')")
    public CommonResult<PageResult<OtaRegionRespVO>> getRegionPage(@Valid OtaRegionPageReqVO pageReqVO) {
        PageResult<OtaRegionDO> pageResult = regionService.getRegionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OtaRegionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出地区编码 Excel")
    @PreAuthorize("@ss.hasPermission('ota:region:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRegionExcel(@Valid OtaRegionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OtaRegionDO> list = regionService.getRegionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "地区编码.xls", "数据", OtaRegionRespVO.class,
                        BeanUtils.toBean(list, OtaRegionRespVO.class));
    }

}