import com.alibaba.fastjson.JSONObject;
import com.gic.clerk.api.service.ClerkService;
import com.gic.commons.util.GICMQClientUtil;
import com.gic.dubbo.util.DubboInvokeUtil;
import com.gic.enterprise.api.service.StoreService;
import com.gic.haoban.app.customer.service.api.service.CustomerApiService;
import com.gic.haoban.manage.api.dto.SecretSettingDTO;
import com.gic.haoban.manage.api.dto.WxEnterpriseQwDTO;
import com.gic.haoban.manage.api.dto.chat.GroupChatPlanDTO;
import com.gic.haoban.manage.api.dto.notify.qdto.NoticeMessageQDTO;
import com.gic.haoban.manage.api.dto.notify.qdto.NotifyMessageBatchQDTO;
import com.gic.haoban.manage.api.enums.NoticeMessageTypeEnum;
import com.gic.haoban.manage.api.enums.SecretTypeEnum;
import com.gic.haoban.manage.api.service.*;
import com.gic.haoban.manage.api.service.hm.HmQrcodeApiService;
import com.gic.haoban.manage.api.service.notify.NoticeMessageApiService;
import com.gic.haoban.manage.service.config.Config;
import com.gic.haoban.manage.service.dao.mapper.StaffMapper;
import com.gic.haoban.manage.service.dao.mapper.TabHaobanExternalClerkRelatedMapper;
import com.gic.haoban.manage.service.entity.TabHaobanExternalClerkRelated;
import com.gic.haoban.manage.service.service.*;
import com.gic.haoban.manage.service.service.chat.GroupChatPlanService;
import com.gic.haoban.manage.service.service.chat.GroupChatService;
import com.gic.haoban.manage.service.service.hm.HmLinkService;
import com.gic.haoban.manage.service.service.hm.WxUserAddLogService;
import com.gic.haoban.manage.service.service.role.HaobanMenuService;
import com.gic.member.api.service.MemberApiService;
import com.gic.member.api.service.MemberEntranceApiService;
import com.gic.member.api.service.MemberService;
import com.gic.member.api.service.MemberStoreService;
import com.gic.member.tag.api.service.MemberTagOpenApiService;
import com.gic.wechat.api.dto.qywx.QywxNewsArticleMessageDTO;
import com.gic.wechat.api.dto.qywx.QywxNewsSendMessageDTO;
import com.gic.wechat.api.service.qywx.QywxExternalUserService;
import com.gic.wechat.api.service.qywx.QywxSuiteApiService;
import com.gic.wechat.api.service.qywx.QywxUserApiService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-conf.xml"})
public class ChatTest {

    private static Logger logger = LoggerFactory.getLogger(ChatTest.class);

    @Autowired
    private GroupChatService croupChatService;
    @Autowired
    private StaffApiService staffApiService ;
    @Autowired
    private QwFriendApiService qwFriendApiService ;
    @Autowired
    private MemberUnionRelatedService memberUnionRelatedService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private QywxUserApiService qywxUserApiService;
    @Autowired
    private WxEnterpriseRelatedService wxEnterpriseRelatedService;
    @Autowired
    private WxEnterpriseService wxEnterpriseService;
    @Autowired
    private ClerkService clerkService;
    @Autowired
    private ClerkMainStoreRelatedService clerkMainStoreRelatedService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private Config config;
    @Autowired
    private QywxSuiteApiService qywxSuiteApiService;
    @Autowired
    private SecretSettingService secretSettingService;
    @Autowired
    private StaffClerkRelationService staffClerkRelationService;
    @Autowired
    private ExternalClerkRelatedService externalClerkRelatedService;
    @Autowired
    private CheckQywxSettingApiService checkQywxSettingApiService;
    @Autowired
    private CustomerApiService customerApiService;
    @Autowired
    private MemberApiService memberApiService;
    @Autowired
    private TabHaobanExternalClerkRelatedMapper externalClerkRelatedMapper;
    @Autowired
    private ExternalClerkRelatedApiService externalClerkRelatedApiService;
    @Autowired
    private MemberStoreService memberStoreService;
    @Autowired
    private HmQrcodeApiService hmQrcodeApiService;
    @Autowired
    private WxUserAddLogService wxUserAddLogService;
    @Autowired
    private KeyDataService keyDataService;
    @Autowired
    private MemberTagOpenApiService memberTagOpenApiService;
    @Autowired
    private HmLinkService hmLinkService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private MemberEntranceApiService memberEntranceApiService;
    @Autowired
    private QywxSendService qywxSendService ;
    @Autowired
    private GroupChatService groupChatService ;
    @Autowired
    private WelcomeSendService welcomeSendService ;
    @Autowired
    private ExternalMemberService externalMemberService ;
    @Autowired
    private ClerkMainStoreRelatedApiService  clerkMainStoreRelatedApiService ;
    @Autowired
    private HaobanMenuService haobanMenuService ;
    @Autowired
    private QywxExternalUserService qywxExternalUserService;

