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

import com.github.pagehelper.PageHelper;
import com.hwrj.cloud.admin.mapper.CmsSubjectCategoryMapper;
import com.hwrj.cloud.admin.model.CmsSubject;
import com.hwrj.cloud.admin.model.CmsSubjectCategory;
import com.hwrj.cloud.admin.model.CmsSubjectCategoryExample;
import com.hwrj.cloud.admin.service.CmsSubjectCategoryService;
import com.hwrj.cloud.admin.service.CmsSubjectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CmsSubjectCategoryServiceImpl implements CmsSubjectCategoryService {
    @Autowired
    private CmsSubjectCategoryMapper cmsSubjectCategoryMapper;

    @Autowired
    private CmsSubjectService cmsSubjectService;

    @Override
    public int add(CmsSubjectCategory category) {
        if (category != null){
            category.setDelFlag(0);
            return cmsSubjectCategoryMapper.insertSelective(category);

        }

        return 0;
    }

    @Override
    public int update(CmsSubjectCategory category) {
        if (category != null){
            category.setDelFlag(0);
            return cmsSubjectCategoryMapper.updateByPrimaryKeySelective(category);

        }
        return 0;
    }

    @Override
    public int delete(Long id) {
        if (id != null){
            List<CmsSubject> subjects = cmsSubjectService.subjectByCategory(id);
            if (subjects == null||subjects.size()<0){
                CmsSubjectCategory cmsSubjectCategory = new CmsSubjectCategory();
                cmsSubjectCategory.setId(id);
                cmsSubjectCategory.setDelFlag(1);
                return cmsSubjectCategoryMapper.updateByPrimaryKeySelective(cmsSubjectCategory);
            }
        }
        return 0;
    }

    @Override
    public CmsSubjectCategory info(Long id) {
        if (id != null){
            return cmsSubjectCategoryMapper.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    public List<CmsSubjectCategory> pageInfo(String keyword, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        CmsSubjectCategoryExample example = new CmsSubjectCategoryExample();
        CmsSubjectCategoryExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(keyword)){
            criteria.andNameLike("%"+keyword+"%");
        }
        return cmsSubjectCategoryMapper.selectByExample(example);
    }

    @Override
    public List<CmsSubjectCategory> listAll(String keyword) {
        CmsSubjectCategoryExample example = new CmsSubjectCategoryExample();
        CmsSubjectCategoryExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(keyword)){
            criteria.andNameLike("%"+keyword+"%");
        }
        return cmsSubjectCategoryMapper.selectByExample(example);
    }
}
