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.SkuServiceTransportDO;
import com.tashow.cloud.product.service.SkuServiceTransportService;
import com.tashow.cloud.productapi.api.product.vo.skuservicetransport.SkuServiceTransportPageReqVO;
import com.tashow.cloud.productapi.api.product.vo.skuservicetransport.SkuServiceTransportRespVO;
import com.tashow.cloud.productapi.api.product.vo.skuservicetransport.SkuServiceTransportSaveReqVO;
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/sku-service-transport")
@Validated
public class SkuServiceTransportController {

    @Resource
    private SkuServiceTransportService skuServiceTransportService;

    @PostMapping("/create")
    @Operation(summary = "创建服务遗体运输")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-transport:create')")
    public CommonResult<Long> createSkuServiceTransport(@Valid @RequestBody SkuServiceTransportSaveReqVO createReqVO) {
        return success(skuServiceTransportService.createSkuServiceTransport(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新服务遗体运输")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-transport:update')")
    public CommonResult<Boolean> updateSkuServiceTransport(@Valid @RequestBody SkuServiceTransportSaveReqVO updateReqVO) {
        skuServiceTransportService.updateSkuServiceTransport(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除服务遗体运输")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:sku-service-transport:delete')")
    public CommonResult<Boolean> deleteSkuServiceTransport(@RequestParam("id") Long id) {
        skuServiceTransportService.deleteSkuServiceTransport(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得服务遗体运输")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-transport:query')")
    public CommonResult<SkuServiceTransportRespVO> getSkuServiceTransport(@RequestParam("id") Long id) {
        SkuServiceTransportDO skuServiceTransport = skuServiceTransportService.getSkuServiceTransport(id);
        return success(BeanUtils.toBean(skuServiceTransport, SkuServiceTransportRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得服务遗体运输分页")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-transport:query')")
    public CommonResult<PageResult<SkuServiceTransportRespVO>> getSkuServiceTransportPage(@Valid SkuServiceTransportPageReqVO pageReqVO) {
        PageResult<SkuServiceTransportDO> pageResult = skuServiceTransportService.getSkuServiceTransportPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SkuServiceTransportRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出服务遗体运输 Excel")
    @PreAuthorize("@ss.hasPermission('tz:sku-service-transport:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSkuServiceTransportExcel(@Valid SkuServiceTransportPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SkuServiceTransportDO> list = skuServiceTransportService.getSkuServiceTransportPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "服务遗体运输.xls", "数据", SkuServiceTransportRespVO.class,
                        BeanUtils.toBean(list, SkuServiceTransportRespVO.class));
    }

}