package weaver.formmode.recruit.modeexpand.position;

import com.engine.recruit.entity.position.PositionSdkInstance;
import com.engine.recruit.enums.PositionReleaseStatusEnum;
import com.weaver.rpa.sdk.clients.core.entity.SDKCore;
import com.weaver.rpa.sdk.clients.core.utils.ERPASDKInitUtils;
import org.apache.commons.collections.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:dxfeng
 * @createTime: 2023/10/07
 * @version: 1.0
 */
public class ClosePositionModeExpand extends AbstractModeExpandJavaCodeNew {

    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        String tableName = "uf_jcl_zp_zpzw";
        BaseBean baseBean = new BaseBean();

        Map<String, String> resultMap = new HashMap<>(16);
        try {
            User user = (User) param.get("user");
            int billId;
            int modeId;
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                billId = Util.getIntValue(requestInfo.getRequestid());
                modeId = Util.getIntValue(requestInfo.getWorkflowid());
                if (billId > 0 && modeId > 0) {
                    RecordSet rs = new RecordSet();
                    // 判断是否配置千里聆，若未配置千里聆信息，则不执行下架职位操作，只更新状态
                    boolean enableSdkClient = PositionSdkInstance.enableSdkClient();
                    if (enableSdkClient) {
                        List<String> platformList = new ArrayList<>();
                        rs.executeQuery("select inner_channel from uf_jcl_qdfb where position = ? and status = ? ", billId, PositionReleaseStatusEnum.PUBLISHED.getValue());
                        while (rs.next()) {
                            platformList.add(rs.getString("inner_channel"));
                        }

                        if (CollectionUtils.isNotEmpty(platformList)) {
                            resultMap.put("errmsg", "该职位第三方渠道仍有发布中状态，请先执行下架操作");
                            return resultMap;
                        }

                    }

                    // 更新招聘状态为招聘结束
                    rs.executeUpdate("update " + tableName + " set zpzt = ? where id = ?", 1, billId);
                }
            }
        } catch (Exception e) {
            baseBean.writeLog(e);
            resultMap.put("errmsg", e.getMessage());
            resultMap.put("flag", "false");
        }
        return resultMap;
    }

    /**
     * 校验是否配置千里聆
     *
     * @return
     */
    private boolean checkUseQll() {
        boolean used;
        try {
            SDKCore sdkCore = ERPASDKInitUtils.genSDKCore();
            used = sdkCore != null;
        } catch (Exception e) {
            used = false;
        }
        return used;
    }

}
