package com.zt.plat.module.base.controller.admin.businessrule;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;

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.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.zt.plat.module.base.controller.admin.businessrule.vo.*;
import com.zt.plat.module.base.dal.dataobject.businessrule.BusinessRuleDO;
import com.zt.plat.module.base.service.businessrule.BusinessRuleService;

@Tag(name = "管理后台 - 业务规则")
@RestController
@RequestMapping("/base/business-rule")
@Validated
public class BusinessRuleController implements BusinessControllerMarker {


    @Resource
    private BusinessRuleService businessRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建业务规则")
    @PreAuthorize("@ss.hasPermission('base:business-rule:create')")
    public CommonResult<BusinessRuleRespVO> createBusinessRule(@Valid @RequestBody BusinessRuleSaveReqVO createReqVO) {
        return success(businessRuleService.createBusinessRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新业务规则")
    @PreAuthorize("@ss.hasPermission('base:business-rule:update')")
    public CommonResult<Boolean> updateBusinessRule(@Valid @RequestBody BusinessRuleSaveReqVO updateReqVO) {
        businessRuleService.updateBusinessRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除业务规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:business-rule:delete')")
    public CommonResult<Boolean> deleteBusinessRule(@RequestParam("id") Long id) {
        businessRuleService.deleteBusinessRule(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除业务规则")
                @PreAuthorize("@ss.hasPermission('base:business-rule:delete')")
    public CommonResult<Boolean> deleteBusinessRuleList(@RequestBody BatchDeleteReqVO req) {
        businessRuleService.deleteBusinessRuleListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得业务规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:business-rule:query')")
    public CommonResult<BusinessRuleRespVO> getBusinessRule(@RequestParam("id") Long id) {
        BusinessRuleDO businessRule = businessRuleService.getBusinessRule(id);
        return success(BeanUtils.toBean(businessRule, BusinessRuleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得业务规则分页")
    @PreAuthorize("@ss.hasPermission('base:business-rule:query')")
    public CommonResult<PageResult<BusinessRuleRespVO>> getBusinessRulePage(@Valid BusinessRulePageReqVO pageReqVO) {
        PageResult<BusinessRuleDO> pageResult = businessRuleService.getBusinessRulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessRuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出业务规则 Excel")
    @PreAuthorize("@ss.hasPermission('base:business-rule:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessRuleExcel(@Valid BusinessRulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessRuleDO> list = businessRuleService.getBusinessRulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "业务规则.xls", "数据", BusinessRuleRespVO.class,
                        BeanUtils.toBean(list, BusinessRuleRespVO.class));
    }

}