package com.yem.wm.task;

import com.yem.wm.utils.RequestEAS;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.exception.KDException;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.List;
import java.util.Map;

public class SynEASIDTask extends AbstractTask {
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        //组织
        DynamicObject[] orgs = BusinessDataServiceHelper.load("bos_org", "id", null);
        for (DynamicObject org : orgs) {
            org = BusinessDataServiceHelper.loadSingle(org.getLong("id"), "bos_org");
            String sql = "select fid from T_ORG_BaseUnit where fnumber ='" + org.getString("number") + "'";

            setEASID(org, sql);
        }
        //物料
        DynamicObject[] mats = BusinessDataServiceHelper.load("bd_material", "id", null);
        for (DynamicObject mat : mats) {
            mat = BusinessDataServiceHelper.loadSingle(mat.getLong("id"),"bd_material");
            String sql = "select fid from t_bd_material where fnumber ='"+mat.getString("number")+"'";
            setEASID(mat,sql);
        }
        //计量单位
        DynamicObject[] bd_measureunits = BusinessDataServiceHelper.load("bd_measureunits", "id", null);
        for (DynamicObject mat : bd_measureunits) {
            mat = BusinessDataServiceHelper.loadSingle(mat.getLong("id"), "bd_measureunits");
            mat.set("number", mat.getString("number").toUpperCase());
            String sql = "select fid from t_bd_measureunit where fnumber ='" + mat.getString("number").toUpperCase() + "'";
            setEASID(mat, sql);
        }
        //人员
        DynamicObject[] bos_users = BusinessDataServiceHelper.load("bos_user", "id", null);
        for (DynamicObject mat : bos_users) {
            mat = BusinessDataServiceHelper.loadSingle(mat.getLong("id"), "bos_user");
            String sql = "select fid from T_BD_Person where fnumber ='" + mat.getString("number") + "'";
            setEASID(mat, sql);
        }
    }

    private void setEASID(DynamicObject org, String sql) {
        try {
            List<Map<String, Object>> list = RequestEAS.exeSql(sql);
            for (Map<String, Object> map : list) {
                Object o = map.get("fid");
                org.set("yem_easid", o);
            }
            SaveServiceHelper.update(org);
        } catch (Exception e) {
            return;
        }

    }
}
