package com.fdkankan.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.ucenter.entity.Cart;
import com.fdkankan.ucenter.entity.Goods;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.mapper.ICartMapper;
import com.fdkankan.ucenter.service.ICartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.ucenter.service.IGoodsService;
import com.fdkankan.ucenter.service.IUserService;
import com.fdkankan.ucenter.vo.request.CartParam;
import com.fdkankan.ucenter.vo.response.CartVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-07-13
 */
@Service
public class CartServiceImpl extends ServiceImpl<ICartMapper, Cart> implements ICartService {

    @Autowired
    IUserService userService;
    @Autowired
    IGoodsService goodsService;

    @Override
    public void add(CartParam param) {
        User user = userService.getByUserName(param.getUserName());
        param.setUserId(user.getId());
        Cart cartEntity = this.common(param);
        if (cartEntity == null){
            cartEntity = new Cart();
            cartEntity.setUserId(user.getId());
            cartEntity.setGoodsId(param.getGoodsId());
            cartEntity.setGoodsCount(param.getGoodsCount());
            cartEntity.setPayStatus(0);
            cartEntity.setSkuSn(param.getSkuSn());
            save(cartEntity);
        } else {
            cartEntity.setGoodsCount(cartEntity.getGoodsCount() + param.getGoodsCount());
            updateById(cartEntity);
        }
    }

    @Override
    public Cart getByUserIdAndGoodsId(Long userId, Long goodsId) {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId,userId);
        wrapper.eq(Cart::getGoodsId,goodsId);
        List<Cart> list = this.list(wrapper);
        if(list != null && list.size() >0){
            return list.get(0);
        }
        return null;
    }

    private Cart common(CartParam param){
        if(param.getGoodsId() == null){
            throw new BusinessException(-1,"商品为空");
        }
        return this.getByUserIdAndGoodsId(param.getUserId(),param.getGoodsId());
    }

    @Override
    public void delete(CartParam param) {
        User user = userService.getByUserName(param.getUserName());
        param.setUserId(user.getId());
        Cart cartEntity = common(param);
        if(cartEntity == null){
            return;
        }
        if(param.getGoodsCount() != 0){
            cartEntity.setGoodsCount(param.getGoodsCount());
            updateById(cartEntity);
        }else {
            this.removeById(cartEntity.getId());

        }
    }

    @Override
    public List<CartVo> listByUser(String username) {
        User user = userService.getByUserName(username);
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId,user.getId());
        List<Cart> list = this.list(wrapper);
        List<Long> goodsIds = list.parallelStream().map(Cart::getGoodsId).collect(Collectors.toList());
        if(goodsIds.size()<=0){
            return new ArrayList<>();
        }
        HashMap<Long, Goods> goodsHashMap = goodsService.getByIds(goodsIds);
        List<CartVo> voList = new ArrayList<>();
        for (Cart cart : list) {
            Goods goods = goodsHashMap.get(cart.getGoodsId());
            if(goods == null){
                continue;
            }
            CartVo cartVo = new CartVo();
            BeanUtils.copyProperties(cart,cartVo);
            cartVo.setPrice(goods.getGoodsPrice());
            voList.add(cartVo);
        }
        return voList;
    }

    @Override
    public void deleteIds(List<String> ids) {
        this.removeByIds(ids);
    }
}
