
package com.tmerclub.cloud.user.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.user.dto.ShopCustomerReqDTO;
import com.tmerclub.cloud.api.user.vo.UserManagerVO;
import com.tmerclub.cloud.common.cache.constant.CacheNames;
import com.tmerclub.cloud.common.cache.constant.UserCacheNames;
import com.tmerclub.cloud.common.cache.util.RedisUtil;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.user.dto.ShopCustomerDTO;
import com.tmerclub.cloud.user.mapper.ShopCustomerMapper;
import com.tmerclub.cloud.user.model.ShopCustomer;
import com.tmerclub.cloud.user.service.ShopCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author tmerclub
 * @date 2023-01-15 15:53:16
 */
@Service
public class ShopCustomerServiceImpl implements ShopCustomerService {

    @Autowired
    private ShopCustomerMapper shopCustomerMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(List<ShopCustomer> shopCustomers) {
        shopCustomerMapper.saveBatch(shopCustomers);
        List<String> keys = new ArrayList<>(shopCustomers.size());
        for (ShopCustomer shopCustomer : shopCustomers) {
            keys.add(UserCacheNames.SHOP_CUSTOMER_KEY + shopCustomer.getShopId() + CacheNames.UNION_KEY + shopCustomer.getUserId());
        }
        RedisUtil.deleteBatch(keys);
    }

    @Override
    public List<UserManagerVO> listByParam(ShopCustomerDTO shopCustomerDTO) {
        return shopCustomerMapper.listByParam(shopCustomerDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBatchByOrders(List<ShopCustomerReqDTO> shopCustomerReqList) {
        if (CollUtil.isEmpty(shopCustomerReqList)) {
            return;
        }
        List<String> keys = new ArrayList<>(shopCustomerReqList.size());
        Map<Long, List<ShopCustomerReqDTO>> shopMap = shopCustomerReqList.stream().collect(Collectors.groupingBy(ShopCustomerReqDTO::getShopId));
        List<ShopCustomer> list = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (Long shopId : shopMap.keySet()) {
            List<Long> userIds = shopMap.get(shopId).stream().map(ShopCustomerReqDTO::getUserId).filter(Objects::nonNull).toList();
            if (CollUtil.isEmpty(userIds)) {
                continue;
            }
            List<ShopCustomer> shopCustomers = shopCustomerMapper.listNotExistByShopIdAndUserIds(shopId, userIds);
            if (CollUtil.isEmpty(shopCustomers)) {
                continue;
            }
            for (ShopCustomer shopCustomer : shopCustomers) {
                shopCustomer.setShopId(shopId);
                keys.add(UserCacheNames.SHOP_CUSTOMER_KEY + shopCustomer.getShopId() + CacheNames.UNION_KEY + shopCustomer.getUserId());
            }
            list.addAll(shopCustomers);
        }
        if (CollUtil.isNotEmpty(list)) {
            shopCustomerMapper.saveBatch(list);
        }
        if (CollUtil.isNotEmpty(keys)) {
            RedisUtil.deleteBatch(keys);
        }
    }

    @Override
    @Cacheable(value = UserCacheNames.SHOP_CUSTOMER_KEY, key = "#shopId + ':' + #userId")
    public Integer getIsShopCustomer(Long shopId, Long userId) {
        return shopCustomerMapper.getIsShopCustomer(shopId, userId);
    }
}
