package org.linlinjava.jz.db.service;

import org.linlinjava.jz.db.dao.JzFaqMapper;
import org.linlinjava.jz.db.dao.ViewJzFaqMapper;
import org.linlinjava.jz.db.domain.JzFaq;
import org.linlinjava.jz.db.domain.ViewJzFaq;
import org.linlinjava.jz.db.domain.ViewJzFaqExample;
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 JzFAQService {

    @Resource
    private JzFaqMapper faqMapper;

    @Resource
    private ViewJzFaqMapper viewJzFaqMapper;

    public List<ViewJzFaq> list(String title, Integer labelId) {

        ViewJzFaqExample example = new ViewJzFaqExample();
        ViewJzFaqExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(title)) {
            criteria.andNameLike("%" + title + "%");
        }
        criteria.andLevelEqualTo(1L);
        return viewJzFaqMapper.selectByExample(example);
    }

    public void add(JzFaq faq) {
        faq.setPid(faq.getCategoryId());
        faq.setCreateTime(LocalDateTime.now());
        faq.setUpdateTime(LocalDateTime.now());
        faqMapper.insertSelective(faq);
    }

    public void update(JzFaq faq) {
        faq.setPid(faq.getCategoryId());
        faq.setUpdateTime(LocalDateTime.now());
        faqMapper.updateByPrimaryKeySelective(faq);
    }

    public void delete(JzFaq faq) {
        faq.setUpdateTime(LocalDateTime.now());
        faq.setDeleted(true);
    }

    public JzFaq queryById(int id) {
        return faqMapper.selectByPrimaryKey(id);
    }

    public List<ViewJzFaq> queryByPid(int pid) {
        ViewJzFaqExample example = new ViewJzFaqExample();
        ViewJzFaqExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(pid);
        return viewJzFaqMapper.selectByExample(example);
    }
}
