package com.tashow.cloud.product.controller;

import com.tashow.cloud.product.dto.ProdEmergencyResponseIntervalsDO;
import com.tashow.cloud.product.service.ProdEmergencyResponseIntervalsService;
import com.tashow.cloud.product.vo.prodemergencyresponseintervals.ProdEmergencyResponseIntervalsPageReqVO;
import com.tashow.cloud.product.vo.prodemergencyresponseintervals.ProdEmergencyResponseIntervalsRespVO;
import com.tashow.cloud.product.vo.prodemergencyresponseintervals.ProdEmergencyResponseIntervalsSaveReqVO;
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-emergency-response-intervals")
@Validated
public class ProdEmergencyResponseIntervalsController {

    @Resource
    private ProdEmergencyResponseIntervalsService prodEmergencyResponseIntervalsService;

    @PostMapping("/create")
    @Operation(summary = "创建紧急响应时间区间设置")
    @PreAuthorize("@ss.hasPermission('tz:prod-emergency-response-intervals:create')")
    public CommonResult<Long> createProdEmergencyResponseIntervals(@Valid @RequestBody ProdEmergencyResponseIntervalsSaveReqVO createReqVO) {
        return success(prodEmergencyResponseIntervalsService.createProdEmergencyResponseIntervals(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新紧急响应时间区间设置")
    @PreAuthorize("@ss.hasPermission('tz:prod-emergency-response-intervals:update')")
    public CommonResult<Boolean> updateProdEmergencyResponseIntervals(@Valid @RequestBody ProdEmergencyResponseIntervalsSaveReqVO updateReqVO) {
        prodEmergencyResponseIntervalsService.updateProdEmergencyResponseIntervals(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除紧急响应时间区间设置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:prod-emergency-response-intervals:delete')")
    public CommonResult<Boolean> deleteProdEmergencyResponseIntervals(@RequestParam("id") Long id) {
        prodEmergencyResponseIntervalsService.deleteProdEmergencyResponseIntervals(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得紧急响应时间区间设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:prod-emergency-response-intervals:query')")
    public CommonResult<ProdEmergencyResponseIntervalsRespVO> getProdEmergencyResponseIntervals(@RequestParam("id") Long id) {
        ProdEmergencyResponseIntervalsDO prodEmergencyResponseIntervals = prodEmergencyResponseIntervalsService.getProdEmergencyResponseIntervals(id);
        return success(BeanUtils.toBean(prodEmergencyResponseIntervals, ProdEmergencyResponseIntervalsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得紧急响应时间区间设置分页")
    @PreAuthorize("@ss.hasPermission('tz:prod-emergency-response-intervals:query')")
    public CommonResult<PageResult<ProdEmergencyResponseIntervalsRespVO>> getProdEmergencyResponseIntervalsPage(@Valid ProdEmergencyResponseIntervalsPageReqVO pageReqVO) {
        PageResult<ProdEmergencyResponseIntervalsDO> pageResult = prodEmergencyResponseIntervalsService.getProdEmergencyResponseIntervalsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProdEmergencyResponseIntervalsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出紧急响应时间区间设置 Excel")
    @PreAuthorize("@ss.hasPermission('tz:prod-emergency-response-intervals:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProdEmergencyResponseIntervalsExcel(@Valid ProdEmergencyResponseIntervalsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProdEmergencyResponseIntervalsDO> list = prodEmergencyResponseIntervalsService.getProdEmergencyResponseIntervalsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "紧急响应时间区间设置.xls", "数据", ProdEmergencyResponseIntervalsRespVO.class,
                        BeanUtils.toBean(list, ProdEmergencyResponseIntervalsRespVO.class));
    }

}