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.SkuServiceMaterialDO;
import com.tashow.cloud.product.service.SkuServiceMaterialService;
import com.tashow.cloud.productapi.api.product.vo.skuservicematerial.SkuServiceMaterialPageReqVO;
import com.tashow.cloud.productapi.api.product.vo.skuservicematerial.SkuServiceMaterialRespVO;
import com.tashow.cloud.productapi.api.product.vo.skuservicematerial.SkuServiceMaterialSaveReqVO;
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-material")
@Validated
public class SkuServiceMaterialController {

    @Resource
    private SkuServiceMaterialService skuServiceMaterialService;

    @PostMapping("/create")
    @Operation(summary = "创建服务物料详情")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-material:create')")
    public CommonResult<Long> createSkuServiceMaterial(@Valid @RequestBody SkuServiceMaterialSaveReqVO createReqVO) {
        return success(skuServiceMaterialService.createSkuServiceMaterial(createReqVO));
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得服务物料详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-material:query')")
    public CommonResult<SkuServiceMaterialRespVO> getSkuServiceMaterial(@RequestParam("id") Long id) {
        SkuServiceMaterialDO skuServiceMaterial = skuServiceMaterialService.getSkuServiceMaterial(id);
        return success(BeanUtils.toBean(skuServiceMaterial, SkuServiceMaterialRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得服务物料详情分页")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-material:query')")
    public CommonResult<PageResult<SkuServiceMaterialRespVO>> getSkuServiceMaterialPage(@Valid SkuServiceMaterialPageReqVO pageReqVO) {
        PageResult<SkuServiceMaterialDO> pageResult = skuServiceMaterialService.getSkuServiceMaterialPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SkuServiceMaterialRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出服务物料详情 Excel")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-material:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSkuServiceMaterialExcel(@Valid SkuServiceMaterialPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SkuServiceMaterialDO> list = skuServiceMaterialService.getSkuServiceMaterialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "服务物料详情.xls", "数据", SkuServiceMaterialRespVO.class,
                        BeanUtils.toBean(list, SkuServiceMaterialRespVO.class));
    }

}