//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nccloud.api.uapbd.supplier.tms.listene;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import nc.bs.bd.baseservice.ArrayClassConvertUtil;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.businessevent.bd.BDCommonEvent;
import nc.bs.logging.Log;
import nc.bs.trade.business.HYPubBO;
import nc.bs.uapbd.util.MyHelper;
import nc.bs.uapbd.util.ThirdPartyPostRequestUtil;
import nc.vo.bd.supplier.SupplierVO;
import nc.vo.pub.BusinessException;
import nccloud.baseapp.core.log.NCCForUAPLogger;
import nccloud.commons.lang.ArrayUtils;

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

/**
 * 供应商推艾普MES
 *
 * @author lihao
 */
public class SupplierVoListener implements IBusinessListener {
    private static final String LOG_INFO_NAME = "dldzlog";
    private static final Log logDl = Log.getInstance(LOG_INFO_NAME);
    private static final String reqUrl = "/prj-v5-web/ext/api/supplier";
    private Map<String, String> configParams;

    public void doAction(IBusinessEvent event) throws BusinessException {
        BDCommonEvent e = (BDCommonEvent) event;
        configParams = MyHelper.getConfigParams("Dldz-config", null);
        String modifyType = "";
        // EventType是事件编码 1002-新增后 1004-修改后 1006-删除后
        if ("1002".equals(e.getEventType())) {
            modifyType = "I";
            SupplierVO[] newvos = ArrayClassConvertUtil.convert(e.getObjs(), SupplierVO.class);
            this.putSupplier(newvos, modifyType);
        } else if ("1004".equals(e.getEventType())) {
            modifyType = "U";
            SupplierVO[] newvos = ArrayClassConvertUtil.convert(e.getObjs(), SupplierVO.class);
            this.putSupplier(newvos, modifyType);
        } else if ("1006".equals(e.getEventType())) {
            modifyType = "D";
            SupplierVO[] newvos = ArrayClassConvertUtil.convert(e.getObjs(), SupplierVO.class);
            this.putSupplier(newvos, modifyType);
        }


    }

    private void putSupplier(SupplierVO[] objs, String modifyType) throws BusinessException {
        if (!ArrayUtils.isEmpty(objs)) {
            List<Map<String, Object>> list = new ArrayList<>();
            for (SupplierVO obj : objs) {
                Map<String, Object> map = new HashMap<>();
                map.put("supplierName", obj.getName());
                map.put("supplierCode", obj.getCode());
                String supplierTypeId = obj.getPk_supplierclass();

                HYPubBO hy = new HYPubBO();
                String supplierType = (String) hy.findColValue("bd_supplierclass", "code", "pk_supplierclass  = '" + supplierTypeId + "' ");
                map.put("supplierType", supplierType.substring(0, 1));
                map.put("status", modifyType);
                list.add(map);
            }
            JSONArray result = JSONArray.parseArray(JSONObject.toJSONString(list));
            pushData(result);
        }
    }

    /**
     * 推送同步数据
     */
    private void pushData(JSONArray param) throws BusinessException {
        // String jsonString = param.toJSONString();
        // 转json字符串的时候保留null值
        String jsonStr = JSON.toJSONString(param,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty
        );
        logDl.error("EpicMes-Supplier-param = " + jsonStr);
        NCCForUAPLogger.debug("EpicMes-Supplier-param = " + jsonStr);
        String baseUrl = configParams.get("epicMesUrl");
        String requestUrl = baseUrl + reqUrl;
        logDl.error("EpicMes-Supplier-url = " + requestUrl);
        String result = ThirdPartyPostRequestUtil.sendPostRequest(requestUrl, jsonStr);
        JSONObject resultObj = JSONObject.parseObject(result);
        logDl.error("EpicMes-Supplier-res = " + result);

        if (!"1".equals(resultObj.getString("flag"))) {
            logDl.error("EpicMes-Supplier-error,result[" + resultObj.toJSONString() + "]");
            // throw new BusinessException("EpicMes-Supplier-error:" + resultObj.getString("msg"));
        }
    }
}
