package com.jxy.marketall.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jxy.common.core.text.Convert;
import com.jxy.common.core.utils.StringUtils;
import com.jxy.common.core.web.Result.AjaxResult;
import com.jxy.common.core.web.Result.PageResult;
import com.jxy.common.redis.service.RedisService;
import com.jxy.marketall.dto.BiAnDto;
import com.jxy.marketall.dto.ETHMintedCountDto;
import com.jxy.marketall.dto.USDMintedDto;
import com.jxy.marketall.entity.BTCBigAmountVout;
import com.jxy.marketall.mapper.USDMapper;
import com.jxy.marketall.service.USDService;
import com.jxy.marketall.vo.ETHMintedVo;
import com.jxy.marketall.vo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Date 2023/4/17 11:36
 * @Author 杜懿
 */
@Service
public class USDServiceImpl implements USDService {

    @Autowired
    private USDMapper usdMapper;

    @Autowired
    private RedisService redisService;

    @Override
    public AjaxResult getUSDTMinted(QueryVo vo) {
        List<ETHMintedCountDto> list = usdMapper.getDailyMinted(vo, "usdt");
        //if(list.size() < 1){
        //    return AjaxResult.error("后台数据异常，请稍后再试");
        //}
        return  AjaxResult.success(list);
    }

    @Override
    public AjaxResult getUSDTMintedList(ETHMintedVo vo) {

        String action = vo.getAction();
        //usdt 铸币对应action:issue 销毁action：redeem
        ETHMintedVo hv = handleVo(vo,"usdt");

        List<USDMintedDto> list = usdMapper.getETHMintedList(hv);
        if(StringUtils.isNotNull(hv.getAddress())){
            //按地址查
            list.addAll(usdMapper.getETHMintedListToAddr(hv));
        }

        list = list.stream().distinct().collect(Collectors.toList());
        PageResult result = new PageResult();
        if(list.size() > 10000){
            result.setCount(10000L);
        }

        System.out.println(action);
        list.stream().forEach(e -> e.setAction(action));


        result.setCount(Convert.toLong(list.size(),0L));
        result.setTotalPage(getTotalPage(list.size(),hv.getPage(),hv.getSize()));
        Collections.reverse(list);
        result.setData(list.stream().skip((hv.getPage() - 1) * hv.getSize()).limit(hv.getSize()).collect(Collectors.toList()));

        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult getUSDCMinted(QueryVo vo) {
        List<ETHMintedCountDto> list = usdMapper.getDailyMinted(vo, "usdc");
        //if(list.size() < 1){
        //    return AjaxResult.error("后台数据异常，请稍后再试");
        //}
        return  AjaxResult.success(list);
    }

    @Override
    public AjaxResult getUSDCMintedList(ETHMintedVo vo) {
        //usdc 铸币对应action:mint 销毁action：burn
        ETHMintedVo hv = handleVo(vo,"usdc");

        List<USDMintedDto> list = usdMapper.getETHMintedList(hv);
        if(StringUtils.isNotNull(hv.getAddress())){
            //按地址查
            list.addAll(usdMapper.getETHMintedListToAddr(hv));
        }

        list = list.stream().distinct().collect(Collectors.toList());
        PageResult result = new PageResult();
        if(list.size() > 10000){
            result.setCount(10000L);
        }

        result.setCount(Convert.toLong(list.size(),0L));
        result.setTotalPage(getTotalPage(list.size(),hv.getPage(),hv.getSize()));
        Collections.reverse(list);
        result.setData(list.stream().skip((hv.getPage() - 1) * hv.getSize()).limit(hv.getSize()).collect(Collectors.toList()));

        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult getBUSDMinted(QueryVo vo) {
        List<ETHMintedCountDto> list = usdMapper.getDailyMinted(vo, "busd");
        //if(list.size() < 1){
        //    return AjaxResult.error("后台数据异常，请稍后再试");
        //}
        return  AjaxResult.success(list);
    }

    @Override
    public AjaxResult getBUSDMintedList(ETHMintedVo vo) {
        String action = vo.getAction();
        //busd 铸币对应action:increaseSupply 销毁action：decreaseSupply
        ETHMintedVo hv = handleVo(vo,"busd");

        List<USDMintedDto> list = usdMapper.getETHMintedList(hv);
        if(StringUtils.isNotNull(hv.getAddress())){
            //按地址查
            list.addAll(usdMapper.getETHMintedListToAddr(hv));
        }

        list = list.stream().distinct().collect(Collectors.toList());
        PageResult result = new PageResult();
        if(list.size() > 10000){
            result.setCount(10000L);
        }


        list.stream().forEach(e -> e.setAction(action));

        result.setCount(Convert.toLong(list.size(),0L));
        result.setTotalPage(getTotalPage(list.size(),hv.getPage(),hv.getSize()));

        Collections.reverse(list);
        result.setData(list.stream().skip((hv.getPage() - 1) * hv.getSize()).limit(hv.getSize()).collect(Collectors.toList()));


        return AjaxResult.success(result);
    }

    /**
     * 处理请求体
     */
    public ETHMintedVo handleVo(ETHMintedVo vo,String coin){

        vo.setCoin(coin);

        //处理total  判断是amount/txid/address
        if(StringUtils.isNotNull(vo.getTotal())){
            Double amount = Convert.toDouble(vo.getTotal().replaceAll(",",""));
            if (StringUtils.isNotNull(amount)){
                vo.setAmount(amount);
            }else {
                if(vo.getTotal().length() >=64){
                    vo.setTxid(vo.getTotal());
                }else {
                    vo.setAddress(vo.getTotal());
                }
            }
        }

        String action = vo.getAction();
        //处理action  action入参仅有 铸造mint  销毁burn 所以需要根据一定规则
        // 规则如下
        //usdc | mint| burn   不做处理
        //usdt | redeem | issue
        //busd | increaseSupply | decreaseSupply
        if("USDT".equals(coin.toUpperCase())){
            if("burn".equals(action.toLowerCase())){
                //传入销毁对应字段burn 需要转换为redeem
                vo.setAction("redeem");
            }else{
                //其他情况默认查询铸造
                vo.setAction("issue");
            }
        }

        if("BUSD".equals(coin.toUpperCase())){
            if("burn".equals(action.toLowerCase())){
                //传入销毁对应字段burn 需要转换为decreaseSupply
                vo.setAction("decreaseSupply");
            }else{
                //其他情况默认查询铸造
                vo.setAction("increaseSupply");
            }
        }
        return vo;
    }

    public Long getTotalPage(int total,Integer page,Integer size){

        if (size == 0) {
            return 0L;
        }
        long pages = total / size;
        if (total % size != 0) {
            pages++;
        }
        return pages;

    }
}
