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

import co.yixiang.yshop.module.member.dal.dataobject.user.MemberUserDO;
import co.yixiang.yshop.module.member.dal.mysql.user.MemberUserMapper;
import co.yixiang.yshop.module.product.dal.dataobject.storeproduct.StoreProductDO;
import co.yixiang.yshop.module.product.dal.mysql.storeproduct.StoreProductMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.lang.reflect.Member;
import java.util.*;
import co.yixiang.yshop.module.cart.controller.admin.storecart.vo.*;
import co.yixiang.yshop.module.cart.dal.dataobject.storecart.StoreCartDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.cart.convert.storecart.StoreCartConvert;
import co.yixiang.yshop.module.cart.dal.mysql.storecart.StoreCartMapper;

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

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

    @Resource
    private StoreCartMapper storeCartMapper;
    @Resource
    private StoreProductMapper storeProductMapper;
    @Resource
    private MemberUserMapper memberUserMapper;


    @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) {
        PageResult<StoreCartDO> pageResult =  storeCartMapper.selectPage(pageReqVO);
        PageResult<StoreCartRespVO> storeCartRespVO =  StoreCartConvert.INSTANCE.convertPage(pageResult);
        for (StoreCartRespVO storeCartRespVO1 : storeCartRespVO.getList()) {
            StoreProductDO storeProductDO = storeProductMapper.selectById(storeCartRespVO1.getProductId());
            MemberUserDO memberUserDO = memberUserMapper.selectById(storeCartRespVO1.getUid());
            storeCartRespVO1.setNickname(memberUserDO.getNickname());
            storeCartRespVO1.setStoreName(storeProductDO.getStoreName());
        }
        return storeCartRespVO;
    }



}
