package com.hwrj.cloud.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.hwrj.cloud.common.exception.ApiException;
import com.hwrj.cloud.portal.mapper.CmsSubjectMapper;
import com.hwrj.cloud.portal.mapper.CmsSubjectProductRelationMapper;
import com.hwrj.cloud.portal.model.CmsSubject;
import com.hwrj.cloud.portal.model.CmsSubjectExample;
import com.hwrj.cloud.portal.model.CmsSubjectProductRelation;
import com.hwrj.cloud.portal.model.CmsSubjectProductRelationExample;
import com.hwrj.cloud.portal.service.CmsSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CmsSubjectServiceImpl implements CmsSubjectService {

    @Autowired
    private CmsSubjectMapper cmsSubjectMapper;

    @Autowired
    private CmsSubjectProductRelationMapper relationMapper;
    @Override
    public List<CmsSubjectProductRelation> subjectProductRelation(Long id,Integer pageNum,Integer pageSize) {
        if (id != null){
            CmsSubjectExample subjectExample = new CmsSubjectExample();
            CmsSubjectExample.Criteria criteria1 = subjectExample.createCriteria();
            criteria1.andDelFlagEqualTo(0);criteria1.andIdEqualTo(id);
            List<CmsSubject> subjects = cmsSubjectMapper.selectByExample(subjectExample);
            if (subjects==null||subjects.size()<1){
                throw new ApiException("没有对应的专题信息");
            }
            PageHelper.startPage(pageNum,pageSize);
            CmsSubjectProductRelationExample example = new CmsSubjectProductRelationExample();
            CmsSubjectProductRelationExample.Criteria criteria = example.createCriteria();
            criteria.andSubjectIdEqualTo(id);
            return relationMapper.selectByExample(example);
        }
        return null;
    }
}
