package com.jsowell.pile.service.impl;

import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.common.util.id.IdUtils;
import com.jsowell.pile.domain.OrderMonitorData;
import com.jsowell.pile.mapper.OrderMonitorDataMapper;
import com.jsowell.pile.service.OrderMonitorDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@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 insertSelective(OrderMonitorData record) {
        int i = 0;
        // 获取锁
        String key = CacheConstants.INSERT_ORDER_MONITOR_DATA + record.getOrderCode();
        try {
            Boolean setnx = redisCache.setnx(key, IdUtils.fastSimpleUUID(), 30);
            if (!setnx) {
                return i;
            }
            // 防止重复入库
            OrderMonitorData orderMonitorData = selectByOrderCode(record.getOrderCode());
            if (orderMonitorData != null) {
                return i;
            }

            i = orderMonitorDataMapper.insertSelective(record);
            if (i > 0) {
                setRedisCache(record);
            }
        } catch (Exception e) {
            log.error("保存OrderMonitorData异常", e);
        } finally {
            redisCache.deleteObject(key);
        }
        return i;
    }

    @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) {
                // 放缓存
                setRedisCache(orderMonitorData);
                // 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) {
                // 放缓存
                setRedisCache(orderMonitorData);
                // 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;
    }

    // 放缓存
    private void setRedisCache(OrderMonitorData orderMonitorData) {
        if (orderMonitorData == null) {
            return;
        }
        String redisKey = CacheConstants.ORDER_MONITOR_DATA_BY_ORDER_CODE + orderMonitorData.getOrderCode();
        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);
    }

}
