/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.groupon.service.impl;

import cn.hutool.core.collection.CollUtil;
import org.springblade.common.enums.AuditStatusEnum;
import org.springblade.common.enums.YesOrNoEnum;
import org.springblade.modules.groupon.entity.GrouponGoods;
import org.springblade.modules.groupon.vo.GrouponGoodsVO;
import org.springblade.modules.groupon.mapper.GrouponGoodsMapper;
import org.springblade.modules.groupon.service.IGrouponGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.mall.entity.GoodsInfo;
import org.springblade.modules.mall.service.IGoodsInfoService;
import org.springblade.modules.mall.vo.GoodsInfoVO;
import org.springblade.modules.mall.wrapper.GoodsInfoWrapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 团购商品表 服务实现类
 *
 * @author BladeX
 * @since 2023-06-02
 */
@Service
public class GrouponGoodsServiceImpl extends ServiceImpl<GrouponGoodsMapper, GrouponGoods> implements IGrouponGoodsService {

	@Resource
	private IGoodsInfoService goodsInfoService;

	@Override
	public IPage<GrouponGoodsVO> selectGrouponGoodsPage(IPage<GrouponGoodsVO> page, GrouponGoodsVO grouponGoods) {
		return page.setRecords(baseMapper.selectGrouponGoodsPage(page, grouponGoods));
	}

	@Override
	public Map<Long, List<GrouponGoodsVO>> getGoodsCover(List<Long> grouponIds) {
		Map<Long, List<GrouponGoodsVO>> collect = new HashMap<>();
		List<GrouponGoodsVO> goodsCover = baseMapper.getGoodsCover(grouponIds);
		if (CollUtil.isNotEmpty(goodsCover)) {
			collect = goodsCover.stream().collect(Collectors.groupingBy(GrouponGoodsVO::getGrouponId));
		}
		return collect;
	}

	@Override
	public List<GoodsInfoVO> getGoodsByGrouponId(Long grouponId) {
		List<GoodsInfoVO> list = this.baseMapper.getGoodsByGrouponId(grouponId);
		for (GoodsInfoVO goodsInfo : list) {
			goodsInfo.setDiscountPrice(goodsInfo.getGrouponPrice());
			goodsInfo.setTotalStock(goodsInfoService.getGodsStock(goodsInfo.getId()));
		}
		return list;
	}


	@Override
	public void addGoods(Long grouponId, List<GrouponGoods> goodsList) {
		if (CollUtil.isNotEmpty(goodsList)) {
			for (int i = 0; i < goodsList.size(); i++) {
				GrouponGoods goods = goodsList.get(i);
				goods.setId(null);
				goods.setGrouponId(grouponId);
				goods.setSort(i + 1);
			}
			saveBatch(goodsList);
		}
	}
}
