package com.hotent.uc.manager.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiV2UserCreateRequest;
import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiV2UserCreateResponse;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.StringUtil;
import com.hotent.base.util.UniqueIdUtil;
import com.hotent.uc.dao.*;
import com.hotent.uc.manager.*;
import com.hotent.uc.model.*;
import com.hotent.uc.util.Constant;
import com.hotent.uc.util.ContextUtil;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

    @Override
    public void saveSynExtOrgLog(List<CommonResult<String>> result,String orgId,String synType,String synDir,String synDeptUser) {
        String fullName= "";
        try {
            if(ContextUtil.getCurrentUser()!=null){
                fullName= ContextUtil.getCurrentUser().getFullname();
            }
        }catch (Exception e){
            e.printStackTrace();
            fullName="超级管理员";
        }

        OrgExternalSynLog orgSynLog=new OrgExternalSynLog();
        orgSynLog.setCreataBy(fullName);
        orgSynLog.setCreateTime(LocalDateTime.now());
        orgSynLog.setOrgId(orgId);
        orgSynLog.setSynDir(synDir);
        orgSynLog.setSynType(synType);
        orgSynLog.setSynDeptUser(synDeptUser);
        List<String> successList= new ArrayList<>();
        List<String> errorList= new ArrayList<>();
        List<String> contentList= new ArrayList<>();
        result.stream().forEach(item->{
            String content= item.getValue();
            boolean flag=item.getState();
            if(flag){
                successList.add("1");
            }else{
                errorList.add("1");
            }
            if(content.contains("[")){
                contentList.add(content.substring(1,content.length()-1));
            }else{
                contentList.add(content);
            }
        });

        orgSynLog.setLogInfo("同步成功："+successList.size()+",失败："+errorList.size());
        orgSynLog.setLogInfoContent(contentList.stream().collect(Collectors.joining(",")));
        this.save(orgSynLog);
    }
}
