package com.ningdatech.pmapi.common.statemachine.action;

import com.ningdatech.pmapi.common.constant.StateMachineConst;
import com.ningdatech.pmapi.common.statemachine.util.StateMachineUtils;
import com.ningdatech.pmapi.common.statemachine.event.ProjectStatusChangeEvent;
import com.ningdatech.pmapi.projectlib.enumeration.ProjectStatusEnum;
import com.ningdatech.pmapi.projectlib.model.entity.Project;
import lombok.extern.slf4j.Slf4j;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.action.Action;

/**
 * 项目申报状态机选择分支action类
 *
 * @author CMM
 * @since 2023/02/07 22:31
 */
@Slf4j
public class ProjectDeclareChoiceAction implements Action<ProjectStatusEnum, ProjectStatusChangeEvent> {

    private static final String PROJECT_DECLARE = StateMachineConst.PROJECT_DECLARE;

    @Override
    public void execute(StateContext<ProjectStatusEnum, ProjectStatusChangeEvent> stateContext) {
        log.info(String.valueOf(stateContext.getTarget().getId()));
        ProjectStatusEnum projectStatusEnum = stateContext.getTarget().getId();
        if (projectStatusEnum.equals(ProjectStatusEnum.PENDING_PREQUALIFICATION_CHOICE)){
            Project project = stateContext.getMessage().getHeaders().get(PROJECT_DECLARE, Project.class);
            log.info("预审申报事件之前，项目的状态为:{}"+project.getStatus());
            if (StateMachineUtils.isCityProject(project) && StateMachineUtils.judgeDeclareAmount(project)){
                project.setStatus(ProjectStatusEnum.JOINT_REVIEW_BY_PROVINCIAL_DEPARTMENTS.getCode());
            }else {
                project.setStatus(ProjectStatusEnum.PRE_APPLYING.getCode());
            }
        }
    }
}
