package com.tashow.cloud.product.controller.admin;

import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.pojo.PageParam;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.common.util.object.BeanUtils;
import com.tashow.cloud.excel.excel.core.util.ExcelUtils;
import com.tashow.cloud.productapi.api.product.dto.SkuServiceDeliverDO;
import com.tashow.cloud.product.service.SkuServiceDeliverService;
import com.tashow.cloud.productapi.api.product.vo.skuservicedeliver.SkuServiceDeliverPageReqVO;
import com.tashow.cloud.productapi.api.product.vo.skuservicedeliver.SkuServiceDeliverRespVO;
import com.tashow.cloud.productapi.api.product.vo.skuservicedeliver.SkuServiceDeliverSaveReqVO;
import com.tashow.cloud.web.apilog.core.annotation.ApiAccessLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.tashow.cloud.common.pojo.CommonResult.success;
import static com.tashow.cloud.web.apilog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 服务交付方式")
@RestController
@RequestMapping("/tz/sku-service-deliver")
@Validated
public class SkuServiceDeliverController {

    @Resource
    private SkuServiceDeliverService skuServiceDeliverService;

    @PostMapping("/create")
    @Operation(summary = "创建服务交付方式")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-deliver:create')")
    public CommonResult<Long> createSkuServiceDeliver(@Valid @RequestBody SkuServiceDeliverSaveReqVO createReqVO) {
        return success(skuServiceDeliverService.createSkuServiceDeliver(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新服务交付方式")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-deliver:update')")
    public CommonResult<Boolean> updateSkuServiceDeliver(@Valid @RequestBody SkuServiceDeliverSaveReqVO updateReqVO) {
        skuServiceDeliverService.updateSkuServiceDeliver(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除服务交付方式")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:sku-service-deliver:delete')")
    public CommonResult<Boolean> deleteSkuServiceDeliver(@RequestParam("id") Long id) {
        skuServiceDeliverService.deleteSkuServiceDeliver(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得服务交付方式")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-deliver:query')")
    public CommonResult<SkuServiceDeliverRespVO> getSkuServiceDeliver(@RequestParam("id") Long id) {
        SkuServiceDeliverDO skuServiceDeliver = skuServiceDeliverService.getSkuServiceDeliver(id);
        return success(BeanUtils.toBean(skuServiceDeliver, SkuServiceDeliverRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得服务交付方式分页")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-deliver:query')")
    public CommonResult<PageResult<SkuServiceDeliverRespVO>> getSkuServiceDeliverPage(@Valid SkuServiceDeliverPageReqVO pageReqVO) {
        PageResult<SkuServiceDeliverDO> pageResult = skuServiceDeliverService.getSkuServiceDeliverPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SkuServiceDeliverRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出服务交付方式 Excel")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-deliver:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSkuServiceDeliverExcel(@Valid SkuServiceDeliverPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SkuServiceDeliverDO> list = skuServiceDeliverService.getSkuServiceDeliverPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "服务交付方式.xls", "数据", SkuServiceDeliverRespVO.class,
                        BeanUtils.toBean(list, SkuServiceDeliverRespVO.class));
    }

}