package co.yixiang.yshop.module.product.service.storeproductrelation;

import co.yixiang.yshop.module.product.controller.app.product.vo.AppStoreProductReplyQueryVo;
import co.yixiang.yshop.module.product.dal.dataobject.storeproductreply.StoreProductReplyDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import co.yixiang.yshop.module.product.controller.admin.storeproductrelation.vo.*;
import co.yixiang.yshop.module.product.dal.dataobject.storeproductrelation.StoreProductRelationDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.product.convert.storeproductrelation.StoreProductRelationConvert;
import co.yixiang.yshop.module.product.dal.mysql.storeproductrelation.StoreProductRelationMapper;

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

/**
 * 商品点赞和收藏 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class StoreProductRelationServiceImpl implements StoreProductRelationService {

    @Resource
    private StoreProductRelationMapper storeProductRelationMapper;


    @Override
    public void deleteStoreProductRelation(Long id) {
        // 校验存在
        validateStoreProductRelationExists(id);
        // 删除
        storeProductRelationMapper.deleteById(id);
    }

    private void validateStoreProductRelationExists(Long id) {
        if (storeProductRelationMapper.selectById(id) == null) {
            throw exception(STORE_PRODUCT_RELATION_NOT_EXISTS);
        }
    }


    @Override
    public PageResult<AppStoreProductReplyQueryVo> getStoreProductRelationPage(StoreProductRelationPageReqVO pageReqVO) {

        Page<StoreProductRelationDO> pageModel = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<AppStoreProductReplyQueryVo> list = storeProductRelationMapper
                .allRelationList(pageModel,pageReqVO.getNickname());
        return new PageResult<>(list, storeProductRelationMapper.allRelationListCount(pageReqVO.getNickname()));
    }



}
