package com.hfkj.service.order.impl;

import com.hfkj.common.utils.RedisUtil;
import com.hfkj.dao.BsGasChannelConfigMapper;
import com.hfkj.entity.BsGasChannelConfig;
import com.hfkj.entity.BsGasChannelConfigExample;
import com.hfkj.service.order.BsGasChannelConfigService;
import com.hfkj.sysenum.merchant.MerchantSourceTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @className: BsGasChannelConfigServiceImpl
 * @author: HuRui
 * @date: 2024/7/10
 **/
@Service("")
public class BsGasChannelConfigServiceImpl implements BsGasChannelConfigService {
    @Resource
    private BsGasChannelConfigMapper gasChannelConfigMapper;
    private final static String CACHE_KEY = "GAS_CHANNEL_CONFIG:";
    @Resource
    private RedisUtil redisUtil;
    @Override
    public void editData(BsGasChannelConfig gasChannelConfig) {
        gasChannelConfig.setUpdateTime(new Date());
        if (gasChannelConfig.getId() == null) {
            gasChannelConfig.setCreateTime(new Date());
            gasChannelConfigMapper.insert(gasChannelConfig);
        } else {
            gasChannelConfigMapper.updateByPrimaryKey(gasChannelConfig);
        }
        // 存入缓存
        redisUtil.set(CACHE_KEY+gasChannelConfig.getChannelType(), gasChannelConfig);
    }

    @Override
    public BsGasChannelConfig getDetail(MerchantSourceTypeEnum gasSourceType) {
        Object obj = redisUtil.get(CACHE_KEY + gasSourceType);
        if (obj != null) {
            return (BsGasChannelConfig) obj;
        }
        BsGasChannelConfigExample example = new BsGasChannelConfigExample();
        example.createCriteria().andGasChannelTypeEqualTo(gasSourceType.getNumber()).andStatusNotEqualTo(0);
        List<BsGasChannelConfig> list = gasChannelConfigMapper.selectByExample(example);
        if (!list.isEmpty()) {
            BsGasChannelConfig gasChannelConfig = list.get(0);
            // 存入缓存
            redisUtil.set(CACHE_KEY+gasChannelConfig.getChannelType(), gasChannelConfig);
            return gasChannelConfig;
        }
        return null;
    }
}
