package com.jsowell.pile.service.impl;

import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.pile.domain.OrderMonitorData;
import com.jsowell.pile.mapper.OrderMonitorDataMapper;
import com.jsowell.pile.service.OrderMonitorDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class OrderMonitorDataServiceImpl implements OrderMonitorDataService {

    @Resource
    private OrderMonitorDataMapper orderMonitorDataMapper;

    @Autowired
    private RedisCache redisCache;

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

    @Override
    public int insert(OrderMonitorData record) {
        return orderMonitorDataMapper.insert(record);
    }

    @Override
    public int insertSelective(OrderMonitorData record) {
        return orderMonitorDataMapper.insertSelective(record);
    }

    @Override
    public OrderMonitorData selectByPrimaryKey(Integer id) {
        return orderMonitorDataMapper.selectByPrimaryKey(id);
    }

    @Override
    public OrderMonitorData selectByOrderCode(String orderCode) {
        String redisKey = CacheConstants.ORDER_MONITOR_DATA_BY_ORDER_CODE + orderCode;
        OrderMonitorData orderMonitorData = redisCache.getCacheObject(redisKey);
        if (orderMonitorData == null) {
            orderMonitorData = orderMonitorDataMapper.selectByOrderCode(orderCode);
            if (orderMonitorData != null) {
                // 放缓存
                redisCache.setCacheObject(redisKey, orderMonitorData, CacheConstants.cache_expire_time_12h);
                String redisKey2 = CacheConstants.ORDER_MONITOR_DATA_BY_TRANSACTION_CODE + orderMonitorData.getTransactionCode();
                redisCache.setCacheObject(redisKey2, orderMonitorData, CacheConstants.cache_expire_time_12h);
            }
        }
        return orderMonitorData;
    }

    @Override
    public OrderMonitorData selectByTransactionCode(String transactionCode) {
        String redisKey = CacheConstants.ORDER_MONITOR_DATA_BY_TRANSACTION_CODE + transactionCode;
        OrderMonitorData orderMonitorData = redisCache.getCacheObject(redisKey);
        if (orderMonitorData == null) {
            orderMonitorData = orderMonitorDataMapper.selectByTransactionCode(transactionCode);
            if (orderMonitorData != null) {
                // 放缓存
                redisCache.setCacheObject(redisKey, orderMonitorData, CacheConstants.cache_expire_time_12h);
                String redisKey2 = CacheConstants.ORDER_MONITOR_DATA_BY_ORDER_CODE + orderMonitorData.getOrderCode();
                redisCache.setCacheObject(redisKey2, orderMonitorData, CacheConstants.cache_expire_time_12h);
            }
        }
        return orderMonitorData;
    }

}
