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.BpmElderlyApi;
import cn.iocoder.yudao.module.system.api.bpm.BpmElderlyChangeApi;
import cn.iocoder.yudao.module.system.api.bpm.BpmElderlyExpenseApi;
import cn.iocoder.yudao.module.system.api.bpm.vo.ElderlyRetreatRecordRespVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;

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

    @Resource
    private BpmBusinessFormService businessFormService;

    @Resource
    private BpmElderlyApi bpmElderlyApi;
    @Resource
    private BpmElderlyExpenseApi bpmElderlyExpenseApi;

    @Resource
    private BpmElderlyChangeApi elderlyChangeApi;

    @Override
    protected String getProcessDefinitionKey() {
        return BpmBusinessTypeEnum.APPLY_RETREAT.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.releaseBed(businessFormDO.getBusinessId());
            bpmElderlyExpenseApi.generateRefundSettlementOrder(businessFormDO.getBusinessId(), businessFormDO.getStartTenantId());
            BpmBusinessFormDO updateDO = new BpmBusinessFormDO();
            updateDO.setId(businessFormDO.getId());
            updateDO.setCompleteStatus(1);
            businessFormService.updateBusinessForm(updateDO);
        }


    }

}
