package com.fs.his.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.core.domain.R;
import com.fs.common.exception.CustomException;
import com.fs.his.domain.FsIntegralCart;
import com.fs.his.domain.FsIntegralGoods;
import com.fs.his.domain.FsUser;
import com.fs.his.mapper.FsIntegralCartMapper;
import com.fs.his.mapper.FsIntegralGoodsMapper;
import com.fs.his.mapper.FsUserMapper;
import com.fs.his.service.IFsIntegralCartService;
import com.fs.his.vo.FsIntegralCartVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service
public class FsIntegralCartServiceImpl extends ServiceImpl<FsIntegralCartMapper, FsIntegralCart> implements IFsIntegralCartService {

    @Resource
    private FsIntegralGoodsMapper goodsMapper;
    @Resource
    private FsUserMapper userMapper;

    /**
     * 添加或修改购物车
     *
     * @param userId  用户ID
     * @param goodsId 积分商品ID
     * @param cartNum 数量
     * @param isCart  是否购物车
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addOrUpdateCart(Long userId, Long goodsId, Integer cartNum, Integer isCart) {
        log.debug("addOrUpdateCart userId: {}, goodsId: {}, cartNum: {}, isCart: {}", userId, goodsId, cartNum, isCart);
        FsUser fsUser = userMapper.selectFsUserByUserId(userId);
        if (Objects.isNull(fsUser)) {
            log.warn("addOrUpdateCart user not find userId: {}", userId);
            throw new CustomException("用户不存在");
        }

        FsIntegralGoods fsIntegralGoods = goodsMapper.selectFsIntegralGoodsByGoodsId(goodsId);
        if (Objects.isNull(fsIntegralGoods)) {
            log.warn("addOrUpdateCart goods not find goodsId: {}", goodsId);
            throw new CustomException("商品不存在");
        }

        if (fsIntegralGoods.getStock() < cartNum) {
            log.warn("addOrUpdateCart goods not enough cartNum: {}", goodsId);
            throw new CustomException("库存不足");
        }
        if (fsIntegralGoods.getStatus() == 0) {
            log.warn("addOrUpdateCart goods not enable cartNum: {}", goodsId);
            throw new CustomException("商品停售");
        }

        FsIntegralCart cart = new FsIntegralCart();
        cart.setUserId(userId);
        cart.setGoodsId(goodsId);
        cart.setCartNum(cartNum);
        cart.setIntegral(fsIntegralGoods.getIntegral());
        cart.setCash(fsIntegralGoods.getCash());
        cart.setCreateTime(LocalDateTime.now());
        cart.setUpdateTime(LocalDateTime.now());
        cart.setAddToExist(isCart == 0);
        baseMapper.insertOrUpdate(cart);
    }

    /**
     * 移除购物车
     * @param userId    用户ID
     * @param ids       购物车ID集合
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delCartByIds(Long userId, List<Long> ids) {
        Wrapper<FsIntegralCart> wrapper = Wrappers.<FsIntegralCart>lambdaQuery()
                .eq(FsIntegralCart::getUserId, userId)
                .in(FsIntegralCart::getId, ids);
        baseMapper.delete(wrapper);
    }

    /**
     * 获取购物车商品数量
     * @param params    参数
     * @return num      数量
     */
    @Override
    public Integer getCartCountByMap(Map<String, Object> params) {
        return baseMapper.getCartCountByMap(params);
    }

    /**
     * 获取购物车列表
     * @param userId    用户ID
     * @return  list
     */
    @Override
    public List<FsIntegralCartVO> getCartsByUserId(Long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        return baseMapper.getCartsByMap(params);
    }

    /**
     * 根据购物车ID查询详情
     * @param userId    用户ID
     * @param ids       购物车ID集合
     * @return  list
     */
    @Override
    public List<FsIntegralCartVO> getCartByIds(Long userId, List<Long> ids) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("ids", ids);
        return baseMapper.getCartsByMap(params);
    }
}
