package com.fzm.mall.server.front.shopping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.front.shopping.mapper.UserShoppingCartMapper;
import com.fzm.mall.server.front.shopping.model.UserShoppingCart;
import com.fzm.mall.server.front.shopping.model.dto.ShoppingCartDto;
import com.fzm.mall.server.front.shopping.service.IUserShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户购物车表 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-03-26
 */
@Service
public class UserShoppingCartServiceImpl extends ServiceImpl<UserShoppingCartMapper, UserShoppingCart> implements IUserShoppingCartService {
    @Autowired
    UserShoppingCartMapper userShoppingCartMapper;

    @Override
    public UserShoppingCart getShopBySkuId(String skuId, String uid, Integer isActive) {
        QueryWrapper<UserShoppingCart> qw = new QueryWrapper<>();
        qw.eq("uid", uid);
        qw.eq("sku_id", skuId);
        qw.eq("is_active", isActive);
        return getOne(qw);
    }

    @Override
    public UserShoppingCart getShopById(String id) {
        QueryWrapper<UserShoppingCart> qw = new QueryWrapper<>();
        qw.eq("id", id);
        return getOne(qw);
    }

    @Override
    @Transactional
    public int saveOrUpdateShoppingCart(String uid, ShoppingCartDto shoppingCartDto) {
        UserShoppingCart newUserShop = new UserShoppingCart();
        String skuId = shoppingCartDto.getSkuId();
        Integer number = shoppingCartDto.getNumber();
        newUserShop.setNumber(number);
        newUserShop.setSkuId(skuId);
        Integer isActive = shoppingCartDto.getIsActive();
        newUserShop.setIsActive(isActive);
        newUserShop.setUid(uid);
        boolean save = false;
        if (shoppingCartDto.getId() == null) {
            //新增或者从其他地方增加相同的sku商品
            //查询数据库是否相同sku
            if (!StringUtil.isNullOrEmpty(skuId)) {
                UserShoppingCart shopCart = getShopBySkuId(skuId, uid, 1);
                if (shopCart != null) {
                    newUserShop.setId(shopCart.getId());
                    newUserShop.setNumber(number + shopCart.getNumber());
                } else {
                    //为空的，查is_active为0的，如果查到就修改这个为0的数量为最新数量
                    UserShoppingCart shopCartDel = getShopBySkuId(skuId, uid, 0);
                    if (shopCartDel != null) {
                        shopCartDel.setNumber(number);
                        shopCartDel.setIsActive(isActive);
                        save = saveOrUpdate(shopCartDel);
                        if (save) {
                            return 1;
                        } else {
                            try {
                                throw new Exception("数据库异常");
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        } else {
            newUserShop.setId(shoppingCartDto.getId());
        }
        save = saveOrUpdate(newUserShop);
        if (save) {
            return 1;
        } else {
            try {
                throw new Exception("数据库异常");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return -1;
    }

    @Override
    public List<UserShoppingCart> getList(String uid) {
        QueryWrapper<UserShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        queryWrapper.eq("is_active", 1);
        queryWrapper.orderByDesc("create_time");
        return list(queryWrapper);
    }

    @Override
    public int delList(String uid, String skuIds) {

        return userShoppingCartMapper.delList(uid, skuIds);
    }
}
