package com.hfkj.service.goods.impl;

import com.hfkj.dao.GoodsShoppingCartMapper;
import com.hfkj.entity.GoodsMsg;
import com.hfkj.entity.GoodsShoppingCart;
import com.hfkj.entity.GoodsShoppingCartExample;
import com.hfkj.service.goods.GoodsShoppingCartService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("goodsShoppingCartService")
public class GoodsShoppingCartServiceImpl implements GoodsShoppingCartService {

    @Resource
    private GoodsShoppingCartMapper goodsShoppingCartMapper;

    @Override
    public void create(GoodsShoppingCart shoppingCart) {
        goodsShoppingCartMapper.insert(shoppingCart);
    }

    @Override
    public void update(GoodsShoppingCart shoppingCart) {
        goodsShoppingCartMapper.updateByPrimaryKeySelective(shoppingCart);
    }

    @Override
    public void delete(Long id) {
        goodsShoppingCartMapper.deleteByPrimaryKey(id);
    }

    @Override
    public GoodsShoppingCart queryDetail(Long id) {
        return goodsShoppingCartMapper.selectByPrimaryKey(id);
    }

    @Override
    public GoodsShoppingCart queryDetailByMap(Map<String, Object> map) {
        GoodsShoppingCartExample example = new GoodsShoppingCartExample();
        GoodsShoppingCartExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map, "userId") != null) {
            criteria.andUserIdEqualTo(MapUtils.getLong(map, "userId"));
        }
        if (MapUtils.getLong(map, "specsId") != null) {
            criteria.andSpecsIdEqualTo(MapUtils.getLong(map, "specsId"));
        }
        if (MapUtils.getLong(map, "goodsId") != null) {
            criteria.andGoodsIdEqualTo(MapUtils.getLong(map, "goodsId"));
        }

        List<GoodsShoppingCart> list = goodsShoppingCartMapper.selectByExample(example);

        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<GoodsShoppingCart> getList(Map<String, Object> map) {
        GoodsShoppingCartExample example = new GoodsShoppingCartExample();
        GoodsShoppingCartExample.Criteria criteria = example.createCriteria();

        criteria.andUserIdEqualTo(MapUtils.getLong(map, "userId")).andStatusNotEqualTo(0);

        return goodsShoppingCartMapper.selectByExample(example);
    }
}
