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.SkuServicesFormDO;
import com.tashow.cloud.product.service.SkuServicesFormService;
import com.tashow.cloud.productapi.api.product.vo.skuservicesform.SkuServicesFormPageReqVO;
import com.tashow.cloud.productapi.api.product.vo.skuservicesform.SkuServicesFormRespVO;
import com.tashow.cloud.productapi.api.product.vo.skuservicesform.SkuServicesFormSaveReqVO;
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 = "管理后台 - 商品SKU扩展服务表单")
@RestController
@RequestMapping("/tz/sku-services-form")
@Validated
public class SkuServicesFormController {

    @Resource
    private SkuServicesFormService skuServicesFormService;

    @PostMapping("/create")
    @Operation(summary = "创建商品SKU扩展服务表单")
    @PreAuthorize("@ss.hasPermission('tz:sku-services-form:create')")
    public CommonResult<Long> createSkuServicesForm(@Valid @RequestBody SkuServicesFormSaveReqVO createReqVO) {
        return success(skuServicesFormService.createSkuServicesForm(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品SKU扩展服务表单")
    @PreAuthorize("@ss.hasPermission('tz:sku-services-form:update')")
    public CommonResult<Boolean> updateSkuServicesForm(@Valid @RequestBody SkuServicesFormSaveReqVO updateReqVO) {
        skuServicesFormService.updateSkuServicesForm(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品SKU扩展服务表单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:sku-services-form:delete')")
    public CommonResult<Boolean> deleteSkuServicesForm(@RequestParam("id") Long id) {
        skuServicesFormService.deleteSkuServicesForm(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品SKU扩展服务表单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:sku-services-form:query')")
    public CommonResult<SkuServicesFormRespVO> getSkuServicesForm(@RequestParam("id") Long id) {
        SkuServicesFormDO skuServicesForm = skuServicesFormService.getSkuServicesForm(id);
        return success(BeanUtils.toBean(skuServicesForm, SkuServicesFormRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品SKU扩展服务表单分页")
    @PreAuthorize("@ss.hasPermission('tz:sku-services-form:query')")
    public CommonResult<PageResult<SkuServicesFormRespVO>> getSkuServicesFormPage(@Valid SkuServicesFormPageReqVO pageReqVO) {
        PageResult<SkuServicesFormDO> pageResult = skuServicesFormService.getSkuServicesFormPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SkuServicesFormRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品SKU扩展服务表单 Excel")
    @PreAuthorize("@ss.hasPermission('tz:sku-services-form:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSkuServicesFormExcel(@Valid SkuServicesFormPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SkuServicesFormDO> list = skuServicesFormService.getSkuServicesFormPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品SKU扩展服务表单.xls", "数据", SkuServicesFormRespVO.class,
                        BeanUtils.toBean(list, SkuServicesFormRespVO.class));
    }

}