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

import com.zt.plat.module.base.controller.admin.templtp.vo.TemplateInstanceItemPageReqVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TemplateInstanceItemRespVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TemplateInstanceItemSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.tmpltp.TemplateInstanceItemDO;
import com.zt.plat.module.base.service.tmpltp.TemplateInstanceItemService;
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/template-instance-item")
@Validated
public class TemplateInstanceItemController implements BusinessControllerMarker {


    @Resource
    private TemplateInstanceItemService templateInstanceItemService;

    @PostMapping("/create")
    @Operation(summary = "创建实例条款值")
    //@PreAuthorize("@ss.hasPermission('base:template-instance-item:create')")
    public CommonResult<TemplateInstanceItemRespVO> createTemplateInstanceItem(@Valid @RequestBody TemplateInstanceItemSaveReqVO createReqVO) {
        return success(templateInstanceItemService.createTemplateInstanceItem(createReqVO));
    }

    @PostMapping("/create-batch")
    @Operation(summary = "批量创建实例条款值")
    //@PreAuthorize("@ss.hasPermission('base:template-instance-item:create')")
    public CommonResult<List<TemplateInstanceItemRespVO>> createBatchTemplateInstanceItem(@Valid @RequestBody List<TemplateInstanceItemSaveReqVO> createReqVOS) {
        return success(templateInstanceItemService.createBatchTemplateInstanceItem(createReqVOS));
    }

    @PutMapping("/update")
    @Operation(summary = "更新实例条款值")
    //@PreAuthorize("@ss.hasPermission('base:template-instance-item:update')")
    public CommonResult<Boolean> updateTemplateInstanceItem(@Valid @RequestBody TemplateInstanceItemSaveReqVO updateReqVO) {
        templateInstanceItemService.updateTemplateInstanceItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除实例条款值")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('base:template-instance-item:delete')")
    public CommonResult<Boolean> deleteTemplateInstanceItem(@RequestParam("id") String id) {
        templateInstanceItemService.deleteTemplateInstanceItem(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除实例条款值")
    //@PreAuthorize("@ss.hasPermission('base:template-instance-item:delete')")
    public CommonResult<Boolean> deleteTemplateInstanceItemList(@RequestBody BatchDeleteReqVO req) {
        List<String> ids = req.getIds().stream().map(String::valueOf).toList();
        templateInstanceItemService.deleteTemplateInstanceItemListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得实例条款值")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:template-instance-item:query')")
    public CommonResult<TemplateInstanceItemRespVO> getTemplateInstanceItem(@RequestParam("id") String id) {
        TemplateInstanceItemDO templateInstanceItem = templateInstanceItemService.getTemplateInstanceItem(id);
        return success(BeanUtils.toBean(templateInstanceItem, TemplateInstanceItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得实例条款值分页")
    @PreAuthorize("@ss.hasPermission('base:template-instance-item:query')")
    public CommonResult<PageResult<TemplateInstanceItemRespVO>> getTemplateInstanceItemPage(@Valid TemplateInstanceItemPageReqVO pageReqVO) {
        PageResult<TemplateInstanceItemDO> pageResult = templateInstanceItemService.getTemplateInstanceItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TemplateInstanceItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出实例条款值 Excel")
    @PreAuthorize("@ss.hasPermission('base:template-instance-item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTemplateInstanceItemExcel(@Valid TemplateInstanceItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TemplateInstanceItemDO> list = templateInstanceItemService.getTemplateInstanceItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "实例条款值.xls", "数据", TemplateInstanceItemRespVO.class,
                        BeanUtils.toBean(list, TemplateInstanceItemRespVO.class));
    }

}
