package weaver.formmode.recruit.modeexpand.record;

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.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.formmode.recruit.modeexpand.util.RecruitModeUtil;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

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

/**
 * @author:dxfeng
 * @createTime: 2023/11/02
 * @version: 1.0
 */
public class RecruitRecordModeExpand extends AbstractModeExpandJavaCodeNew {
    @Override
    public Map<String, String> doModeExpand(Map<String, Object> params) {
        Map<String, String> result = new HashMap<>(2);
        try {
            //数据id
            int billId;
            //模块id
            int modeId;
            RequestInfo requestInfo = (RequestInfo) params.get("RequestInfo");
            User user = (User) params.get("user");
            String recordType = Util.null2String(params.get("recordType"));
            String layoutId = Util.null2String(params.get("layoutid"));
            String formId = Util.null2String(params.get("formId"));
            if (requestInfo != null) {
                billId = Util.getIntValue(requestInfo.getRequestid());
                modeId = Util.getIntValue(requestInfo.getWorkflowid());
                if (billId > 0 && modeId > 0) {
                    MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
                    Property[] properties = mainTableInfo.getProperty();
                    Map<String, Object> mainDataMap = new HashMap<>();
                    for (Property property : properties) {
                        mainDataMap.put(property.getName(), property.getValue());
                    }

                    // 插入应聘过程
                    if (StringUtils.isNotBlank(recordType)) {
                        RecordOperateEnum operateEnum = RecordOperateEnum.getOperateEnum(recordType);
                        if (null != operateEnum) {
                            String createName = RecruitModeUtil.getResourceNames(requestInfo.getCreatorid());
                            mainDataMap.put("createName", createName);
                            ApplicantRecordPo recordPo = ApplicantRecordPo.builder()
                                    .billId(String.valueOf(billId))
                                    .modeId(String.valueOf(modeId))
                                    .formId(String.valueOf(formId))
                                    .layoutId(String.valueOf(layoutId))
                                    .operateTime(DateUtil.getDateTime())
                                    .user(user)
                                    .otherParam(mainDataMap)
                                    .recordOperateType(operateEnum)
                                    .build();
                            recordPo.execute();
                        }
                    }
                }
            }
        } catch (Exception e) {
            new BaseBean().writeLog(e);
            result.put("errmsg", "自定义出错信息");
            result.put("flag", "false");
        }

        return result;
    }
}
