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

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.base.dal.dataobject.tmpltp.TmplInscDatBsnDO;
import com.zt.plat.module.base.service.tmpltp.TmplInscDatBsnService;
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-dat-bsn")
@Validated
public class TmplInscDatBsnController implements BusinessControllerMarker {


    @Resource
    private TmplInscDatBsnService tmplInscDatBsnService;

    @PostMapping("/create")
    @Operation(summary = "创建业务实例字段值")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-insc-dat-bsn:create')")
    public CommonResult<TmplInscDatBsnRespVO> createTmplInscDatBsn(@Valid @RequestBody TmplInscDatBsnSaveReqVO createReqVO) {
        return success(tmplInscDatBsnService.createTmplInscDatBsn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新业务实例字段值")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-insc-dat-bsn:update')")
    public CommonResult<Boolean> updateTmplInscDatBsn(@Valid @RequestBody TmplInscDatBsnSaveReqVO updateReqVO) {
        tmplInscDatBsnService.updateTmplInscDatBsn(updateReqVO);
        return success(true);
    }

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

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除业务实例字段值")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-insc-dat-bsn:delete')")
    public CommonResult<Boolean> deleteTmplInscDatBsnList(@RequestBody BatchDeleteReqVO req) {
        tmplInscDatBsnService.deleteTmplInscDatBsnListByIds(req.getIds().stream().map(String::valueOf).toList());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得业务实例字段值")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:tmpl-insc-dat-bsn:query')")
    public CommonResult<TmplInscDatBsnRespVO> getTmplInscDatBsn(@RequestParam("id") String id) {
        TmplInscDatBsnDO tmplInscDatBsn = tmplInscDatBsnService.getTmplInscDatBsn(id);
        TmplInscDatBsnRespVO tmplInscDatBsnRespVO = BeanUtils.toBean(tmplInscDatBsn, TmplInscDatBsnRespVO.class);
        tmplInscDatBsnRespVO.setFldVal(CollUtil.toList(tmplInscDatBsn.getFldVal().split(",")));
        return success(tmplInscDatBsnRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得业务实例字段值分页")
    @PreAuthorize("@ss.hasPermission('base:tmpl-insc-dat-bsn:query')")
    public CommonResult<PageResult<TmplInscDatBsnRespVO>> getTmplInscDatBsnPage(@Valid TmplInscDatBsnPageReqVO pageReqVO) {
        PageResult<TmplInscDatBsnDO> pageResult = tmplInscDatBsnService.getTmplInscDatBsnPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TmplInscDatBsnRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出业务实例字段值 Excel")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-insc-dat-bsn:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTmplInscDatBsnExcel(@Valid TmplInscDatBsnPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TmplInscDatBsnDO> list = tmplInscDatBsnService.getTmplInscDatBsnPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "业务实例字段值.xls", "数据", TmplInscDatBsnRespVO.class,
                        BeanUtils.toBean(list, TmplInscDatBsnRespVO.class));
    }

}
