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.ProdServiceOverAreaRulesDO;
import com.tashow.cloud.product.service.ProdServiceOverAreaRulesService;
import com.tashow.cloud.productapi.api.product.vo.prodserviceoverarearules.ProdServiceOverAreaRulesPageReqVO;
import com.tashow.cloud.productapi.api.product.vo.prodserviceoverarearules.ProdServiceOverAreaRulesRespVO;
import com.tashow.cloud.productapi.api.product.vo.prodserviceoverarearules.ProdServiceOverAreaRulesSaveReqVO;
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-service-over-area-rules")
@Validated
public class ProdServiceOverAreaRulesController {

    @Resource
    private ProdServiceOverAreaRulesService prodServiceOverAreaRulesService;

    @PostMapping("/create")
    @Operation(summary = "创建超区规则")
    @PreAuthorize("@ss.hasPermission('tz:prod-service-over-area-rules:create')")
    public CommonResult<Long> createProdServiceOverAreaRules(@Valid @RequestBody ProdServiceOverAreaRulesSaveReqVO createReqVO) {
        return success(prodServiceOverAreaRulesService.createProdServiceOverAreaRules(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新超区规则")
    @PreAuthorize("@ss.hasPermission('tz:prod-service-over-area-rules:update')")
    public CommonResult<Boolean> updateProdServiceOverAreaRules(@Valid @RequestBody ProdServiceOverAreaRulesSaveReqVO updateReqVO) {
        prodServiceOverAreaRulesService.updateProdServiceOverAreaRules(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除超区规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:prod-service-over-area-rules:delete')")
    public CommonResult<Boolean> deleteProdServiceOverAreaRules(@RequestParam("id") Long id) {
        prodServiceOverAreaRulesService.deleteProdServiceOverAreaRules(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得超区规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:prod-service-over-area-rules:query')")
    public CommonResult<ProdServiceOverAreaRulesRespVO> getProdServiceOverAreaRules(@RequestParam("id") Long id) {
        ProdServiceOverAreaRulesDO prodServiceOverAreaRules = prodServiceOverAreaRulesService.getProdServiceOverAreaRules(id);
        return success(BeanUtils.toBean(prodServiceOverAreaRules, ProdServiceOverAreaRulesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得超区规则分页")
    @PreAuthorize("@ss.hasPermission('tz:prod-service-over-area-rules:query')")
    public CommonResult<PageResult<ProdServiceOverAreaRulesRespVO>> getProdServiceOverAreaRulesPage(@Valid ProdServiceOverAreaRulesPageReqVO pageReqVO) {
        PageResult<ProdServiceOverAreaRulesDO> pageResult = prodServiceOverAreaRulesService.getProdServiceOverAreaRulesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProdServiceOverAreaRulesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出超区规则 Excel")
    @PreAuthorize("@ss.hasPermission('tz:prod-service-over-area-rules:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProdServiceOverAreaRulesExcel(@Valid ProdServiceOverAreaRulesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProdServiceOverAreaRulesDO> list = prodServiceOverAreaRulesService.getProdServiceOverAreaRulesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "超区规则.xls", "数据", ProdServiceOverAreaRulesRespVO.class,
                        BeanUtils.toBean(list, ProdServiceOverAreaRulesRespVO.class));
    }

}