package com.jsowell.pile.service.impl;

import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.WxpayCallbackRecord;
import com.jsowell.pile.mapper.WxpayCallbackRecordMapper;
import com.jsowell.pile.service.WxpayCallbackRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Slf4j
@Service
public class WxpayCallbackRecordServiceImpl implements WxpayCallbackRecordService {

    @Resource
    private WxpayCallbackRecordMapper wxpayCallbackRecordMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return wxpayCallbackRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(WxpayCallbackRecord record) {
        return wxpayCallbackRecordMapper.insert(record);
    }

    @Override
    public int insertSelective(WxpayCallbackRecord record) {
        return wxpayCallbackRecordMapper.insertSelective(record);
    }

    @Override
    public WxpayCallbackRecord selectByPrimaryKey(Integer id) {
        return wxpayCallbackRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(WxpayCallbackRecord record) {
        return wxpayCallbackRecordMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(WxpayCallbackRecord record) {
        return wxpayCallbackRecordMapper.updateByPrimaryKey(record);
    }

    @Override
    public WxpayCallbackRecord selectByOrderCode(String orderCode) {
        if (StringUtils.isBlank(orderCode)) {
            return null;
        }
        String redisKey = CacheConstants.QUERY_ORDER_CALLBACK + orderCode;
        WxpayCallbackRecord wxpayCallbackRecord =  redisCache.getCacheObject(redisKey);
        if (wxpayCallbackRecord != null) {
            return wxpayCallbackRecord;
        }
        // WxpayCallbackRecord wxpayCallbackRecord = null;
        try {
            wxpayCallbackRecord = wxpayCallbackRecordMapper.selectByOrderCode(orderCode);
            if (wxpayCallbackRecord != null) {
                redisCache.setCacheObject(redisKey, wxpayCallbackRecord, CacheConstants.cache_expire_time_1d);
            }
        } catch (Exception e) {
            log.error("根据订单号查询微信支付记录:{}", orderCode, e);
        }
        return wxpayCallbackRecord;
    }

    @Override
    public WxpayCallbackRecord selectByOutTradeNo(String outTradeNo) {
        return wxpayCallbackRecordMapper.selectByOutTradeNo(outTradeNo);
    }

    @Override
    public List<WxpayCallbackRecord> queryBalanceRechargeRecordOfTheLatestYear(String memberId) {
        LocalDateTime now = LocalDateTime.now();
        LocalDate localDate = now.minusYears(1).toLocalDate();
        LocalTime localTime = now.toLocalTime();
        LocalDateTime lastYear = LocalDateTime.of(localDate, localTime);
        // 查询最近一年的余额充值记录
        return wxpayCallbackRecordMapper.selectBalanceRechargeRecord(memberId, lastYear);
    }

    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        LocalDate localDate = now.minusYears(1).toLocalDate();
        LocalTime localTime = now.toLocalTime();
        LocalDateTime of = LocalDateTime.of(localDate, localTime);
        System.out.println(of);
    }

}
