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.service.ticket.WfactionService;
import com.gyee.frame.util.StringUtils;
import com.gyee.frame.util.ticket.TicketUtil;
import com.gyee.frame.model.ticket.Workticket;
import com.gyee.frame.mapper.ticket.WorkticketMapper;
import com.gyee.frame.service.ticket.WorkticketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenmh
 * @since 2020-06-01
 */
@Service
public class WorkticketServiceImpl extends ServiceImpl<WorkticketMapper, Workticket> implements WorkticketService {

    @Autowired
    WfactionService wfactionService;

    @Override
    public IPage<Workticket> selectPage(long pageNum, String type, String dept) {
        String ticketType = TicketUtil.ticketType(type);
        QueryWrapper<Workticket> wrapper = new QueryWrapper();

        wrapper.eq("WTICKETTYPE", ticketType);
        wrapper.orderByDesc("ID");
        if (TicketUtil.allDept().contains(dept)){
            wrapper.eq("WORKDEPT", dept);
        }else{
            wrapper.in("WORKDEPT", TicketUtil.allDept());
        }

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

        return iPage;
    }

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

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

        return true;
    }
}