    @Test
    public void test() {


        for(int i=0;i<3;i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    TabHaobanExternalClerkRelated dto = new TabHaobanExternalClerkRelated() ;
                    dto.setExternalUserId("1234567");
                    dto.setClerkId("123");
                    dto.setStatusFlag(1);
                    externalClerkRelatedService.insert(dto) ;
                }
            }).start();
        }
        if(true)
        for(;;){

        }
       // Object token =  this.qywxExternalUserService.token("ww468ed06ba5fa5604","StuEbLhpszQKMNWyRqwDeeX4AGRKm__DAF6k7UwXL-A","wp8KhBDgAAaE3jm1L7ARKe2yxc1hci4A","1000045",1,"http://122.9.98.129:8990#192.168.0.58") ;
       // logger.info("获取token"+JSONObject.toJSONString(token));


       //  Object token =  this.qywxExternalUserService.token("ww468ed06ba5fa5604","StuEbLhpszQKMNWyRqwDeeX4AGRKm__DAF6k7UwXL-A","wp8KhBDgAAE90YWr3_wIt6XXljkZvZXg","1000172",1,"http://122.9.98.129:8990#192.168.0.58") ;
       //  logger.info("获取token-润臣"+JSONObject.toJSONString(token));

        String tokensy = "kxCbwqAa7KrxnmW4gOFCa-KGM2N3gbkgVLNKiWUDk5AfiBQK8Xus6AP74U9eBKVDZxM8_C6EtZ0WWBQonUx3wnjfTCVyH0yorHXU2gR08dTZjUKE4Ej8ERMdYisZ6f30tFv37zgQqangmweC6vp7eFpUQC81Bg2qTylmqV0mVupyXWGqp4m7rzCKh4-MUn6Ry-mGy3xPYUwkpImRFmneBQ" ;

        String tokenyc = "t3Uu9b0cDIl_y5uyDRnQw3fhmUx-C1pCK1N6xHfDROrySlxUD74ophI0Pel2RQzy4f6qVGnpJu5k6w3j2LolMh2JqkmfAFe8-991h0a4zwlGgFzZAlY1jtcE8mJ911oJkowAvuf8cLuO-iaM2BwjBg8VGrjaVmtLJiNczHOQkVHmhSVF7ubWMj52HCIgxv51pGpMpG5G8SvRnbzIjyW5hw";

        Object a1 =  this.qywxExternalUserService.req("ww468ed06ba5fa5604","StuEbLhpszQKMNWyRqwDeeX4AGRKm__DAF6k7UwXL-A","https://qyapi.weixin.qq.com/cgi-bin/corpgroup/unionid_to_pending_id?access_token=",JSONObject.parseObject("{\"unionid\":\"orXl9t6P-XBJQyaNCaJj38GLyYd0\",\"openid\":\"o0Yvd0246SId1HwEjQa54LzMcFU0\"}"),"http://122.9.98.129:8990#192.168.0.58") ;
        logger.info("unionid查询pending_id"+JSONObject.toJSONString(a1));

        Object a2 =  this.qywxExternalUserService.req("https://qyapi.weixin.qq.com/cgi-bin/corpgroup/batch/external_userid_to_pending_id?access_token="+tokenyc,JSONObject.parseObject("{\"external_userid\":[\"wm8KhBDgAAlUiFWOeNzuyRR0EoMGLmRw\",\"wm8KhBDgAASGKj-mQQ2RAuU7e07gfJBg\"]}"),"http://122.9.98.129:8990#192.168.0.58") ;
        logger.info("external_userid查询pending_id"+JSONObject.toJSONString(a2));

        Object a3 =  this.qywxExternalUserService.req("ww468ed06ba5fa5604","StuEbLhpszQKMNWyRqwDeeX4AGRKm__DAF6k7UwXL-A","https://qyapi.weixin.qq.com/cgi-bin/corpgroup/batch/external_userid_to_pending_id?access_token=",JSONObject.parseObject("{\"external_userid\":[\"wm8KhBDgAASGKj-mQQ2RAuU7e07gfJBg\"]}"),"http://122.9.98.129:8990#192.168.0.58") ;
        logger.info("external_userid查询pending_id3"+JSONObject.toJSONString(a3));

        Object a4 = this.qywxExternalUserService.unionidToExternalUseridCorpgroup("ww468ed06ba5fa5604","StuEbLhpszQKMNWyRqwDeeX4AGRKm__DAF6k7UwXL-A","orXl9t6P-XBJQyaNCaJj38GLyYd0","o0Yvd0246SId1HwEjQa54LzMcFU0","http://122.9.98.129:8990#192.168.0.58") ;
        logger.info("通过unionid和openid查询external_userid"+JSONObject.toJSONString(a4));


        Object o1 =  this.qywxExternalUserService.listcorpid("ww468ed06ba5fa5604","StuEbLhpszQKMNWyRqwDeeX4AGRKm__DAF6k7UwXL-A","1000031",1,"http://122.9.98.129:8990#192.168.0.58") ;
        logger.info("查询企业列表"+JSONObject.toJSONString(o1));

        if(true) {
            return ;
        }
        // 4Y6h7OfJSCzGYv3puNFLYO57s0TQubbFuIxLyqJjINflwa4Vezt2DgDf53k2ajRAFM6nm1uk4SsDSOmzSSUO-EbHAHm6bpTIkDjiMvAtQxWjzhpQW5y6MShFKCo91zjaqWCdXr8CMKNXfXlBW_Dlx3Tym7SJrs4FEN_h0lFftCe12gcgbfPqPxOMeP-sk3XaYPdtfZVkYFNAsFJGgPbzBA
        Object o2 =  this.qywxExternalUserService.req("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token="+tokensy+"&external_userid=wm8KhBDgAAl30re_xOKcrdP-XJW4hL9w",new JSONObject(),"http://122.9.98.129:8990#192.168.0.58") ;
        logger.info("客户详情"+JSONObject.toJSONString(o2));

        Object o3 =  this.qywxExternalUserService.req("https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token="+tokensy+"&department_id=1",new JSONObject(),"http://122.9.98.129:8990#192.168.0.58") ;
        logger.info("通讯录"+JSONObject.toJSONString(o3));

        Object o4 =  this.qywxExternalUserService.req("https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token="+tokensy+"&userid=wo8KhBDgAAoj4rHdDE0P0vNNfjs6cl3w",new JSONObject(),"http://122.9.98.129:8990#192.168.0.58") ;
        logger.info("成员详情"+JSONObject.toJSONString(o4));



        Object o6 =  this.qywxExternalUserService.req("https://qyapi.weixin.qq.com/cgi-bin/agent/get?access_token="+tokensy+"&agentid=1000045",new JSONObject(),"http://122.9.98.129:8990#192.168.0.58") ;
        logger.info("应用信息"+JSONObject.toJSONString(o6));


        // {"errcode":0,"chains":[{"chain_id":"wgfc938e1ba44e30f5","chain_name":"达摩科技的经销商"}],"errmsg":"ok"}
        Object o5 =  this.qywxExternalUserService.req("ww468ed06ba5fa5604","StuEbLhpszQKMNWyRqwDeeX4AGRKm__DAF6k7UwXL-A","https://qyapi.weixin.qq.com/cgi-bin/corpgroup/corp/get_chain_list?access_token=",new JSONObject(),"http://122.9.98.129:8990#192.168.0.58") ;
        logger.info("滴答滴答滴答滴答滴答滴答5"+JSONObject.toJSONString(o5));

        // {"errcode":0,"errmsg":"ok","groups":[{"group_name":"达摩科技的经销商","groupid":1,"parentid":0,"order":100000000},
        // {"group_name":"一级经销商","groupid":3,"parentid":1,"order":99999000},{"group_name":"二级经销商","groupid":4,"parentid":1,"order":99998000}]}
        Object o7 =  this.qywxExternalUserService.req("ww468ed06ba5fa5604","StuEbLhpszQKMNWyRqwDeeX4AGRKm__DAF6k7UwXL-A","https://qyapi.weixin.qq.com/cgi-bin/corpgroup/corp/get_chain_group?access_token=",JSONObject.parseObject("{\"chain_id\":\"wgfc938e1ba44e30f5\"}"),"http://122.9.98.129:8990#192.168.0.58") ;
        logger.info("上下游"+JSONObject.toJSONString(o7));

        Object o8 =  this.qywxExternalUserService.req("ww468ed06ba5fa5604","StuEbLhpszQKMNWyRqwDeeX4AGRKm__DAF6k7UwXL-A","https://qyapi.weixin.qq.com/cgi-bin/corpgroup/corp/get_chain_list?access_token=",new JSONObject(),"http://122.9.98.129:8990#192.168.0.58") ;
        logger.info("get_chain_list"+JSONObject.toJSONString(o8));



        String wxEnterpriseId = "b18ffdc9d0644912865a248859914d80";
        WxEnterpriseQwDTO qwDTO = this.wxEnterpriseService.getQwInfo(wxEnterpriseId);
        SecretSettingDTO secretSetting = secretSettingService.getSecretSetting(wxEnterpriseId, SecretTypeEnum.CUSTOMIZED_APP.getVal());

      //  String unionIdJson = qywxUserApiService.getCorpSelfExternalUseridInfo(qwDTO.getDkCorpid(), secretSetting.getSecretVal(), "wm8KhBDgAAl30re_xOKcrdP-XJW4hL9w", qwDTO.getUrlHost());

      //  logger.info(unionIdJson);

     //    this.qwFriendApiService.getPendingIdByUnionid("ff80808189dd5e490189dd88daf60003","orXl9t6P-XBJQyaNCaJj38GLyYd0","") ;

    }

    @Test
    public void sendMessageTest() throws Exception {

        String params = "{\"enterpriseId\":\"ff8080815dacd3a2015dacd3ef5c0000\",\"memberId\":\"ff80808187732d6001877333a8030000\",\"opt\":1,\"unionId\":\"orXl9t70uwsdD7kqjNmWu03nKSCg\"}";
        GICMQClientUtil.getClientInstance().sendMessage("memberIdChangeNotify", params);
    }

}
