package com.hotent.uc.manager.impl;

import com.aliyun.dingtalkcontact_1_0.models.GetUserHeaders;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hotent.base.exception.BaseException;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.base.util.UniqueIdUtil;
import com.hotent.uc.dao.OrgExternalSettingDao;
import com.hotent.uc.dao.ScanLoginDao;
import com.hotent.uc.dao.UserDao;
import com.hotent.uc.manager.OrgExternalSettingManager;
import com.hotent.uc.manager.ScanLoginManager;
import com.hotent.uc.model.OrgExternalSetting;
import com.hotent.uc.model.ScanLogin;
import com.hotent.uc.model.User;
import com.hotent.uc.util.QrCodeUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户与外部通讯录关系表 服务实现类
 *
 * @company 广州宏天软件股份有限公司
 * @author pangquan
 * @since 2020-10-30
 */
@Service
public class OrgExternalSettingManagerImpl extends BaseManagerImpl<OrgExternalSettingDao, OrgExternalSetting> implements OrgExternalSettingManager {


    @Override
    public CommonResult<String> addOrgExtSetting(OrgExternalSetting orgExtSetting) throws Exception {
        if (StringUtil.isEmpty(orgExtSetting.getOrgId())) {
//            throw new BaseException("添加参数失败，组织id不能为空！");
            return new CommonResult<String>(false, "添加参数失败，组织id不能为空！", "");
        }
        LambdaQueryWrapper<OrgExternalSetting> qw=new LambdaQueryWrapper<>();
//        qw.eq(OrgExternalSetting::getOrgId,orgExtSetting.getOrgId());
        qw.eq(OrgExternalSetting::getDingCorpId,orgExtSetting.getDingCorpId());
        List<OrgExternalSetting> entity=this.baseMapper.selectList(qw);
        if(entity.size()>0){
            if(entity.get(0).getOrgId().equals(orgExtSetting.getOrgId())){
                OrgExternalSetting enTemp=new OrgExternalSetting();
                enTemp.setId(entity.get(0).getId());
                enTemp.setOrgId(entity.get(0).getOrgId());
                enTemp.setTenantId(entity.get(0).getTenantId());
                BeanUtils.copyNotNullProperties(enTemp,orgExtSetting);
                this.saveOrUpdate(enTemp);
            }else{
                return new CommonResult<String>(false, "添加参数失败，钉钉CorpId重复！", "");
            }
        }else{
            if(BeanUtils.isEmpty(orgExtSetting.getId())){
                String uid=UniqueIdUtil.getSuid();
                orgExtSetting.setId(uid);
            }
            this.saveOrUpdate(orgExtSetting);
        }

        return new CommonResult<String>(true, "添加组织配置成功！", "");
    }

    @Override
    public CommonResult<String> updateOrgExtSetting(OrgExternalSetting orgExtSetting) throws Exception {
        LambdaQueryWrapper<OrgExternalSetting> qw=new LambdaQueryWrapper<>();
        qw.eq(OrgExternalSetting::getOrgId,orgExtSetting.getOrgId());
        OrgExternalSetting entity=this.baseMapper.selectOne(qw);
        if(entity==null){
            return new CommonResult<String>(false, "更新配置出错，没有找到对应数据！", "");
        }
        BeanUtils.copyNotNullProperties(entity,orgExtSetting);
        this.saveOrUpdate(entity);
        return new CommonResult<String>(true, "更新组织配置成功！", "");
    }

    @Override
    public CommonResult<OrgExternalSetting> querySettingByCorpId(String orgId) throws Exception {
        LambdaQueryWrapper<OrgExternalSetting> qw=new LambdaQueryWrapper<>();
        qw.eq(OrgExternalSetting::getOrgId,orgId);
        OrgExternalSetting entity=this.baseMapper.selectOne(qw);
        if(entity==null){
            entity=new OrgExternalSetting();
            return new CommonResult<OrgExternalSetting>(true, "获取组织配置参数失败！",entity);
        }
        return new CommonResult<OrgExternalSetting>(true, "获取组织配置参数成功！", entity);
    }

    @Override
    public OrgExternalSetting querySettingByCorpIdCode(String corpId) throws Exception {
        LambdaQueryWrapper<OrgExternalSetting> qw=new LambdaQueryWrapper<>();
        qw.eq(OrgExternalSetting::getDingCorpId,corpId);
        OrgExternalSetting entity=this.baseMapper.selectOne(qw);
        return  entity;
    }

    @Override
    public OrgExternalSetting querySettingByCorpIdReturn(String orgId) throws Exception {
        LambdaQueryWrapper<OrgExternalSetting> qw=new LambdaQueryWrapper<>();
        qw.eq(OrgExternalSetting::getOrgId,orgId);
        OrgExternalSetting entity=this.baseMapper.selectOne(qw);
        return entity;
    }

    @Override
    public OrgExternalSetting queryByCorpId(String corpId) throws Exception {
        LambdaQueryWrapper<OrgExternalSetting> qw=new LambdaQueryWrapper<>();
        qw.eq(OrgExternalSetting::getDingCorpId,corpId);
        OrgExternalSetting entity=this.baseMapper.selectOne(qw);
        return entity;
    }
}
