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

import com.zt.plat.module.base.controller.admin.templtp.vo.TemplateInstanceDataPageReqVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TemplateInstanceDataRespVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TemplateInstanceDataSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.tmpltp.TemplateInstanceDataDO;
import com.zt.plat.module.base.service.tmpltp.TemplateInstanceDataService;
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-data")
@Validated
public class TemplateInstanceDataController implements BusinessControllerMarker {


    @Resource
    private TemplateInstanceDataService templateInstanceDataService;

    @PostMapping("/create")
    @Operation(summary = "创建实例字段值")
    //@PreAuthorize("@ss.hasPermission('base:template-instance-data:create')")
    public CommonResult<TemplateInstanceDataRespVO> createTemplateInstanceData(@Valid @RequestBody TemplateInstanceDataSaveReqVO createReqVO) {
        return success(templateInstanceDataService.createTemplateInstanceData(createReqVO));
    }

    @PostMapping("/create-batch")
    @Operation(summary = "批量创建实例字段值")
    //@PreAuthorize("@ss.hasPermission('base:template-instance-data:create')")
    public CommonResult<List<TemplateInstanceDataRespVO>> createBatchTemplateInstanceData(@Valid @RequestBody List<TemplateInstanceDataSaveReqVO> createReqVOS) {
        return success(templateInstanceDataService.createBatchTemplateInstanceData(createReqVOS));
    }


    @PutMapping("/update")
    @Operation(summary = "更新实例字段值")
    //@PreAuthorize("@ss.hasPermission('base:template-instance-data:update')")
    public CommonResult<Boolean> updateTemplateInstanceData(@Valid @RequestBody TemplateInstanceDataSaveReqVO updateReqVO) {
        templateInstanceDataService.updateTemplateInstanceData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除实例字段值")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('base:template-instance-data:delete')")
    public CommonResult<Boolean> deleteTemplateInstanceData(@RequestParam("id") Long id) {
        templateInstanceDataService.deleteTemplateInstanceData(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除实例字段值")
    //@PreAuthorize("@ss.hasPermission('base:template-instance-data:delete')")
    public CommonResult<Boolean> deleteTemplateInstanceDataList(@RequestBody BatchDeleteReqVO req) {
        templateInstanceDataService.deleteTemplateInstanceDataListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得实例字段值")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:template-instance-data:query')")
    public CommonResult<TemplateInstanceDataRespVO> getTemplateInstanceData(@RequestParam("id") Long id) {
        TemplateInstanceDataDO templateInstanceData = templateInstanceDataService.getTemplateInstanceData(id);
        return success(BeanUtils.toBean(templateInstanceData, TemplateInstanceDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得实例字段值分页")
    @PreAuthorize("@ss.hasPermission('base:template-instance-data:query')")
    public CommonResult<PageResult<TemplateInstanceDataRespVO>> getTemplateInstanceDataPage(@Valid TemplateInstanceDataPageReqVO pageReqVO) {
        PageResult<TemplateInstanceDataDO> pageResult = templateInstanceDataService.getTemplateInstanceDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TemplateInstanceDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出实例字段值 Excel")
    @PreAuthorize("@ss.hasPermission('base:template-instance-data:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTemplateInstanceDataExcel(@Valid TemplateInstanceDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TemplateInstanceDataDO> list = templateInstanceDataService.getTemplateInstanceDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "实例字段值.xls", "数据", TemplateInstanceDataRespVO.class,
                        BeanUtils.toBean(list, TemplateInstanceDataRespVO.class));
    }
}
