package cn.iocoder.yudao.module.system.api.auth;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils;
import cn.iocoder.yudao.module.system.api.auth.dto.AdminOauthUserOtherInfoApiDTO;
import cn.iocoder.yudao.module.system.api.auth.vo.AdminOauthUserOtherInfoApiVO;
import cn.iocoder.yudao.module.system.dal.dataobject.auth.AdminOauthUserOtherInfoDO;
import cn.iocoder.yudao.module.system.service.auth.AdminOauthUserOtherInfoService;
import cn.iocoder.yudao.module.system.service.auth.dto.AdminOauthUserOtherInfoDTO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class AdminOauthUserOtherInfoApiImpl implements AdminOauthUserOtherInfoApi {

    @Resource
    private AdminOauthUserOtherInfoService adminOauthUserOtherInfoService;


    @Override
    public CommonResult<List<AdminOauthUserOtherInfoApiVO>> getOpenIdByCondition(AdminOauthUserOtherInfoApiDTO dto) {
        AdminOauthUserOtherInfoDTO adminOauthUserOtherInfoDTO = BeanUtil.copyProperties(dto, AdminOauthUserOtherInfoDTO.class);
        AtomicReference<List<AdminOauthUserOtherInfoDO>> list = new AtomicReference<>(new ArrayList<>());
        DataPermissionUtils.executeIgnore(
                () -> list.set(adminOauthUserOtherInfoService.getOpenIdByCondition(adminOauthUserOtherInfoDTO))
        );
        return CommonResult.success(BeanUtil.copyToList(list.get(), AdminOauthUserOtherInfoApiVO.class));
    }

    @Override
    public CommonResult<AdminOauthUserOtherInfoApiVO> getByCondition(AdminOauthUserOtherInfoApiDTO dto) {
        AdminOauthUserOtherInfoDTO adminOauthUserOtherInfoDTO = BeanUtil.copyProperties(dto, AdminOauthUserOtherInfoDTO.class);
        AtomicReference<List<AdminOauthUserOtherInfoDO>> list = new AtomicReference<>(new ArrayList<>());
        DataPermissionUtils.executeIgnore(
                () -> list.set(adminOauthUserOtherInfoService.getOpenIdByCondition(adminOauthUserOtherInfoDTO))
        );
        if (CollUtil.isNotEmpty(list.get())) {
            return CommonResult.success(BeanUtil.copyProperties(list.get().get(0), AdminOauthUserOtherInfoApiVO.class));
        }
        return CommonResult.success(null);
    }
}
