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.ProdEmergencyResponseDO;
import com.tashow.cloud.product.service.ProdEmergencyResponseService;
import com.tashow.cloud.productapi.api.product.vo.prodemergencyresponse.ProdEmergencyResponsePageReqVO;
import com.tashow.cloud.productapi.api.product.vo.prodemergencyresponse.ProdEmergencyResponseRespVO;
import com.tashow.cloud.productapi.api.product.vo.prodemergencyresponse.ProdEmergencyResponseSaveReqVO;
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-emergency-response")
@Validated
public class ProdEmergencyResponseController {

    @Resource
    private ProdEmergencyResponseService prodEmergencyResponseService;

    @PostMapping("/create")
    @Operation(summary = "创建商品紧急响应服务设置")
    @PreAuthorize("@ss.hasPermission('tz:prod-emergency-response:create')")
    public CommonResult<Long> createProdEmergencyResponse(@Valid @RequestBody ProdEmergencyResponseSaveReqVO createReqVO) {
        return success(prodEmergencyResponseService.createProdEmergencyResponse(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品紧急响应服务设置")
    @PreAuthorize("@ss.hasPermission('tz:prod-emergency-response:update')")
    public CommonResult<Boolean> updateProdEmergencyResponse(@Valid @RequestBody ProdEmergencyResponseSaveReqVO updateReqVO) {
        prodEmergencyResponseService.updateProdEmergencyResponse(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得商品紧急响应服务设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:prod-emergency-response:query')")
    public CommonResult<ProdEmergencyResponseRespVO> getProdEmergencyResponse(@RequestParam("id") Long id) {
        ProdEmergencyResponseDO prodEmergencyResponse = prodEmergencyResponseService.getProdEmergencyResponse(id);
        return success(BeanUtils.toBean(prodEmergencyResponse, ProdEmergencyResponseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品紧急响应服务设置分页")
    @PreAuthorize("@ss.hasPermission('tz:prod-emergency-response:query')")
    public CommonResult<PageResult<ProdEmergencyResponseRespVO>> getProdEmergencyResponsePage(@Valid ProdEmergencyResponsePageReqVO pageReqVO) {
        PageResult<ProdEmergencyResponseDO> pageResult = prodEmergencyResponseService.getProdEmergencyResponsePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProdEmergencyResponseRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品紧急响应服务设置 Excel")
    @PreAuthorize("@ss.hasPermission('tz:prod-emergency-response:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProdEmergencyResponseExcel(@Valid ProdEmergencyResponsePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProdEmergencyResponseDO> list = prodEmergencyResponseService.getProdEmergencyResponsePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品紧急响应服务设置.xls", "数据", ProdEmergencyResponseRespVO.class,
                        BeanUtils.toBean(list, ProdEmergencyResponseRespVO.class));
    }

}