package org.linlinjava.jz.db.service;

import org.linlinjava.jz.db.dao.JzTicketRelationMapper;
import org.linlinjava.jz.db.domain.JzTicketRelation;
import org.linlinjava.jz.db.domain.JzTicketRelationExample;
import org.springframework.stereotype.Service;

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

/**
 * 工单关联电访、话务等
 */
@Service
public class JzTicketRelationService {

    private final JzTicketRelation.Column[] result = new JzTicketRelation.Column[]{JzTicketRelation.Column.id, JzTicketRelation.Column.ticketId, JzTicketRelation.Column.fId};

    @Resource
    private JzTicketRelationMapper ticketCalllogMapper;

    public List<JzTicketRelation> queryByTocketId(Integer id) {
        JzTicketRelationExample example = new JzTicketRelationExample();
        example.or().andTicketIdEqualTo(id);
        return ticketCalllogMapper.selectByExampleSelective(example, result);
    }

    public int add(JzTicketRelation relation) {
        relation.setCreateTime(LocalDateTime.now());
        return ticketCalllogMapper.insertSelective(relation);
    }

    public int deleteById(JzTicketRelation relation) {
        return   ticketCalllogMapper.updateByPrimaryKeySelective(relation);
    }

    public int updateById(JzTicketRelation relation) {
        return ticketCalllogMapper.updateByPrimaryKeySelective(relation);
    }

    public int findHmAppFIdById(int ticketId) {
        JzTicketRelationExample example = new JzTicketRelationExample();
        example.or().andTicketIdEqualTo(ticketId).andTypeEqualTo((short) 3);
        JzTicketRelation relation = ticketCalllogMapper.selectOneByExample(example);
        if (relation == null)
            return 0;
        return relation.getfId();
    }
    
    public int findTicketIdByHmAppNoticeId(int noticeId) {
        JzTicketRelationExample example = new JzTicketRelationExample();
        example.or().andFIdEqualTo(noticeId).andTypeEqualTo((short) 3);
        JzTicketRelation relation = ticketCalllogMapper.selectOneByExample(example);
        if (relation == null)
            return 0;
        return relation.getTicketId();
    }

    public JzTicketRelation findById(Integer id) {
        return ticketCalllogMapper.selectByPrimaryKey(id);
    }
}
