package com.poyee;

import com.alibaba.fastjson.JSONObject;
import com.tzy.PoyiApplication;
import com.tzy.common.constant.MqConstans;
import com.tzy.common.exception.AppAssert;
import com.tzy.common.utils.DateUtils;
import com.tzy.elasticsearch.dto.CardGroupInfoGoodsDTO;
import com.tzy.sportcard.api.dto.ResourceData;
import com.tzy.sportcard.api.service.GroupApiService;
import com.tzy.sportcard.group.domain.CardGroupOrderInfo;
import com.tzy.sportcard.group.domain.TzyMerchantInfo;
import com.tzy.sportcard.group.mapper.CardGroupOrderInfoMapper;
import com.tzy.sportcard.group.mapper.TzyMerchantInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
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.math.BigDecimal;
import java.util.Date;

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

	@Resource
	private TzyMerchantInfoMapper tzyMerchantInfoMapper;
	@Resource
	private CardGroupOrderInfoMapper cardGroupOrderInfoMapper;
	@Autowired
	private GroupApiService groupApiService;
	@Autowired
	private PreparedSql preparedSql;

	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Test
	public void testGroupEsSync() {
		//同步es数据
		CardGroupInfoGoodsDTO param = new CardGroupInfoGoodsDTO().setSyncType(CardGroupInfoGoodsDTO.SYNC_TYPE_EDIT).setGroupInfoId(1447L)
		.setSoldTimeEnd(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date())).setStatus("203");
		rabbitTemplate.convertAndSend(MqConstans.EXCHANGE_GROUP_GOODS_SYNC_ES, MqConstans.ROUTING_KEY_GROUP_GOODS_SYNC_ES, JSONObject.toJSONString(param));
	}

	@Test
	public void testNull() {
		TzyMerchantInfo merchantInfo=null;
		AppAssert.notNull(merchantInfo,"null");
	}

	@Test
	public void testTeamGoods() {
		int count = groupApiService.selectOrderCountByTeam(638, "独行侠", false);
		System.out.println(count);
	}

	@Test
	public void testCallBack()  {
		String no ="GU120220309508294129";
		callBack(no);
	}

	private void callBack(String no) {
		CardGroupOrderInfo orderInfo = cardGroupOrderInfoMapper.getByOrderNo(no);
		ResourceData resourceData=new ResourceData();
		resourceData.setOut_trade_no(no);
		resourceData.setTrade_type(orderInfo.getPaymentType());//支付类型
		resourceData.setTransaction_id(String.valueOf(new Date().getTime()));//支付流水号，微信支付订单号
		resourceData.setSuccess_time(DateUtils.dateTime(new Date()));//支付成功时间
		resourceData.setTrade_state("TRADE_SUCCESS");//支付状态
		resourceData.setTrade_state_desc("TRADE_SUCCESS");//支付状态说明
		resourceData.setBank_type("支付宝");//付款银行
		ResourceData.PayAmount payAmount = new ResourceData.PayAmount();
		payAmount.setTotal(orderInfo.getActualPayment().multiply(new BigDecimal(100)).intValue());
		payAmount.setPayer_total(payAmount.getTotal());
		resourceData.setAmount(payAmount);
		groupApiService.paymentResult(resourceData);
	}


}
