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

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.otaDistributorRegion.vo.*;
import com.yc.ship.module.ota.dal.dataobject.otaDistributorRegion.OtaDistributorRegionDO;
import com.yc.ship.module.ota.service.otaDistributorRegion.OtaDistributorRegionService;

@Tag(name = "管理后台 - 旅行社与代理区域关联")
@RestController
@RequestMapping("/ota/distributor-region")
@Validated
public class OtaDistributorRegionController {

    @Resource
    private OtaDistributorRegionService distributorRegionService;

    @PostMapping("/create")
    @Operation(summary = "创建旅行社与代理区域关联")
    @PreAuthorize("@ss.hasPermission('ota:distributor-region:create')")
    public CommonResult<Long> createDistributorRegion(@Valid @RequestBody OtaDistributorRegionSaveReqVO createReqVO) {
        return success(distributorRegionService.createDistributorRegion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新旅行社与代理区域关联")
    @PreAuthorize("@ss.hasPermission('ota:distributor-region:update')")
    public CommonResult<Boolean> updateDistributorRegion(@Valid @RequestBody OtaDistributorRegionSaveReqVO updateReqVO) {
        distributorRegionService.updateDistributorRegion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除旅行社与代理区域关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ota:distributor-region:delete')")
    public CommonResult<Boolean> deleteDistributorRegion(@RequestParam("id") String id) {
        distributorRegionService.deleteDistributorRegion(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得旅行社与代理区域关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ota:distributor-region:query')")
    public CommonResult<OtaDistributorRegionRespVO> getDistributorRegion(@RequestParam("id") String id) {
        OtaDistributorRegionDO distributorRegion = distributorRegionService.getDistributorRegion(id);
        return success(BeanUtils.toBean(distributorRegion, OtaDistributorRegionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得旅行社与代理区域关联分页")
    @PreAuthorize("@ss.hasPermission('ota:distributor-region:query')")
    public CommonResult<PageResult<OtaDistributorRegionRespVO>> getDistributorRegionPage(@Valid OtaDistributorRegionPageReqVO pageReqVO) {
        PageResult<OtaDistributorRegionDO> pageResult = distributorRegionService.getDistributorRegionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OtaDistributorRegionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出旅行社与代理区域关联 Excel")
    @PreAuthorize("@ss.hasPermission('ota:distributor-region:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDistributorRegionExcel(@Valid OtaDistributorRegionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OtaDistributorRegionDO> list = distributorRegionService.getDistributorRegionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "旅行社与代理区域关联.xls", "数据", OtaDistributorRegionRespVO.class,
                        BeanUtils.toBean(list, OtaDistributorRegionRespVO.class));
    }

}