/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.flowable.service;

import com.google.common.collect.Maps;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.BaseMapper;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.flowable.entity.FlowEntity;
import com.jeeplus.modules.flowable.entity.Flow;
import org.flowable.engine.IdentityService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 审批Service
 *
 * @author jeeplus
 * @version 2017-05-16
 */
@Service
@Transactional(readOnly = true)
public abstract class FlowService<M extends BaseMapper<T>, T extends FlowEntity<T>> extends CrudService {

    @Autowired
    private FlowTaskService flowTaskService;

    @Autowired
    private IdentityService identityService;

    @Autowired
    private FlowProcessService flowProcessService;

    @Autowired
    private RuntimeService runtimeService;


    /**
     * 持久层对象
     */
    @Autowired
    protected M mapper;

    /**
     * 获取单条数据
     *
     * @param id
     * @return
     */
    public T get(String id) {
        return mapper.get(id);
    }

    /**
     * 审核新增或编辑
     *
     * @param tFlowEntity
     */
    @Transactional(readOnly = false)
    public void save(T tFlowEntity) {

        // 申请发起
        if (StringUtils.isBlank(tFlowEntity.getId())) {
            tFlowEntity.preInsert();
            mapper.insert(tFlowEntity);
            // 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
            identityService.setAuthenticatedUserId(tFlowEntity.getCurrentUser().getId());

            ProcessDefinition p = flowProcessService.getProcessDefinition(tFlowEntity.getAct().getProcDefId());
            String title = tFlowEntity.getCurrentUser().getName() + "在" + DateUtils.getDateTime() + "发起" + p.getName();
            flowTaskService.startProcess(p.getKey(), tFlowEntity.getBusinessTable(), tFlowEntity.getId(), title);
        }
        // 重新编辑申请
        else {
            tFlowEntity.preUpdate();
            mapper.update(tFlowEntity);

            tFlowEntity.getAct().setComment(("yes".equals(tFlowEntity.getAct().getFlag()) ? "[重申] " : "[销毁] ") + tFlowEntity.getAct().getComment());
            // 完成流程任务
            Map<String, Object> vars = Maps.newHashMap();
            vars.put("pass", "yes".equals(tFlowEntity.getAct().getFlag()) ? true : false);
            flowTaskService.complete(tFlowEntity.getAct().getTaskId(), tFlowEntity.getAct().getProcInsId(), tFlowEntity.getAct().getComment(), tFlowEntity.getContent(), vars);
        }
    }


    /**
     * 审核新增或编辑
     *
     * @param flow
     */
    @Transactional(readOnly = false)
    public void startAct(Flow flow) {
        // 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
        identityService.setAuthenticatedUserId(flow.getCurrentUser().getId());
        ProcessDefinition p = flowProcessService.getProcessDefinition(flow.getProcDefId());
        String title = flow.getCurrentUser().getName() + "在" + DateUtils.getDateTime() + "发起" + p.getName();
        flowTaskService.startProcess(p.getKey(), flow.getBusinessTable(), flow.getId(), title);
    }


    /**
     * 审核新增或编辑
     *
     * @param flow
     */
    @Transactional(readOnly = false)
    public void completeAct(Flow flow) {

        flow.setComment(("yes".equals(flow.getFlag()) ? "[重申] " : "[销毁] ") + flow.getComment());
        // 完成流程任务
        Map<String, Object> vars = Maps.newHashMap();
        vars.put("pass", "yes".equals(flow.getFlag()) ? true : false);
        flowTaskService.complete(flow.getTaskId(), flow.getProcInsId(), flow.getComment(), "", vars);
    }


}
