package com.weaver.seconddev.chapanda.beisen.esb;

import com.alibaba.fastjson.JSONObject;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.chapanda.beisen.util.BlackList2BeiSenUtil;
import com.weaver.seconddev.chapanda.beisen.util.Token2BeiSenUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("Esb2BeisenDeleteBlackListAction")
public class Esb2BeisenDeleteBlackListAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2BeisenDeleteBlackListAction.class);

    @Autowired
    BlackList2BeiSenUtil blackList2BeiSenUtil;

    @Autowired
    Token2BeiSenUtil token2BeiSenUtil ;

    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {
        log.error("Esb2BeisenDeleteBlackListAction start");
        String objectIds = (String) params.get("objectIds");
        log.error("objectIds:{}",objectIds);

        String msg = "";
        String token = token2BeiSenUtil.getToken();
        if(StringUtils.isNotBlank(token)){
            List<String> userIds = Arrays.asList(objectIds.split(","));
            msg = blackList2BeiSenUtil.batchDeleteBlackList(token,userIds);
        }

        String failDatas = "";
        int failCount = -1 ;
        if(StringUtils.isNotBlank(msg)){
            JSONObject bodyData = JSONObject.parseObject(msg);
            if(bodyData.containsKey("code")){
                String dataCode = bodyData.getString("code");
                if("200".equals(dataCode)){
                    JSONObject dataObject = bodyData.getJSONObject("data");
                    if(dataObject.containsKey("failCount")){
                        failCount = dataObject.getInteger("failCount");
                        failDatas = dataObject.getString("failDatas");

                    }
                }
            }
        }

        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("msg","Esb2BeisenDeleteBlackListAction");
        actionMap.put("failCount",failCount);
        actionMap.put("failDatas",failDatas);
        return WeaResult.success(actionMap);
    }

}
