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

import com.zt.plat.module.base.controller.admin.templtp.vo.TmplInscBsnRelPageReqVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TmplInscBsnRelRespVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TmplInscBsnRelSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.tmpltp.TmplInscBsnRelDO;
import com.zt.plat.module.base.service.tmpltp.TmplInscBsnRelService;
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.*;


@Tag(name = "管理后台 - 模板实例与业务中间")
@RestController
@RequestMapping("/base/tmpl-insc-bsn-rel")
@Validated
public class TmplInscBsnRelController implements BusinessControllerMarker {


    @Resource
    private TmplInscBsnRelService tmplInscBsnRelService;

    @PostMapping("/create")
    @Operation(summary = "创建模板实例与业务中间")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-insc-bsn-rel:create')")
    public CommonResult<TmplInscBsnRelRespVO> createTmplInscBsnRel(@Valid @RequestBody TmplInscBsnRelSaveReqVO createReqVO) {
        return success(tmplInscBsnRelService.createTmplInscBsnRel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模板实例与业务中间")
    // @PreAuthorize("@ss.hasPermission('base:tmpl-insc-bsn-rel:update')")
    public CommonResult<Boolean> updateTmplInscBsnRel(@Valid @RequestBody TmplInscBsnRelSaveReqVO updateReqVO) {
        tmplInscBsnRelService.updateTmplInscBsnRel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模板实例与业务中间")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('base:tmpl-insc-bsn-rel:delete')")
    public CommonResult<Boolean> deleteTmplInscBsnRel(@RequestParam("id") Long id) {
        tmplInscBsnRelService.deleteTmplInscBsnRel(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模板实例与业务中间")
    // @PreAuthorize("@ss.hasPermission('base:tmpl-insc-bsn-rel:delete')")
    public CommonResult<Boolean> deleteTmplInscBsnRelList(@RequestBody BatchDeleteReqVO req) {
        tmplInscBsnRelService.deleteTmplInscBsnRelListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模板实例与业务中间")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-insc-bsn-rel:query')")
    public CommonResult<TmplInscBsnRelRespVO> getTmplInscBsnRel(@RequestParam("id") Long id) {
        return success(tmplInscBsnRelService.getTmplInscBsnRel(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模板实例与业务中间分页")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-insc-bsn-rel:query')")
    public CommonResult<PageResult<TmplInscBsnRelRespVO>> getTmplInscBsnRelPage(@Valid TmplInscBsnRelPageReqVO pageReqVO) {
        PageResult<TmplInscBsnRelDO> pageResult = tmplInscBsnRelService.getTmplInscBsnRelPage(pageReqVO);
        PageResult<TmplInscBsnRelRespVO> tmplInscBsnRelRespVOPageResult = BeanUtils.toBean(pageResult, TmplInscBsnRelRespVO.class);
        tmplInscBsnRelRespVOPageResult.getList().forEach(tmplInscBsnRelRespVO -> {
            if (tmplInscBsnRelRespVO != null) {
                tmplInscBsnRelService.getTmplInscBsnRelDetails(tmplInscBsnRelRespVO);
            }
        });
        return success(tmplInscBsnRelRespVOPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模板实例与业务中间 Excel")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-insc-bsn-rel:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTmplInscBsnRelExcel(@Valid TmplInscBsnRelPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TmplInscBsnRelDO> list = tmplInscBsnRelService.getTmplInscBsnRelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模板实例与业务中间.xls", "数据", TmplInscBsnRelRespVO.class,
                BeanUtils.toBean(list, TmplInscBsnRelRespVO.class));
    }


    @GetMapping("/value-info")
    @Operation(summary = "根据合Id和实例Id获取字段和条款")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-insc-bsn-rel:query')")
    public CommonResult<Map<String, Object>> valueInfo(@RequestParam("cttId") @Valid @NotEmpty(message = "合同Id不能为空") String cttId, @RequestParam("inscId") @Valid @NotEmpty(message = "模版实例id不能为空") String inscId) {
        return success(tmplInscBsnRelService.valueInfo(cttId, inscId));
    }

    @GetMapping("/bsn-info")
    @Operation(summary = "根据合同id和实例id获取中间表")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-insc-bsn-rel:query')")
    public CommonResult<TmplInscBsnRelRespVO> bseInfo(@RequestParam("cttId") @Valid @NotEmpty(message = "合同Id不能为空") String cttId, @RequestParam("inscId") @Valid @NotEmpty(message = "模版实例id不能为空") String inscId) {
        return success(tmplInscBsnRelService.bseInfo(cttId, inscId));
    }

    @GetMapping("/bsn-info-id")
    @Operation(summary = "根据中间表id获取对应字段和条款")
    //@PreAuthorize("@ss.hasPermission('base:tmpl-insc-bsn-rel:query')")
    public CommonResult<Map<String, Object> > bseInfoById(String id) {
        return success(tmplInscBsnRelService.bseInfoById(id));
    }

}
