package com.jsowell.pile.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.pile.domain.ClearingBillDetail;
import com.jsowell.pile.mapper.ClearingBillDetailMapper;
import com.jsowell.pile.service.ClearingBillDetailService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ClearingBillDetailServiceImpl implements ClearingBillDetailService{

    @Resource
    private ClearingBillDetailMapper clearingBillDetailMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return clearingBillDetailMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteByClearingBillCode(String clearingBillCode) {
        return clearingBillDetailMapper.deleteByClearingBillCode(clearingBillCode);
    }

    @Override
    public int insert(ClearingBillDetail record) {
        return clearingBillDetailMapper.insert(record);
    }

    @Override
    public int insertOrUpdate(ClearingBillDetail record) {
        return clearingBillDetailMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(ClearingBillDetail record) {
        return clearingBillDetailMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(ClearingBillDetail record) {
        return clearingBillDetailMapper.insertSelective(record);
    }

    @Override
    public ClearingBillDetail selectByPrimaryKey(Integer id) {
        return clearingBillDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ClearingBillDetail record) {
        return clearingBillDetailMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ClearingBillDetail record) {
        return clearingBillDetailMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateBatch(List<ClearingBillDetail> list) {
        return clearingBillDetailMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<ClearingBillDetail> list) {
        return clearingBillDetailMapper.batchInsert(list);
    }

    /**
     * 根据订单编号批量查询
     * @param orderCodeList
     * @return
     */
    @Override
    public Map<String, ClearingBillDetail> selectByOrderCodeList(List<String> orderCodeList) {
        Map<String, ClearingBillDetail> resultMap = Maps.newHashMap();
        if (CollectionUtils.isEmpty(orderCodeList)) {
            return resultMap;
        }
        List<String> redisKeyList = Lists.newArrayList();
        for (String orderCode : orderCodeList) {
            redisKeyList.add(CacheConstants.CLEARING_BILL_DETAIL_BY_ORDER_CODE + orderCode);
        }
        // 批量从redis获取
        List<ClearingBillDetail> resultList = redisCache.multiGet(redisKeyList);
        resultList = resultList.stream().filter(Objects::nonNull).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(resultList)) {
            for (ClearingBillDetail clearingBillDetail : resultList) {
                resultMap.put(clearingBillDetail.getOrderCode(), clearingBillDetail);
            }

            // 剔除已经从缓存中获取到的数据
            Iterator<String> iterator = orderCodeList.iterator();
            while (iterator.hasNext()) {
                String orderCode = iterator.next();
                ClearingBillDetail detail = resultMap.get(orderCode);
                if (detail != null) {
                    iterator.remove();
                }
            }
        }

        if (CollectionUtils.isNotEmpty(orderCodeList)) {
            List<ClearingBillDetail> detailList = clearingBillDetailMapper.selectByOrderCodes(orderCodeList);
            if (CollectionUtils.isNotEmpty(detailList)) {
                for (ClearingBillDetail clearingBillDetail : detailList) {
                    resultMap.put(clearingBillDetail.getOrderCode(), clearingBillDetail);
                }
            }
        }
        return resultMap;
    }

}
