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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.frame.common.exception.TicketException;
import com.gyee.frame.model.ticket.Wfassignment;
import com.gyee.frame.mapper.ticket.WfassignmentMapper;
import com.gyee.frame.model.ticket.Wftaskassign;
import com.gyee.frame.service.ticket.WfassignmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.frame.service.ticket.WftaskassignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenmh
 * @since 2020-07-21
 */
@Service
public class WfassignmentServiceImpl extends ServiceImpl<WfassignmentMapper, Wfassignment> implements WfassignmentService {

    @Autowired
    WftaskassignService wftaskassignService;

    @Override
    public int selectMaxId() {
        return baseMapper.selectMaxId();
    }

    @Override
    public IPage<Wfassignment> selectPage(String labor, long pageNum) {
        QueryWrapper<Wfassignment> wrapper = new QueryWrapper();
        wrapper.eq("ASSIGNCODE", labor);
        wrapper.eq("ASSIGNSTATUS", "活动");
        wrapper.orderByDesc("ID");

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

        return iPage;
    }

    @Override
    public List<Wfassignment> listAssignment(double instanceId, double ownerId) {
        QueryWrapper<Wfassignment> wrapper = new QueryWrapper<>();
        wrapper.eq("WFINSTID", instanceId);
        wrapper.eq("OWNERID", ownerId);
        wrapper.orderByAsc("ID");

        List<Wfassignment> wfassignments = baseMapper.selectList(wrapper);

        return wfassignments;
    }

    @Override
    public Wfassignment updateAssignment(double instId, double version, double ownerId, String wfName, String processor) {
        QueryWrapper<Wfassignment> wrapper = new QueryWrapper<>();
        wrapper.eq("WFREVISION", version);
        wrapper.eq("WFINSTID", instId);
        wrapper.eq("WFNAME", wfName);
        wrapper.eq("OWNERID", ownerId);
        wrapper.eq("ASSIGNSTATUS", "活动");

        Wfassignment wfassignment = null;
        List<Wfassignment> list = baseMapper.selectList(wrapper);
        //根据登录角色删除其它多余项
        if (list != null && list.size() > 1){
            List<Double> ids = new ArrayList();
            for (Wfassignment assignment : list){
                if (!assignment.getAssigncode().equals(processor))
                    ids.add(assignment.getId());
                else
                    wfassignment = assignment;
            }
            // 删除之前判断是否待办人
            if (wfassignment == null)
                throw new TicketException("你没有流程任务分配，不能发送工作流程");

            baseMapper.deleteBatchIds(ids);
        }else {
            wfassignment = list.get(0);
            if (!wfassignment.getAssigncode().equals(processor))
                throw new TicketException("你没有流程任务分配，不能发送工作流程");
        }
        wfassignment.setAssignstatus("完成");
        wfassignment.setEnddate(new Date());

        int update = baseMapper.updateById(wfassignment);
        if (update == 0)
            throw new RuntimeException();

        return wfassignment;
    }

    @Transactional
    @Override
    public boolean insertAssignment(double currNodeId, double instId, double version, double ownerId, String wfName,
                                         String ticketNum, String starter, String table, String role, List<String> labors) {
        int maxId = selectMaxId();
        Wftaskassign task= wftaskassignService.taskAssign(currNodeId, version, wfName);

        for (int i = 0; i < labors.size(); i++){
            Wfassignment wfassignment = new Wfassignment();
            double id = (double)maxId + i + 1;
            wfassignment.setId(id);
            wfassignment.setDescription(task.getDescription().replace(":{TICKETNUM}", ticketNum));
            wfassignment.setAssigncode(labors.get(i));
            wfassignment.setApp(task.getApp());
            wfassignment.setStartdate(new Date());
            wfassignment.setEmailnotify("否");
            wfassignment.setAssignstatus("活动");
            wfassignment.setWfinstid(instId);
            wfassignment.setWfname(wfName);
            wfassignment.setWfrevision(version);
            wfassignment.setInitperson(starter);
            wfassignment.setOwnertable(table);
            wfassignment.setOwnerid(ownerId);
            wfassignment.setWfrole(role);
            wfassignment.setDealtimeout(0.0);
            wfassignment.setDealtype("0");
            wfassignment.setDealtype("自动发送");
            wfassignment.setPriority(0.0);
            wfassignment.setEnddate(null);
            wfassignment.setNodeid(currNodeId);
            wfassignment.setTaskid(task.getId());
            wfassignment.setNeedpass("否");

            // 插入新的待办任务
            int insert = baseMapper.insert(wfassignment);
            if (insert == 0)
                throw new RuntimeException();
        }

        return true;
    }

    @Override
    public List<Wfassignment> getActiveItem(double instId, double version, double ownerId, String status) {
        QueryWrapper<Wfassignment> wrapper = new QueryWrapper<>();
        if (status != null)
            wrapper.eq("ASSIGNSTATUS", status);
        wrapper.eq("WFREVISION", version);
        wrapper.eq("WFINSTID", instId);
        wrapper.eq("OWNERID", ownerId);
        wrapper.orderByAsc("ID");

        List<Wfassignment> wfassignments = baseMapper.selectList(wrapper);

        return wfassignments;
    }
}
