package com.ruoyi.framework.recovery.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.recovery.domain.vo.CommodityVo;
import com.ruoyi.framework.recovery.domain.vo.GoodsVo;
import com.ruoyi.framework.recovery.domain.vo.RecStallGoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.framework.recovery.mapper.RecStallMapper;
import com.ruoyi.framework.recovery.domain.RecStall;
import com.ruoyi.framework.recovery.service.IRecStallService;

/**
 * 回收档口Service业务层处理
 *
 * @author jin
 * @date 2024-11-16
 */
@Service
public class RecStallServiceImpl implements IRecStallService
{
    @Autowired
    private RecStallMapper recStallMapper;

    /**
     * 查询回收档口
     *
     * @param id 回收档口主键
     * @return 回收档口
     */
    @Override
    public RecStall selectRecStallById(Long id)
    {
        return recStallMapper.selectRecStallById(id);
    }

    /**
     * 查询回收档口列表
     *
     * @param recStall 回收档口
     * @return 回收档口
     */
    @Override
    public List<RecStall> selectRecStallList(RecStall recStall)
    {
        return recStallMapper.selectRecStallList(recStall);
    }

    /**
     * 新增回收档口
     *
     * @param recStall 回收档口
     * @return 结果
     */
    @Override
    public int insertRecStall(RecStall recStall)
    {
        recStall.setCreateDate(new Date());
        return recStallMapper.insertRecStall(recStall);
    }

    /**
     * 修改回收档口
     *
     * @param recStall 回收档口
     * @return 结果
     */
    @Override
    public int updateRecStall(RecStall recStall)
    {
        return recStallMapper.updateRecStall(recStall);
    }

    /**
     * 批量删除回收档口
     *
     * @param ids 需要删除的回收档口主键
     * @return 结果
     */
    @Override
    public int deleteRecStallByIds(Long[] ids)
    {
        return recStallMapper.deleteRecStallByIds(ids);
    }

    /**
     * 删除回收档口信息
     *
     * @param id 回收档口主键
     * @return 结果
     */
    @Override
    public int deleteRecStallById(Long id)
    {
        return recStallMapper.deleteRecStallById(id);
    }

    @Override
    public List<RecStallGoodsVo> selectGoodsList() {
        List<GoodsVo> goodsVos =  recStallMapper.selectAllGoods();
        // 根据品牌名称分组
        Map<String, List<GoodsVo>> brandMap = goodsVos.stream().collect(Collectors.groupingBy(GoodsVo::getBrand));
        List<RecStallGoodsVo> recStallGoodsVos = new ArrayList<>();
        // 分组后的内容分别插入vo，最终形式为["brand":"华为",["id":"2","model":"p70","specs":"256G黑"]]
        for (Map.Entry<String, List<GoodsVo>> entry : brandMap.entrySet()) {
            RecStallGoodsVo stallVo = new RecStallGoodsVo();
            stallVo.setBrand(entry.getKey());
            List<CommodityVo> goods = new ArrayList<>();
            List<GoodsVo> value = entry.getValue();
            for (GoodsVo vo : value) {
                CommodityVo commodityVo = new CommodityVo();
                BeanUtils.copyBeanProp(commodityVo, vo);
                goods.add(commodityVo);
            }
            stallVo.setGoods(goods);
            recStallGoodsVos.add(stallVo);
        }
        return recStallGoodsVos;
    }
}
