package com.hfkj.service.merchant.impl;

import com.hfkj.common.utils.RedisUtil;
import com.hfkj.dao.BsMerchantPayConfigMapper;
import com.hfkj.entity.BsMerchantPayConfig;
import com.hfkj.entity.BsMerchantPayConfigExample;
import com.hfkj.service.merchant.BsMerchantPayConfigService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @className: BsMerchantPayConfigServiceImpl
 * @author: HuRui
 * @date: 2024/3/13
 **/
@Service("merchantPayConfigService")
public class BsMerchantPayConfigServiceImpl implements BsMerchantPayConfigService {

    @Resource
    private BsMerchantPayConfigMapper merchantPayConfigMapper;
    @Resource
    private RedisUtil redisUtil;

    private final static String MER_PAY_CONFIG_KEY = "MER_PAY_CONFIG_KEY:";

    @Override
    public void editData(BsMerchantPayConfig data) {
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            data.setUpdateTime(new Date());
            merchantPayConfigMapper.insert(data);
        } else {
            data.setUpdateTime(new Date());
            merchantPayConfigMapper.updateByPrimaryKey(data);
        }
        redisUtil.set(MER_PAY_CONFIG_KEY + data.getMerNo(), data);
    }

    @Override
    public BsMerchantPayConfig getConfig(String merNo) {
        Object obj = redisUtil.get(MER_PAY_CONFIG_KEY + merNo);
        if (obj != null) {
            return (BsMerchantPayConfig)obj;
        }
        BsMerchantPayConfigExample example = new BsMerchantPayConfigExample();
        example.createCriteria().andMerNoEqualTo(merNo);
        List<BsMerchantPayConfig> list = merchantPayConfigMapper.selectByExample(example);
        if (!list.isEmpty()) {
            BsMerchantPayConfig config = list.get(0);
            redisUtil.set(MER_PAY_CONFIG_KEY + config.getMerNo(), config);
            return config;
        }
        return null;
    }
}
