package org.linlinjava.jz.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.jz.db.dao.JzFaqCategoryMapper;
import org.linlinjava.jz.db.domain.JzFaqCategory;
import org.linlinjava.jz.db.domain.JzFaqCategoryExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 知识库咨询类型
 */
@Service
public class JzFAQCategoryService {

    @Resource
    private JzFaqCategoryMapper categoryMapper;
    private JzFaqCategory.Column[] CHANNEL = {JzFaqCategory.Column.id, JzFaqCategory.Column.name, JzFaqCategory.Column.iconUrl,JzFaqCategory.Column.keywords};

    public List<JzFaqCategory> queryL1WithoutRecommend(int offset, int limit) {
        JzFaqCategoryExample example = new JzFaqCategoryExample();
        example.or().andLevelEqualTo("L1").andNameNotEqualTo("推荐").andDeletedEqualTo(false);
        PageHelper.startPage(offset, limit);
        return categoryMapper.selectByExample(example);
    }

    public List<JzFaqCategory> queryL1(int offset, int limit) {
        JzFaqCategoryExample example = new JzFaqCategoryExample();
        example.or().andLevelEqualTo("L1").andDeletedEqualTo(false);
        PageHelper.startPage(offset, limit);
        return categoryMapper.selectByExample(example);
    }

    public List<JzFaqCategory> queryL1() {
        JzFaqCategoryExample example = new JzFaqCategoryExample();
        example.or().andLevelEqualTo("L1").andDeletedEqualTo(false);
        return categoryMapper.selectByExample(example);
    }

    public List<JzFaqCategory> queryByPid(Integer pid) {
        JzFaqCategoryExample example = new JzFaqCategoryExample();
        example.or().andPidEqualTo(pid).andDeletedEqualTo(false);
        return categoryMapper.selectByExample(example);
    }

    public List<JzFaqCategory> queryL2ByIds(List<Integer> ids) {
        JzFaqCategoryExample example = new JzFaqCategoryExample();
        example.or().andIdIn(ids).andLevelEqualTo("L2").andDeletedEqualTo(false);
        return categoryMapper.selectByExample(example);
    }

    public JzFaqCategory findById(Integer id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    public List<JzFaqCategory> querySelective(String id, String name, Integer page, Integer size, String sort, String order) {
        JzFaqCategoryExample example = new JzFaqCategoryExample();
        JzFaqCategoryExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return categoryMapper.selectByExample(example);
    }

    public int updateById(JzFaqCategory category) {
        category.setUpdateTime(LocalDateTime.now());
        return categoryMapper.updateByPrimaryKeySelective(category);
    }

    public void deleteById(Integer id) {
        categoryMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(JzFaqCategory category) {
        category.setAddTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.insertSelective(category);
    }

    public List<JzFaqCategory> queryChannel() {
        JzFaqCategoryExample example = new JzFaqCategoryExample();
        example.or().andLevelEqualTo("L1").andDeletedEqualTo(false);
        return categoryMapper.selectByExampleSelective(example, CHANNEL);
    }
}
