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.ProdReservationConfigDO;
import com.tashow.cloud.product.service.ProdReservationConfigService;
import com.tashow.cloud.productapi.api.product.vo.prodreservationconfig.ProdReservationConfigPageReqVO;
import com.tashow.cloud.productapi.api.product.vo.prodreservationconfig.ProdReservationConfigRespVO;
import com.tashow.cloud.productapi.api.product.vo.prodreservationconfig.ProdReservationConfigSaveReqVO;
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-reservation-config")
@Validated
public class ProdReservationConfigController {

    @Resource
    private ProdReservationConfigService prodReservationConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建商品预约配置")
    @PreAuthorize("@ss.hasPermission('tz:prod-reservation-config:create')")
    public CommonResult<Long> createProdReservationConfig(@Valid @RequestBody ProdReservationConfigSaveReqVO createReqVO) {
        return success(prodReservationConfigService.createProdReservationConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品预约配置")
    @PreAuthorize("@ss.hasPermission('tz:prod-reservation-config:update')")
    public CommonResult<Boolean> updateProdReservationConfig(@Valid @RequestBody ProdReservationConfigSaveReqVO updateReqVO) {
        prodReservationConfigService.updateProdReservationConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品预约配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:prod-reservation-config:delete')")
    public CommonResult<Boolean> deleteProdReservationConfig(@RequestParam("id") Long id) {
        prodReservationConfigService.deleteProdReservationConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品预约配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:prod-reservation-config:query')")
    public CommonResult<ProdReservationConfigRespVO> getProdReservationConfig(@RequestParam("id") Long id) {
        ProdReservationConfigDO prodReservationConfig = prodReservationConfigService.getProdReservationConfig(id);
        return success(BeanUtils.toBean(prodReservationConfig, ProdReservationConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品预约配置分页")
    @PreAuthorize("@ss.hasPermission('tz:prod-reservation-config:query')")
    public CommonResult<PageResult<ProdReservationConfigRespVO>> getProdReservationConfigPage(@Valid ProdReservationConfigPageReqVO pageReqVO) {
        PageResult<ProdReservationConfigDO> pageResult = prodReservationConfigService.getProdReservationConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProdReservationConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品预约配置 Excel")
    @PreAuthorize("@ss.hasPermission('tz:prod-reservation-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProdReservationConfigExcel(@Valid ProdReservationConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProdReservationConfigDO> list = prodReservationConfigService.getProdReservationConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品预约配置.xls", "数据", ProdReservationConfigRespVO.class,
                        BeanUtils.toBean(list, ProdReservationConfigRespVO.class));
    }

}