import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.content.api.qdto.activity.ContentActivityQDTO;
import com.gic.haoban.manage.api.dto.chat.GroupChatActivityHmDTO;
import com.gic.haoban.manage.api.dto.chat.GroupChatActivityRewardDTO;
import com.gic.haoban.manage.api.dto.chat.GroupChatActivityRewardDetailDTO;
import com.gic.haoban.manage.api.dto.chat.GroupChatHmDTO;
import com.gic.haoban.manage.api.qdto.chat.GroupChatActivityHmQDTO;
import com.gic.haoban.manage.api.qdto.chat.GroupChatActivityJoinQDTO;
import com.gic.haoban.manage.api.qdto.chat.GroupChatActivityQDTO;
import com.gic.haoban.manage.api.service.chat.GroupChatActivityApiService;
import com.gic.haoban.manage.api.service.chat.GroupChatActivityShareApiService;
import com.gic.haoban.manage.service.pojo.bo.chat.GroupChatActivityBaseContext;
import com.gic.haoban.manage.service.pojo.bo.chat.GroupChatActivityContext;
import com.gic.haoban.manage.service.service.chat.GroupChatActivityInviteLogService;
import com.gic.haoban.manage.service.service.chat.GroupChatActivityRewardLogService;
import com.gic.haoban.manage.service.service.chat.GroupChatActivityService;
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.*;

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

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

    @Autowired
    private GroupChatActivityApiService groupChatActivityApiService;
    @Autowired
    private GroupChatActivityService groupChatActivityService;
    @Autowired
    private GroupChatActivityShareApiService groupChatActivityShareApiService;
    @Autowired
    private GroupChatActivityInviteLogService groupChatActivityInviteLogService;

    @Test
    public void saveActivity() throws Exception{
        GroupChatActivityQDTO qdto = new GroupChatActivityQDTO();
        qdto.setActivityName("测试活动2");
        qdto.setStartDate(new Date("2024/10/11"));
        qdto.setEndDate(new Date("2025/10/10"));
        qdto.setActivityImg("123");
        qdto.setActivityInviteImg("123");
        qdto.setActivityDesc("123");
        qdto.setActivityShareTitle("123");
        qdto.setActivityShareImg("123");
        qdto.setActivityPosterImg("123");
        qdto.setInviteRewardType(2);
        qdto.setJoinType(2);
//        qdto.setJoinRule();
        qdto.setJoinChatHmId(709467854075881635L);
        qdto.setInviteType(1);
        qdto.setInviteMemberType(1);
        qdto.setLabelFlag(0);
//        qdto.setLabelJson();
        qdto.setExceptionNotice(0);
        qdto.setExceptionNoticeNum(0);
        qdto.setEnterpriseId("ff8080815dacd3a2015dacd3ef5c0000");
        qdto.setWxEnterpriseId("ca66a01b79474c40b3e7c7f93daf1a3b");
        qdto.setCreatorId("fefd1c81641711e69d0818c58a146fd2");
        qdto.setCreatorName("达摩管理");
        List<GroupChatActivityRewardDTO> rewardList = new ArrayList<>();
        GroupChatActivityRewardDTO rewardDTO = new GroupChatActivityRewardDTO();
        rewardDTO.setInviteNumber(1);
        rewardDTO.setInviteLevel(1);
        rewardDTO.setRewardInvite(1);
        rewardDTO.setLimitReward(3);
        Map<Integer, List<GroupChatActivityRewardDTO>> rewardMap = new HashMap<>();
        List<GroupChatActivityRewardDetailDTO> detailList = new ArrayList<>();
        GroupChatActivityRewardDetailDTO rewardDetail = new GroupChatActivityRewardDetailDTO();
        rewardDetail.setRewardType(1);
        rewardDetail.setRewardValue("2");
        rewardDetail.setRewardName("2积分");
        detailList.add(rewardDetail);
        rewardDTO.setDetailList(detailList);
        rewardList.add(rewardDTO);
        rewardMap.put(1, rewardList);
        rewardDTO = new GroupChatActivityRewardDTO();
        rewardDTO.setInviteNumber(1);
        rewardDTO.setInviteLevel(1);
        rewardDTO.setRewardInvite(2);
        rewardDTO.setLimitReward(1);
        List<GroupChatActivityRewardDetailDTO> detailList2 = new ArrayList<>();
        GroupChatActivityRewardDetailDTO rewardDetail2 = new GroupChatActivityRewardDetailDTO();
        rewardDetail2.setRewardType(1);
        rewardDetail2.setRewardValue("8");
        rewardDetail2.setRewardName("8积分");
        detailList2.add(rewardDetail2);
        rewardDTO.setDetailList(detailList2);
        rewardList.add(rewardDTO);
        rewardMap.put(2, rewardList);
        qdto.setRewardMap(rewardMap);
        List<GroupChatActivityJoinQDTO> joinList = new ArrayList<>();
        GroupChatActivityJoinQDTO join = new GroupChatActivityJoinQDTO();
        join.setMemberType(0);
        join.setFilterJson("");
        join.setFilterJsonView("");
        join.setIndex(2);
        join.setChatHmId(709467854075881635L);
        joinList.add(join);
        qdto.setJoinList(joinList);
        join = new GroupChatActivityJoinQDTO();
        join.setMemberType(0);
        join.setFilterJson("");
        join.setFilterJsonView("");
        join.setIndex(1);
        join.setChatHmId(709467854075881635L);
        joinList.add(join);
        qdto.setJoinList(joinList);
        ServiceResponse<Long> response = groupChatActivityApiService.saveActivity(qdto);
        System.out.println(JSONObject.toJSONString(response));
    }

    @Test
    public void saveActivity2() throws Exception{
        String s = "{\"activityDesc\":\"亲爱的用户，为了回馈大家对我们社群的支持，我们推出了邀请有礼活动！只需邀请好友加入我们的社群，您和好友都有机会获得丰厚奖励！\\n\\n活动规则：\\n\\n邀请方式：分享专属邀请链接，好友通过链接成功加入社群后即算有效邀请。\\n奖励机制：\\n成功邀请1名好友，即获【XX奖励】。\\n成功邀请3名好友，即获【XX奖励】。\\n成功邀请5名好友及以上，还有机会赢取神秘大礼！\\n活动时间：XX年XX月XX日至XX年XX月XX日。\\n快来邀请你的好友们一起加入吧！更多好友，更多奖励，等你来拿！\",\"activityImg\":\"https://platform-1251519181.cos.ap-shanghai.myqcloud.com/image/communityImgs/activity_img.png\",\"activityInviteImg\":\"https://platform-1251519181.cos.ap-shanghai.myqcloud.com/image/communityImgs/invite_img.png\",\"activityName\":\"邀请好友，共享好礼！定向\",\"activityPosterImg\":\"https://platform-1251519181.cos.ap-shanghai.myqcloud.com/image/communityImgs/poster_img.png\",\"activityShareImg\":\"https://platform-1251519181.cos.ap-shanghai.myqcloud.com/image/communityImgs/share_card.png\",\"activityShareTitle\":\"进群获取奖励，快来和我一起参加吧\",\"chatActivityId\":720266914303074307,\"creatorId\":\"fefd1c81641711e69d0818c58a146fd2\",\"creatorName\":\"达摩管理\",\"endDate\":1730390399000,\"enterpriseId\":\"ff8080815dacd3a2015dacd3ef5c0000\",\"exceptionNotice\":0,\"inviteMemberType\":0,\"inviteRewardType\":2,\"inviteType\":0,\"joinChatHmId\":640618380658762018,\"joinList\":[{\"chatActivityId\":720266914303074307,\"chatActivityJoinId\":720266914529566729,\"chatHmId\":640618380658762018,\"filterJson\":\"{\\\"list\\\":[{\\\"type\\\":\\\"or\\\",\\\"list\\\":[{\\\"type\\\":\\\"data\\\",\\\"data\\\":{\\\"key\\\":\\\"costTimes\\\",\\\"compute\\\":\\\"=\\\",\\\"value\\\":\\\"10\\\"}}]}],\\\"type\\\":\\\"and\\\"}\",\"filterJsonView\":\"\",\"index\":1,\"memberType\":0},{\"chatActivityId\":720266914303074307,\"chatActivityJoinId\":720266914630230026,\"chatHmId\":719956093060538621,\"filterJson\":\"0bead493b5454f339dce99127a43fd9b\",\"filterJsonView\":\"\",\"index\":2,\"memberType\":1},{\"chatActivityId\":720266914303074307,\"chatActivityJoinId\":720266914705727499,\"chatHmId\":720265997814095873,\"filterJson\":\"\",\"filterJsonView\":\"\",\"index\":3,\"memberType\":0}],\"joinRule\":\"[{\\\"openFlag\\\":0,\\\"val\\\":1},{\\\"openFlag\\\":0,\\\"val\\\":2},{\\\"openFlag\\\":0,\\\"val\\\":3},{\\\"openFlag\\\":0,\\\"val\\\":4}]\",\"joinType\":2,\"labelFlag\":1,\"labelJson\":\"{\\\"id\\\":\\\"10678a05583047a2836a43852c5d6123\\\",\\\"name\\\":\\\"7777\\\"}\",\"rewardMap\":{\"1\":[{\"chatActivityId\":720266914303074307,\"detailList\":[{\"chatActivityRewardId\":720266914449874950,\"rewardName\":\"[{\\\"name\\\":\\\"3积分\\\"}]\",\"rewardType\":1,\"rewardValue\":\"3\"},{\"chatActivityRewardId\":720266914458263559,\"rewardName\":\"[{\\\"name\\\":\\\"3成长值\\\"}]\",\"rewardType\":2,\"rewardValue\":\"3\"},{\"chatActivityRewardId\":720266914466652168,\"rewardName\":\"[{\\\"id\\\":\\\"ff8080818edf6b8b018ee07e6b53001f\\\",\\\"name\\\":\\\"子管理创建\\\",\\\"status\\\":0}]\",\"rewardType\":3,\"rewardValue\":\"ff8080818edf6b8b018ee07e6b53001f\"}],\"inviteLevel\":1,\"inviteNumber\":1,\"limitReward\":1,\"rewardInvite\":1},{\"chatActivityId\":720266914303074307,\"detailList\":[{\"chatActivityRewardId\":720266914449874950,\"rewardName\":\"[{\\\"name\\\":\\\"3积分\\\"}]\",\"rewardType\":1,\"rewardValue\":\"3\"},{\"chatActivityRewardId\":720266914458263559,\"rewardName\":\"[{\\\"name\\\":\\\"3成长值\\\"}]\",\"rewardType\":2,\"rewardValue\":\"3\"},{\"chatActivityRewardId\":720266914466652168,\"rewardName\":\"[{\\\"id\\\":\\\"ff8080818edf6b8b018ee07e6b53001f\\\",\\\"name\\\":\\\"子管理创建\\\",\\\"status\\\":0}]\",\"rewardType\":3,\"rewardValue\":\"ff8080818edf6b8b018ee07e6b53001f\"}],\"inviteLevel\":2,\"inviteNumber\":2,\"limitReward\":1,\"rewardInvite\":1},{\"chatActivityId\":720266914303074307,\"detailList\":[{\"chatActivityRewardId\":720266914449874950,\"rewardName\":\"[{\\\"name\\\":\\\"3积分\\\"}]\",\"rewardType\":1,\"rewardValue\":\"3\"},{\"chatActivityRewardId\":720266914458263559,\"rewardName\":\"[{\\\"name\\\":\\\"3成长值\\\"}]\",\"rewardType\":2,\"rewardValue\":\"3\"},{\"chatActivityRewardId\":720266914466652168,\"rewardName\":\"[{\\\"id\\\":\\\"ff8080818edf6b8b018ee07e6b53001f\\\",\\\"name\\\":\\\"子管理创建\\\",\\\"status\\\":0}]\",\"rewardType\":3,\"rewardValue\":\"ff8080818edf6b8b018ee07e6b53001f\"}],\"inviteLevel\":3,\"inviteNumber\":3,\"limitReward\":1,\"rewardInvite\":1}]},\"startDate\":1729180800000,\"wxEnterpriseId\":\"b18ffdc9d0644912865a248859914d80\"}";
        groupChatActivityService.saveActivity(JSONObject.parseObject(s,GroupChatActivityQDTO.class));
    }

    @Test
    public void listGroupChatHm() throws Exception{
        GroupChatActivityHmQDTO groupChatActivityHmQDTO = new GroupChatActivityHmQDTO();
        groupChatActivityHmQDTO.setChatActivityId(716721754221821953L);
        groupChatActivityHmQDTO.setInviterMemberId("ff808081906def5201907645bfc7005a");
        groupChatActivityHmQDTO.setInviteeMemberId("ff8080818215878101821949a845003c");
        groupChatActivityHmQDTO.setInviteeUnionId("123456");
        groupChatActivityHmQDTO.setEnterpriseId("ff8080815dacd3a2015dacd3ef5c0000");
        groupChatActivityHmQDTO.setDistrictCode("330100");
        groupChatActivityHmQDTO.setLatitude("30.247100");
        groupChatActivityHmQDTO.setLongitude("120.209000");
        groupChatActivityHmQDTO.setSkipArea(false);
        ServiceResponse<List<GroupChatActivityHmDTO>> listServiceResponse = groupChatActivityShareApiService.listGroupChatHm(groupChatActivityHmQDTO);
        System.out.println(JSONObject.toJSONString(listServiceResponse));
    }

    @Test
    public void groupChatInviteMQ() throws Exception{
//        groupChatActivityInviteLogService.groupChatInviteMQ("ff8080815dacd3a2015dacd3ef5c0000","123456",717384135185809471L);
    }

    @Test
    public void dealRewardAndSaveLog() throws Exception{
        GroupChatActivityBaseContext context = new GroupChatActivityBaseContext();
        context.setChatUserId(717384135185809471L);
        context.setChatActivityId(716721754221821953L);
        context.setInviterMemberId("ff808081906def5201907645bfc7005a");
        context.setEnterpriseId("ff8080815dacd3a2015dacd3ef5c0000");
        groupChatActivityInviteLogService.groupChatInvite(context);
    }
}
