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.ProdAdditionalFeeDatesDO;
import com.tashow.cloud.product.service.ProdAdditionalFeeDatesService;
import com.tashow.cloud.productapi.api.product.vo.prodadditionalfeedates.ProdAdditionalFeeDatesPageReqVO;
import com.tashow.cloud.productapi.api.product.vo.prodadditionalfeedates.ProdAdditionalFeeDatesRespVO;
import com.tashow.cloud.productapi.api.product.vo.prodadditionalfeedates.ProdAdditionalFeeDatesSaveReqVO;
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-dates")
@Validated
public class ProdAdditionalFeeDatesController {

    @Resource
    private ProdAdditionalFeeDatesService prodAdditionalFeeDatesService;

    @PostMapping("/create")
    @Operation(summary = "创建特殊日期附加费用规则")
    @PreAuthorize("@ss.hasPermission('tz:prod-additional-fee-dates:create')")
    public CommonResult<Long> createProdAdditionalFeeDates(@Valid @RequestBody ProdAdditionalFeeDatesSaveReqVO createReqVO) {
        return success(prodAdditionalFeeDatesService.createProdAdditionalFeeDates(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新特殊日期附加费用规则")
    @PreAuthorize("@ss.hasPermission('tz:prod-additional-fee-dates:update')")
    public CommonResult<Boolean> updateProdAdditionalFeeDates(@Valid @RequestBody ProdAdditionalFeeDatesSaveReqVO updateReqVO) {
        prodAdditionalFeeDatesService.updateProdAdditionalFeeDates(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得特殊日期附加费用规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:prod-additional-fee-dates:query')")
    public CommonResult<ProdAdditionalFeeDatesRespVO> getProdAdditionalFeeDates(@RequestParam("id") Long id) {
        ProdAdditionalFeeDatesDO prodAdditionalFeeDates = prodAdditionalFeeDatesService.getProdAdditionalFeeDates(id);
        return success(BeanUtils.toBean(prodAdditionalFeeDates, ProdAdditionalFeeDatesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得特殊日期附加费用规则分页")
    @PreAuthorize("@ss.hasPermission('tz:prod-additional-fee-dates:query')")
    public CommonResult<PageResult<ProdAdditionalFeeDatesRespVO>> getProdAdditionalFeeDatesPage(@Valid ProdAdditionalFeeDatesPageReqVO pageReqVO) {
        PageResult<ProdAdditionalFeeDatesDO> pageResult = prodAdditionalFeeDatesService.getProdAdditionalFeeDatesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProdAdditionalFeeDatesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出特殊日期附加费用规则 Excel")
    @PreAuthorize("@ss.hasPermission('tz:prod-additional-fee-dates:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProdAdditionalFeeDatesExcel(@Valid ProdAdditionalFeeDatesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProdAdditionalFeeDatesDO> list = prodAdditionalFeeDatesService.getProdAdditionalFeeDatesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "特殊日期附加费用规则.xls", "数据", ProdAdditionalFeeDatesRespVO.class,
                        BeanUtils.toBean(list, ProdAdditionalFeeDatesRespVO.class));
    }

}