package com.hfkj.service.agent.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.RedisUtil;
import com.hfkj.dao.BsAgentDiscountMapper;
import com.hfkj.entity.BsAgentDiscount;
import com.hfkj.entity.BsAgentDiscountExample;
import com.hfkj.service.agent.BsAgentDiscountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @className: BsAgentDiscountServiceImpl
 * @author: HuRui
 * @date: 2024/9/3
 **/
@Service("agentDiscountService")
public class BsAgentDiscountServiceImpl implements BsAgentDiscountService {
    @Resource
    private BsAgentDiscountMapper agentDiscountMapper;
    @Resource
    private RedisUtil redisUtil;
    private final static String CACHE_KEY = "AGENT_DISCOUNT";
    @Override
    public void create(BsAgentDiscount data) {
        data.setCreateTime(new Date());
        data.setUpdateTime(new Date());
        data.setStatus(1);
        agentDiscountMapper.insert(data);

        List<BsAgentDiscount> list = getList(data.getAgentId());
        list.add(data);
        // 加入缓存
        redisUtil.hset(CACHE_KEY,""+data.getAgentId(), list);
    }

    @Override
    public void delete(Long id) {
        // 查询数据
        BsAgentDiscount data = agentDiscountMapper.selectByPrimaryKey(id);
        if (data == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据");
        }
        data.setUpdateTime(new Date());
        data.setStatus(0);
        agentDiscountMapper.updateByPrimaryKey(data);

        // 删除缓存
        redisUtil.hdel(CACHE_KEY, ""+data.getAgentId());
    }

    @Override
    public BsAgentDiscount getDetail(Long agentId, Integer type, String objectNo) {
        List<BsAgentDiscount> list = getList(agentId)
                .stream()
                .filter(o -> o.getType().equals(type))
                .filter(o -> o.getObjectNo().equals(objectNo))
                .collect(Collectors.toList());
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsAgentDiscount> getList(Long agentId) {
        Object cacheObj = redisUtil.hget(CACHE_KEY, "" + agentId);
        if (cacheObj != null) {
            return (List<BsAgentDiscount>) cacheObj;
        }
        BsAgentDiscountExample example = new BsAgentDiscountExample();
        example.createCriteria().andStatusNotEqualTo(0).andAgentIdEqualTo(agentId);
        example.setOrderByClause("create_time desc");
        List<BsAgentDiscount> list = agentDiscountMapper.selectByExample(example);
        // 加入缓存
        redisUtil.hset(CACHE_KEY, ""+agentId, list);
        return list;
    }
}
