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

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.businessdictionarytype.vo.*;
import com.zt.plat.module.base.dal.dataobject.businessdictionarytype.BusinessDictionaryTypeDO;
import com.zt.plat.module.base.dal.dataobject.businessdictionarytype.BusinessDictionaryDataDO;
import com.zt.plat.module.base.service.businessdictionarytype.BusinessDictionaryTypeService;

@Tag(name = "管理后台 - 业务字典类型")
@RestController
@RequestMapping("/base/business-dictionary-type")
@Validated
public class BusinessDictionaryTypeController implements BusinessControllerMarker {


    @Resource
    private BusinessDictionaryTypeService businessDictionaryTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建业务字典类型")
    @PreAuthorize("@ss.hasPermission('base:business-dictionary-type:create')")
    public CommonResult<BusinessDictionaryTypeRespVO> createBusinessDictionaryType(@Valid @RequestBody BusinessDictionaryTypeSaveReqVO createReqVO) {
        return success(businessDictionaryTypeService.createBusinessDictionaryType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新业务字典类型")
    @PreAuthorize("@ss.hasPermission('base:business-dictionary-type:update')")
    public CommonResult<Boolean> updateBusinessDictionaryType(@Valid @RequestBody BusinessDictionaryTypeSaveReqVO updateReqVO) {
        businessDictionaryTypeService.updateBusinessDictionaryType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除业务字典类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:business-dictionary-type:delete')")
    public CommonResult<Boolean> deleteBusinessDictionaryType(@RequestParam("id") Long id) {
        businessDictionaryTypeService.deleteBusinessDictionaryType(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除业务字典类型")
                @PreAuthorize("@ss.hasPermission('base:business-dictionary-type:delete')")
    public CommonResult<Boolean> deleteBusinessDictionaryTypeList(@RequestBody BatchDeleteReqVO req) {
        businessDictionaryTypeService.deleteBusinessDictionaryTypeListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得业务字典类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:business-dictionary-type:query')")
    public CommonResult<BusinessDictionaryTypeRespVO> getBusinessDictionaryType(@RequestParam("id") Long id) {
        BusinessDictionaryTypeDO businessDictionaryType = businessDictionaryTypeService.getBusinessDictionaryType(id);
        return success(BeanUtils.toBean(businessDictionaryType, BusinessDictionaryTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得业务字典类型分页")
    @PreAuthorize("@ss.hasPermission('base:business-dictionary-type:query')")
    public CommonResult<PageResult<BusinessDictionaryTypeRespVO>> getBusinessDictionaryTypePage(@Valid BusinessDictionaryTypePageReqVO pageReqVO) {
        PageResult<BusinessDictionaryTypeDO> pageResult = businessDictionaryTypeService.getBusinessDictionaryTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessDictionaryTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出业务字典类型 Excel")
    @PreAuthorize("@ss.hasPermission('base:business-dictionary-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessDictionaryTypeExcel(@Valid BusinessDictionaryTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessDictionaryTypeDO> list = businessDictionaryTypeService.getBusinessDictionaryTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "业务字典类型.xls", "数据", BusinessDictionaryTypeRespVO.class,
                        BeanUtils.toBean(list, BusinessDictionaryTypeRespVO.class));
    }

    // ==================== 子表（业务字典数据） ====================

    @GetMapping("/business-dictionary-data/list-by-dictionary-type-id")
    @Operation(summary = "获得业务字典数据列表")
    @Parameter(name = "dictionaryTypeId", description = "字典类型")
    @PreAuthorize("@ss.hasPermission('base:business-dictionary-type:query')")
    public CommonResult<List<BusinessDictionaryDataDO>> getBusinessDictionaryDataListByDictionaryTypeId(@RequestParam("dictionaryTypeId") Long dictionaryTypeId) {
        return success(businessDictionaryTypeService.getBusinessDictionaryDataListByDictionaryTypeId(dictionaryTypeId));
    }

    @GetMapping("/business-dictionary-data/list-by-type")
    @Operation(summary = "根据字典类型编码获取业务字典数据列表")
    @PreAuthorize("@ss.hasPermission('base:business-dictionary-type:query')")
    public CommonResult<List<BusinessDictionaryDataDO>> getBusinessDictionaryDataListByType(@RequestParam("type") String type) {
        // 通过类型编码直接获取对应的业务字典项，避免前端重复查询字典类型 ID
        return success(businessDictionaryTypeService.getBusinessDictionaryDataListByType(type));
    }

}