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.ProdServiceAreasDO;
import com.tashow.cloud.product.service.ProdServiceAreasService;
import com.tashow.cloud.productapi.api.product.vo.prodserviceareas.ProdServiceAreasPageReqVO;
import com.tashow.cloud.productapi.api.product.vo.prodserviceareas.ProdServiceAreasRespVO;
import com.tashow.cloud.productapi.api.product.vo.prodserviceareas.ProdServiceAreasSaveReqVO;
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/prod-service-areas")
@Validated
public class ProdServiceAreasController {

    @Resource
    private ProdServiceAreasService prodServiceAreasService;

    @PostMapping("/create")
    @Operation(summary = "创建服务区域")
    @PreAuthorize("@ss.hasPermission('tz:prod-service-areas:create')")
    public CommonResult<Long> createProdServiceAreas(@Valid @RequestBody ProdServiceAreasSaveReqVO createReqVO) {
        return success(prodServiceAreasService.createProdServiceAreas(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新服务区域")
    @PreAuthorize("@ss.hasPermission('tz:prod-service-areas:update')")
    public CommonResult<Boolean> updateProdServiceAreas(@Valid @RequestBody ProdServiceAreasSaveReqVO updateReqVO) {
        prodServiceAreasService.updateProdServiceAreas(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除服务区域")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:prod-service-areas:delete')")
    public CommonResult<Boolean> deleteProdServiceAreas(@RequestParam("id") Long id) {
        prodServiceAreasService.deleteProdServiceAreas(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得服务区域")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:prod-service-areas:query')")
    public CommonResult<ProdServiceAreasRespVO> getProdServiceAreas(@RequestParam("id") Long id) {
        ProdServiceAreasDO prodServiceAreas = prodServiceAreasService.getProdServiceAreas(id);
        return success(BeanUtils.toBean(prodServiceAreas, ProdServiceAreasRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得服务区域分页")
    @PreAuthorize("@ss.hasPermission('tz:prod-service-areas:query')")
    public CommonResult<PageResult<ProdServiceAreasRespVO>> getProdServiceAreasPage(@Valid ProdServiceAreasPageReqVO pageReqVO) {
        PageResult<ProdServiceAreasDO> pageResult = prodServiceAreasService.getProdServiceAreasPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProdServiceAreasRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出服务区域 Excel")
    @PreAuthorize("@ss.hasPermission('tz:prod-service-areas:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProdServiceAreasExcel(@Valid ProdServiceAreasPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProdServiceAreasDO> list = prodServiceAreasService.getProdServiceAreasPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "服务区域.xls", "数据", ProdServiceAreasRespVO.class,
                        BeanUtils.toBean(list, ProdServiceAreasRespVO.class));
    }

}