package com.jxy.marketall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxy.common.core.utils.DateUtils;
import com.jxy.common.core.utils.StringUtils;
import com.jxy.common.core.web.Result.AjaxResult;
import com.jxy.common.redis.service.RedisService;
import com.jxy.marketall.dto.HolderBalanceDto;
import com.jxy.marketall.dto.HolderDto;
import com.jxy.marketall.entity.Holder;
import com.jxy.marketall.mapper.HolderBalanceMapper;
import com.jxy.marketall.mapper.HolderMapper;
import com.jxy.marketall.vo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jxy.marketall.service.HolderService;


/**
 * @Description TODO
 * @Date 2022/7/18 14:29
 * @Author 杜懿
 */
@Service
public class HolderServiceImpl extends ServiceImpl<HolderMapper, Holder> implements HolderService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private HolderBalanceMapper holderBalanceMapper;

    @Override
    public AjaxResult getNewHolder(QueryVo vo) {

        if(StringUtils.isNotNull(vo.getStart())){

            HolderDto holderData = baseMapper.getHolderByTime(vo);
            if(StringUtils.isNull(holderData)){
                holderData = new HolderDto();
                holderData.setDateTime(DateUtils.parseDate(vo.getStart()));
                return AjaxResult.success("没有查询到数据",holderData);
            }

            return AjaxResult.success(holderData);
        }

        HolderDto holderData = redisService.getCacheObject("HolderData");

        if(StringUtils.isNull(holderData)){
            holderData = new HolderDto();
            holderData.setDateTime(DateUtils.parseDate(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD)));
            return AjaxResult.success("没有查询到数据",holderData);
        }

        return AjaxResult.success(holderData);
    }

    @Override
    public AjaxResult getNewHolderBalance(QueryVo vo) {

        if(StringUtils.isNotNull(vo.getStart())){


            HolderBalanceDto holderBalanceData = holderBalanceMapper.getHolderBalanceByTime(vo);

            if(StringUtils.isNull(holderBalanceData)){
                holderBalanceData = new HolderBalanceDto();
                holderBalanceData.setDateTime(DateUtils.parseDate(vo.getStart()));
                return AjaxResult.success("没有查询到数据",holderBalanceData);
            }

            return AjaxResult.success(holderBalanceData);
        }

        HolderBalanceDto holderBalanceData = redisService.getCacheObject("HolderBalanceData");


        if(StringUtils.isNull(holderBalanceData)){
            holderBalanceData = new HolderBalanceDto();
            holderBalanceData.setDateTime(DateUtils.parseDate(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD)));
            return AjaxResult.success("没有查询到数据",holderBalanceData);
        }

        return AjaxResult.success(holderBalanceData);
    }
}
