package com.jeethink.flow.service.impl;

import com.jeethink.common.constant.Constants;
import com.jeethink.common.utils.SecurityUtils;
import com.jeethink.common.utils.uuid.IdUtils;
import com.jeethink.flow.domain.Flow;
import com.jeethink.flow.domain.FlowRecord;
import com.jeethink.flow.domain.FlowStep;
import com.jeethink.flow.mapper.FlowMapper;
import com.jeethink.flow.service.IFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlowServiceImpl implements IFlowService {

    @Autowired
    private FlowMapper flowMapper;

    /**
     * 插入新流程
     * @param objectId
     * @param flowType
     */
    @Override
    public void insertFlow(String objectId, String flowType) {

        Flow flow = new Flow();
        String flowId = IdUtils.simpleUUID();
        flow.setFlowId(flowId);
        flow.setObjectId(objectId);
        flow.setFlowType(flowType);
        flow.setFlowStatus(Constants.FLOW_STATUS_UNDERWAY);
        flow.setStepNum(1);
        flow.setCreateBy(SecurityUtils.getUsername());
        flowMapper.insertFlow(flow);

        insertRecord(flowId, 1, Constants.APPROVE_PASS, null);
    }

    /**
     * 更新流程进度
     * @param objectId
     * @param flowType
     * @param status
     * @param remark
     */
    @Override
    public void updateFlow(String objectId, String flowType, String status, String remark) {

        Flow flow = flowMapper.selectFlowByObjectIdAndFlowType(objectId, flowType);
        if (flow != null) {
            Integer stepNum = flow.getStepNum() + 1;
            flow.setStepNum(stepNum);
            flow.setUpdateBy(SecurityUtils.getUsername());
            flowMapper.updateFlow(flow);

            insertRecord(flow.getFlowId(), stepNum, status, remark);
        }
    }

    /**
     * 完成流程
     * @param objectId
     * @param flowType
     */
    @Override
    public void finishFlow(String objectId, String flowType) {

        Flow flow = new Flow();
        flow.setObjectId(objectId);
        flow.setFlowType(flowType);
        flow.setFlowStatus(Constants.FLOW_STATUS_FINISH);
        flow.setUpdateBy(SecurityUtils.getUsername());
        flowMapper.changeStatusByObjectIdAndFlowType(flow);
    }

    /**
     * 废弃流程
     * @param objectId
     * @param flowType
     */
    @Override
    public void destroyFlow(String objectId, String flowType) {



        Flow flow = new Flow();
        flow.setObjectId(objectId);
        flow.setFlowType(flowType);
        flow.setFlowStatus(Constants.FLOW_STATUS_DESTROY);
        flow.setUpdateBy(SecurityUtils.getUsername());
        flowMapper.changeStatusByObjectIdAndFlowType(flow);
    }

    /**
     * 查询流程详情
     * @param objectId
     * @param flowType
     * @return
     */
    @Override
    public List<FlowRecord> selectRecord(String objectId, String flowType) {

        return flowMapper.selectRecordByObjectIdAndFlowType(objectId, flowType);
    }

    /**
     * 通过流程流程获取流程步骤
     * @param flowType
     * @return
     */
    @Override
    public List<FlowStep> selectStepsByFlowType(String flowType) {

        return flowMapper.selectStepsByFlowType(flowType);
    }


    /**
     * 插入流程详情
     * @param flowId
     * @param stepNum
     * @param status
     * @param remark
     */
    private void insertRecord(String flowId, Integer stepNum, String status, String remark) {

        FlowRecord record = new FlowRecord();
        record.setRecordId(IdUtils.simpleUUID());
        record.setFlowId(flowId);
        record.setStepNum(stepNum);
        record.setStatus(status);
        record.setRemark(remark);
        record.setCreateBy(SecurityUtils.getUsername());
        flowMapper.insertRecord(record);
    }
}
