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.vo.ElderlyChangeRecordReqVO;
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 BpmBedChangeStatusListener extends BpmProcessInstanceStatusEventListener {

    @Resource
    private BpmBusinessFormService businessFormService;

    @Resource
    private BpmElderlyChangeApi changeApi;

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




//        if (BpmProcessInstanceStatusEnum.APPROVE.getStatus().equals(event.getStatus())) {
//            // 生成账单
//            changeApi.generateBill(businessFormDO.getBusinessId(), 1, businessFormDO.getStartTenantId());
//            // 转换床位
//            changeApi.convertBed(businessFormDO.getBusinessId());
//        }
    }

}
