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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.GroupTenantInfoVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.GroupTenantListVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.GroupTenantSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantTreeResVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserRespVO;
import cn.iocoder.yudao.module.system.convert.user.UserConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.enums.common.BooleanEnum;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import cn.iocoder.yudao.module.system.service.tenant.GroupTenantService;
import cn.iocoder.yudao.module.system.service.tenant.TenantService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

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

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;


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

    @Resource
    private GroupTenantService groupTenantService;
    @Resource
    private AdminUserService userService;

    @Resource
    private TenantService tenantService;

    @Resource
    private DeptService deptService;

    @PostMapping("/create")
    @Operation(summary = "创建集团租户关联")
    @TenantIgnore
    public CommonResult<Boolean> createGroupTenant(@Valid @RequestBody GroupTenantSaveReqVO createReqVO) {
        groupTenantService.createGroupTenant(createReqVO);
        return success(true);
    }


    @PutMapping("/update")
    @Operation(summary = "更新集团租户关联")
    public CommonResult<Boolean> updateGroupTenant(@Valid @RequestBody GroupTenantSaveReqVO updateReqVO) {
        groupTenantService.updateGroupTenant(updateReqVO);
        return success(true);
    }

    //
//    @DeleteMapping("/delete")
//    @Operation(summary = "删除集团租户关联")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('system:group-tenant:delete')")
//    public CommonResult<Boolean> deleteGroupTenant(@RequestParam("id") Long id) {
//        groupTenantService.deleteGroupTenant(id);
//        return success(true);
//    }
//
//    @GetMapping("/getGroupTenantList")
//    @Operation(summary = "获得集团租户关联列表")
//    @Parameters({@Parameter(name = "groupTenantId", description = "集团租户ID", required = true, example = "1024"),
//            @Parameter(name = "province", description = "省份")})
//    @Deprecated
//    public CommonResult<List<GroupTenantInfoVO>> getGroupTenantList(@RequestParam("groupTenantId") Long groupTenantId, @RequestParam(value = "province", required = false) String province) {
//        return success(groupTenantService.getGroupTenant(groupTenantId, province));
//    }


    @GetMapping("/findAllGroupList")
    @Operation(summary = "获取所有集团列表")
    @TenantIgnore
    public CommonResult<List<GroupTenantListVO>> findAllGroupList(){
        return success(groupTenantService.findAllGroupList());
    }


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


    @GetMapping("/changeTenantLogin")
    @Operation(summary = "控制台登录")
    @TenantIgnore
    public CommonResult<AuthLoginRespVO> changeTenantLogin(@RequestParam("tenantId") Long tenantId) {
        TenantContextHolder.setTenantId(tenantId);
        return success(groupTenantService.changeTenantLogin(tenantId));
    }


    @GetMapping("/getGroupTenantTree")
    @Operation(summary = "集团机构树形数据")
    @TenantIgnore
    public CommonResult<List<TenantTreeResVO>> getGroupTenantTree(@RequestParam("groupTenantId") Long groupTenantId){
        return success(groupTenantService.getGroupTenantTree(groupTenantId));
    }

    @PostMapping("/getUserPage")
    @Operation(summary = "获得用户分页列表")
    @TenantIgnore
    public CommonResult<PageResult<UserRespVO>> getUserPage(@RequestBody UserPageReqVO pageReqVO) {
        PageResult<AdminUserDO> pageResult = userService.getUserPage(pageReqVO);
        Map<Long, DeptDO> deptMap = deptService.getDeptMap(
                convertList(pageResult.getList(), AdminUserDO::getDeptId));
        PageResult<UserRespVO> userRespVOPageResult = new PageResult<>(UserConvert.INSTANCE.convertList(pageResult.getList(), deptMap),
                pageResult.getTotal());
        //插入租户名称
        for (UserRespVO userRespVO : userRespVOPageResult.getList()) {
            userRespVO.setTenantName(tenantService.getTenant(userRespVO.getTenantId()).getName());
        }
        return success(userRespVOPageResult);
    }


}