package com.tashow.cloud.product.controller;

import com.tashow.cloud.product.dto.ProdServiceAreaRelevanceDO;
import com.tashow.cloud.product.service.ProdServiceAreaRelevanceService;
import com.tashow.cloud.product.vo.prodservicearearelevance.ProdServiceAreaRelevancePageReqVO;
import com.tashow.cloud.product.vo.prodservicearearelevance.ProdServiceAreaRelevanceRespVO;
import com.tashow.cloud.product.vo.prodservicearearelevance.ProdServiceAreaRelevanceSaveReqVO;
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-service-area-relevance")
@Validated
public class ProdServiceAreaRelevanceController {

    @Resource
    private ProdServiceAreaRelevanceService prodServiceAreaRelevanceService;

    @PostMapping("/create")
    @Operation(summary = "创建商品与服务区域关联")
    @PreAuthorize("@ss.hasPermission('tz:prod-service-area-relevance:create')")
    public CommonResult<Long> createProdServiceAreaRelevance(@Valid @RequestBody ProdServiceAreaRelevanceSaveReqVO createReqVO) {
        return success(prodServiceAreaRelevanceService.createProdServiceAreaRelevance(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品与服务区域关联")
    @PreAuthorize("@ss.hasPermission('tz:prod-service-area-relevance:update')")
    public CommonResult<Boolean> updateProdServiceAreaRelevance(@Valid @RequestBody ProdServiceAreaRelevanceSaveReqVO updateReqVO) {
        prodServiceAreaRelevanceService.updateProdServiceAreaRelevance(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品与服务区域关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:prod-service-area-relevance:delete')")
    public CommonResult<Boolean> deleteProdServiceAreaRelevance(@RequestParam("id") Long id) {
        prodServiceAreaRelevanceService.deleteProdServiceAreaRelevance(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品与服务区域关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:prod-service-area-relevance:query')")
    public CommonResult<ProdServiceAreaRelevanceRespVO> getProdServiceAreaRelevance(@RequestParam("id") Long id) {
        ProdServiceAreaRelevanceDO prodServiceAreaRelevance = prodServiceAreaRelevanceService.getProdServiceAreaRelevance(id);
        return success(BeanUtils.toBean(prodServiceAreaRelevance, ProdServiceAreaRelevanceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品与服务区域关联分页")
    @PreAuthorize("@ss.hasPermission('tz:prod-service-area-relevance:query')")
    public CommonResult<PageResult<ProdServiceAreaRelevanceRespVO>> getProdServiceAreaRelevancePage(@Valid ProdServiceAreaRelevancePageReqVO pageReqVO) {
        PageResult<ProdServiceAreaRelevanceDO> pageResult = prodServiceAreaRelevanceService.getProdServiceAreaRelevancePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProdServiceAreaRelevanceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品与服务区域关联 Excel")
    @PreAuthorize("@ss.hasPermission('tz:prod-service-area-relevance:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProdServiceAreaRelevanceExcel(@Valid ProdServiceAreaRelevancePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProdServiceAreaRelevanceDO> list = prodServiceAreaRelevanceService.getProdServiceAreaRelevancePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品与服务区域关联.xls", "数据", ProdServiceAreaRelevanceRespVO.class,
                        BeanUtils.toBean(list, ProdServiceAreaRelevanceRespVO.class));
    }

}