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.ProdAdditionalFeePeriodsDO;
import com.tashow.cloud.product.service.ProdAdditionalFeePeriodsService;
import com.tashow.cloud.productapi.api.product.vo.prodadditionalfeeperiods.ProdAdditionalFeePeriodsPageReqVO;
import com.tashow.cloud.productapi.api.product.vo.prodadditionalfeeperiods.ProdAdditionalFeePeriodsRespVO;
import com.tashow.cloud.productapi.api.product.vo.prodadditionalfeeperiods.ProdAdditionalFeePeriodsSaveReqVO;
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-additional-fee-periods")
@Validated
public class ProdAdditionalFeePeriodsController {

    @Resource
    private ProdAdditionalFeePeriodsService prodAdditionalFeePeriodsService;

    @PostMapping("/create")
    @Operation(summary = "创建特殊时段附加费用规则")
    @PreAuthorize("@ss.hasPermission('tz:prod-additional-fee-periods:create')")
    public CommonResult<Long> createProdAdditionalFeePeriods(@Valid @RequestBody ProdAdditionalFeePeriodsSaveReqVO createReqVO) {
        return success(prodAdditionalFeePeriodsService.createProdAdditionalFeePeriods(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新特殊时段附加费用规则")
    @PreAuthorize("@ss.hasPermission('tz:prod-additional-fee-periods:update')")
    public CommonResult<Boolean> updateProdAdditionalFeePeriods(@Valid @RequestBody ProdAdditionalFeePeriodsSaveReqVO updateReqVO) {
        prodAdditionalFeePeriodsService.updateProdAdditionalFeePeriods(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除特殊时段附加费用规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:prod-additional-fee-periods:delete')")
    public CommonResult<Boolean> deleteProdAdditionalFeePeriods(@RequestParam("id") Long id) {
        prodAdditionalFeePeriodsService.deleteProdAdditionalFeePeriods(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得特殊时段附加费用规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:prod-additional-fee-periods:query')")
    public CommonResult<ProdAdditionalFeePeriodsRespVO> getProdAdditionalFeePeriods(@RequestParam("id") Long id) {
        ProdAdditionalFeePeriodsDO prodAdditionalFeePeriods = prodAdditionalFeePeriodsService.getProdAdditionalFeePeriods(id);
        return success(BeanUtils.toBean(prodAdditionalFeePeriods, ProdAdditionalFeePeriodsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得特殊时段附加费用规则分页")
    @PreAuthorize("@ss.hasPermission('tz:prod-additional-fee-periods:query')")
    public CommonResult<PageResult<ProdAdditionalFeePeriodsRespVO>> getProdAdditionalFeePeriodsPage(@Valid ProdAdditionalFeePeriodsPageReqVO pageReqVO) {
        PageResult<ProdAdditionalFeePeriodsDO> pageResult = prodAdditionalFeePeriodsService.getProdAdditionalFeePeriodsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProdAdditionalFeePeriodsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出特殊时段附加费用规则 Excel")
    @PreAuthorize("@ss.hasPermission('tz:prod-additional-fee-periods:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProdAdditionalFeePeriodsExcel(@Valid ProdAdditionalFeePeriodsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProdAdditionalFeePeriodsDO> list = prodAdditionalFeePeriodsService.getProdAdditionalFeePeriodsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "特殊时段附加费用规则.xls", "数据", ProdAdditionalFeePeriodsRespVO.class,
                        BeanUtils.toBean(list, ProdAdditionalFeePeriodsRespVO.class));
    }

}