package com.malk.aiwei.service.impl;

import cn.hutool.core.util.StrUtil;
import com.malk.aiwei.service.AwDingService;
import com.malk.service.dingtalk.DDClient;
import com.malk.service.dingtalk.DDClient_Group;
import com.malk.utils.UtilEnv;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class AwDingServiceImpl implements AwDingService {

    @Autowired
    private DDClient_Group ddClient_group;
    @Autowired
    private DDClient ddClient;

    String _matchFormData(String code) {
        Map<String, String> formUuid = UtilMap.empty();
        if (true || UtilEnv.getActiveProfile().equals(UtilEnv.ENV_PROD)) {
            formUuid.put("OWNER_USER_ID", "153620324221442254");// todo 需要替换艾为tbmanager userId
            formUuid.put("TEMPLATE_ID", "17d5c0fc-79f8-43bf-86ab-bad1e254ec2d");// todo 需要在艾为组织创建场景群模板，并替换模板ID
        } else {
            formUuid.put("OWNER_USER_ID", "153620324221442254");
            formUuid.put("TEMPLATE_ID", "17d5c0fc-79f8-43bf-86ab-bad1e254ec2d");
        }
        return formUuid.get(code);
    }

    @Override
    public void saveGroup(Map upMap, Map formData) {
        try {
            String ddGroupId=String.valueOf(formData.get("textField_luqvxhec"));// 群ID
            List<Map> list= (List<Map>) formData.get("tableField_lqxtykcf");// 项目角色子表单
            String newUsers="";
            for (Map map:list){
                newUsers=newUsers.concat(String.join(",",UtilMap.getList(map,"employeeField_lqxtykch_id"))).concat(",");
            }
            newUsers=newUsers.substring(0,newUsers.length()-1);
            if(StrUtil.isBlank(ddGroupId)){
                String name=String.valueOf(formData.get("textField_lqxtykcd")).concat("项目群");// todo 这里是群名称
                ddGroupId=ddClient_group.createGroupByTemp(ddClient.getAccessToken(),name,_matchFormData("TEMPLATE_ID"),_matchFormData("OWNER_USER_ID"),newUsers);
                // 创建群
                upMap.put("textField_luqvxhec",ddGroupId);// 群ID
                upMap.put("textField_luqvxhed",newUsers);// 群成员ID
            }else{
                // 判断并更新群成员
                String oldUsers=String.valueOf(formData.get("textField_luqvxhed"));
                // 删除群成员
                List delList=findDifferentElements(Arrays.asList(oldUsers.split(",")),Arrays.asList(newUsers.split(",")));
                if(delList.size()>0){
                    ddClient_group.delGroupUser(ddClient.getAccessToken(),ddGroupId,String.join(",",delList));
                }
                // 新增群成员
                List addList=findDifferentElements(Arrays.asList(newUsers.split(",")),Arrays.asList(oldUsers.split(",")));
                if(addList.size()>0){
                    ddClient_group.addGroupUser(ddClient.getAccessToken(),ddGroupId,String.join(",",addList));
                }
                upMap.put("textField_luqvxhed",newUsers);// 群成员ID
            }
        }catch (Exception e){
            log.error("创建钉钉项目群异常了,{}",e);
            e.printStackTrace();
        }
    }

    private List findDifferentElements(List firstArray,List secondArray){
        Set<T> set1 = new HashSet<>(firstArray);
        set1.removeAll(secondArray);
        return new ArrayList<>(set1);
    }
}
