package com.poyee;

import com.alibaba.fastjson.JSONObject;
import com.tzy.PoyiApplication;
import com.tzy.common.utils.DateUtils;
import com.tzy.common.utils.bean.JSONTools;
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.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

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

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


	@Test
	public void tesMerchant() {
		TzyMerchantInfo merchantInfo = new TzyMerchantInfo();
		merchantInfo.setId(13L);
		//merchantInfo.setCheckStatus(1);
		//merchantInfo.setLiveType("work_wx");
		//merchantInfo.setWorkWechatAuth("yu");
		//List<LivingAuthDTO> data = new ArrayList<>();
		//data.add(new LivingAuthDTO(179,"summer"));
		//data.add(new LivingAuthDTO(172,"yu"));
		//merchantInfo.setLivingAuthConfig(JSONTools.obj2json(data));
		tzyMerchantInfoMapper.updateMerchantAuth(merchantInfo);
	}

	@Test
	public void tesMerchantRyAuth() {
		TzyMerchantInfo merchantInfo = tzyMerchantInfoMapper.selectTzyMerchantInfoById(13L);
		String livingAuthConfig = merchantInfo.getLivingAuthConfig();
		List<JSONObject> livingAuthDTOS = JSONTools.jsonStr2obj(livingAuthConfig, List.class);
		List<Map<String, Object>> list = new ArrayList<>(livingAuthDTOS.size());
		for (JSONObject s : livingAuthDTOS) {
			Map<String, Object> data = new HashMap<>(1);
			data.put("value", s.get("id"));
			data.put("label", s.get("nickName"));
			list.add(data);
		}
		System.out.println(list);
	}


	@Test
	public void testCallBack()  {
		String no ="GU152022022166674388";
		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);
	}


}
