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

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.combo.mapper.ComboSpuMapper;
import com.tmerclub.cloud.combo.model.ComboSpu;
import com.tmerclub.cloud.combo.service.ComboSpuService;
import com.tmerclub.cloud.combo.service.ComboSpuSkuService;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.product.vo.ComboSpuSkuVO;
import com.tmerclub.cloud.common.product.vo.ComboSpuVO;
import com.tmerclub.cloud.common.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 套装商品项
 *
 * @author tmerclub
 * @date 2022-08-16 09:18:06
 */
@Service
public class ComboSpuServiceImpl implements ComboSpuService {

    @Autowired
    private ComboSpuMapper comboSpuMapper;
    @Autowired
    private ComboSpuSkuService comboSpuSkuService;

    @Override
    public PageVO<ComboSpuVO> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> comboSpuMapper.list());
    }

    @Override
    public ComboSpuVO getByComboSpuId(Long comboProdId) {
        return comboSpuMapper.getByComboSpuId(comboProdId);
    }

    @Override
    public void save(ComboSpu comboSpu) {
        comboSpuMapper.save(comboSpu);
    }

    @Override
    public void update(ComboSpu comboProd) {
        comboSpuMapper.update(comboProd);
    }

    @Override
    public void deleteById(Long comboProdId) {
        comboSpuMapper.deleteById(comboProdId);
    }

    @Override
    public void insertBatch(List<ComboSpuVO> spuList, Long comboId) {
        if (CollUtil.isEmpty(spuList)) {
            return;
        }
        for (ComboSpuVO comboSpuVO : spuList) {
            List<ComboSpuSkuVO> skuListS = comboSpuVO.getSkuList();
            long price = 0L;
            for (ComboSpuSkuVO list : skuListS) {
                price += list.getMatchingPrice() * comboSpuVO.getLeastNum();
            }
            comboSpuVO.setComboPrice(price);
            comboSpuVO.setType(2);
            comboSpuVO.setStatus(1);
            comboSpuVO.setComboId(comboId);
            ComboSpu comboSpu = BeanUtil.map(comboSpuVO, ComboSpu.class);
            comboSpuMapper.save(comboSpu);
            //保存sku信息
            for (ComboSpuSkuVO comboSpuSkuVO : comboSpu.getSkuList()) {
                comboSpuSkuVO.setSkuStatus(comboSpuVO.getStatus());
            }
            comboSpuSkuService.insertBatch(comboSpu.getSkuList(), comboSpu.getComboSpuId());
        }
    }

    @Override
    public void deleteBatch(List<Long> comboSpuIdList) {
        comboSpuMapper.deleteBatch(comboSpuIdList);
    }

    @Override
    public List<ComboSpuVO> comboSpuByComboId(Long comboId) {
        return comboSpuMapper.comboSpuByComboIds(Collections.singletonList(comboId));
    }

    @Override
    public void comboSpuOfflineDispose(List<Long> spuIdList) {
        if (spuIdList.size() != 0) {
            comboSpuMapper.batchComboSpuStatus(spuIdList);
        }
    }
}
