import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.DateUtil;
import com.gic.haoban.manage.api.dto.AlertMessageDTO;
import com.gic.haoban.manage.api.dto.WxEnterpriseQwDTO;
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.AlertTypeEnum;
import com.gic.haoban.manage.api.enums.NoticeMessageTypeEnum;
import com.gic.haoban.manage.api.service.CheckQywxSettingApiService;
import com.gic.haoban.manage.api.service.QywxTagApiService;
import com.gic.haoban.manage.api.service.notify.NoticeMessageApiService;
import com.gic.haoban.manage.service.config.Config;
import com.gic.haoban.manage.service.service.WxEnterpriseService;
import com.gic.wechat.api.dto.qdto.group.QywxGroupMessageQDTO;
import com.gic.wechat.api.dto.qywx.QywxNewsArticleMessageDTO;
import com.gic.wechat.api.dto.qywx.QywxNewsSendMessageDTO;
import com.gic.wechat.api.dto.qywx.QywxXcxSendMessageDTO;
import com.gic.wechat.api.dto.qywx.group.QywxGroupMessageInfoDTO;
import com.gic.wechat.api.dto.qywx.group.QywxGroupMsgResp;
import com.gic.wechat.api.service.qywx.QywxSuiteApiService;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.time.DateUtils;
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.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

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

    @Autowired
    private QywxTagApiService qywxTagApiService;

    @Autowired
    private NoticeMessageApiService noticeMessageApiService;
    @Autowired
    private QywxSuiteApiService qywxSuiteApiService;

    @Autowired
    private Config config;

    @Autowired
    private WxEnterpriseService wxEnterpriseService;

    @Test
    public void test() {
        Map<String, String> contentMap = new HashMap<String, String>();
        contentMap.put("test", "test");

        Map<String, Object> extMap = new HashMap<String, Object>();
        extMap.put("test111", "tes111t");

        NoticeMessageQDTO noticeMessageQDTO = new NoticeMessageQDTO();
        noticeMessageQDTO.setEnterpriseId("ff8080815dacd3a2015dacd3ef5c0000");
        noticeMessageQDTO.setMessageType(NoticeMessageTypeEnum.ACTIVITY_AWARD.getType());
        noticeMessageQDTO.setContentMap(contentMap);
        noticeMessageQDTO.setClerkId("70eb25bfb94b4ffb998d4bd817fbd7e2");
        noticeMessageQDTO.setOptTargetId("testtatget");
        noticeMessageQDTO.setExtendContent(extMap);

        String str = "{\"clerkId\":\"fbc508e395f846ef9005852c420e1c4f\",\"enterpriseId\":\"ff8080815dacd3a2015dacd3ef5c0000\",\"contentMap\":{\"activityName\":\"活动名称测试\",\"rank\":\"1\"},\"messageType\":1007}";
        noticeMessageApiService.noticeMessageMq(str);
    }

    @Test
    public void test2() {


    }

    @Test
    public void test3() {
        qywxTagApiService.checkQywxSync("-1");
    }

    @Test
    public void batchSendMessage() {
//        NotifyMessageBatchQDTO qdto = new NotifyMessageBatchQDTO();
//        qdto.setStoreIdList(Lists.newArrayList("ff8080817c2b4c55017c30a4482800d7"));
//        qdto.setOptTargetId("-1");
//        Map<String, Object> map1 = new HashMap<>();
//        map1.put("materialNotifyId", 111);
//        qdto.setExtendContent(map1);
//        qdto.setEnterpriseId("ff8080815dacd3a2015dacd3ef5c0000");
//        qdto.setMessageType(NoticeMessageTypeEnum.MATERIAL_NEW_NOTIFY.getType());
//        qdto.setTemplateCode(NoticeMessageTypeEnum.MATERIAL_NEW_NOTIFY.getTemplateCode());
//        qdto.setTitle("素材上新通知");
//        Map<String, String> map = new HashMap<>();
//        map.put("content", "有新的素材了，快来看啊！");
//        qdto.setContentMap(map);
        NotifyMessageBatchQDTO batchQDTO = JSONObject.parseObject("{\"checkFlag\":false,\"contentMap\":{\"content\":\"新素材快去转发呀！！！\"},\"enterpriseId\":\"ff8080815dacd3a2015dacd3ef5c0000\",\"extendContent\":{\"materialNotifyId\":508693912927727617},\"messageType\":5001,\"optTargetId\":\"508693912927727617\",\"storeIdList\":[\"ff8080817c2b4c55017c30a4482800d7\"],\"templateCode\":\"material_new_notify\",\"title\":\"上新素材啦！！\"}", NotifyMessageBatchQDTO.class);
        noticeMessageApiService.batchSendMessage(batchQDTO);
    }

    @Test
    public void sendNewsMessageTest(){

        String wxEnterpriseId = "ca66a01b79474c40b3e7c7f93daf1a3b";
        WxEnterpriseQwDTO qwDTO = this.wxEnterpriseService.getQwInfo(wxEnterpriseId) ;
        String corpid = qwDTO.getThirdCorpid() ;
        QywxNewsSendMessageDTO messageDTO = new QywxNewsSendMessageDTO();
        messageDTO.setAgentId(Integer.parseInt(qwDTO.getAgentId()));
        messageDTO.setUserIds(Collections.singletonList("wo59NLDQAA2fEL1xL51lJIy9y2N2aulA"));
        QywxNewsArticleMessageDTO articleInfo = new QywxNewsArticleMessageDTO();
        articleInfo.setAppid(config.getAppid());
        articleInfo.setTitle("素材使用月报");
        articleInfo.setDescription("3月1日～3月31日的素材使用月报已生成，请查收");
        articleInfo.setPagepath("/monthList");
        articleInfo.setPicurl("https://platform-1251519181.cos.ap-shanghai.myqcloud.com/image/newdmwltest/enterprise_common-e1e8b3d46fdb4486ad545aae6cba67a1.png");

        messageDTO.setArticleMessages(Arrays.asList(articleInfo));
        /*boolean b = qywxSuiteApiService.sendMessage(corpid, config.getWxSuiteid(), messageDTO);
        System.out.println(b);*/
    }

    @Test
    public void queryMessageTest(){

        String wxEnterpriseId = "ca66a01b79474c40b3e7c7f93daf1a3b";
        WxEnterpriseQwDTO qwDTO = this.wxEnterpriseService.getQwInfo(wxEnterpriseId) ;
        QywxGroupMessageQDTO search = new QywxGroupMessageQDTO();
        search.setCorpid(qwDTO.getThirdCorpid());
        search.setSuiteid(config.getWxSuiteid());
        search.setChatType("single");
        Date date = DateUtils.addDays(new Date(), -1);
        //ServiceResponse<QywxGroupMsgResp> serviceResponse = qywxSuiteApiService.queryQywxGroupMessageList(search);

    }

    @Test
    public void convert(){
        String json = "{\"errcode\":0,\"next_cursor\":\"\",\"errmsg\":\"ok\",\"group_msg_list\":[{\"attachments\":[{\"image\":{\"media_id\":\"WWME_59NLDQAA808IWhrsFqod3IeQuyZDcw\"},\"msgtype\":\"image\"}],\"create_time\":1679207801,\"msgid\":\"msg59NLDQAAMSF0sKheZ-wwUy06iY1eoQ\",\"create_type\":0,\"text\":{\"content\":\"元宵快乐\"}}]}";

    }

}
