package weaver.interfaces.recruit.action;

import com.engine.recruit.entity.record.ApplicantRecordPo;
import com.engine.recruit.enums.RecordOperateEnum;
import org.apache.commons.lang3.StringUtils;
import weaver.common.DateUtil;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.HashMap;
import java.util.Map;

public class RecruitRecordAction implements Action {

    private String recordType;

    @Override
    public String execute(RequestInfo requestInfo) {
        {
            MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
            Property[] properties = mainTableInfo.getProperty();
            Map<String, Object> mainDataMap = new HashMap<>();
            for (Property property : properties) {
                mainDataMap.put(property.getName(), property.getValue());
            }
            String requestId = requestInfo.getRequestid();
            RequestManager requestManager = requestInfo.getRequestManager();
            User user = requestManager.getUser();
            int formId = requestManager.getFormid();


            // 插入应聘过程
            if (StringUtils.isNotBlank(recordType)) {
                RecordOperateEnum operateEnum = RecordOperateEnum.getOperateEnum(recordType);
                if (null != operateEnum) {
                    String createName = requestInfo.getCreatorid();
                    String requestName = requestManager.getRequestname();
                    mainDataMap.put("createName", createName);
                    mainDataMap.put("requestName", requestName);
                    String link = "/spa/workflow/static4form/index.html#/main/workflow/req?requestid=" + requestId;

                    ApplicantRecordPo recordPo = ApplicantRecordPo.builder()
                            .formId(String.valueOf(formId))
                            .billId(requestId)
                            .operateTime(DateUtil.getDateTime())
                            .user(user)
                            .otherParam(mainDataMap)
                            .recordOperateType(operateEnum)
                            .linkUrl(link)
                            .build();
                    recordPo.execute();
                }
            }
        }
        return null;
    }

    public String getRecordType() {
        return recordType;
    }

    public void setRecordType(String recordType) {
        this.recordType = recordType;
    }
}
