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


import cn.iocoder.yudao.module.bpm.controller.admin.business.vo.BpmApplyRetreatCreateReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.business.BpmApplyAccommodationDO;
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.bpm.service.business.BpmElderlApplyService;
import cn.iocoder.yudao.module.bpm.service.business.BpmElderlApplyServiceImpl;
import cn.iocoder.yudao.module.system.api.bpm.BpmElderlyApi;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: jinyd
 * @CreateTime: 2024/8/6  11:48
 * @Description: 入住申请的结果的监听器实现类
 * @Version: 4.0
 */
@Component
public class BpmApplyAccommodationStatusListener extends BpmProcessInstanceStatusEventListener {

    @Resource
    private BpmBusinessFormService businessFormService;

    @Resource
    private BpmElderlyApi bpmElderlyApi;

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

    @Override
    protected void onEvent(BpmProcessInstanceStatusEvent event) {
        // 监听Spring事件修改状态
        BpmBusinessFormDO businessFormDO = businessFormService.getBusinessFormByInstanceId(event.getId());
        businessFormDO.setStatus(event.getStatus());
        businessFormService.updateBusinessForm(businessFormDO);

        if (BpmProcessInstanceStatusEnum.APPROVE.getStatus().equals(event.getStatus())) {
            bpmElderlyApi.generateAdmissionAssessment(businessFormDO.getBusinessId2(), businessFormDO.getStartTenantId());
        }
    }

}
