package com.qxgmat.service.inline;

import com.github.pagehelper.Page;
import com.nuliji.tools.AbstractService;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;
import com.nuliji.tools.mybatis.Example;
import com.qxgmat.data.dao.SentenceArticleMapper;
import com.qxgmat.data.dao.entity.Manager;
import com.qxgmat.data.dao.entity.SentenceArticle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

@Service
public class SentenceArticleService extends AbstractService {
    private static final Logger logger = LoggerFactory.getLogger(SentenceArticleService.class);

    @Resource
    private SentenceArticleMapper sentenceArticleMapper;

    /**
     * 获取长难句单个章节所有文章
     * @param chapter
     * @return
     */
    public List<SentenceArticle> listByChapter(Number chapter){
        Example example = new Example(SentenceArticle.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("chapter", chapter)
        );
        example.orderBy("part").asc();
        return select(sentenceArticleMapper, example);
    }

    /**
     * 获取所有试用章节文章
     * @return
     */
    public List<SentenceArticle> listByTrail(){
        Example example = new Example(SentenceArticle.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("isTrail", 1)
        );
        example.setOrderByClause("chapter asc,part asc");
        return select(sentenceArticleMapper, example);
    }

    /**
     * 获取章节下最大
     * @param chapter
     * @return
     */
    public Integer maxPart(Integer chapter){
        Example example = new Example(SentenceArticle.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("chapter", chapter)
        );
        example.selectProperties("part");
        example.setOrderByClause("part desc");
        SentenceArticle entity = one(sentenceArticleMapper, example);
        return entity != null ? entity.getPart() : 0;
    }

    public SentenceArticle add(SentenceArticle article){
        int result = insert(sentenceArticleMapper, article);
        article = one(sentenceArticleMapper, article.getId());
        if(article == null){
            throw new SystemException("文章添加失败");
        }
        return article;
    }

    public SentenceArticle edit(SentenceArticle article){
        SentenceArticle in = one(sentenceArticleMapper, article.getId());
        if(in == null){
            throw new ParameterException("文章不存在");
        }
        int result = update(sentenceArticleMapper, article);
        return article;
    }

    public boolean delete(Number id){
        SentenceArticle in = one(sentenceArticleMapper, id);
        if(in == null){
            throw new ParameterException("文章不存在");
        }
        int result = delete(sentenceArticleMapper, id);
        return result > 0;
    }

    public SentenceArticle get(Number id){
        SentenceArticle in = one(sentenceArticleMapper, id);

        if(in == null){
            throw new ParameterException("文章不存在");
        }
        return in;
    }

    public Page<SentenceArticle> select(int page, int pageSize){
        return select(sentenceArticleMapper, page, pageSize);
    }

    public Page<SentenceArticle> select(int page, int pageSize, Number chapter, Number part){
        Example example = new Example(SentenceArticle.class);
        if (chapter!=null){
            example.and(
                    example.createCriteria()
                            .andEqualTo("chapter", chapter)
            );
        }
        if (part!=null){
            example.and(
                    example.createCriteria()
                            .andEqualTo("part", part)
            );
        }
        return select(sentenceArticleMapper, example, page, pageSize);
    }

    public List<SentenceArticle> select(Collection ids){
        return select(sentenceArticleMapper, ids);
    }

}
