package com.willalp.canteen.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.canteen.domain.HsUserShopCar;
import com.willalp.canteen.domain.vo.UserShopCarJsonDoMainVo;
import com.willalp.canteen.mapper.HsUserShopCarMapper;
import com.willalp.canteen.service.IHsUserShopCarService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户购物车Service业务层处理
 *
 * @author willalp
 * @date 2022-12-15
 */
@Service
public class HsUserShopCarServiceImpl extends ServiceImpl<HsUserShopCarMapper, HsUserShopCar> implements IHsUserShopCarService {


    /**
     * 查询用户购物车列表
     *
     *
     * @param hsUserShopCar 用户购物车
     * @return 用户购物车
     */
    @Override
    public List<HsUserShopCar> selectHsUserShopCarList(HsUserShopCar hsUserShopCar) {
        return baseMapper.selectHsUserShopCarList(hsUserShopCar);
    }

    @Override
    public List<UserShopCarJsonDoMainVo> queryShopCarByUser(String userCode) {
        try {
            List<HsUserShopCar> userShopCars = baseMapper.selectList(new QueryWrapper<HsUserShopCar>()
                    .eq("user_code", userCode));
            if (userShopCars == null) {
                return null;
            }
            List<UserShopCarJsonDoMainVo> userShopVo = new ArrayList<>();
            for (HsUserShopCar userShopCar : userShopCars) {
                UserShopCarJsonDoMainVo uscjdmv = JSONObject.parseObject(userShopCar.getJsonData(), UserShopCarJsonDoMainVo.class);
                userShopVo.add(uscjdmv);
            }
            return userShopVo;
        } catch (Exception e) {
            throw new BaseException("购物车为空");
        }
    }

    @Override
    public int jsonAdd(UserShopCarJsonDoMainVo userShopCarJsonDoMainVo) {
        Integer integer = baseMapper.countJson(userShopCarJsonDoMainVo.getGiftId(), SecurityUtils.getUsername());
        if (null != integer && integer > 0) {
            if (userShopCarJsonDoMainVo.getPlusOrMinus() == 0) {
                userShopCarJsonDoMainVo.setNum(integer + 1);
            } else {
                if ((integer - 1) == 0) {
                    return baseMapper.deleteJson(userShopCarJsonDoMainVo.getGiftId(), SecurityUtils.getUsername());
                }
                userShopCarJsonDoMainVo.setNum(integer - 1);
            }
            if (userShopCarJsonDoMainVo.getNum() <= userShopCarJsonDoMainVo.getAmount()) {
                return baseMapper.addJsonNum(userShopCarJsonDoMainVo.getGiftId(), userShopCarJsonDoMainVo.getNum());
            } else {
                throw new BaseException("抱歉，您需要的[" + userShopCarJsonDoMainVo.getGiftName() + "]，当前库存不足");
            }
        }
        userShopCarJsonDoMainVo.setNum(1);
        if (userShopCarJsonDoMainVo.getNum() <= userShopCarJsonDoMainVo.getAmount()) {
            String username = SecurityUtils.getUsername();
            HsUserShopCar userShopCar = new HsUserShopCar();
            userShopCar.setUserCode(username);
            userShopCarJsonDoMainVo.setNum(1);
            userShopCar.setJsonData(JSONObject.toJSONString(userShopCarJsonDoMainVo));
            return baseMapper.insert(userShopCar);
        } else {
            throw new BaseException("抱歉，您需要的[" + userShopCarJsonDoMainVo.getGiftName() + "]，当前库存不足");
        }
    }

    @Override
    public Long getShopCarNum(String userCode) {
        return baseMapper.selectCount(new QueryWrapper<HsUserShopCar>()
                .eq("user_code", userCode));
    }

    @Override
    public Integer deleteJson(String id, String userCode) {
        return baseMapper.deleteJson(id, userCode);
    }

}
