package com.ghy.order.service.impl;

import com.ghy.order.domain.OrderGoods;
import com.ghy.order.mapper.OrderGoodsMapper;
import com.ghy.order.service.OrderGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class OrderGoodsServiceImpl implements OrderGoodsService {

    @Resource
    OrderGoodsMapper orderGoodsMapper;

    @Override
    public int insertOrderGoods(OrderGoods orderGoods) {
        return orderGoodsMapper.insertOrderGoods(orderGoods);
    }

    @Override
    public List<OrderGoods> selectByOrderMasterId(Long orderMasterId) {
        return orderGoodsMapper.selectByOrderId(orderMasterId);
    }

    @Override
    public List<OrderGoods> selectByOrderMasterIds(List<Long> orderMasterIds) {
        if (orderMasterIds == null || orderMasterIds.isEmpty()) {
            return new ArrayList<>();
        }
        return orderGoodsMapper.selectByOrderMasterIds(orderMasterIds);
    }

    @Override
    public List<OrderGoods> selectByOrderDetailId(Long orderDetailId) {
        return orderGoodsMapper.selectByOrderDetailId(orderDetailId);
    }

    @Override
    public int updateOrderGoods(OrderGoods orderGoods) {
        Assert.notNull(orderGoods.getOrderGoodsId(), "OrderGoodsId cannot be null!");
        return orderGoodsMapper.updateOrderGoods(orderGoods);
    }

    @Override
    public List<OrderGoods> selectOrderGoodsList(OrderGoods orderGoods) {
        return orderGoodsMapper.selectOrderGoodsList(orderGoods);
    }

    @Override
    public OrderGoods selectById(Long orderGoodsId) {
        return orderGoodsMapper.selectById(orderGoodsId);
    }

    @Override
    public int deleteOrderGoodsByIds(Long[] orderGoodsIds) {
        return orderGoodsMapper.deleteOrderGoodsByIds(orderGoodsIds);
    }

    @Override
    public Map<Long, List<OrderGoods>> byOrderIdInMap(List<Long> orderIdList) {
        Map<Long, List<OrderGoods>> longByOrderGoodsListMap = new HashMap<>();
        if(orderIdList != null && orderIdList.size() > 0){
            List<OrderGoods> orderGoodsList = orderGoodsMapper.getByOrderIdList(orderIdList);
            if(orderGoodsList != null && orderGoodsList.size() > 0){
                for(OrderGoods orderGoods : orderGoodsList){
                    List<OrderGoods> orderDetailListByMapCrw = longByOrderGoodsListMap.get(orderGoods.getOrderId());
                    if(orderDetailListByMapCrw != null && orderDetailListByMapCrw.size() > 0){
                        List<OrderGoods> orderDetailListByMapNew = new ArrayList<>();
                        orderDetailListByMapNew.add(orderGoods);
                        longByOrderGoodsListMap.put(orderGoods.getOrderId(), orderDetailListByMapNew);
                    }else {
                        List<OrderGoods> orderDetailListByMapNew = new ArrayList<>();
                        orderDetailListByMapNew.add(orderGoods);
                        longByOrderGoodsListMap.put(orderGoods.getOrderId(), orderDetailListByMapNew);
                    }
                }
            }
        }
        return longByOrderGoodsListMap;
    }

    @Override
    public int deleteByOrderDetailId(Long orderDetailId) {
        return orderGoodsMapper.deleteByOrderDetailId(orderDetailId);
    }
}
