package com.ruoyi.mall.service.impl;

import java.math.BigDecimal;
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.apache.ibatis.annotations.Param;
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)
    {
        List<MallOrderGoods> mallOrderGoodsList = mallOrderGoodsMapper.selectMallOrderGoodsList(mallOrderGoods);
        mallOrderGoodsList.forEach(item ->{
            item.setTotalPrice(item.getPrice().multiply(new BigDecimal(item.getTotal())));
        });
        return mallOrderGoodsList;
    }

    /**
     * 订单商品客户购买数量统计
     *
     * @param mallOrderGoods 订单商品
     * @return 订单商品
     */
    @Override
    public List<MallOrderGoods> selectMallOrderGoodsTotal(MallOrderGoods mallOrderGoods)
    {
        List<MallOrderGoods> mallOrderGoodsList = mallOrderGoodsMapper.selectMallOrderGoodsTotal(mallOrderGoods);
        mallOrderGoodsList.forEach(item ->{
            item.setTotalPrice(item.getPrice().multiply(new BigDecimal(item.getTotal())));
        });
        return mallOrderGoodsList;
    }

    /**
     * 新增订单商品
     *
     * @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);
    }

    /**
     * 查询订单商品列表
     *
     * @param mallOrderGoods 订单商品
     * @return 订单商品集合
     */
    @Override
    public List<MallOrderGoods> getOrderBackList(MallOrderGoods mallOrderGoods) {
        return mallOrderGoodsMapper.getOrderBackList(mallOrderGoods);
    }
    /**
     * 更改订单商品退货状态
     *
     * @param orderId 订单ID
     * @param returnStatus 退货状态：0-未退货，1-退货中，2-已退货，3-已拒绝，4-取消退货
     * @return 结果
     */
    @Override
    public int updateMallOrderGoodsReturnStatus(Long orderId, String returnStatus) {
        return mallOrderGoodsMapper.updateMallOrderGoodsReturnStatus(orderId, returnStatus);
    }

    // 根据订单ID和商品ID查询订单商品
    @Override
    public MallOrderGoodsVO selectMallOrderGoodsByOrderIdAndGoodsId(Long orderId, Long goodsId) {
        return mallOrderGoodsMapper.selectMallOrderGoodsByOrderIdAndGoodsId(orderId, goodsId);
    }

    //根据商品ID和商品订单回退退款失败的商品数量
    @Override
    public int updateMallOrderGoodsByOrderIdAndGoodsId(Long orderId,Long goodsId,String returnStatus,String returnTotal,String returnableQuantity) {
        return mallOrderGoodsMapper.updateMallOrderGoodsByOrderIdAndGoodsId(orderId, goodsId, returnStatus, returnTotal, returnableQuantity);
    }
}
