package com.poyee;

import com.tzy.PoyiApplication;
import com.tzy.common.constant.Constants;
import com.tzy.common.utils.DateUtils;
import com.tzy.dto.ImUserDTO;
import com.tzy.framework.util.RedisUtils;
import com.tzy.sportcard.api.domain.AppUserInfoDto;
import com.tzy.sportcard.api.dto.prize.AppActPrizeDTO;
import com.tzy.sportcard.api.mapper.MineApiMapper;
import com.tzy.sportcard.api.service.GroupApiService;
import com.tzy.sportcard.api.service.MineApiService;
import com.tzy.sportcard.group.mapper.CardGroupOrderInfoMapper;
import com.tzy.sportcard.group.mapper.TzyMerchantInfoMapper;
import com.tzy.sportcard.group.service.AppActService;
import com.tzy.sportcard.group.service.ICardGroupLivesConfigService;
import com.tzy.sportcard.point.domain.AppUserPointRecord;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.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.SpringRunner;
import org.teasoft.bee.osql.PreparedSql;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@SpringBootTest(classes = PoyiApplication.class)
@RunWith(SpringRunner.class)
class AppUserTest {

	@Resource
	private TzyMerchantInfoMapper tzyMerchantInfoMapper;
	@Resource
	private CardGroupOrderInfoMapper cardGroupOrderInfoMapper;
	@Autowired
	private GroupApiService groupApiService;
	@Autowired
	private PreparedSql preparedSql;
	@Resource
	private MineApiMapper mineApiMapper;
	@Autowired
	private MineApiService mineApiService;
	@Autowired
	private AppActService appActService;
	@Resource
	private RedisUtils redisUtils;
	@Resource
	private ICardGroupLivesConfigService livesConfigService;

	@Test
	public void tesLiveUser() {
		List<ImUserDTO> users = livesConfigService.findLivingRoomUser(13, Arrays.asList(15596,2442), false, 1, 200);
		log.info("查询到用户:{}",users.size());
	}


	private AppUserInfoDto getUserInfo(){
		AppUserInfoDto appUserInfo = new AppUserInfoDto();
		// account id
		appUserInfo.setId(236);
		List<AppUserInfoDto> appUserInfoDtos = mineApiMapper.searchUserBaseInfo(appUserInfo);
		return appUserInfoDtos.get(0);
	}


	@Test
	public void tesDrawPrize() {
		AppUserInfoDto userInfo = getUserInfo();
		int i=0;
		List<AppActPrizeDTO> drawPrizeList = new ArrayList<>();
		while(true) {
			if(i>50){
				break;
			}
			AppActPrizeDTO drawPrize = appActService.drawPrize(1, userInfo,null);
			drawPrizeList.add(drawPrize);
			//log.info("本次抽到:{}",drawPrize.getName());
			i++;
		}
		Map<String, List<AppActPrizeDTO>> map = drawPrizeList.stream().collect(Collectors.groupingBy(AppActPrizeDTO::getName));
		map.entrySet().forEach(entry -> log.info("抽奖类型:{},次数:{}",entry.getKey(),entry.getValue().size()));
	}

	@Test
	public void tesUserPoint() {
		AppUserPointRecord pointRecord = new AppUserPointRecord();
		//pointRecord.setType(Constants.POINT_TYPE_GOODS);
		//pointRecord.setChangePoint(1);
		//pointRecord.setUserId(2457L);
		//pointRecord.setOrderId(22788L);
		//pointRecord.setOrderNo("SO892022062820626522");
		pointRecord.setType(Constants.POINT_TYPE_USER_GROWTH);
		pointRecord.setChangePoint(200000L);
		pointRecord.setUserId(2442L);
		pointRecord.setOrderId(106223L);
		pointRecord.setOrderNo("SO782023032713209871");
		pointRecord.setMerchantId(86L);
		mineApiService.addUserPoint(pointRecord);
	}


	@Test
	public void tesUserQuery() {
		AppUserInfoDto appUserInfo = new AppUserInfoDto();
		appUserInfo.setPhone("15618709098");
		List<AppUserInfoDto> appUserInfoDtos = mineApiMapper.searchUserBaseInfo(appUserInfo);
		System.out.println(appUserInfoDtos);
	}

	@Test
	public void tesUserRedis() {
		String day = DateUtils.dateTime();
		String key = Constants.MERCHANT_USER_ORDER_NUM + day + 11;
		redisUtils.hset(key, "1", 1);
		redisUtils.expire(key, Constants.dayForSecond);
	}





}
