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

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.*;
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.businessdepartmentindicator.vo.*;
import com.zt.plat.module.base.dal.dataobject.businessdepartmentindicator.BusinessDepartmentIndicatorDO;
import com.zt.plat.module.base.service.businessdepartmentindicator.BusinessDepartmentIndicatorService;

@Tag(name = "管理后台 - 部门持有指标")
@RestController
@RequestMapping("/base/business-department-indicator")
@Validated
public class BusinessDepartmentIndicatorController implements BusinessControllerMarker {


    @Resource
    private BusinessDepartmentIndicatorService businessDepartmentIndicatorService;

    @PostMapping("/create")
    @Operation(summary = "创建部门持有指标")
    @PreAuthorize("@ss.hasPermission('base:business-department-indicator:create')")
    public CommonResult<BusinessDepartmentIndicatorRespVO> createBusinessDepartmentIndicator(@Valid @RequestBody BusinessDepartmentIndicatorSaveReqVO createReqVO) {
        return success(businessDepartmentIndicatorService.createBusinessDepartmentIndicator(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新部门持有指标")
    @PreAuthorize("@ss.hasPermission('base:business-department-indicator:update')")
    public CommonResult<Boolean> updateBusinessDepartmentIndicator(@Valid @RequestBody BusinessDepartmentIndicatorSaveReqVO updateReqVO) {
        businessDepartmentIndicatorService.updateBusinessDepartmentIndicator(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除部门持有指标")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:business-department-indicator:delete')")
    public CommonResult<Boolean> deleteBusinessDepartmentIndicator(@RequestParam("id") Long id) {
        businessDepartmentIndicatorService.deleteBusinessDepartmentIndicator(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除部门持有指标")
                @PreAuthorize("@ss.hasPermission('base:business-department-indicator:delete')")
    public CommonResult<Boolean> deleteBusinessDepartmentIndicatorList(@RequestBody BatchDeleteReqVO req) {
        businessDepartmentIndicatorService.deleteBusinessDepartmentIndicatorListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得部门持有指标")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:business-department-indicator:query')")
    public CommonResult<BusinessDepartmentIndicatorRespVO> getBusinessDepartmentIndicator(@RequestParam("id") Long id) {
        BusinessDepartmentIndicatorDO businessDepartmentIndicator = businessDepartmentIndicatorService.getBusinessDepartmentIndicator(id);
        return success(BeanUtils.toBean(businessDepartmentIndicator, BusinessDepartmentIndicatorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得部门持有指标分页")
    @PreAuthorize("@ss.hasPermission('base:business-department-indicator:query')")
    public CommonResult<PageResult<BusinessDepartmentIndicatorRespVO>> getBusinessDepartmentIndicatorPage(@Valid BusinessDepartmentIndicatorPageReqVO pageReqVO) {
        PageResult<BusinessDepartmentIndicatorDO> pageResult = businessDepartmentIndicatorService.getBusinessDepartmentIndicatorPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessDepartmentIndicatorRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出部门持有指标 Excel")
    @PreAuthorize("@ss.hasPermission('base:business-department-indicator:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessDepartmentIndicatorExcel(@Valid BusinessDepartmentIndicatorPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessDepartmentIndicatorDO> list = businessDepartmentIndicatorService.getBusinessDepartmentIndicatorPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "部门持有指标.xls", "数据", BusinessDepartmentIndicatorRespVO.class,
                        BeanUtils.toBean(list, BusinessDepartmentIndicatorRespVO.class));
    }

}