package com.engine.recruit.service.impl;

import com.engine.core.impl.Service;
import com.engine.integration.cmd.schedule.ImmediatelyRunCmd;
import com.engine.recruit.exception.CustomizeRunTimeException;
import com.engine.recruit.service.ResumeRemindService;
import org.apache.commons.lang3.StringUtils;
import weaver.general.Util;
import weaver.interfaces.recruit.cronjob.ResumePushJob;
import weaver.systeminfo.SystemEnv;

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

/**
 * @author:dxfeng
 * @createTime: 2024/03/13
 * @version: 1.0
 */
public class ResumeRemindServiceImpl extends Service implements ResumeRemindService {
    @Override
    public Map<String, Object> doRemindJob(Map<String, Object> param) {
        String pointId = "ResumePushJob";
        HashMap<String, Boolean> locks = ImmediatelyRunCmd.locks;
        boolean checkLock = true;
        if (locks.containsKey(pointId)) {
            checkLock = locks.get(pointId);
        } else {
            locks.put(pointId, checkLock);
        }

        String dateStr = Util.null2String(param.get("dateStr"));
        if (StringUtils.isNotBlank(dateStr)) {
            String[] split = dateStr.split(",");
            if (split.length == 2) {
                String startDate = split[0];
                String endDate = split[1];
                if (StringUtils.isNoneBlank(startDate, endDate)) {
                    if (checkLock) {
                        checkLock = false;
                        locks.put(pointId, checkLock);
                        try {
                            new ResumePushJob(startDate, endDate).execute();
                        } catch (Exception e) {
                            throw new CustomizeRunTimeException("ResumePushJob计划任务执行失败", e);
                        } finally {
                            checkLock = true;
                            locks.put(pointId, checkLock);
                        }
                        return null;
                    } else {
                        locks.put(pointId, false);
                        throw new CustomizeRunTimeException(SystemEnv.getHtmlLabelName(532655, Util.getIntValue(user.getLanguage())));
                    }
                }
            }
        } else {
            throw new CustomizeRunTimeException("请选择简历投递开始日期、结束日期");
        }
        throw new CustomizeRunTimeException("简历推送异常");
    }
}
