package com.engine.organization.service.impl;

import com.engine.core.impl.Service;
import com.engine.organization.service.JclOrgService;
import com.engine.organization.util.field.StaticFieldName;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;

/**
 * @author:dxfeng
 * @createTime: 2022/12/16
 * @version: 1.0
 */
public class JclOrgServiceImpl extends Service implements JclOrgService {

    @Override
    public void supplementResourceInfo(Integer userId) {
        // 查询职等职级字段
        RecordSet rs = new RecordSet();
        // 所有字段信息存在，执行业务逻辑
        if (StringUtils.isNoneBlank(StaticFieldName.zdzjFieldName, StaticFieldName.schemeFieldName, StaticFieldName.levelFieldName, StaticFieldName.gradeFieldName, StaticFieldName.sequenceFieldName)) {
            String zdzjFieldValue = "";
            String schemeFieldValue = "";
            String levelFieldValue = "";
            String gradeFieldValue = "";
            String sequenceFieldValue = "";
            String sql = "select " + StaticFieldName.zdzjFieldName + " from cus_fielddata where scope='HrmCustomFieldByInfoType' and scopeid=3 and id= " + userId;
            rs.execute(sql);
            if (rs.next()) {
                zdzjFieldValue = rs.getString(StaticFieldName.zdzjFieldName);
            }
            // 职等职级为空时不处理
            if (StringUtils.isBlank(zdzjFieldValue)) {
                return;
            }
            sql = "select xlid, zjid, zdid from v_jcl_zdzjs where fid='" + zdzjFieldValue.substring(zdzjFieldValue.indexOf("_") + 1) + "'";
            rs.execute(sql);
            if (rs.next()) {
                levelFieldValue = rs.getString("zdid");
                gradeFieldValue = rs.getString("zjid");
                sequenceFieldValue = rs.getString("xlid");
            }
            sql = "select scheme_id from jcl_org_sequence where id='" + sequenceFieldValue + "'";
            rs.execute(sql);
            if (rs.next()) {
                schemeFieldValue = rs.getString("scheme_id");
            }

            // 更新字段信息
            sql = "update cus_fielddata set " + StaticFieldName.sequenceFieldName + " = '" + sequenceFieldValue + "' where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=" + userId;
            rs.execute(sql);
            sql = "update cus_fielddata set " + StaticFieldName.schemeFieldName + " = '" + schemeFieldValue + "' where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=" + userId;
            rs.execute(sql);
            sql = "update cus_fielddata set " + StaticFieldName.levelFieldName + " = '" + levelFieldValue + "' where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=" + userId;
            rs.execute(sql);
            sql = "update cus_fielddata set " + StaticFieldName.gradeFieldName + " = '" + gradeFieldValue + "' where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=" + userId;
            rs.execute(sql);
        }
    }
}
