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

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.spuship.vo.*;
import com.yc.ship.module.product.dal.dataobject.spuship.SpuShipDO;
import com.yc.ship.module.product.service.spuship.SpuShipService;

@Tag(name = "管理后台 - 产品游船")
@RestController
@RequestMapping("/product/spu-ship")
@Validated
public class SpuShipController {

    @Resource
    private SpuShipService spuShipService;

    @PostMapping("/create")
    @Operation(summary = "创建产品游船")
    @PreAuthorize("@ss.hasPermission('product:spu-ship:create')")
    public CommonResult<Long> createSpuShip(@Valid @RequestBody SpuShipSaveReqVO createReqVO) {
        return success(spuShipService.createSpuShip(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品游船")
    @PreAuthorize("@ss.hasPermission('product:spu-ship:update')")
    public CommonResult<Boolean> updateSpuShip(@Valid @RequestBody SpuShipSaveReqVO updateReqVO) {
        spuShipService.updateSpuShip(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品游船")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:spu-ship:delete')")
    public CommonResult<Boolean> deleteSpuShip(@RequestParam("id") Long id) {
        spuShipService.deleteSpuShip(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品游船")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:spu-ship:query')")
    public CommonResult<SpuShipRespVO> getSpuShip(@RequestParam("id") Long id) {
        SpuShipDO spuShip = spuShipService.getSpuShip(id);
        return success(BeanUtils.toBean(spuShip, SpuShipRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品游船分页")
    @PreAuthorize("@ss.hasPermission('product:spu-ship:query')")
    public CommonResult<PageResult<SpuShipRespVO>> getSpuShipPage(@Valid SpuShipPageReqVO pageReqVO) {
        PageResult<SpuShipDO> pageResult = spuShipService.getSpuShipPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SpuShipRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品游船 Excel")
    @PreAuthorize("@ss.hasPermission('product:spu-ship:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSpuShipExcel(@Valid SpuShipPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SpuShipDO> list = spuShipService.getSpuShipPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品游船.xls", "数据", SpuShipRespVO.class,
                        BeanUtils.toBean(list, SpuShipRespVO.class));
    }

}