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.SkuServiceDetailsDO;
import com.tashow.cloud.product.service.SkuServiceDetailsService;
import com.tashow.cloud.productapi.api.product.vo.skuservicedetails.SkuServiceDetailsPageReqVO;
import com.tashow.cloud.productapi.api.product.vo.skuservicedetails.SkuServiceDetailsRespVO;
import com.tashow.cloud.productapi.api.product.vo.skuservicedetails.SkuServiceDetailsSaveReqVO;
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-details")
@Validated
public class SkuServiceDetailsController {

    @Resource
    private SkuServiceDetailsService skuServiceDetailsService;

    @PostMapping("/create")
    @Operation(summary = "创建服务详情")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-details:create')")
    public CommonResult<Long> createSkuServiceDetails(@Valid @RequestBody SkuServiceDetailsSaveReqVO createReqVO) {
        return success(skuServiceDetailsService.createSkuServiceDetails(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新服务详情")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-details:update')")
    public CommonResult<Boolean> updateSkuServiceDetails(@Valid @RequestBody SkuServiceDetailsSaveReqVO updateReqVO) {
        skuServiceDetailsService.updateSkuServiceDetails(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除服务详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:sku-service-details:delete')")
    public CommonResult<Boolean> deleteSkuServiceDetails(@RequestParam("id") Long id) {
        skuServiceDetailsService.deleteSkuServiceDetails(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得服务详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-details:query')")
    public CommonResult<SkuServiceDetailsRespVO> getSkuServiceDetails(@RequestParam("id") Long id) {
        SkuServiceDetailsDO skuServiceDetails = skuServiceDetailsService.getSkuServiceDetails(id);
        return success(BeanUtils.toBean(skuServiceDetails, SkuServiceDetailsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得服务详情分页")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-details:query')")
    public CommonResult<PageResult<SkuServiceDetailsRespVO>> getSkuServiceDetailsPage(@Valid SkuServiceDetailsPageReqVO pageReqVO) {
        PageResult<SkuServiceDetailsDO> pageResult = skuServiceDetailsService.getSkuServiceDetailsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SkuServiceDetailsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出服务详情 Excel")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-details:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSkuServiceDetailsExcel(@Valid SkuServiceDetailsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SkuServiceDetailsDO> list = skuServiceDetailsService.getSkuServiceDetailsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "服务详情.xls", "数据", SkuServiceDetailsRespVO.class,
                        BeanUtils.toBean(list, SkuServiceDetailsRespVO.class));
    }

}