package com.yh.saas.plugin.yiliangyiyun.workflow;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.yh.saas.common.support.workflow.AbstractWorkflowTaskListener;
import com.yh.saas.common.support.workflow.annotation.WorkflowTaskListener;
import com.yh.saas.plugin.yiliangyiyun.constant.StatusEnum;
import com.yh.saas.plugin.yiliangyiyun.entity.InOutWarehouseTask;
import com.yh.saas.plugin.yiliangyiyun.service.IInOutWarehouseTaskService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

@WorkflowTaskListener(name = "仓库管理", group = "出入库任务")
public class InOutWharehoseTaskListener extends AbstractWorkflowTaskListener {

    private static final long serialVersionUID = 4103656734835351132L;

    @Autowired
    private IInOutWarehouseTaskService iInOutWarehouseTaskService;

    @Override
    public void handleStatusChanged(String businessKey, String status, String statusEn, boolean processIsEnd) {
        InOutWarehouseTask inOutWarehouseTask = iInOutWarehouseTaskService.selectById(businessKey);
        if (inOutWarehouseTask == null) {
            return;
        }
        if (processIsEnd) {
            inOutWarehouseTask.setTaskStatusKey(StatusEnum.TASK_DO.getFlag());
            inOutWarehouseTask.setTaskStatus(StatusEnum.TASK_DO.getName());
            inOutWarehouseTask.setWorkflowId(null);
            inOutWarehouseTask.setApproveStatus(null);
        } else {
            inOutWarehouseTask.setApproveStatus(status);
        }
        iInOutWarehouseTaskService.updateAllColumnById(inOutWarehouseTask);
    }

    @Override
    public void handleRejectApplicationRepulsed(String workflowId, String businessKey, boolean needReapply) {
        InOutWarehouseTask inOutWarehouseTask = iInOutWarehouseTaskService.selectById(businessKey);
        if (inOutWarehouseTask == null) {
            return;
        }
        inOutWarehouseTask.setTaskStatusKey(StatusEnum.TASK_RETURN.getFlag());
        inOutWarehouseTask.setTaskStatus(StatusEnum.TASK_RETURN.getName());
        inOutWarehouseTask.setApproveStatus(null);
        iInOutWarehouseTaskService.updateAllColumnById(inOutWarehouseTask);

    }

}
