package com.ruoyi.mall.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.MallGoodsProduct;
import com.ruoyi.mall.mapper.MallGoodsProductMapper;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallMemberCartMapper;
import com.ruoyi.mall.domain.MallMemberCart;
import com.ruoyi.mall.service.IMallMemberCartService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 购物车Service业务层处理
 *
 * @author chang
 * @date 2021-11-25
 */
@Service
public class MallMemberCartServiceImpl implements IMallMemberCartService
{
    @Autowired
    private MallMemberCartMapper mallMemberCartMapper;

    @Autowired
    private MallGoodsProductMapper mallGoodsProductMapper;

    /**
     * 商品列表修改数量
     * @param param
     * @return
     */
    @Override
    public ApiResult goodsListEditCount(Map<String,Object> param){
        //用户id
        Long userId = MapUtils.getLong(param, "userId");
        //商品id
        Long goodsId = MapUtils.getLong(param, "goodsId");
        //规格id
        Long productId = MapUtils.getLong(param, "productId");
        //类型（0-减少，1-增加）
        Long type = MapUtils.getLong(param, "type");
        //数量
        Long count = MapUtils.getLong(param, "count");

        MallGoodsProduct mallGoodsProduct = mallGoodsProductMapper.selectMallGoodsProductById(productId);
        if (ObjectUtils.isEmpty(mallGoodsProduct)){
            return ApiResult.error("规格信息异常");
        }

        if (type == 1 && count > mallGoodsProduct.getStock()){
            return ApiResult.error("此规格库存不足");
        }

        //查询购物车是否存在此商品
        MallMemberCart mallMemberCart = new MallMemberCart();
        mallMemberCart.setUserId(userId);
        mallMemberCart.setGoodsId(goodsId);
        mallMemberCart.setProductId(productId);
        MallMemberCart memberCart = mallMemberCartMapper.selectMallMemberCartByCart(mallMemberCart);

        if (ObjectUtils.isEmpty(memberCart)){
            if (type == 0){//减少
                return ApiResult.error("未加入购物车");
            }else if (type == 1){//增加
                mallMemberCart.setTotal(count.intValue());
                mallMemberCart.setUpdateAt(DateUtils.getTime());
                mallMemberCart.setDelFlag("0");
                return ApiResult.toAjax(mallMemberCartMapper.insertMallMemberCart(mallMemberCart));
            }
        }else{
            if (type == 0){//减少
                Integer total = memberCart.getTotal();
                //如果数量大于或等于原购物车数量，从购物车中删除
                if (count.intValue() >= total){
                    return ApiResult.toAjax(mallMemberCartMapper.deleteMallMemberCartById(memberCart.getId()));
                }else{
                    memberCart.setTotal(memberCart.getTotal()-count.intValue());
                    memberCart.setUpdateAt(DateUtils.getTime());
                    return ApiResult.toAjax(mallMemberCartMapper.updateMallMemberCart(memberCart));
                }
            }else if (type == 1){//增加
                memberCart.setTotal(memberCart.getTotal()+count.intValue());
                memberCart.setUpdateAt(DateUtils.getTime());
                return ApiResult.toAjax(mallMemberCartMapper.updateMallMemberCart(memberCart));
            }
        }
        return ApiResult.ok();
    }

    /**
     * 查询购物车
     *
     * @param id 购物车主键
     * @return 购物车
     */
    @Override
    public MallMemberCart selectMallMemberCartById(Long id)
    {
        return mallMemberCartMapper.selectMallMemberCartById(id);
    }

    /**
     * 查询购物车列表
     *
     * @param mallMemberCart 购物车
     * @return 购物车
     */
    @Override
    public List<MallMemberCart> selectMallMemberCartList(MallMemberCart mallMemberCart)
    {
        return mallMemberCartMapper.selectMallMemberCartList(mallMemberCart);
    }

    /**
     * 新增购物车
     *
     * @param mallMemberCart 购物车
     * @return 结果
     */
    @Override
    public int insertMallMemberCart(MallMemberCart mallMemberCart)
    {
        return mallMemberCartMapper.insertMallMemberCart(mallMemberCart);
    }

    /**
     * 修改购物车
     *
     * @param mallMemberCart 购物车
     * @return 结果
     */
    @Override
    public int updateMallMemberCart(MallMemberCart mallMemberCart)
    {
        return mallMemberCartMapper.updateMallMemberCart(mallMemberCart);
    }

    /**
     * 批量删除购物车
     *
     * @param ids 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberCartByIds(Long[] ids)
    {
        return mallMemberCartMapper.deleteMallMemberCartByIds(ids);
    }

    /**
     * 删除购物车信息
     *
     * @param id 购物车主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberCartById(Long id)
    {
        return mallMemberCartMapper.deleteMallMemberCartById(id);
    }

    /*
    * 批量删除购物车
    * */
    @Transactional
    @Override
    public void removeByUserIdAndCardIds(String cartsId, String userId) {
        String[] cartIdList = cartsId.split(",");
        for (String cartId :cartIdList) {
            MallMemberCart cart = new MallMemberCart();
            cart.setId(Long.parseLong(cartId));
            cart.setUserId(Long.parseLong(userId));
            mallMemberCartMapper.deleteMallMemberCartByCart(cart);
        }
    }

    @Override
    public MallMemberCart selectMallMemberCartByCart(MallMemberCart cart) {
        return mallMemberCartMapper.selectMallMemberCartByCart(cart);
    }
}
