package com.yc.ship.module.product.controller.admin.voyagestock;

import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.module.product.controller.admin.voyage.vo.VoyageRespVO;
import com.yc.ship.module.resource.api.ship.ShipApi;
import com.yc.ship.module.resource.api.ship.dto.ShipRespDTO;
import com.yc.ship.module.resource.dal.dataobject.route.ResourceRouteDO;
import com.yc.ship.module.resource.dal.dataobject.ship.ResourceShipDO;
import com.yc.ship.module.resource.service.ship.ResourceShipService;
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.product.controller.admin.voyagestock.vo.*;
import com.yc.ship.module.product.dal.dataobject.voyagestock.VoyageStockDO;
import com.yc.ship.module.product.service.voyagestock.VoyageStockService;

@Tag(name = "管理后台 - 航次库存")
@RestController
@RequestMapping("/product/voyage-stock")
@Validated
public class VoyageStockController {

    @Resource
    private VoyageStockService voyageStockService;


    @PostMapping("/create")
    @Operation(summary = "创建航次库存")
    @PreAuthorize("@ss.hasPermission('product:voyage-stock:create')")
    public CommonResult<Long> createVoyageStock(@Valid @RequestBody VoyageStockSaveReqVO createReqVO) {
        return success(voyageStockService.createVoyageStock(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新航次库存")
    @PreAuthorize("@ss.hasPermission('product:voyage-stock:update')")
    public CommonResult<Boolean> updateVoyageStock(@Valid @RequestBody VoyageStockSaveReqVO updateReqVO) {
        voyageStockService.updateVoyageStock(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除航次库存")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:voyage-stock:delete')")
    public CommonResult<Boolean> deleteVoyageStock(@RequestParam("id") Long id) {
        voyageStockService.deleteVoyageStock(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得航次库存")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:voyage-stock:query')")
    public CommonResult<VoyageStockRespVO> getVoyageStock(@RequestParam("id") Long id) {
        VoyageStockDO voyageStock = voyageStockService.getVoyageStock(id);
        return success(BeanUtils.toBean(voyageStock, VoyageStockRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得航次库存分页")
    @PreAuthorize("@ss.hasPermission('product:voyage-stock:query')")
    public CommonResult<PageResult<VoyageStockRespVO>> getVoyageStockPage(@Valid VoyageStockPageReqVO pageReqVO) {
        PageResult<VoyageStockRespVO> pageResult = voyageStockService.getVoyageStockPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/total")
    @Operation(summary = "获得航次库存合计")
    @PreAuthorize("@ss.hasPermission('product:voyage-stock:query')")
    public CommonResult<VoyageStockTotalVO> getVoyageStockTotal(@Valid VoyageStockPageReqVO pageReqVO) {
        VoyageStockTotalVO totalVO = voyageStockService.getVoyageStockTotal(pageReqVO);
        return success(totalVO);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出航次库存 Excel")
    @PreAuthorize("@ss.hasPermission('product:voyage-stock:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVoyageStockExcel(@Valid VoyageStockPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VoyageStockRespVO> list = voyageStockService.getVoyageStockPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "航次库存.xls", "数据", VoyageStockRespVO.class,
                       list);
    }

}