package com.gyee.frame.service.impl.ticket;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.frame.model.ticket.Wfaction;
import com.gyee.frame.model.ticket.Wobugeq;
import com.gyee.frame.mapper.ticket.WobugeqMapper;
import com.gyee.frame.model.ticket.Workticket;
import com.gyee.frame.service.ticket.WfactionService;
import com.gyee.frame.service.ticket.WobugeqService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.frame.util.ticket.TicketUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenmh
 * @since 2020-06-02
 */
@Service
public class WobugeqServiceImpl extends ServiceImpl<WobugeqMapper, Wobugeq> implements WobugeqService {

    @Autowired
    WfactionService wfactionService;

    @Override
    public IPage<Wobugeq> selectPage(long pageNum, String dept) {
        QueryWrapper<Wobugeq> wrapper = new QueryWrapper();
        wrapper.orderByDesc("ID");
        if (TicketUtil.allDept().contains(dept)){
            wrapper.eq("SITENUM", dept);
        }else{
            wrapper.in("SITENUM", TicketUtil.allDept());
        }

        Page<Wobugeq> page = new Page<>(pageNum, 15);
        IPage<Wobugeq> iPage = baseMapper.selectPage(page, wrapper);

        return iPage;
    }

    @Override
    public boolean updateTicket(double id, double startNodeId, double endNodeId) {
        Wfaction action = wfactionService.getActionStatus(startNodeId, endNodeId);

        Wobugeq ticket = new Wobugeq();
        ticket.setId(id);
        ticket.setStatus(action.getAction());
        ticket.setStatusdate(new Date());
        int i = baseMapper.updateById(ticket);
        if (i <= 0)
            throw new RuntimeException();

        return true;
    }
}
