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

import com.zt.plat.module.base.controller.admin.templtp.vo.DepartmentInstanceRelativityPageReqVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.DepartmentInstanceRelativityRespVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.DepartmentInstanceRelativitySaveReqVO;
import com.zt.plat.module.base.dal.dataobject.tmpltp.DepartmentInstanceRelativityDO;
import com.zt.plat.module.base.service.tmpltp.DepartmentInstanceRelativityService;
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 com.zt.plat.framework.business.annotation.FileUploadController;
import com.zt.plat.framework.business.controller.AbstractFileUploadController;

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/department-instance-relativity")
@Validated
@FileUploadController(source = "base.departmentinstancerelativity")
public class DepartmentInstanceRelativityController extends AbstractFileUploadController implements BusinessControllerMarker{

    static {
        FileUploadController annotation = DepartmentInstanceRelativityController.class.getAnnotation(FileUploadController.class);
        if (annotation != null) {
        setFileUploadInfo(annotation);
        }
    }

    @Resource
    private DepartmentInstanceRelativityService departmentInstanceRelativityService;

    @PostMapping("/create")
    @Operation(summary = "创建部门与实例关联")
    //@PreAuthorize("@ss.hasPermission('base:department-instance-relativity:create')")
    public CommonResult<DepartmentInstanceRelativityRespVO> createDepartmentInstanceRelativity(@Valid @RequestBody DepartmentInstanceRelativitySaveReqVO createReqVO) {
        return success(departmentInstanceRelativityService.createDepartmentInstanceRelativity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新部门与实例关联")
    //@PreAuthorize("@ss.hasPermission('base:department-instance-relativity:update')")
    public CommonResult<Boolean> updateDepartmentInstanceRelativity(@Valid @RequestBody DepartmentInstanceRelativitySaveReqVO updateReqVO) {
        departmentInstanceRelativityService.updateDepartmentInstanceRelativity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除部门与实例关联")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('base:department-instance-relativity:delete')")
    public CommonResult<Boolean> deleteDepartmentInstanceRelativity(@RequestParam("id") String id) {
        departmentInstanceRelativityService.deleteDepartmentInstanceRelativity(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除部门与实例关联")
    //@PreAuthorize("@ss.hasPermission('base:department-instance-relativity:delete')")
    public CommonResult<Boolean> deleteDepartmentInstanceRelativityList(@RequestBody BatchDeleteReqVO req) {
        departmentInstanceRelativityService.deleteDepartmentInstanceRelativityListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得部门与实例关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:department-instance-relativity:query')")
    public CommonResult<DepartmentInstanceRelativityRespVO> getDepartmentInstanceRelativity(@RequestParam("id") String id) {
        DepartmentInstanceRelativityDO departmentInstanceRelativity = departmentInstanceRelativityService.getDepartmentInstanceRelativity(id);
        return success(BeanUtils.toBean(departmentInstanceRelativity, DepartmentInstanceRelativityRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得部门与实例关联分页")
    @PreAuthorize("@ss.hasPermission('base:department-instance-relativity:query')")
    public CommonResult<PageResult<DepartmentInstanceRelativityRespVO>> getDepartmentInstanceRelativityPage(@Valid DepartmentInstanceRelativityPageReqVO pageReqVO) {
        PageResult<DepartmentInstanceRelativityDO> pageResult = departmentInstanceRelativityService.getDepartmentInstanceRelativityPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DepartmentInstanceRelativityRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出部门与实例关联 Excel")
    @PreAuthorize("@ss.hasPermission('base:department-instance-relativity:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDepartmentInstanceRelativityExcel(@Valid DepartmentInstanceRelativityPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DepartmentInstanceRelativityDO> list = departmentInstanceRelativityService.getDepartmentInstanceRelativityPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "部门与实例关联.xls", "数据", DepartmentInstanceRelativityRespVO.class,
                        BeanUtils.toBean(list, DepartmentInstanceRelativityRespVO.class));
    }

}
