package com.hfkj.service.rebate.impl;

import com.hfkj.common.utils.RedisUtil;
import com.hfkj.dao.BsRebateConfigMapper;
import com.hfkj.entity.BsRebateConfig;
import com.hfkj.entity.BsRebateConfigExample;
import com.hfkj.service.rebate.BsRebateConfigService;
import com.hfkj.sysenum.RebateConfigTypeEnum;
import org.springframework.stereotype.Service;

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

/**
 * @className: BsRebateConfigServiceImpl
 * @author: HuRui
 * @date: 2023/10/11
 **/
@Service("rebateConfigService")
public class BsRebateConfigServiceImpl implements BsRebateConfigService {

    @Resource
    private BsRebateConfigMapper rebateConfigMapper;
    @Resource
    private RedisUtil redisUtil;
    private final static String REBATE_CONFIG = "REBATE_CONFIG";
    @Override
    public void editData(BsRebateConfig config) {
        if (config.getId() == null) {
            config.setCreateTime(new Date());
            config.setUpdateTime(new Date());
            rebateConfigMapper.insert(config);
        } else {
            config.setUpdateTime(new Date());
            rebateConfigMapper.updateByPrimaryKey(config);
        }
        redisUtil.hset(REBATE_CONFIG, config.getType().toString(), config);
    }

    @Override
    public BsRebateConfig getConfig(RebateConfigTypeEnum configType) {
        Object obj = redisUtil.hget(REBATE_CONFIG, configType.getNumber().toString());
        if (obj != null) {
            return (BsRebateConfig) obj;
        }
        BsRebateConfigExample example = new BsRebateConfigExample();
        example.createCriteria().andTypeEqualTo(configType.getNumber()).andStatusNotEqualTo(0);
        List<BsRebateConfig> list = rebateConfigMapper.selectByExample(example);
        if (list.size() > 0) {
            BsRebateConfig config = list.get(0);
            redisUtil.hset(REBATE_CONFIG, configType.getNumber().toString(), config);
            return config;
        }
        return null;
    }
}
