package com.ruoyi.mall.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ruoyi.mall.domain.MallImg;
import com.ruoyi.mall.domain.MallOrderComment;
import com.ruoyi.mall.domain.vo.MallOrderGoodsVO;
import com.ruoyi.mall.service.IMallImgService;
import com.ruoyi.mall.service.IMallOrderCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallOrderGoodsMapper;
import com.ruoyi.mall.domain.MallOrderGoods;
import com.ruoyi.mall.service.IMallOrderGoodsService;

/**
 * 订单商品Service业务层处理
 * 
 * @author chang
 * @date 2021-11-25
 */
@Service
public class MallOrderGoodsServiceImpl implements IMallOrderGoodsService 
{
    @Autowired
    private MallOrderGoodsMapper mallOrderGoodsMapper;

    @Autowired
    private IMallOrderCommentService orderCommentService;

    @Autowired
    private IMallImgService imgService;

    /**
     * 查询订单商品
     * 
     * @param id 订单商品主键
     * @return 订单商品
     */
    @Override
    public MallOrderGoods selectMallOrderGoodsById(Long id)
    {
        MallOrderGoods orderGoods = mallOrderGoodsMapper.selectMallOrderGoodsById(id);
        MallOrderComment orderComment = new MallOrderComment();
        orderComment.setOrderId(orderGoods.getOrderId());
        orderComment.setGoodsId(orderGoods.getGoodsId());
        List<Map<String, Object>>list = orderCommentService.selectMallOrderCommentList(orderComment);
        if (list.size() > 0){
            Map<String, Object> result = list.get(0);
            orderGoods.setContext(result.get("context").toString());

            MallImg img = new MallImg();
            img.setResId(Long.parseLong(result.get("id").toString()));
            img.setType("2");
            List<MallImg> imgList = imgService.selectMallImgList(img);
            List<String> urlList = new ArrayList<>();
            for (MallImg mallImg: imgList) {
                urlList.add(mallImg.getImg());
            }
            orderGoods.setImgList(urlList);
        }

        return orderGoods;
    }

    /**
     * 查询订单商品列表
     * 
     * @param mallOrderGoods 订单商品
     * @return 订单商品
     */
    @Override
    public List<MallOrderGoods> selectMallOrderGoodsList(MallOrderGoods mallOrderGoods)
    {
        return mallOrderGoodsMapper.selectMallOrderGoodsList(mallOrderGoods);
    }

    /**
     * 新增订单商品
     * 
     * @param mallOrderGoods 订单商品
     * @return 结果
     */
    @Override
    public int insertMallOrderGoods(MallOrderGoods mallOrderGoods)
    {
        return mallOrderGoodsMapper.insertMallOrderGoods(mallOrderGoods);
    }

    /**
     * 修改订单商品
     * 
     * @param mallOrderGoods 订单商品
     * @return 结果
     */
    @Override
    public int updateMallOrderGoods(MallOrderGoods mallOrderGoods)
    {
        return mallOrderGoodsMapper.updateMallOrderGoods(mallOrderGoods);
    }

    /**
     * 批量删除订单商品
     * 
     * @param ids 需要删除的订单商品主键
     * @return 结果
     */
    @Override
    public int deleteMallOrderGoodsByIds(Long[] ids)
    {
        return mallOrderGoodsMapper.deleteMallOrderGoodsByIds(ids);
    }

    /**
     * 删除订单商品信息
     * 
     * @param id 订单商品主键
     * @return 结果
     */
    @Override
    public int deleteMallOrderGoodsById(Long id)
    {
        return mallOrderGoodsMapper.deleteMallOrderGoodsById(id);
    }

    @Override
    public List<MallOrderGoodsVO> selectMallOrderGoodsByOrderId(Long id) {
        return mallOrderGoodsMapper.selectMallOrderGoodsByOrderId(id);
    }
}
