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

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.product.vo.SpuDetailVO;
import com.tmerclub.cloud.product.mapper.SpuDetailMapper;
import com.tmerclub.cloud.product.model.SpuDetail;
import com.tmerclub.cloud.product.service.SpuDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品详情信息
 *
 * @author tmerclub
 * @date 2020-10-28 15:27:24
 */
@Service
public class SpuDetailServiceImpl implements SpuDetailService {

    @Autowired
    private SpuDetailMapper spuDetailMapper;

    @Override
    public void batchSave(List<SpuDetail> spuDetailList) {
        spuDetailMapper.batchSave(spuDetailList);
    }

    @Override
    public void batchUpdate(List<SpuDetail> spuDetailList) {
        spuDetailMapper.batchUpdate(spuDetailList);
    }

    @Override
    public void deleteById(Long spuId) {
        spuDetailMapper.deleteById(spuId);
    }

    @Override
    public void update(List<SpuDetail> spuDetailList, List<SpuDetailVO> detailListDb, Long spuId) {
        if (CollUtil.isEmpty(spuDetailList) && CollUtil.isEmpty(detailListDb)) {
            return;
        }
        if (CollUtil.isEmpty(detailListDb)) {
            spuDetailMapper.batchSave(spuDetailList);
            return;
        }
        List<Integer> langList = detailListDb.stream().map(SpuDetailVO::getLang).collect(Collectors.toList());
        if (CollUtil.isEmpty(spuDetailList)) {
            spuDetailMapper.deleteBatchBySpuIdAndLang(spuId, langList);
            return;
        }
        Iterator<SpuDetail> iterator = spuDetailList.iterator();
        List<SpuDetail> saveList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        while (iterator.hasNext()) {
            SpuDetail spuDetail = iterator.next();
            if (langList.contains(spuDetail.getLang())) {
                langList.remove(spuDetail.getLang());
                continue;
            }
            saveList.add(spuDetail);
            iterator.remove();
        }
        if (CollUtil.isNotEmpty(saveList)) {
            spuDetailMapper.batchSave(saveList);
        }
        if (CollUtil.isNotEmpty(spuDetailList)) {
            spuDetailMapper.batchUpdate(spuDetailList);
        }
        if (CollUtil.isNotEmpty(langList)) {
            spuDetailMapper.deleteBatchBySpuIdAndLang(spuId, langList);
        }
    }
}
