package com.hfkj.service.spread.impl;

import com.hfkj.common.utils.RedisUtil;
import com.hfkj.dao.BsUserSpreadOilChannelMapper;
import com.hfkj.entity.BsUserSpreadOilChannel;
import com.hfkj.entity.BsUserSpreadOilChannelExample;
import com.hfkj.service.spread.BsUserSpreadOilChannelService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

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

/**
 * @className: BsUserSpreadOilChannelServiceImpl
 * @author: HuRui
 * @date: 2025/3/31
 **/
@Service("userSpreadOilChannelService")
public class BsUserSpreadOilChannelServiceImpl implements BsUserSpreadOilChannelService {
    @Resource
    private BsUserSpreadOilChannelMapper userSpreadOilChannelMapper;
    @Resource
    private RedisUtil redisUtil;
    @Override
    public void editData(BsUserSpreadOilChannel data) {
        data.setUpdateTime(new Date());
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            userSpreadOilChannelMapper.insert(data);
        } else {
            // 存入缓存
            redisUtil.hset("USER_SPREAD_OIL_CHANNEL", data.getChannel() + "", data);
            userSpreadOilChannelMapper.updateByPrimaryKey(data);
        }
    }

    @Override
    public BsUserSpreadOilChannel getDetail(Long id) {
        return userSpreadOilChannelMapper.selectByPrimaryKey(id);
    }

    @Override
    public BsUserSpreadOilChannel getDetailByChannel(Integer channel) {
        // 读取缓存
        Object obj = redisUtil.hget("USER_SPREAD_OIL_CHANNEL", channel + "");
        if (obj != null) {
            return (BsUserSpreadOilChannel) obj;
        }
        BsUserSpreadOilChannelExample example = new BsUserSpreadOilChannelExample();
        example.createCriteria().
                andStatusNotEqualTo(0)
                .andChannelEqualTo(channel);
        List<BsUserSpreadOilChannel> list = userSpreadOilChannelMapper.selectByExample(example);
        if (!list.isEmpty()) {
            // 存入缓存
            redisUtil.hset("USER_SPREAD_OIL_CHANNEL", channel + "", list.get(0));
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<BsUserSpreadOilChannel> getList(Map<String, Object> param) {
        BsUserSpreadOilChannelExample example = new BsUserSpreadOilChannelExample();
        BsUserSpreadOilChannelExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(0);

        if (MapUtils.getInteger(param,"channel") != null) {
            criteria.andChannelEqualTo(MapUtils.getInteger(param,"channel"));
        }

        example.setOrderByClause("channel");
        return userSpreadOilChannelMapper.selectByExample(example);
    }
}
