EcEntrustInfoBiz.java 3.3 KB
package com.cjs.site.biz.ec;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import com.cjs.site.model.entrust.EcEntrustInfo;
import com.cjs.site.model.user.UserAttentionInfo;
import com.cjs.site.util.lang.JsonUtil;

/**
 * 买卖委托信息
 *
 * @author tongyufu
 *
 */
@Service
public class EcEntrustInfoBiz {

    public String processData(String data, String flag) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag", flag);
        result.put("data", JsonUtil.fromJson(data, Object.class));
        return JsonUtil.toJson(result);
    }


    public void sendToInnovate(WebSocketSession session, List<EcEntrustInfo> entrusts, String[] innovates)
            throws IOException
    {
        if ((entrusts == null) || (innovates == null) || (entrusts.isEmpty()) || (innovates.length <= 0)) {
            return;
        }
        List userEntrusts = new ArrayList();
        for (EcEntrustInfo entrust : entrusts) {
            for (String obj : innovates) {
                if (StringUtils.equals(entrust.getOtcCode(), obj)) {
                    userEntrusts.add(entrust);
                    break;
                }
            }
        }
        if (!userEntrusts.isEmpty())
            session.sendMessage(new TextMessage(JsonUtil.toJson(userEntrusts)));
    }

    /**
     * 发送到用户自选行情
     *
     * @param session
     * @param entrusts
     * @param attentions
     * @throws IOException
     */
    public void sendToClient(WebSocketSession session, List<EcEntrustInfo> entrusts,
                             List<UserAttentionInfo> attentions) throws IOException {
        if (entrusts == null || attentions == null || entrusts.isEmpty() || attentions.isEmpty()) {
            return;
        }
        List<EcEntrustInfo> userEntrusts = new ArrayList<EcEntrustInfo>();
        for (EcEntrustInfo entrust : entrusts) {
            for (UserAttentionInfo attention : attentions) {
                if (StringUtils.equals(entrust.getOtcCode(), attention.getOtcCode())) {
                    userEntrusts.add(entrust);
                    break;
                }
            }
        }
        if (!userEntrusts.isEmpty()) {
            session.sendMessage(new TextMessage(JsonUtil.toJson(userEntrusts)));
        }
    }

    /**
     * 发送到用户自选行情
     *
     * @param sessions
     * @param entrusts 行情
     * @throws IOException
     */
    public void sendToClient(final Map<WebSocketSession, List<UserAttentionInfo>> sessions,
                             final List<EcEntrustInfo> entrusts) throws IOException {
        final EcEntrustInfoBiz biz = this;
        new Thread(new Runnable() {

            @Override
            public void run() {
                for (WebSocketSession session : sessions.keySet()) {
                    try {
                        biz.sendToClient(session, entrusts, sessions.get(session));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}