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.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/27  16:14
 * @Description: 餐饮变更的监听器实现类
 * @Version: 4.0
 */
@Component
public class BpmCateringChangeStatusListener extends BpmProcessInstanceStatusEventListener {

    @Resource
    private BpmBusinessFormService businessFormService;

    @Resource
    private BpmElderlyChangeApi changeApi;

    @Override
    protected String getProcessDefinitionKey() {
        return BpmBusinessTypeEnum.CATERING_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(), 3, businessFormDO.getStartTenantId());
            BpmBusinessFormDO updateDO = new BpmBusinessFormDO();
            updateDO.setId(businessFormDO.getId());
            updateDO.setCompleteStatus(1);
            businessFormService.updateById(updateDO);
        }
    }

}
