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

import com.zt.plat.module.base.dal.dataobject.tmpltp.TmplInscItmBsnDO;
import com.zt.plat.module.base.service.tmpltp.TmplInscItmBsnService;
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.constraints.*;
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.templtp.vo.*;

@Tag(name = "管理后台 - 业务实例条款值")
@RestController
@RequestMapping("/base/tmpl-insc-itm-bsn")
@Validated
public class TmplInscItmBsnController implements BusinessControllerMarker {


    @Resource
    private TmplInscItmBsnService tmplInscItmBsnService;

    @PostMapping("/create")
    @Operation(summary = "创建业务实例条款值")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-insc-itm-bsn:create')")
    public CommonResult<TmplInscItmBsnRespVO> createTmplInscItmBsn(@Valid @RequestBody TmplInscItmBsnSaveReqVO createReqVO) {
        return success(tmplInscItmBsnService.createTmplInscItmBsn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新业务实例条款值")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-insc-itm-bsn:update')")
    public CommonResult<Boolean> updateTmplInscItmBsn(@Valid @RequestBody TmplInscItmBsnSaveReqVO updateReqVO) {
        tmplInscItmBsnService.updateTmplInscItmBsn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除业务实例条款值")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('base:tmpl-insc-itm-bsn:delete')")
    public CommonResult<Boolean> deleteTmplInscItmBsn(@RequestParam("id") String id) {
        tmplInscItmBsnService.deleteTmplInscItmBsn(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除业务实例条款值")
    // @PreAuthorize("@ss.hasPermission('base:tmpl-insc-itm-bsn:delete')")
    public CommonResult<Boolean> deleteTmplInscItmBsnList(@RequestBody BatchDeleteReqVO req) {
        tmplInscItmBsnService.deleteTmplInscItmBsnListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得业务实例条款值")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:tmpl-insc-itm-bsn:query')")
    public CommonResult<TmplInscItmBsnRespVO> getTmplInscItmBsn(@RequestParam("id") String id) {
        TmplInscItmBsnDO tmplInscItmBsn = tmplInscItmBsnService.getTmplInscItmBsn(id);
        return success(BeanUtils.toBean(tmplInscItmBsn, TmplInscItmBsnRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得业务实例条款值分页")
    @PreAuthorize("@ss.hasPermission('base:tmpl-insc-itm-bsn:query')")
    public CommonResult<PageResult<TmplInscItmBsnRespVO>> getTmplInscItmBsnPage(@Valid TmplInscItmBsnPageReqVO pageReqVO) {
        PageResult<TmplInscItmBsnDO> pageResult = tmplInscItmBsnService.getTmplInscItmBsnPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TmplInscItmBsnRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出业务实例条款值 Excel")
    // @PreAuthorize("@ss.hasPermission('base:tmpl-insc-itm-bsn:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTmplInscItmBsnExcel(@Valid TmplInscItmBsnPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TmplInscItmBsnDO> list = tmplInscItmBsnService.getTmplInscItmBsnPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "业务实例条款值.xls", "数据", TmplInscItmBsnRespVO.class,
                        BeanUtils.toBean(list, TmplInscItmBsnRespVO.class));
    }

}
