package com.ghy.goods.service.impl;

import com.ghy.goods.domain.Goods;
import com.ghy.goods.domain.GoodsStandard;
import com.ghy.goods.mapper.GoodsStandardMapper;
import com.ghy.goods.request.AppGoodsRequest;
import com.ghy.goods.service.GoodsService;
import com.ghy.goods.service.GoodsStandardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 商品规格impl层
 *
 * @author clunt
 */
@Slf4j
@Service
public class GoodsStandardServiceImpl implements GoodsStandardService {

    @Resource
    private GoodsStandardMapper goodsStandardMapper;
    
    @Resource
    private GoodsService goodsService;

    @Override
    public GoodsStandard selectById(Long goodsStandardId) {
        return goodsStandardMapper.selectById(goodsStandardId);
    }

    @Override
    public List<GoodsStandard> selectByIds(Collection<Long> goodsStandardIds) {
        return goodsStandardMapper.selectByIds(goodsStandardIds);
    }

    @Override
    public List<GoodsStandard> selectByGoodsId(Long goodsId) {
        return goodsStandardMapper.selectByGoodsId(goodsId);
    }

    @Override
    public List<GoodsStandard> selectByGoodsStandardId(Long goodsStandardId) {
        return goodsStandardMapper.selectByGoodsStandardId(goodsStandardId);
    }

    @Override
    public int batchInsert(List<GoodsStandard> goodsStandardList) {
        return goodsStandardMapper.batchInsert(goodsStandardList);
    }

    @Override
    public int insertGoodsStandard(GoodsStandard goodsStandard) {
        return goodsStandardMapper.insertGoodsStandard(goodsStandard);
    }

    @Override
    public void deleteByGoodsId(Long goodsId) {
        goodsStandardMapper.deleteByGoodsId(goodsId);
    }

    @Override
    public List<GoodsStandard> checkStore(AppGoodsRequest request) {
        return goodsStandardMapper.checkStore(request);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(List<GoodsStandard> goodsStandardList) {
        Set<Long> goodsIds = goodsStandardList.stream().map(GoodsStandard::getGoodsId).collect(Collectors.toSet());
        Assert.isTrue(goodsIds.size() == 1, "商品规格保存失败，商品id不一致");
        Long goodsId = goodsIds.iterator().next();
        goodsStandardMapper.deleteByGoodsId(goodsId);
        return goodsStandardMapper.batchInsert(goodsStandardList);
    }

    @Override
    public int updateSaleNum(GoodsStandard goodsStandard) {
        return goodsStandardMapper.updateSaleNum(goodsStandard);
    }

    @Override
    public int update(GoodsStandard goodsStandard) {
        return goodsStandardMapper.update(goodsStandard);
    }

    @Override
    public List<GoodsStandard> selectByStandardNameLike(String standardName) {
        return goodsStandardMapper.selectByStandardNameLike(standardName);
    }

    @Override
    public List<Goods> selectGoodsByDeptGoodsCategoryId(Long deptGoodsCategoryId) {
        // 1. 根据deptGoodsCategoryId查询所有商品规格
        List<GoodsStandard> goodsStandardList = goodsStandardMapper.selectByDeptGoodsCategoryId(deptGoodsCategoryId);
        
        // 2. 提取所有不重复的goodsId
        Set<Long> goodsIds = goodsStandardList.stream()
                .map(GoodsStandard::getGoodsId)
                .collect(Collectors.toSet());
        

        // 4. 通过goodsId批量查询Goods实体
        return goodsService.selectByIds(goodsIds);
    }
}
