package com.dayou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dayou.entity.BrokerageBaseAmount;
import com.dayou.exception.ErrorCode;
import com.dayou.mapper.BrokerageBaseAmountMapper;
import com.dayou.service.IBrokerageBaseAmountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.vo.BaseAmountVO;
import com.google.common.cache.LoadingCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.math.BigDecimal;
import java.util.List;
import java.util.regex.Pattern;

import static com.dayou.common.Constants.MATH_REGULAR;

/**
 * <p>
 * 自定义提成基数 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-03-10
 */
@Service
public class BrokerageBaseAmountServiceImpl extends ServiceImpl<BrokerageBaseAmountMapper, BrokerageBaseAmount> implements IBrokerageBaseAmountService {


    @Autowired
    private LoadingCache<Long, BigDecimal> baseAmountsCache;

    @Autowired
    private BrokerageBaseAmountMapper brokerageBaseAmountMapper;
    @Override
    @SuppressWarnings("unchecked")
    public Page<BaseAmountVO> selectPage(Page page, BaseAmountVO baseAmountVO){
        Page<BaseAmountVO> xPage = brokerageBaseAmountMapper.xPage(page,baseAmountVO);
        return xPage;
    }


    @Override
    public BrokerageBaseAmount detail(Long id){
        return this.getById(id);
    }

    @Override
    public Boolean add(BrokerageBaseAmount brokerageBaseAmount){
        return  this.save(brokerageBaseAmount);
    }

    @Override
    public Boolean update(BrokerageBaseAmount brokerageBaseAmount){
        boolean b = this.saveOrUpdate(brokerageBaseAmount);
        this.cacheBaseAmount();
        return b;
    }

    @Override
    public Boolean delete(Long id){
        //逻辑删除
        return this.removeById(id);
    }

    public void cacheBaseAmount(){
        List<BrokerageBaseAmount> list = this.list(new LambdaQueryWrapper<BrokerageBaseAmount>().isNotNull(BrokerageBaseAmount::getBaseAmount));
        list.stream().forEach(x->{
            baseAmountsCache.put(x.getUserId(),x.getBaseAmount());
        });
    }
}
