package cn.iocoder.yudao.module.bpm.service.business.listener;

import cn.iocoder.yudao.module.bpm.dal.dataobject.business.BpmBusinessFormDO;
import cn.iocoder.yudao.module.bpm.enums.BpmBusinessTypeEnum;
import cn.iocoder.yudao.module.bpm.enums.task.BpmProcessInstanceStatusEnum;
import cn.iocoder.yudao.module.bpm.event.BpmProcessInstanceStatusEvent;
import cn.iocoder.yudao.module.bpm.event.BpmProcessInstanceStatusEventListener;
import cn.iocoder.yudao.module.bpm.service.business.BpmBusinessFormService;
import cn.iocoder.yudao.module.system.api.bpm.BpmCheckInWaitApi;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 长者轮候入住流程状态监听器
 */
@Component
public class BpmCheckInWaitStatusListener extends BpmProcessInstanceStatusEventListener {

    @Resource
    private BpmBusinessFormService businessFormService;

    @Resource
    private BpmCheckInWaitApi checkInWaitApi;

    @Override
    protected String getProcessDefinitionKey() {
        return BpmBusinessTypeEnum.CHECK_IN_WAIT.getKey();
    }

    @Override
    protected void onEvent(BpmProcessInstanceStatusEvent event) {
        BpmBusinessFormDO businessFormDO = businessFormService.getBusinessFormByInstanceId(event.getId());
        businessFormDO.setStatus(event.getStatus());
        businessFormService.updateBusinessForm(businessFormDO);

        if (BpmProcessInstanceStatusEnum.APPROVE.getStatus().equals(event.getStatus())) {
            checkInWaitApi.updateCheckInWaitStatus(businessFormDO.getBusinessId(), 10);
            BpmBusinessFormDO updateDO = new BpmBusinessFormDO();
            updateDO.setId(businessFormDO.getId());
            updateDO.setCompleteStatus(1);
            businessFormService.updateBusinessForm(updateDO);
        }
    }
}
