package com.engine.htsc.lhtybx.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;

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

/**
 * @author K1810039 WUXU
 */

public class GetMainJobCmd extends AbstractCommonCommand<Map<String,Object>> {

    public GetMainJobCmd(Map<String,Object> params) {
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {

        Map<String, Object> apidatas = new HashMap<String, Object>();

        try {
            RecordSet rs = new RecordSet();
            String sql = "select belongto from hrmresource where id=?";
            // 获取请求中携带的报销人id
            int bxrId = Util.getIntValue(Util.null2String(params.get("id")));

            rs.executeQuery(sql,bxrId);
            if (rs.next()) {
                writeLog("请求中携带的报销人id--->"+bxrId);
                // 获取belongto字段的值
                String belongto = rs.getString("belongto");
                writeLog("belongto字段的值--->"+belongto);
                // 判断belongto是否为空
                if (StringUtils.isNotBlank(belongto)){
                    // belongto不为空，说明该账号是次账号，则返回主账号的id，即belongto字段的值
                    apidatas.put("id",Integer.parseInt(belongto));
                    writeLog("belongto不为空,apidatas--->"+apidatas);
                }else {
                    // belongto为空，说明该账号是主账号，则返回原来表单中的报销人的id
                    apidatas.put("id",bxrId);
                    writeLog("belongto为空,apidatas--->"+apidatas);
                }
            }else {
                // 如果查询结果集为空，返回原id，不做更改
                apidatas.put("id",bxrId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        writeLog("return--->apidatas:"+apidatas);
        return apidatas;
    }
}
