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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.*;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.UserGroupTenantDO;
import cn.iocoder.yudao.module.system.service.tenant.UserGroupTenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 集团租户和用户关联")
@RestController
@RequestMapping("/system/userGroupTenant")
@Validated
public class UserGroupTenantController {

    @Resource
    private UserGroupTenantService userGroupTenantService;


    @PostMapping("/create")
    @Operation(summary = "创建集团租户和用户关联")
    @TenantIgnore
    public CommonResult<Boolean> createUserGroupTenant(@Valid @RequestBody List<UserGroupTenantSaveReqVO> createReqVO) {
        userGroupTenantService.createUserGroupTenant(createReqVO);
        return success(true);
    }


    //    @PutMapping("/update")
//    @Operation(summary = "更新集团租户和用户关联")
//    @PreAuthorize("@ss.hasPermission('system:user-group-tenant:update')")
//    public CommonResult<Boolean> updateUserGroupTenant(@Valid @RequestBody UserGroupTenantSaveReqVO updateReqVO) {
//        userGroupTenantService.updateUserGroupTenant(updateReqVO);
//        return success(true);
//    }
//
//    @DeleteMapping("/delete")
//    @Operation(summary = "删除集团租户和用户关联")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('system:user-group-tenant:delete')")
//    public CommonResult<Boolean> deleteUserGroupTenant(@RequestParam("id") Long id) {
//        userGroupTenantService.deleteUserGroupTenant(id);
//        return success(true);
//    }
//
    @GetMapping("/getGroupTenantByUser")
    @Operation(summary = "根据登录人获取所绑定集团列表（下拉）")
    @TenantIgnore
    public CommonResult<List<TenantDO>> getGroupTenantByUser() {
        return success(userGroupTenantService.getGroupTenantByUser());
    }


    @GetMapping("/get")
    @Operation(summary = "获得集团租户和用户关联")
    @Parameter(name = "userId", description = "用户ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<List<UserGroupTenantRespVO>> getUserGroupTenant(@RequestParam("userId") Long userId,
                                                                        @RequestParam(value = "province", required = false) String province,
                                                                        @RequestParam(value = "groupTenantId",required = false) Long groupTenantId) {
        return success(userGroupTenantService.getUserGroupTenant(userId, province,groupTenantId));
    }


    @GetMapping("/findGroupTenantTree")
    @Operation(summary = "集团租户下拉列表")
    public CommonResult<List<GroupTenantTreeRespVO>> findGroupTenantTree(){
        return success(userGroupTenantService.findGroupTenantTree());
    }


//
//    @GetMapping("/page")
//    @Operation(summary = "获得集团租户和用户关联分页")
//    @PreAuthorize("@ss.hasPermission('system:user-group-tenant:query')")
//    public CommonResult<PageResult<UserGroupTenantRespVO>> getUserGroupTenantPage(@Valid UserGroupTenantPageReqVO pageReqVO) {
//        PageResult<UserGroupTenantDO> pageResult = userGroupTenantService.getUserGroupTenantPage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, UserGroupTenantRespVO.class));
//    }
//
//    @GetMapping("/export-excel")
//    @Operation(summary = "导出集团租户和用户关联 Excel")
//    @PreAuthorize("@ss.hasPermission('system:user-group-tenant:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportUserGroupTenantExcel(@Valid UserGroupTenantPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<UserGroupTenantDO> list = userGroupTenantService.getUserGroupTenantPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "集团租户和用户关联.xls", "数据", UserGroupTenantRespVO.class,
//                        BeanUtils.toBean(list, UserGroupTenantRespVO.class));
//    }

}