package com.m2pool.oapi.service.impl;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.m2pool.common.core.utils.DateUtils;
import com.m2pool.common.core.utils.StringUtils;
import com.m2pool.common.core.web.Result.AjaxResult;
import com.m2pool.oapi.dto.HashRateDto;
import com.m2pool.oapi.dto.HashRateHistory24hDto;
import com.m2pool.oapi.dto.HashRateHistory30mDto;
import com.m2pool.oapi.dto.HashRateRealDto;
import com.m2pool.oapi.enums.PoolUnits;
import com.m2pool.oapi.mapper.MinerMapper;
import com.m2pool.oapi.service.MinerService;
import com.m2pool.oapi.utils.EnumUtils;
import com.m2pool.oapi.vo.AccountMinerDateVo;
import com.m2pool.oapi.vo.AccountMinerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @Description TODO
 * @Date 2024/9/13 10:49
 * @Author 杜懿
 */
@Service
public class MinerServiceImpl implements MinerService {

    @Autowired
    private MinerMapper minerMapper;


    @Override
    public AjaxResult getHashRateReal(AccountMinerVo vo) {
        PoolUnits unit = (PoolUnits) EnumUtils.get(PoolUnits.class, vo.getCoin());
        if(StringUtils.isNull(unit)){
            return AjaxResult.error("coin error");
        }

        HashRateRealDto dto = minerMapper.getMinerRealHashRateByCoinAndAccountMiner(vo);

        dto.setUnit(unit.getUnit());
        return AjaxResult.success(dto);
    }

    @Override
    public AjaxResult getHashRateHistory(AccountMinerDateVo vo) {
        PoolUnits unit = (PoolUnits) EnumUtils.get(PoolUnits.class, vo.getCoin());
        if(StringUtils.isNull(unit)){
            return AjaxResult.error("coin error");
        }

        Date today = new Date();
        today.setHours(0);
        today.setMinutes(0);
        today.setMinutes(0);

        Calendar ca = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        ca.setTime(today);
        ca.add(Calendar.MONTH,-3);

        Date firstDate = ca.getTime();

        if (StringUtils.isBlank(vo.getStart())){
            vo.setStart(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,firstDate));
        }else {
            if(DateUtils.parseDate(vo.getStart()).before(firstDate)){
                vo.setStart(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,firstDate));
            }else {
                //在三个月之内 不做处理
                try {
                    DateUtils.parseDate(vo.getStart());
                }catch (Exception e){
                    //出错说明格式错误有
                    return AjaxResult.error("start param error");
                }
            }
        }

        if (StringUtils.isNotBlank(vo.getEnd())){
            //在三个月之内 不做处理
            try {
                Date end = DateUtils.parseDate(vo.getEnd());
                if(end.before(DateUtils.parseDate(vo.getStart()))){
                    //end时间在start之前
                    return AjaxResult.error("end Date can not before start Date");
                }

                if(firstDate.before(end)){
                    return AjaxResult.error("end Date can not before "+DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,firstDate));
                }
            }catch (Exception e){
                //出错说明格式错误有
                return AjaxResult.error("end param error");
            }
        }else {
            //没有end 则不做处理
        }

        List<HashRateDto> mhsList = minerMapper.getMinerHashRateHistory24hListByCoinAndAccountMiner(vo);
        HashRateHistory24hDto dto = new HashRateHistory24hDto();
        dto.setList(mhsList);
        dto.setUnit(unit.getUnit());
        return AjaxResult.success(dto);
    }

    @Override
    public AjaxResult getHashRateLast24h(AccountMinerVo vo) {

        long t0 = System.currentTimeMillis();

        PoolUnits unit = (PoolUnits) EnumUtils.get(PoolUnits.class, vo.getCoin());
        if(StringUtils.isNull(unit)){
            return AjaxResult.error("coin error");
        }

        Date today = new Date();

        Calendar ca = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        ca.setTime(today);
        ca.add(Calendar.DATE,-1);

        String start = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,ca.getTime());
        long t1 = System.currentTimeMillis();
        List<HashRateDto> list = minerMapper.getMinerHashRateHistory30mListByCoinAndAccountMiner(vo, start);
        long t2 = System.currentTimeMillis();
        HashRateHistory30mDto dto = new HashRateHistory30mDto();
        dto.setList(list);
        dto.setUnit(unit.getUnit());
        long t3 = System.currentTimeMillis();

        return AjaxResult.success(dto);
    }
}
