package co.yixiang.yshop.module.cart.service.storecart;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.cart.controller.admin.storecart.vo.StoreCartPageReqVO;
import co.yixiang.yshop.module.cart.controller.admin.storecart.vo.StoreCartRespVO;
import co.yixiang.yshop.module.cart.dal.mysql.storecart.StoreCartMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.cart.enums.ErrorCodeConstants.STORE_CART_NOT_EXISTS;

/**
 * 购物车 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class StoreCartServiceImpl implements StoreCartService {

    @Resource
    private StoreCartMapper storeCartMapper;


    @Override
    public void deleteStoreCart(Long id) {
        // 校验存在
        validateStoreCartExists(id);
        // 删除
        storeCartMapper.deleteById(id);
    }

    private void validateStoreCartExists(Long id) {
        if (storeCartMapper.selectById(id) == null) {
            throw exception(STORE_CART_NOT_EXISTS);
        }
    }


    @Override
    public PageResult<StoreCartRespVO> getStoreCartPage(StoreCartPageReqVO pageReqVO) {
        List<StoreCartRespVO> list = storeCartMapper.pageList(new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize()),
                pageReqVO.getNickname());

        return new PageResult<>(list, storeCartMapper.pageListCount(pageReqVO.getNickname()));
    }

}
