package com.yc.ship.module.resource.controller.admin.routecost;

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.resource.controller.admin.routecost.vo.*;
import com.yc.ship.module.resource.dal.dataobject.routecost.ResourceRouteCostDO;
import com.yc.ship.module.resource.service.routecost.ResourceRouteCostService;

@Tag(name = "管理后台 - 航线管理-费用包含")
@RestController
@RequestMapping("/resource/route-cost")
@Validated
public class ResourceRouteCostController {

    @Resource
    private ResourceRouteCostService routeCostService;

    @PostMapping("/create")
    @Operation(summary = "创建航线管理-费用包含")
    @PreAuthorize("@ss.hasPermission('resource:route-cost:create')")
    public CommonResult<Long> createRouteCost(@Valid @RequestBody ResourceRouteCostSaveReqVO createReqVO) {
        return success(routeCostService.createRouteCost(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新航线管理-费用包含")
    @PreAuthorize("@ss.hasPermission('resource:route-cost:update')")
    public CommonResult<Boolean> updateRouteCost(@Valid @RequestBody ResourceRouteCostSaveReqVO updateReqVO) {
        routeCostService.updateRouteCost(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除航线管理-费用包含")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('resource:route-cost:delete')")
    public CommonResult<Boolean> deleteRouteCost(@RequestParam("id") Long id) {
        routeCostService.deleteRouteCost(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得航线管理-费用包含")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:route-cost:query')")
    public CommonResult<ResourceRouteCostRespVO> getRouteCost(@RequestParam("id") Long id) {
        ResourceRouteCostDO routeCost = routeCostService.getRouteCost(id);
        return success(BeanUtils.toBean(routeCost, ResourceRouteCostRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得航线管理-费用包含分页")
    @PreAuthorize("@ss.hasPermission('resource:route-cost:query')")
    public CommonResult<PageResult<ResourceRouteCostRespVO>> getRouteCostPage(@Valid ResourceRouteCostPageReqVO pageReqVO) {
        PageResult<ResourceRouteCostDO> pageResult = routeCostService.getRouteCostPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ResourceRouteCostRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出航线管理-费用包含 Excel")
    @PreAuthorize("@ss.hasPermission('resource:route-cost:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRouteCostExcel(@Valid ResourceRouteCostPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceRouteCostDO> list = routeCostService.getRouteCostPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "航线管理-费用包含.xls", "数据", ResourceRouteCostRespVO.class,
                        BeanUtils.toBean(list, ResourceRouteCostRespVO.class));
    }

}