package com.liquidnet.service.chime.test;

import com.liquidnet.common.third.easemob.util.EasemobUtil;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.commons.lang.util.RandomUtil;
import com.liquidnet.service.chime.biz.ChimeUserBiz;
import com.liquidnet.service.chime.dto.ChimeUserInfoDto;
import com.liquidnet.service.chime.dto.ChimeUserListQueryReqDto;
import com.liquidnet.service.chime.dto.ChimeUserUpdateReqDto;
import com.liquidnet.service.chime.dto.NewPageResult;
import com.liquidnet.service.chime.service.IChimePerformanceService;
import com.liquidnet.service.chime.service.IChimeUserService;
import com.liquidnet.service.chime.utils.DataUtils;
import com.liquidnet.service.chime.vo.mongo.ChimeUserInfoVo;
import com.liquidnet.service.chime.vo.mongo.ChimeUserTagsMappingVo;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author AnJiabin <anjiabin@zhengzai.tv>
 * @version V1.0
 * @Description: TODO
 * @class: TestDataUtils
 * @Package com.liquidnet.service.chime.test
 * @Copyright: LightNet @ Copyright (c) 2021
 * @date 2021/9/12 15:44
 */
@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class TestDataUtils {
    @Autowired
    private DataUtils dataUtils;

    @Autowired
    private EasemobUtil easemobUtil;

    @Autowired
    private ChimeUserBiz chimeUserBiz;

    @Autowired
    private IChimePerformanceService chimePerformanceService;

    @Autowired
    private IChimeUserService chimeUserService;

    @Test
    public void getPerformanceCount(){
        System.out.println(chimeUserBiz.getSexDesc("{\"val\":\"MS01\",\"desc\":\"女3性\"}"));
    }

    @Test
    public void updateUserInfo(){
        String sex = "{\"val\":\"MS01\",\"desc\":\"女性\"}";
        String tags = "[ { \"val\": \"MMS0102\", \"desc\": \"B\" }, { \"val\": \"MMS0101\", \"desc\": \"A\" }, { \"val\": \"MMS0201\", \"desc\": \"C\" } ]";
        ChimeUserUpdateReqDto reqDto = ChimeUserUpdateReqDto.getNew();
//        reqDto.setUserId("TEST202109191447591475263969");
//        reqDto.setUserId("TEST202109191447591490063014");
        reqDto.setUserId("TEST202109191447591501070250");
        reqDto.setSex(sex);
        reqDto.setTags(tags);
        chimeUserService.updateUserInfo(reqDto);
    }

    @Test
    public void getUserImgList(){
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        Future future = executorService.submit(new Callable(){
            public Object call() throws Exception {
                System.out.println("Asynchronous Callable");
                return "Callable Result";
            }
        });

        try {
            System.out.println("future.get() = " + future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void createUserInfoVo(){
        ExecutorService executorService = Executors.newFixedThreadPool(200);

        long start = System.currentTimeMillis();
        for(int i=0;i<1000000;i++){
            int num = i+1;
            Future future = executorService.submit(new Callable(){
                public Object call() throws Exception {
                    ChimeUserInfoVo chimeUserInfoVo = ChimeUserInfoVo.getNew();
                    chimeUserInfoVo.setUserId("TEST"+ IDGenerator.nextTimeId());
//                    chimeUserInfoVo.setPassWord("123456");
                    chimeUserInfoVo.setSex("男");
//                    chimeUserInfoVo.setUserName("TestName"+num);
//                    chimeUserInfoVo.setUserMobile("1381111"+num);
//                    chimeUserInfoVo.setAvatar("https://img.zhengzai.tv/user/2021/07/27/a4cc2a4e6dcd44d1812dc60e079086b4.png");
//                    chimeUserInfoVo.setUserCover("https://img.zhengzai.tv/user/2021/07/27/a4cc2a4e6dcd44d1812dc60e079086b4.png");
//                    chimeUserInfoVo.setBirthday("2021-09-01");
//                    chimeUserInfoVo.setSignature("测试签名");
//                    chimeUserInfoVo.setArea("山西省 太原市 迎泽区");
                    chimeUserInfoVo.setJoinPerformanceId("-1");
//                    easemobUtil.createUser(chimeUserInfoVo.getUserId(),chimeUserInfoVo.getPassWord());
                    dataUtils.createChimeUser(chimeUserInfoVo);
                    //插入userTagMapping
                    List<ChimeUserTagsMappingVo> userTagList = new ArrayList<>();
                    for(int j=0;j<5;j++){
                        ChimeUserTagsMappingVo chimeUserTagDto = ChimeUserTagsMappingVo.getNew();
                        chimeUserTagDto.setTagCode("tags"+(j+1));
                        chimeUserTagDto.setTagDesc("标签"+(j+1));
                        userTagList.add(chimeUserTagDto);
                    }
                    dataUtils.createUserTagMapping(userTagList);
                    return Boolean.parseBoolean("true");
                }
            });
//            Future future = executorService.submit(new Runnable() {
//                public void run() {
//                    chimeUserInfoVo chimeUserInfoVo = chimeUserInfoVo.getNew();
//                    chimeUserInfoVo.setUserId("TEST"+ IDGenerator.nextTimeId());
//                    chimeUserInfoVo.setPassWord("123456");
//                    chimeUserInfoVo.setSex("男");
//                    chimeUserInfoVo.setUserName("TestName"+num);
//                    chimeUserInfoVo.setUserMobile("1381111"+num);
//                    chimeUserInfoVo.setAvatar("https://img.zhengzai.tv/user/2021/07/27/a4cc2a4e6dcd44d1812dc60e079086b4.png");
//                    chimeUserInfoVo.setUserCover("https://img.zhengzai.tv/user/2021/07/27/a4cc2a4e6dcd44d1812dc60e079086b4.png");
//                    chimeUserInfoVo.setBirthday("2021-09-01");
//                    chimeUserInfoVo.setSignature("测试签名");
//                    chimeUserInfoVo.setArea("山西省 太原市 迎泽区");
//                    chimeUserInfoVo.setJoinPerformanceId("-1");
//                    List<ChimeUserTagDto> imgList = new ArrayList<>();
//                    for(int j=0;j<5;j++){
//                        ChimeUserTagDto chimeUserTagDto = ChimeUserTagDto.getNew();
//                        chimeUserTagDto.setUserId(chimeUserInfoVo.getUserId());
//                        chimeUserTagDto.setTagCode("tags"+(j+1));
//                        chimeUserTagDto.setTagDesc("标签"+(j+1));
//                        imgList.add(chimeUserTagDto);
//                    }
//                    chimeUserInfoVo.setUserTags(imgList);
////                    easemobUtil.createUser(chimeUserInfoVo.getUserId(),chimeUserInfoVo.getPassWord());
//                    dataUtils.createChimeUser(chimeUserInfoVo);
//                }
//            });
            try {
                System.out.println("执行第几个===="+num+" future.get()=="+future.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        System.out.println("耗时："+(System.currentTimeMillis() - start)/1000 + "秒");

        executorService.shutdown();
//        EasemobUtil.createUsers();
    }

    @Test
    public void createUserInfoNewVo(){
        ExecutorService executorService = Executors.newFixedThreadPool(200);

        long start = System.currentTimeMillis();
        List<ChimeUserInfoVo> voNewList = new ArrayList<>();

        int totalNum = 400000;
        int pageSize = 1000;
        int pageNum = 1;
        int totalPage = totalNum % pageSize == 0 ? totalNum / pageSize : (totalNum / pageSize) + 1;;
        for(int i=0;i<totalNum;i++){
            try {
                int num = i+1;

                ChimeUserInfoVo chimeUserInfoVo = ChimeUserInfoVo.getNew();
                chimeUserInfoVo.setUserId("TEST"+ IDGenerator.nextTimeId());
                chimeUserInfoVo.setSex("男");
//                chimeUserInfoVo.setUserName("TestName"+num);
//                chimeUserInfoVo.setUserMobile("1381111"+num);
//                chimeUserInfoVo.setAvatar("https://img.zhengzai.tv/user/2021/07/27/a4cc2a4e6dcd44d1812dc60e079086b4.png");
//                chimeUserInfoVo.setBirthday("2021-09-01");
//                chimeUserInfoVo.setSignature("测试签名");
//                chimeUserInfoVo.setArea("山西省 太原市 迎泽区");
                chimeUserInfoVo.setJoinPerformanceId("-1");
//                    easemobUtil.createUser(chimeUserInfoVo.getUserId(),chimeUserInfoVo.getPassWord());
                List<ChimeUserTagsMappingVo> userTagList = new ArrayList<>();
                for(int j=0;j<5;j++){
                    ChimeUserTagsMappingVo chimeUserTagDto = ChimeUserTagsMappingVo.getNew();
                    chimeUserTagDto.setTagCode("tags"+ RandomUtil.getRandomInt(1,10)+j);
                    chimeUserTagDto.setTagDesc("标签"+  RandomUtil.getRandomInt(1,10)+j);
                    userTagList.add(chimeUserTagDto);
                }
                chimeUserInfoVo.setUserTagsVoList(userTagList);
                voNewList.add(chimeUserInfoVo);


                if(num%pageSize==0){
                    dataUtils.createBatchChimeUser(voNewList);
                    voNewList = new ArrayList<>();
//                    Future future = executorService.submit(new Callable(){
//                        public Object call() throws Exception {
//
//                            dataUtils.createBatchChimeUser(voNewList);
//
//                            return Boolean.parseBoolean("true");
//                        }
//                    });
                    System.out.println("=================执行第几批次============"+pageNum);
                    pageNum++;
                }
                System.out.println("执行第几个===="+num);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("耗时："+(System.currentTimeMillis() - start)/1000 + "秒");

        executorService.shutdown();
//        EasemobUtil.createUsers();
    }

    @Test
    public void testUserList(){
        for(int i=0;i<10;i++){
            long startTime = System.currentTimeMillis();
            ChimeUserListQueryReqDto reqDto = ChimeUserListQueryReqDto.getNew();
//        reqDto.setPerformanceId("-1");
            reqDto.setPerformanceId("-1");
            reqDto.setPageNum(1);
            reqDto.setPageSize(50);
            reqDto.setOrderItem(null);
            reqDto.setOrderSc(null);
            reqDto.setSex("男");
//            reqDto.setTags("tags40,tags80");
            NewPageResult<ChimeUserInfoDto> pageInfo = chimePerformanceService.getUserListByCon(reqDto);
            log.info("总共获取总数pagecount:{}",pageInfo.getList().size());
            pageInfo.getList().forEach(dto -> {
//                System.out.println(dto.getUserId());
//                System.out.println(JsonUtils.toJson(dto.getUserTags()));
            });
            long endTime = System.currentTimeMillis();
            log.info("耗时：{}ms",endTime-startTime);
        }
    }
}
