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

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.*;

import com.zt.plat.module.base.controller.admin.businessdimension.vo.*;
import com.zt.plat.module.base.dal.dataobject.businessdimension.BusinessDimensionDO;
import com.zt.plat.module.base.service.businessdimension.BusinessDimensionService;

@Tag(name = "管理后台 - 经营指标维度")
@RestController
@RequestMapping("/base/business-dimension")
@Validated
public class BusinessDimensionController implements BusinessControllerMarker {


    @Resource
    private BusinessDimensionService businessDimensionService;

    @PostMapping("/create")
    @Operation(summary = "创建经营指标维度")
    @PreAuthorize("@ss.hasPermission('base:business-dimension:create')")
    public CommonResult<BusinessDimensionRespVO> createBusinessDimension(@Valid @RequestBody BusinessDimensionSaveReqVO createReqVO) {
        return success(businessDimensionService.createBusinessDimension(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新经营指标维度")
    @PreAuthorize("@ss.hasPermission('base:business-dimension:update')")
    public CommonResult<Boolean> updateBusinessDimension(@Valid @RequestBody BusinessDimensionSaveReqVO updateReqVO) {
        businessDimensionService.updateBusinessDimension(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除经营指标维度")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:business-dimension:delete')")
    public CommonResult<Boolean> deleteBusinessDimension(@RequestParam("id") Long id) {
        businessDimensionService.deleteBusinessDimension(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除经营指标维度")
                @PreAuthorize("@ss.hasPermission('base:business-dimension:delete')")
    public CommonResult<Boolean> deleteBusinessDimensionList(@RequestBody BatchDeleteReqVO req) {
        businessDimensionService.deleteBusinessDimensionListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得经营指标维度")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:business-dimension:query')")
    public CommonResult<BusinessDimensionRespVO> getBusinessDimension(@RequestParam("id") Long id) {
        BusinessDimensionDO businessDimension = businessDimensionService.getBusinessDimension(id);
        return success(BeanUtils.toBean(businessDimension, BusinessDimensionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得经营指标维度分页")
    @PreAuthorize("@ss.hasPermission('base:business-dimension:query')")
    public CommonResult<PageResult<BusinessDimensionRespVO>> getBusinessDimensionPage(@Valid BusinessDimensionPageReqVO pageReqVO) {
        PageResult<BusinessDimensionDO> pageResult = businessDimensionService.getBusinessDimensionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessDimensionRespVO.class));
    }

    @GetMapping("/list-by-parent-id")
    @Operation(summary = "获得经营指标维度列表通过父级ID")
    @Parameter(name = "parentId", description = "父级ID", example = "0")
    @PreAuthorize("@ss.hasPermission('base:business-dimension:query')")
    public CommonResult<List<BusinessDimensionRespVO>> getBusinessDimensionListByParentId(@RequestParam(value = "parentId", required = false) Long parentId,
                                                                                          @RequestParam(value = "level", required = false) Integer level) {
        List<BusinessDimensionDO> list = businessDimensionService.getBusinessDimensionListByParentId(parentId, level);
        return success(BeanUtils.toBean(list, BusinessDimensionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出经营指标维度 Excel")
    @PreAuthorize("@ss.hasPermission('base:business-dimension:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessDimensionExcel(@Valid BusinessDimensionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessDimensionDO> list = businessDimensionService.getBusinessDimensionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "经营指标维度.xls", "数据", BusinessDimensionRespVO.class,
                        BeanUtils.toBean(list, BusinessDimensionRespVO.class));
    }

}