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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.frame.model.ticket.*;
import com.gyee.frame.mapper.ticket.WftransMapper;
import com.gyee.frame.service.ticket.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenmh
 * @since 2020-07-21
 */
@Service
public class WftransServiceImpl extends ServiceImpl<WftransMapper, Wftrans> implements WftransService {

    @Autowired
    IbsidgenService ibsidgenService;
    @Autowired
    WfactionService wfactionService;
    @Autowired
    WfinstanceService wfinstanceService;
    @Autowired
    WfassignmentService wfassignmentService;
    @Autowired
    WorkticketService workticketService;
    @Autowired
    WfnodeService wfnodeService;
    @Autowired
    WobugeqService wobugeqService;


    @Override
    public List<Wftrans> listTrans(double instanceId) {
        QueryWrapper<Wftrans> wrapper = new QueryWrapper<>();
        wrapper.eq("WFINSTID", instanceId);
        wrapper.in("TRANSTYPE", "开始", "任务", "等待", "结束");
        wrapper.orderByAsc("ID");

        List<Wftrans> trans = baseMapper.selectList(wrapper);

        return trans;
    }

    @Transactional
    @Override
    public boolean insertTrans(double currNodeId, double preNodeId, double actionId, double instId, double version, double ownerId,
                               String wfName, String ticketNum, String role, List<String> labors, String processor) {
        Wfinstance instance = wfinstanceService.instance(ownerId);
        if (instance == null || instance.getActive().equals("否"))
            return false;

        Wfnode node = wfnodeService.getNode(currNodeId, version, wfName);
        if (node == null)
            return false;

        Ibsidgen gen = ibsidgenService.selectId("WFTRANS");
        double id = gen.getIdvalue();

        // 流程结束
        if (node.getNodetype().equals("结束")){
            wfinstanceService.updateInstance(instId, currNodeId, "否");
            // 更新之前的待办任务
            Wfassignment assignment = wfassignmentService.updateAssignment(instId, version, ownerId, wfName, processor);
            // 插入事务
            saveTrans(id += 1, preNodeId, instId, assignment.getId(), actionId, "任务", "任务",
                    wfName, version, instance.getOwnertable(), ownerId, assignment.getAssigncode());
            // 插入结束事务
            saveTrans(id += 2, currNodeId, instId, 0, actionId, "结束", "结束",
                    wfName, version, instance.getOwnertable(), ownerId, assignment.getAssigncode());

            //更新主键
            ibsidgenService.updateGen(gen, id);

            // 更新票据状态
            if (wfName.equals("缺陷处理流程")){
                wobugeqService.updateTicket(ownerId, preNodeId, currNodeId);
            }else {
                workticketService.updateTicket(ownerId, preNodeId, currNodeId);
            }
        }else {
            wfinstanceService.updateInstance(instId, currNodeId, null);
            // 更新之前的待办任务
            Wfassignment assignment = wfassignmentService.updateAssignment(instId, version, ownerId, wfName, processor);
            // 插入新的待办任务
            wfassignmentService.insertAssignment(currNodeId, instId, version, ownerId, wfName, ticketNum,
                    instance.getStartor(), instance.getOwnertable(), role, labors);

            // 插入事务
            saveTrans(id += 1, preNodeId, instId, assignment.getId(), actionId, "任务", "任务",
                    wfName, version, instance.getOwnertable(), ownerId, assignment.getAssigncode());

            //更新主键
            ibsidgenService.updateGen(gen, id);

            // 更新票据状态
            if (wfName.equals("缺陷处理流程")){
                wobugeqService.updateTicket(ownerId, preNodeId, currNodeId);
            }else {
                workticketService.updateTicket(ownerId, preNodeId, currNodeId);
            }
        }

        return true;
    }

    private void saveTrans(double id, double nodeId, double instId, double assignId, double actionId, String transType,
                           String nodeType, String wfName, double version, String table, double ownerId, String assignCode){
        Wftrans trans = new Wftrans();
        trans.setId(id);
        trans.setNodeid(nodeId);
        trans.setWfinstid(instId);
        if (assignId > 0)
            trans.setAssignid(assignId);
        trans.setActionid(actionId);
        trans.setTranstype(transType);
        trans.setTransdate(new Date());
        trans.setMemo(null);
        trans.setNodetype(nodeType);
        trans.setWfname(wfName);
        trans.setWfrevision(version);
        trans.setOwnertable(table);
        trans.setOwnerid(ownerId);
        trans.setAssigncode(assignCode);
        trans.setPriority(Double.valueOf(0));

        // 事务里边插入之前的待办任务
        int insert = baseMapper.insert(trans);
        if (insert == 0)
            throw new RuntimeException();
    }
}
