package org.linlinjava.jz.db.service;

import com.alibaba.druid.util.StringUtils;
import org.linlinjava.jz.db.dao.JzDmTicketConsultTypeMapper;
import org.linlinjava.jz.db.domain.JzDmTicketConsultType;
import org.linlinjava.jz.db.domain.JzDmTicketConsultTypeExample;
import org.springframework.stereotype.Service;

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

/**
 * @author leo
 * 工单咨询类型
 */
@Service
public class JzDmTicketConsultTypeService
{
    @Resource
    private JzDmTicketConsultTypeMapper consultMapper;

    public JzDmTicketConsultType queryById(Integer id){
        return  consultMapper.selectByPrimaryKey(id);
    }

    public void add(JzDmTicketConsultType type){

        type.setCreateTime(LocalDateTime.now());
        type.setUpdateTime(LocalDateTime.now());
        consultMapper.insertSelective(type);
    }

    public  void  update(JzDmTicketConsultType type){
        type.setUpdateTime(LocalDateTime.now());
        consultMapper.updateByPrimaryKeySelective(type);
    }

    public List<JzDmTicketConsultType> querySelective(String name) {
        JzDmTicketConsultTypeExample example = new JzDmTicketConsultTypeExample();
        JzDmTicketConsultTypeExample.Criteria criteria = example.createCriteria();

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

        return consultMapper.selectByExample(example);
    }

    public List<JzDmTicketConsultType> all() {
        JzDmTicketConsultTypeExample example = new JzDmTicketConsultTypeExample();
        example.or();//.andDeletedEqualTo(false);
        return consultMapper.selectByExample(example);
    }

    public JzDmTicketConsultType findById(int id){
        return consultMapper.selectByPrimaryKey(id);
    }


    public List<JzDmTicketConsultType> queryByLevel(short level){
        JzDmTicketConsultTypeExample example = new JzDmTicketConsultTypeExample();
        example.or().andDeletedEqualTo(false).andLevelEqualTo(level);
        return  consultMapper.selectByExample(example);
    }

    public List<JzDmTicketConsultType> queryByPid(int id){
        JzDmTicketConsultTypeExample example = new JzDmTicketConsultTypeExample();
        example.or().andPidEqualTo(id);
        return  consultMapper.selectByExample(example);
    }

    /**
     * 根据类型 返回咨询类型列表
     * @param type 0咨询 1投诉
     * @return
     */
    public List<JzDmTicketConsultType> queryByType(short type){
        JzDmTicketConsultTypeExample example = new JzDmTicketConsultTypeExample();
        example.or().andDeletedEqualTo(false).andTypeEqualTo(type);
        return  consultMapper.selectByExample(example);
    }


    public JzDmTicketConsultType findByPid(int id){
        JzDmTicketConsultTypeExample example = new JzDmTicketConsultTypeExample();
        example.or().andIdEqualTo(id);
        return  consultMapper.selectOneByExample(example);
    }

    public void enable(JzDmTicketConsultType type){
        type.setUpdateTime(LocalDateTime.now());
        type.setDeleted(false);
        consultMapper.updateByPrimaryKeySelective(type);
    }

    public String path(JzDmTicketConsultType type){
        if(type.getLevel()!=3) return null;

        String path=type.getName();
        JzDmTicketConsultType levle2=findByPid(type.getPid());
        path=levle2.getName()+"->"+path;

        JzDmTicketConsultType levle3=findByPid(levle2.getPid());
        path=levle3.getName()+"->"+path;

        return path;
    }


    public void delete(int id){
        consultMapper.logicalDeleteByPrimaryKey(id);
    }

}
