package com.tashow.cloud.product.controller;

import com.tashow.cloud.product.dto.ProdPropValueDO;
import com.tashow.cloud.product.service.ProdPropValueService;
import com.tashow.cloud.product.vo.prodpropvalue.ProdPropValuePageReqVO;
import com.tashow.cloud.product.vo.prodpropvalue.ProdPropValueRespVO;
import com.tashow.cloud.product.vo.prodpropvalue.ProdPropValueSaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.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 jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.tashow.cloud.common.pojo.PageParam;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.util.object.BeanUtils;
import static com.tashow.cloud.common.pojo.CommonResult.success;

import com.tashow.cloud.excel.excel.core.util.ExcelUtils;

import com.tashow.cloud.web.apilog.core.annotation.ApiAccessLog;
import static com.tashow.cloud.web.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 属性规则")
@RestController
@RequestMapping("/tz/prod-prop-value")
@Validated
public class ProdPropValueController {

    @Resource
    private ProdPropValueService prodPropValueService;

    @PostMapping("/create")
    @Operation(summary = "创建属性规则")
    @PreAuthorize("@ss.hasPermission('tz:prod-prop-value:create')")
    public CommonResult<Long> createProdPropValue(@Valid @RequestBody ProdPropValueSaveReqVO createReqVO) {
        return success(prodPropValueService.createProdPropValue(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新属性规则")
    @PreAuthorize("@ss.hasPermission('tz:prod-prop-value:update')")
    public CommonResult<Boolean> updateProdPropValue(@Valid @RequestBody ProdPropValueSaveReqVO updateReqVO) {
        prodPropValueService.updateProdPropValue(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除属性规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:prod-prop-value:delete')")
    public CommonResult<Boolean> deleteProdPropValue(@RequestParam("id") Long id) {
        prodPropValueService.deleteProdPropValue(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得属性规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:prod-prop-value:query')")
    public CommonResult<ProdPropValueRespVO> getProdPropValue(@RequestParam("id") Long id) {
        ProdPropValueDO prodPropValue = prodPropValueService.getProdPropValue(id);
        return success(BeanUtils.toBean(prodPropValue, ProdPropValueRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得属性规则分页")
    @PreAuthorize("@ss.hasPermission('tz:prod-prop-value:query')")
    public CommonResult<PageResult<ProdPropValueRespVO>> getProdPropValuePage(@Valid ProdPropValuePageReqVO pageReqVO) {
        PageResult<ProdPropValueDO> pageResult = prodPropValueService.getProdPropValuePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProdPropValueRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出属性规则 Excel")
    @PreAuthorize("@ss.hasPermission('tz:prod-prop-value:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProdPropValueExcel(@Valid ProdPropValuePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProdPropValueDO> list = prodPropValueService.getProdPropValuePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "属性规则.xls", "数据", ProdPropValueRespVO.class,
                        BeanUtils.toBean(list, ProdPropValueRespVO.class));
    }

}