package cn.iocoder.yudao.module.system.controller.admin.dictTenant;

import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeSimpleRespVO;
import cn.iocoder.yudao.module.system.controller.admin.dictTenant.vo.type.DictTypeTenantPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dictTenant.vo.type.DictTypeTenantRespVO;
import cn.iocoder.yudao.module.system.controller.admin.dictTenant.vo.type.DictTypeTenantSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictTypeDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dictTenant.DictTypeTenantDO;
import cn.iocoder.yudao.module.system.service.dictTenant.DictTypeTenantService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 租户字典类型")
@RestController
@RequestMapping("/system/tenant/dict-type")
@Validated
public class DictTypeTenantController {

    @Resource
    private DictTypeTenantService dictTypeTenantService;

    @PostMapping("/create")
    @Operation(summary = "创建租户字典类型")
    @ApiOperationSupport(order = 1)
    @PreAuthorize("@ss.hasPermission('tenant:dict-type:create')")
    public CommonResult<Long> createDictType(@Valid @RequestBody DictTypeTenantSaveReqVO createReqVO) {
        return success(dictTypeTenantService.createDictType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新租户字典类型")
    @ApiOperationSupport(order = 2)
    @PreAuthorize("@ss.hasPermission('tenant:dict-type:update')")
    public CommonResult<Boolean> updateDictType(@Valid @RequestBody DictTypeTenantSaveReqVO updateReqVO) {
        dictTypeTenantService.updateDictType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除租户字典类型")
    @ApiOperationSupport(order = 3)
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tenant:dict-type:delete')")
    public CommonResult<Boolean> deleteDictType(@RequestParam("id") Long id) {
        dictTypeTenantService.deleteDictType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得租户字典类型")
    @ApiOperationSupport(order = 4)
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tenant:dict-type:query')")
    public CommonResult<DictTypeTenantRespVO> getDictType(@RequestParam("id") Long id) {
        DictTypeTenantDO dictType = dictTypeTenantService.getDictType(id);
        return success(BeanUtils.toBean(dictType, DictTypeTenantRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得租户字典类型分页")
    @ApiOperationSupport(order = 5)
    @PreAuthorize("@ss.hasPermission('tenant:dict-type:query')")
    public CommonResult<PageResult<DictTypeTenantRespVO>> getDictTypePage(@Valid @ParameterObject DictTypeTenantPageReqVO pageReqVO) {
        PageResult<DictTypeTenantDO> pageResult = dictTypeTenantService.getDictTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DictTypeTenantRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出租户字典类型 Excel")
    @ApiOperationSupport(order = 6)
    @PreAuthorize("@ss.hasPermission('tenant:dict-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDictTypeExcel(@Valid @ParameterObject DictTypeTenantPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DictTypeTenantDO> list = dictTypeTenantService.getDictTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "租户字典类型.xls", "数据", DictTypeTenantRespVO.class,
                        BeanUtils.toBean(list, DictTypeTenantRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple", "simple-list"})
    @ApiOperationSupport(order = 7)
    @Operation(summary = "获得全部字典类型列表", description = "包括开启 + 禁用的字典类型，主要用于前端的下拉选项")
    // 无需添加权限认证，因为前端全局都需要
    public CommonResult<List<DictTypeSimpleRespVO>> getSimpleDictTypeList() {
        List<DictTypeTenantDO> list = dictTypeTenantService.getDictTypeList();
        return success(BeanUtils.toBean(list, DictTypeSimpleRespVO.class));
    }

    @GetMapping("/initDictTypeAndDataForTenant")
    @Operation(summary = "初始化租户字典类型数据")
    @Parameter(name = "dictType", description = "字典类型", required = false)
    @ApiOperationSupport(order = 8)
    public CommonResult<Map<String, String>> initDictTypeAndDataForTenant(@RequestParam(value = "dictType", required = false) String dictType) {
        Map<String, String> result = dictTypeTenantService.initDictTypeAndDataForTenant(dictType);
        return success(result);
    }

}